-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHarvestRpde.d.ts
144 lines (141 loc) · 4.64 KB
/
HarvestRpde.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { AxiosError } from 'axios';
import { FeedContext } from './FeedContext';
import { RpdePageProcessor } from './RpdePageProcessor';
// When this is more typed, turn this into HarvestRpdeArgs<T> when T is the type of `modified`. modified can etiher be
// string or number depending on if it is a lossless harvest or not.
export type HarvestRpdeArgs = {
/**
* Feed URL to harvest
*/
baseUrl: string;
/**
* Unique identifier for feed within the dataset eg ScheduledSession
*/
feedContextIdentifier: string;
/**
* Function that returns headers needed to make a request to the feed URL
*/
headers: () => Promise<{ [key: string]: string }>;
/**
* Callback is called when a page (that is NOT the last page - for the last
* page, see onReachedEndOfFeed) has been processed.
*
* It contains all the items in the page. Use this to process each item e.g.
* store it, validate it, etc.
*/
processPage: (args: {
rpdePage: any,
feedContextIdentifier: string;
isInitialHarvestComplete: () => boolean;
reqUrl: string;
responseTime: number;
}) => Promise<void>;
/**
* Callback is called when the feed's [last
* page](https://openactive.io/realtime-paged-data-exchange/#last-page-definition)
* has been (successfully) reached - and so when all items in the feed have
* been processed..
*
* This is not the end of harvesting, as the feed will continue to be polled
* afterwards for updates.
*
* This function may be called multiple times if new items are added after the
* first time `harvestRPDE()` reaches the last page.
*/
onReachedEndOfFeed: ({
lastPageUrl: string,
isInitialHarvestComplete: boolean,
// feedContext: FeedContext,
responseTime: number,
}) => Promise<void>;
/**
* Callback is called when a request to the feed fails due to an HTTP error.
*
* Note that harvester will then retry the request afterwards, but you may
* e.g. want to log the error here.
*/
onRetryDueToHttpError?: (
reqUrl: string,
reqHeaders: Record<string, string>,
resStatusCode: number,
error: AxiosError,
/** Number of milliseconds that the harvester will wait before the next retry */
delayMs: number,
/** Number of retries that have been attempted so far on this request */
numberOfRetries: number
) => Promise<void>;
/**
* This callback (if provided) is called before each new request to the feed.
*
* This can be useful for these scenarios:
*
* 1. If the feed has a rate limit.
* 2. To pause harvesting.
*/
optionallyWaitBeforeNextRequest?: () => Promise<void>;
/**
* Is the feed an Orders feed?
*/
isOrdersFeed: boolean;
/**
* Optional logging functions. Defaults will be used if not provided.
*/
loggingFns?: {
/** Normal logging. Default: console.log */
log?: (message?: any, ...optionalParams: any[]) => void;
/** Error logging. Default: console.error */
logError?: (message?: any, ...optionalParams: any[]) => void;
/** Error logging during the harvest. Default: console.error */
logErrorDuringHarvest?: (message?: any, ...optionalParams: any[]) => void;
};
config?: {
/**
* How long to wait, in milliseconds, before re-polling a feed after
* fetching the last page ([RPDE
* spec](https://openactive.io/realtime-paged-data-exchange/#polling-for-near-real-time-updates)).
* Default: `() => 500`
*/
howLongToSleepAtFeedEnd?: () => number;
/**
* Whether to include extra logging around each feed request.
*/
REQUEST_LOGGING_ENABLED?: boolean;
};
/**
* @deprecated TODO this arg is only used by Broker Microservice, so that it
* can share its context with the harvestRPDE function. This should be cleaned
* up so that each project manages their own contexts separately.
*
* If provided, harvestRPDE will start with this FeedContext, rather than its
* usual behaviour of creating its own FeedContext.
*/
overrideContext?: FeedContext;
};
export type HarvestRpdeResponse = {
error: {
type: 'unexpected-non-http-error';
reqUrl: string;
reqHeaders: Record<string, string>;
error: Error;
message: string;
} | {
type: 'feed-not-found';
reqUrl: string;
reqHeaders: Record<string, string>;
resStatusCode: number;
error: Error;
} | {
type: 'retry-limit-exceeded-for-http-error';
reqUrl: string;
reqHeaders: Record<string, string>;
resStatusCode: number;
error: Error;
numberOfRetries: number;
} | {
type: 'rpde-validation-error';
reqUrl: string;
reqHeaders: Record<string, string>;
// TODO get the type for these
rpdeValidationErrors: unknown[];
}
};