zenodo 2.0.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/lib/Zenodo.d.ts +66 -7
- package/lib/Zenodo.d.ts.map +1 -1
- package/lib/Zenodo.js +145 -29
- package/lib/Zenodo.js.map +1 -1
- package/lib/ZenodoAuthenticationStates.d.ts +22 -0
- package/lib/ZenodoAuthenticationStates.d.ts.map +1 -0
- package/lib/ZenodoAuthenticationStates.js +18 -0
- package/lib/ZenodoAuthenticationStates.js.map +1 -0
- package/lib/ZenodoFile.d.ts +22 -33
- package/lib/ZenodoFile.d.ts.map +1 -1
- package/lib/ZenodoFile.js +3 -19
- package/lib/ZenodoFile.js.map +1 -1
- package/lib/fetchZenodo.d.ts +45 -2
- package/lib/fetchZenodo.d.ts.map +1 -1
- package/lib/fetchZenodo.js +297 -15
- package/lib/fetchZenodo.js.map +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -17
- package/lib/index.js.map +1 -1
- package/{lib-esm/depositions → lib/records}/ListDepositionsOptions.d.ts +4 -2
- package/lib/records/ListDepositionsOptions.d.ts.map +1 -0
- package/lib/records/ListDepositionsOptions.js.map +1 -0
- package/lib/records/Record.d.ts +34 -0
- package/lib/records/Record.d.ts.map +1 -0
- package/lib/records/Record.js +225 -0
- package/lib/records/Record.js.map +1 -0
- package/lib/records/RecordType.d.ts +513 -0
- package/lib/records/RecordType.d.ts.map +1 -0
- package/lib/records/RecordType.js +2 -0
- package/lib/records/RecordType.js.map +1 -0
- package/lib/records/RequestType.d.ts +37 -0
- package/lib/records/RequestType.d.ts.map +1 -0
- package/lib/records/RequestType.js +2 -0
- package/lib/records/RequestType.js.map +1 -0
- package/lib/responseStatuses.js +1 -4
- package/lib/responseStatuses.js.map +1 -1
- package/lib/utilities/zipFiles.d.ts +9 -0
- package/lib/utilities/zipFiles.d.ts.map +1 -0
- package/lib/utilities/zipFiles.js +28 -0
- package/lib/utilities/zipFiles.js.map +1 -0
- package/package.json +28 -22
- package/src/Zenodo.ts +180 -26
- package/src/ZenodoAuthenticationStates.ts +23 -0
- package/src/ZenodoFile.ts +24 -18
- package/src/fetchZenodo.ts +491 -18
- package/src/index.ts +3 -1
- package/src/{depositions → records}/ListDepositionsOptions.ts +4 -2
- package/src/records/Record.ts +298 -0
- package/src/records/RecordType.ts +479 -0
- package/src/records/RequestType.ts +37 -0
- package/src/utilities/zipFiles.ts +36 -0
- package/lib/depositions/Deposition.d.ts +0 -14
- package/lib/depositions/Deposition.d.ts.map +0 -1
- package/lib/depositions/Deposition.js +0 -55
- package/lib/depositions/Deposition.js.map +0 -1
- package/lib/depositions/ListDepositionsOptions.d.ts +0 -32
- package/lib/depositions/ListDepositionsOptions.d.ts.map +0 -1
- package/lib/depositions/ListDepositionsOptions.js +0 -3
- package/lib/depositions/ListDepositionsOptions.js.map +0 -1
- package/lib/depositions/depositionSchema.d.ts +0 -982
- package/lib/depositions/depositionSchema.d.ts.map +0 -1
- package/lib/depositions/depositionSchema.js +0 -141
- package/lib/depositions/depositionSchema.js.map +0 -1
- package/lib-esm/Zenodo.d.ts +0 -19
- package/lib-esm/Zenodo.d.ts.map +0 -1
- package/lib-esm/Zenodo.js +0 -51
- package/lib-esm/Zenodo.js.map +0 -1
- package/lib-esm/ZenodoFile.d.ts +0 -45
- package/lib-esm/ZenodoFile.d.ts.map +0 -1
- package/lib-esm/ZenodoFile.js +0 -33
- package/lib-esm/ZenodoFile.js.map +0 -1
- package/lib-esm/depositions/Deposition.d.ts +0 -14
- package/lib-esm/depositions/Deposition.d.ts.map +0 -1
- package/lib-esm/depositions/Deposition.js +0 -51
- package/lib-esm/depositions/Deposition.js.map +0 -1
- package/lib-esm/depositions/ListDepositionsOptions.d.ts.map +0 -1
- package/lib-esm/depositions/ListDepositionsOptions.js.map +0 -1
- package/lib-esm/depositions/depositionSchema.d.ts +0 -982
- package/lib-esm/depositions/depositionSchema.d.ts.map +0 -1
- package/lib-esm/depositions/depositionSchema.js +0 -138
- package/lib-esm/depositions/depositionSchema.js.map +0 -1
- package/lib-esm/fetchZenodo.d.ts +0 -12
- package/lib-esm/fetchZenodo.d.ts.map +0 -1
- package/lib-esm/fetchZenodo.js +0 -31
- package/lib-esm/fetchZenodo.js.map +0 -1
- package/lib-esm/index.d.ts +0 -2
- package/lib-esm/index.d.ts.map +0 -1
- package/lib-esm/index.js +0 -2
- package/lib-esm/index.js.map +0 -1
- package/lib-esm/responseStatuses.d.ts +0 -8
- package/lib-esm/responseStatuses.d.ts.map +0 -1
- package/lib-esm/responseStatuses.js +0 -68
- package/lib-esm/responseStatuses.js.map +0 -1
- package/src/__tests__/Zenodo.test.ts +0 -79
- package/src/__tests__/getConfig.ts +0 -9
- package/src/depositions/Deposition.ts +0 -60
- package/src/depositions/depositionSchema.ts +0 -154
- /package/{lib-esm/depositions → lib/records}/ListDepositionsOptions.js +0 -0
package/src/fetchZenodo.ts
CHANGED
|
@@ -1,26 +1,487 @@
|
|
|
1
|
-
import type { Zenodo } from './Zenodo';
|
|
2
|
-
import {
|
|
1
|
+
import type { Zenodo } from './Zenodo.ts';
|
|
2
|
+
import { ZenodoAuthenticationStates } from './ZenodoAuthenticationStates.ts';
|
|
3
|
+
import { responseStatuses } from './responseStatuses.ts';
|
|
3
4
|
|
|
4
5
|
interface FetchZenodoOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The route to append to the Zenodo base URL.
|
|
8
|
+
* @default 'user/records'
|
|
9
|
+
*/
|
|
5
10
|
route?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The HTTP method to use for the request.
|
|
13
|
+
* @default 'GET'
|
|
14
|
+
*/
|
|
6
15
|
method?: string;
|
|
16
|
+
/**
|
|
17
|
+
* The content type of the request body.
|
|
18
|
+
* @default 'application/json' if body is not a FormData, undefined otherwise
|
|
19
|
+
*/
|
|
7
20
|
contentType?: string;
|
|
21
|
+
/**
|
|
22
|
+
* The expected status code of the response. If the response status code does not match this
|
|
23
|
+
* value, we will retry.
|
|
24
|
+
* @default 200
|
|
25
|
+
*/
|
|
8
26
|
expectedStatus?: number;
|
|
9
27
|
searchParams?: Record<string, string>;
|
|
10
|
-
body?: string |
|
|
28
|
+
body?: string | File;
|
|
29
|
+
/**
|
|
30
|
+
* Maximum number of retry attempts.
|
|
31
|
+
* @default 3
|
|
32
|
+
*/
|
|
33
|
+
maxRetries?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Base delay between retries in milliseconds.
|
|
36
|
+
* @default 1000
|
|
37
|
+
*/
|
|
38
|
+
baseDelay?: number;
|
|
39
|
+
/**
|
|
40
|
+
* Whether to use exponential backoff for retry delays.
|
|
41
|
+
* @default true
|
|
42
|
+
*/
|
|
43
|
+
useExponentialBackoff?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Whether to respect rate limit headers and wait accordingly.
|
|
46
|
+
* @default true
|
|
47
|
+
*/
|
|
48
|
+
respectRateLimit?: boolean;
|
|
11
49
|
}
|
|
12
50
|
|
|
13
|
-
|
|
51
|
+
interface RateLimitInfo {
|
|
52
|
+
limit: number;
|
|
53
|
+
remaining: number;
|
|
54
|
+
reset: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Extract rate limit information from response headers.
|
|
59
|
+
* @param response - The fetch response object.
|
|
60
|
+
* @returns The rate limit information or null if not present.
|
|
61
|
+
*/
|
|
62
|
+
function extractRateLimitInfo(response: Response): RateLimitInfo | null {
|
|
63
|
+
const limit = response.headers.get('X-RateLimit-Limit');
|
|
64
|
+
const remaining = response.headers.get('X-RateLimit-Remaining');
|
|
65
|
+
const reset = response.headers.get('X-RateLimit-Reset');
|
|
66
|
+
|
|
67
|
+
if (!limit || !remaining || !reset) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
limit: Number.parseInt(limit, 10),
|
|
73
|
+
remaining: Number.parseInt(remaining, 10),
|
|
74
|
+
reset: Number.parseInt(reset, 10),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Calculate delay based on rate limit information.
|
|
80
|
+
* @param rateLimitInfo - The rate limit information extracted from response headers.
|
|
81
|
+
* @returns Delay in milliseconds to wait before retrying.
|
|
82
|
+
*/
|
|
83
|
+
function calculateRateLimitDelay(rateLimitInfo: RateLimitInfo): number {
|
|
84
|
+
if (rateLimitInfo.remaining > 0) {
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const now = Math.floor(Date.now() / 1000);
|
|
89
|
+
const waitTime = Math.max(0, rateLimitInfo.reset - now);
|
|
90
|
+
|
|
91
|
+
return (waitTime + 1) * 1000;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Determine if an error is retryable based on status code.
|
|
96
|
+
* @param status - The HTTP status code of the response.
|
|
97
|
+
* @param zenodo - The Zenodo instance
|
|
98
|
+
* @returns True if the error is retryable, false otherwise.
|
|
99
|
+
*/
|
|
100
|
+
async function shouldRetry(status: number, zenodo: Zenodo): Promise<boolean> {
|
|
101
|
+
if (status === 429 || (status >= 500 && status < 600) || status === 408) {
|
|
102
|
+
zenodo.logger?.debug(`Retrying status ${status} - server/rate limit error`);
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const isAuthError =
|
|
107
|
+
(status === 401 || status === 403) &&
|
|
108
|
+
(zenodo.authenticationState === ZenodoAuthenticationStates.NOT_TRIED ||
|
|
109
|
+
zenodo.authenticationState === ZenodoAuthenticationStates.SUCCEEDED);
|
|
110
|
+
|
|
111
|
+
if (isAuthError) {
|
|
112
|
+
zenodo.logger?.debug(
|
|
113
|
+
`Auth error detected (${status}), current state: ${zenodo.authenticationState}`,
|
|
114
|
+
);
|
|
115
|
+
zenodo.logger?.debug('Attempting to verify authentication...');
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
await zenodo.verifyAuthentication();
|
|
119
|
+
zenodo.logger?.debug(
|
|
120
|
+
`Authentication verification completed, new state: ${zenodo.authenticationState}`,
|
|
121
|
+
);
|
|
122
|
+
return true;
|
|
123
|
+
} catch (error) {
|
|
124
|
+
zenodo.logger?.error(
|
|
125
|
+
`Authentication verification failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
126
|
+
);
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Sleep for a specified number of milliseconds.
|
|
136
|
+
* @param ms - The number of milliseconds to sleep
|
|
137
|
+
*/
|
|
138
|
+
function sleep(ms: number): Promise<void> {
|
|
139
|
+
return new Promise<void>((resolve) => {
|
|
140
|
+
setTimeout(resolve, ms);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Calculate the appropriate delay for a retry attempt.
|
|
146
|
+
* @param attempt - Current attempt number (0-based)
|
|
147
|
+
* @param response - The failed response
|
|
148
|
+
* @param rateLimitInfo - Rate limit information from headers
|
|
149
|
+
* @param options - Retry options
|
|
150
|
+
* @returns Delay in milliseconds
|
|
151
|
+
*/
|
|
152
|
+
function calculateRetryDelay(
|
|
153
|
+
attempt: number,
|
|
154
|
+
response: Response,
|
|
155
|
+
rateLimitInfo: RateLimitInfo | null,
|
|
156
|
+
options: Pick<
|
|
157
|
+
FetchZenodoOptions,
|
|
158
|
+
'baseDelay' | 'useExponentialBackoff' | 'respectRateLimit'
|
|
159
|
+
>,
|
|
160
|
+
): number {
|
|
161
|
+
const {
|
|
162
|
+
baseDelay = 1000,
|
|
163
|
+
useExponentialBackoff = true,
|
|
164
|
+
respectRateLimit = true,
|
|
165
|
+
} = options;
|
|
166
|
+
|
|
167
|
+
if (response.status === 429 && respectRateLimit && rateLimitInfo) {
|
|
168
|
+
// Rate limit hit - respect the rate limit headers
|
|
169
|
+
return calculateRateLimitDelay(rateLimitInfo);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Other retryable error - use exponential backoff
|
|
173
|
+
const delay = useExponentialBackoff ? baseDelay * 2 ** attempt : baseDelay;
|
|
174
|
+
|
|
175
|
+
// Add jitter to prevent thundering herd
|
|
176
|
+
return delay + Math.random() * 1000;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Perform a single fetch attempt.
|
|
181
|
+
* @param zenodo - The Zenodo instance
|
|
182
|
+
* @param url - The URL to fetch
|
|
183
|
+
* @param requestOptions - The fetch options
|
|
184
|
+
* @param expectedStatus - The expected response status
|
|
185
|
+
* @param attempt - Current attempt number (0-based)
|
|
186
|
+
* @returns The response or throws an error
|
|
187
|
+
*/
|
|
188
|
+
async function performFetchAttempt(
|
|
189
|
+
zenodo: Zenodo,
|
|
190
|
+
url: string,
|
|
191
|
+
requestOptions: RequestInit,
|
|
192
|
+
expectedStatus: number,
|
|
193
|
+
attempt: number,
|
|
194
|
+
): Promise<Response> {
|
|
195
|
+
zenodo.logger?.debug(
|
|
196
|
+
`Attempt ${attempt + 1} for ${requestOptions.method} ${url}`,
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
const response = await fetch(url, requestOptions);
|
|
201
|
+
|
|
202
|
+
// Check rate limit headers
|
|
203
|
+
const rateLimitInfo = extractRateLimitInfo(response);
|
|
204
|
+
if (rateLimitInfo && zenodo.logger) {
|
|
205
|
+
zenodo.logger.debug(
|
|
206
|
+
`Rate limit status: ${rateLimitInfo.remaining}/${rateLimitInfo.limit} remaining, resets at ${rateLimitInfo.reset}`,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Log response details for debugging
|
|
211
|
+
zenodo.logger?.debug(
|
|
212
|
+
`Response status: ${response.status} ${response.statusText}`,
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
return response;
|
|
216
|
+
} catch (fetchError) {
|
|
217
|
+
zenodo.logger?.error(
|
|
218
|
+
{
|
|
219
|
+
url,
|
|
220
|
+
method: requestOptions.method,
|
|
221
|
+
error:
|
|
222
|
+
fetchError instanceof Error ? fetchError.message : String(fetchError),
|
|
223
|
+
},
|
|
224
|
+
`Network error on attempt ${attempt + 1}:`,
|
|
225
|
+
);
|
|
226
|
+
throw fetchError;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Handle a failed response and determine if retry is appropriate.
|
|
232
|
+
* @param zenodo - The Zenodo instance
|
|
233
|
+
* @param response - The failed response
|
|
234
|
+
* @param url - The request URL
|
|
235
|
+
* @param method - The request method
|
|
236
|
+
* @param contentType - The request content type
|
|
237
|
+
* @param body - The request body
|
|
238
|
+
* @returns Error to throw
|
|
239
|
+
*/
|
|
240
|
+
async function handleFailedResponse(
|
|
241
|
+
zenodo: Zenodo,
|
|
242
|
+
response: Response,
|
|
243
|
+
url: string,
|
|
244
|
+
method: string,
|
|
245
|
+
contentType: string | undefined,
|
|
246
|
+
body: string | File | undefined,
|
|
247
|
+
): Promise<Error> {
|
|
248
|
+
const baseErrorMessage =
|
|
249
|
+
responseStatuses[response.status]?.description || response.statusText;
|
|
250
|
+
let detailedErrorMessage = baseErrorMessage;
|
|
251
|
+
let errorDetails: unknown = null;
|
|
252
|
+
try {
|
|
253
|
+
const responseText = await response.text();
|
|
254
|
+
if (responseText) {
|
|
255
|
+
try {
|
|
256
|
+
errorDetails = JSON.parse(responseText);
|
|
257
|
+
if (errorDetails && typeof errorDetails === 'object') {
|
|
258
|
+
const errorObj = errorDetails as Record<string, unknown>;
|
|
259
|
+
if (errorObj.message && typeof errorObj.message === 'string') {
|
|
260
|
+
detailedErrorMessage = `${baseErrorMessage}: ${errorObj.message}`;
|
|
261
|
+
} else if (errorObj.error && typeof errorObj.error === 'string') {
|
|
262
|
+
detailedErrorMessage = `${baseErrorMessage}: ${errorObj.error}`;
|
|
263
|
+
} else if (errorObj.errors && Array.isArray(errorObj.errors)) {
|
|
264
|
+
const errorMessages = errorObj.errors
|
|
265
|
+
.map((err: unknown) =>
|
|
266
|
+
typeof err === 'string'
|
|
267
|
+
? err
|
|
268
|
+
: err &&
|
|
269
|
+
typeof err === 'object' &&
|
|
270
|
+
'message' in err &&
|
|
271
|
+
typeof (err as Record<string, unknown>).message ===
|
|
272
|
+
'string'
|
|
273
|
+
? ((err as Record<string, unknown>).message as string)
|
|
274
|
+
: JSON.stringify(err),
|
|
275
|
+
)
|
|
276
|
+
.join(', ');
|
|
277
|
+
detailedErrorMessage = `${baseErrorMessage}: ${errorMessages}`;
|
|
278
|
+
} else {
|
|
279
|
+
detailedErrorMessage = `${baseErrorMessage}: ${JSON.stringify(errorDetails)}`;
|
|
280
|
+
}
|
|
281
|
+
} else {
|
|
282
|
+
detailedErrorMessage = `${baseErrorMessage}: ${responseText}`;
|
|
283
|
+
}
|
|
284
|
+
} catch {
|
|
285
|
+
detailedErrorMessage = `${baseErrorMessage}: ${responseText}`;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
} catch (textError) {
|
|
289
|
+
// If we can't read the response body, log the attempt
|
|
290
|
+
zenodo.logger?.warn(
|
|
291
|
+
`Could not read error response body: ${textError instanceof Error ? textError.message : String(textError)}`,
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
if (!(await shouldRetry(response.status, zenodo))) {
|
|
295
|
+
zenodo.logger?.error(
|
|
296
|
+
`Non-retryable error fetching ${url} with ${method}: ${detailedErrorMessage}`,
|
|
297
|
+
);
|
|
298
|
+
zenodo.logger?.error(
|
|
299
|
+
{
|
|
300
|
+
url,
|
|
301
|
+
method,
|
|
302
|
+
contentType,
|
|
303
|
+
status: response.status,
|
|
304
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
305
|
+
errorDetails,
|
|
306
|
+
},
|
|
307
|
+
`Request details: ${errorDetails ? JSON.stringify(errorDetails) : 'No details'}`,
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
const error = new Error(detailedErrorMessage, {
|
|
311
|
+
cause: {
|
|
312
|
+
url,
|
|
313
|
+
method,
|
|
314
|
+
contentType,
|
|
315
|
+
body,
|
|
316
|
+
response,
|
|
317
|
+
status: response.status,
|
|
318
|
+
statusText: response.statusText,
|
|
319
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
320
|
+
errorDetails,
|
|
321
|
+
},
|
|
322
|
+
});
|
|
323
|
+
return error;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Retry logic using recursive approach to avoid await in loops.
|
|
328
|
+
* @param zenodo - The Zenodo instance
|
|
329
|
+
* @param url - The URL to fetch
|
|
330
|
+
* @param requestOptions - The fetch options
|
|
331
|
+
* @param fetchOptions - The original fetch options
|
|
332
|
+
* @param attempt - Current attempt number (0-based)
|
|
333
|
+
* @returns The successful response
|
|
334
|
+
*/
|
|
335
|
+
async function retryFetch(
|
|
336
|
+
zenodo: Zenodo,
|
|
337
|
+
url: string,
|
|
338
|
+
requestOptions: RequestInit,
|
|
339
|
+
fetchOptions: Required<
|
|
340
|
+
Pick<
|
|
341
|
+
FetchZenodoOptions,
|
|
342
|
+
| 'expectedStatus'
|
|
343
|
+
| 'maxRetries'
|
|
344
|
+
| 'baseDelay'
|
|
345
|
+
| 'useExponentialBackoff'
|
|
346
|
+
| 'respectRateLimit'
|
|
347
|
+
>
|
|
348
|
+
> &
|
|
349
|
+
Pick<FetchZenodoOptions, 'method' | 'contentType' | 'body'>,
|
|
350
|
+
attempt: number,
|
|
351
|
+
): Promise<Response> {
|
|
352
|
+
const {
|
|
353
|
+
expectedStatus,
|
|
354
|
+
maxRetries,
|
|
355
|
+
method = 'GET',
|
|
356
|
+
contentType,
|
|
357
|
+
body,
|
|
358
|
+
} = fetchOptions;
|
|
359
|
+
|
|
360
|
+
try {
|
|
361
|
+
const response = await performFetchAttempt(
|
|
362
|
+
zenodo,
|
|
363
|
+
url,
|
|
364
|
+
requestOptions,
|
|
365
|
+
expectedStatus,
|
|
366
|
+
attempt,
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
if (response.status === expectedStatus) {
|
|
370
|
+
if (attempt > 0) {
|
|
371
|
+
zenodo.logger?.info(`Request succeeded after ${attempt} retries`);
|
|
372
|
+
}
|
|
373
|
+
return response;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (!(await shouldRetry(response.status, zenodo))) {
|
|
377
|
+
throw await handleFailedResponse(
|
|
378
|
+
zenodo,
|
|
379
|
+
response,
|
|
380
|
+
url,
|
|
381
|
+
method,
|
|
382
|
+
contentType,
|
|
383
|
+
body,
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (attempt >= maxRetries) {
|
|
388
|
+
throw await handleFailedResponse(
|
|
389
|
+
zenodo,
|
|
390
|
+
response,
|
|
391
|
+
url,
|
|
392
|
+
method,
|
|
393
|
+
contentType,
|
|
394
|
+
body,
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const rateLimitInfo = extractRateLimitInfo(response);
|
|
399
|
+
const delay = calculateRetryDelay(
|
|
400
|
+
attempt,
|
|
401
|
+
response,
|
|
402
|
+
rateLimitInfo,
|
|
403
|
+
fetchOptions,
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
if (
|
|
407
|
+
response.status === 429 &&
|
|
408
|
+
fetchOptions.respectRateLimit &&
|
|
409
|
+
rateLimitInfo
|
|
410
|
+
) {
|
|
411
|
+
zenodo.logger?.warn(
|
|
412
|
+
`Rate limit exceeded. Waiting ${delay}ms before retry (attempt ${attempt + 1}/${maxRetries + 1})`,
|
|
413
|
+
);
|
|
414
|
+
} else {
|
|
415
|
+
zenodo.logger?.warn(
|
|
416
|
+
`Retryable error (${response.status}). Waiting ${delay}ms before retry (attempt ${attempt + 1}/${maxRetries + 1})`,
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
await sleep(delay);
|
|
421
|
+
return await retryFetch(
|
|
422
|
+
zenodo,
|
|
423
|
+
url,
|
|
424
|
+
requestOptions,
|
|
425
|
+
fetchOptions,
|
|
426
|
+
attempt + 1,
|
|
427
|
+
);
|
|
428
|
+
} catch (error) {
|
|
429
|
+
if (error instanceof Error) {
|
|
430
|
+
if (
|
|
431
|
+
error.cause &&
|
|
432
|
+
typeof error.cause === 'object' &&
|
|
433
|
+
'response' in error.cause
|
|
434
|
+
) {
|
|
435
|
+
throw error;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (attempt >= maxRetries) {
|
|
439
|
+
zenodo.logger?.error(
|
|
440
|
+
`All ${maxRetries + 1} attempts failed for ${method} ${url}`,
|
|
441
|
+
);
|
|
442
|
+
throw error;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const delay = fetchOptions.useExponentialBackoff
|
|
446
|
+
? fetchOptions.baseDelay * 2 ** attempt
|
|
447
|
+
: fetchOptions.baseDelay;
|
|
448
|
+
|
|
449
|
+
zenodo.logger?.warn(
|
|
450
|
+
`Network error: ${error.message}. Waiting ${delay}ms before retry (attempt ${attempt + 1}/${maxRetries + 1})`,
|
|
451
|
+
);
|
|
452
|
+
|
|
453
|
+
await sleep(delay);
|
|
454
|
+
return retryFetch(zenodo, url, requestOptions, fetchOptions, attempt + 1);
|
|
455
|
+
} else {
|
|
456
|
+
throw error;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Fetch data from the Zenodo API with retry logic.
|
|
463
|
+
* @param zenodo - The Zenodo instance containing the base URL and access token.
|
|
464
|
+
* @param options - The options for the fetch request.
|
|
465
|
+
* @returns The response from the Zenodo API.
|
|
466
|
+
*/
|
|
467
|
+
export async function fetchZenodo(
|
|
468
|
+
zenodo: Zenodo,
|
|
469
|
+
options: FetchZenodoOptions,
|
|
470
|
+
): Promise<Response> {
|
|
14
471
|
const {
|
|
15
472
|
body,
|
|
16
|
-
route = '
|
|
473
|
+
route = 'user/records',
|
|
17
474
|
method = 'GET',
|
|
18
475
|
contentType = body instanceof FormData ? undefined : 'application/json',
|
|
19
476
|
expectedStatus = 200,
|
|
20
477
|
searchParams,
|
|
478
|
+
maxRetries = 3,
|
|
479
|
+
baseDelay = 1000,
|
|
480
|
+
useExponentialBackoff = true,
|
|
481
|
+
respectRateLimit = true,
|
|
21
482
|
} = options;
|
|
22
483
|
|
|
23
|
-
let url;
|
|
484
|
+
let url: string;
|
|
24
485
|
if (searchParams) {
|
|
25
486
|
const urlSearchParams = new URLSearchParams(searchParams);
|
|
26
487
|
url =
|
|
@@ -31,23 +492,35 @@ export async function fetchZenodo(zenodo: Zenodo, options: FetchZenodoOptions) {
|
|
|
31
492
|
url = zenodo.baseURL + route;
|
|
32
493
|
}
|
|
33
494
|
|
|
34
|
-
const headers = new Headers(
|
|
35
|
-
|
|
36
|
-
|
|
495
|
+
const headers = new Headers();
|
|
496
|
+
if (zenodo.accessToken) {
|
|
497
|
+
headers.set('Authorization', `Bearer ${zenodo.accessToken}`);
|
|
498
|
+
}
|
|
37
499
|
if (contentType) {
|
|
38
500
|
headers.set('Content-Type', contentType);
|
|
39
501
|
}
|
|
502
|
+
if (method !== 'PUT' && !route.includes('files')) {
|
|
503
|
+
headers.set('Accept', 'application/vnd.inveniordm.v1+json');
|
|
504
|
+
} else {
|
|
505
|
+
headers.set('Accept', 'application/json');
|
|
506
|
+
}
|
|
40
507
|
|
|
41
|
-
const
|
|
508
|
+
const requestOptions: RequestInit = {
|
|
42
509
|
method,
|
|
43
510
|
headers,
|
|
44
511
|
body,
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
const fetchOptionsWithDefaults = {
|
|
515
|
+
expectedStatus,
|
|
516
|
+
maxRetries,
|
|
517
|
+
baseDelay,
|
|
518
|
+
useExponentialBackoff,
|
|
519
|
+
respectRateLimit,
|
|
520
|
+
method,
|
|
521
|
+
contentType,
|
|
522
|
+
body,
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
return retryFetch(zenodo, url, requestOptions, fetchOptionsWithDefaults, 0);
|
|
53
526
|
}
|
package/src/index.ts
CHANGED
|
@@ -16,7 +16,7 @@ export interface ListDepositionsOptions {
|
|
|
16
16
|
* Possible values: 'bestmatch' | 'mostrecent'
|
|
17
17
|
* Prefix with minus to change from ascending to descending (e.g., '-mostrecent').
|
|
18
18
|
*/
|
|
19
|
-
sort?: 'bestmatch' | 'mostrecent' |
|
|
19
|
+
sort?: 'bestmatch' | 'mostrecent' | '-bestmatch' | '-mostrecent';
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Page number for pagination.
|
|
@@ -30,7 +30,9 @@ export interface ListDepositionsOptions {
|
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* Show or hide all versions of deposits.
|
|
33
|
+
* the query is not camelCase but for consistency with other options.
|
|
34
|
+
* the query is translated to `all_versions` in this library.
|
|
33
35
|
* Possible values: 'true' | 'false' | 1 | 0
|
|
34
36
|
*/
|
|
35
|
-
|
|
37
|
+
allVersions?: 'true' | 'false' | 1 | 0;
|
|
36
38
|
}
|