zenodo 1.0.2 → 3.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 +60 -0
- package/lib/Zenodo.d.ts +91 -0
- package/lib/Zenodo.d.ts.map +1 -0
- package/lib/Zenodo.js +186 -0
- package/lib/Zenodo.js.map +1 -0
- package/lib/ZenodoAuthenticationStates.d.ts +21 -0
- package/lib/ZenodoAuthenticationStates.d.ts.map +1 -0
- package/lib/ZenodoAuthenticationStates.js +20 -0
- package/lib/ZenodoAuthenticationStates.js.map +1 -0
- package/lib/ZenodoFile.d.ts +9 -0
- package/lib/ZenodoFile.d.ts.map +1 -0
- package/lib/ZenodoFile.js +22 -0
- package/lib/ZenodoFile.js.map +1 -0
- package/lib/depositions/Deposition.d.ts +98 -0
- package/lib/depositions/Deposition.d.ts.map +1 -0
- package/lib/depositions/Deposition.js +224 -0
- package/lib/depositions/Deposition.js.map +1 -0
- package/lib/depositions/ListDepositionsOptions.d.ts +34 -0
- package/lib/depositions/ListDepositionsOptions.d.ts.map +1 -0
- package/lib/depositions/ListDepositionsOptions.js +2 -0
- package/lib/depositions/ListDepositionsOptions.js.map +1 -0
- package/lib/depositions/Record.d.ts +10 -0
- package/lib/depositions/Record.d.ts.map +1 -0
- package/lib/depositions/Record.js +19 -0
- package/lib/depositions/Record.js.map +1 -0
- package/lib/fetchZenodo.d.ts +55 -0
- package/lib/fetchZenodo.d.ts.map +1 -0
- package/lib/fetchZenodo.js +233 -0
- package/lib/fetchZenodo.js.map +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +4 -0
- package/lib/index.js.map +1 -0
- package/lib/responseStatuses.d.ts +8 -0
- package/lib/responseStatuses.d.ts.map +1 -0
- package/lib/responseStatuses.js +68 -0
- package/lib/responseStatuses.js.map +1 -0
- package/lib/utilities/ZenodoDepositionSchema.d.ts +473 -0
- package/lib/utilities/ZenodoDepositionSchema.d.ts.map +1 -0
- package/lib/utilities/ZenodoDepositionSchema.js +80 -0
- package/lib/utilities/ZenodoDepositionSchema.js.map +1 -0
- package/lib/utilities/ZenodoFileSchema.d.ts +39 -0
- package/lib/utilities/ZenodoFileSchema.d.ts.map +1 -0
- package/lib/utilities/ZenodoFileSchema.js +37 -0
- package/lib/utilities/ZenodoFileSchema.js.map +1 -0
- package/lib/utilities/ZenodoMetadataSchema.d.ts +400 -0
- package/lib/utilities/ZenodoMetadataSchema.d.ts.map +1 -0
- package/lib/utilities/ZenodoMetadataSchema.js +602 -0
- package/lib/utilities/ZenodoMetadataSchema.js.map +1 -0
- package/lib/utilities/ZenodoRecordSchema.d.ts +544 -0
- package/lib/utilities/ZenodoRecordSchema.d.ts.map +1 -0
- package/lib/utilities/ZenodoRecordSchema.js +70 -0
- package/lib/utilities/ZenodoRecordSchema.js.map +1 -0
- package/lib/utilities/ZenodoReviewSchema.d.ts +112 -0
- package/lib/utilities/ZenodoReviewSchema.d.ts.map +1 -0
- package/lib/utilities/ZenodoReviewSchema.js +80 -0
- package/lib/utilities/ZenodoReviewSchema.js.map +1 -0
- package/lib/utilities/licensesEnum.d.ts +4 -0
- package/lib/utilities/licensesEnum.d.ts.map +1 -0
- package/lib/utilities/licensesEnum.js +449 -0
- package/lib/utilities/licensesEnum.js.map +1 -0
- package/lib/utilities/schemaValidation.d.ts +50 -0
- package/lib/utilities/schemaValidation.d.ts.map +1 -0
- package/lib/utilities/schemaValidation.js +124 -0
- package/lib/utilities/schemaValidation.js.map +1 -0
- 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 +41 -8
- package/src/Zenodo.ts +238 -0
- package/src/ZenodoAuthenticationStates.ts +22 -0
- package/src/ZenodoFile.ts +26 -0
- package/src/depositions/Deposition.ts +272 -0
- package/src/depositions/ListDepositionsOptions.ts +38 -0
- package/src/depositions/Record.ts +25 -0
- package/src/fetchZenodo.ts +439 -0
- package/src/index.ts +5 -0
- package/src/responseStatuses.ts +85 -0
- package/src/utilities/ZenodoDepositionSchema.ts +90 -0
- package/src/utilities/ZenodoFileSchema.ts +40 -0
- package/src/utilities/ZenodoMetadataSchema.ts +608 -0
- package/src/utilities/ZenodoRecordSchema.ts +75 -0
- package/src/utilities/ZenodoReviewSchema.ts +84 -0
- package/src/utilities/licensesEnum.ts +448 -0
- package/src/utilities/schemaValidation.ts +150 -0
- package/src/utilities/zipFiles.ts +36 -0
- package/History.md +0 -73
- package/src/index.js +0 -171
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import type { Zenodo } from './Zenodo.ts';
|
|
2
|
+
import { ZenodoAuthenticationStates } from './ZenodoAuthenticationStates.ts';
|
|
3
|
+
import { responseStatuses } from './responseStatuses.ts';
|
|
4
|
+
|
|
5
|
+
interface FetchZenodoOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The route to append to the Zenodo base URL.
|
|
8
|
+
* @default 'deposit/depositions'
|
|
9
|
+
*/
|
|
10
|
+
route?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The HTTP method to use for the request.
|
|
13
|
+
* @default 'GET'
|
|
14
|
+
*/
|
|
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
|
+
*/
|
|
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
|
+
*/
|
|
26
|
+
expectedStatus?: number;
|
|
27
|
+
searchParams?: Record<string, string>;
|
|
28
|
+
body?: string | FormData;
|
|
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;
|
|
49
|
+
}
|
|
50
|
+
|
|
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
|
+
const response = await fetch(url, requestOptions);
|
|
200
|
+
|
|
201
|
+
// Check rate limit headers
|
|
202
|
+
const rateLimitInfo = extractRateLimitInfo(response);
|
|
203
|
+
if (rateLimitInfo && zenodo.logger) {
|
|
204
|
+
zenodo.logger.debug(
|
|
205
|
+
`Rate limit status: ${rateLimitInfo.remaining}/${rateLimitInfo.limit} remaining, resets at ${rateLimitInfo.reset}`,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return response;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Handle a failed response and determine if retry is appropriate.
|
|
214
|
+
* @param zenodo - The Zenodo instance
|
|
215
|
+
* @param response - The failed response
|
|
216
|
+
* @param url - The request URL
|
|
217
|
+
* @param method - The request method
|
|
218
|
+
* @param contentType - The request content type
|
|
219
|
+
* @param body - The request body
|
|
220
|
+
* @returns Error to throw
|
|
221
|
+
*/
|
|
222
|
+
async function handleFailedResponse(
|
|
223
|
+
zenodo: Zenodo,
|
|
224
|
+
response: Response,
|
|
225
|
+
url: string,
|
|
226
|
+
method: string,
|
|
227
|
+
contentType: string | undefined,
|
|
228
|
+
body: string | FormData | undefined,
|
|
229
|
+
): Promise<Error> {
|
|
230
|
+
const errorMessage =
|
|
231
|
+
responseStatuses[response.status]?.description || response.statusText;
|
|
232
|
+
|
|
233
|
+
if (!(await shouldRetry(response.status, zenodo))) {
|
|
234
|
+
zenodo.logger?.error(
|
|
235
|
+
`Non-retryable error fetching ${url} with ${method}: ${errorMessage}`,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return new Error(errorMessage, {
|
|
240
|
+
cause: { url, method, contentType, body, response },
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Retry logic using recursive approach to avoid await in loops.
|
|
246
|
+
* @param zenodo - The Zenodo instance
|
|
247
|
+
* @param url - The URL to fetch
|
|
248
|
+
* @param requestOptions - The fetch options
|
|
249
|
+
* @param fetchOptions - The original fetch options
|
|
250
|
+
* @param attempt - Current attempt number (0-based)
|
|
251
|
+
* @returns The successful response
|
|
252
|
+
*/
|
|
253
|
+
async function retryFetch(
|
|
254
|
+
zenodo: Zenodo,
|
|
255
|
+
url: string,
|
|
256
|
+
requestOptions: RequestInit,
|
|
257
|
+
fetchOptions: Required<
|
|
258
|
+
Pick<
|
|
259
|
+
FetchZenodoOptions,
|
|
260
|
+
| 'expectedStatus'
|
|
261
|
+
| 'maxRetries'
|
|
262
|
+
| 'baseDelay'
|
|
263
|
+
| 'useExponentialBackoff'
|
|
264
|
+
| 'respectRateLimit'
|
|
265
|
+
>
|
|
266
|
+
> &
|
|
267
|
+
Pick<FetchZenodoOptions, 'method' | 'contentType' | 'body'>,
|
|
268
|
+
attempt: number,
|
|
269
|
+
): Promise<Response> {
|
|
270
|
+
const {
|
|
271
|
+
expectedStatus,
|
|
272
|
+
maxRetries,
|
|
273
|
+
method = 'GET',
|
|
274
|
+
contentType,
|
|
275
|
+
body,
|
|
276
|
+
} = fetchOptions;
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
const response = await performFetchAttempt(
|
|
280
|
+
zenodo,
|
|
281
|
+
url,
|
|
282
|
+
requestOptions,
|
|
283
|
+
expectedStatus,
|
|
284
|
+
attempt,
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
if (response.status === expectedStatus) {
|
|
288
|
+
if (attempt > 0) {
|
|
289
|
+
zenodo.logger?.info(`Request succeeded after ${attempt} retries`);
|
|
290
|
+
}
|
|
291
|
+
return response;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (!(await shouldRetry(response.status, zenodo))) {
|
|
295
|
+
throw await handleFailedResponse(
|
|
296
|
+
zenodo,
|
|
297
|
+
response,
|
|
298
|
+
url,
|
|
299
|
+
method,
|
|
300
|
+
contentType,
|
|
301
|
+
body,
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (attempt >= maxRetries) {
|
|
306
|
+
throw await handleFailedResponse(
|
|
307
|
+
zenodo,
|
|
308
|
+
response,
|
|
309
|
+
url,
|
|
310
|
+
method,
|
|
311
|
+
contentType,
|
|
312
|
+
body,
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const rateLimitInfo = extractRateLimitInfo(response);
|
|
317
|
+
const delay = calculateRetryDelay(
|
|
318
|
+
attempt,
|
|
319
|
+
response,
|
|
320
|
+
rateLimitInfo,
|
|
321
|
+
fetchOptions,
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
if (
|
|
325
|
+
response.status === 429 &&
|
|
326
|
+
fetchOptions.respectRateLimit &&
|
|
327
|
+
rateLimitInfo
|
|
328
|
+
) {
|
|
329
|
+
zenodo.logger?.warn(
|
|
330
|
+
`Rate limit exceeded. Waiting ${delay}ms before retry (attempt ${attempt + 1}/${maxRetries + 1})`,
|
|
331
|
+
);
|
|
332
|
+
} else {
|
|
333
|
+
zenodo.logger?.warn(
|
|
334
|
+
`Retryable error (${response.status}). Waiting ${delay}ms before retry (attempt ${attempt + 1}/${maxRetries + 1})`,
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
await sleep(delay);
|
|
339
|
+
return await retryFetch(
|
|
340
|
+
zenodo,
|
|
341
|
+
url,
|
|
342
|
+
requestOptions,
|
|
343
|
+
fetchOptions,
|
|
344
|
+
attempt + 1,
|
|
345
|
+
);
|
|
346
|
+
} catch (error) {
|
|
347
|
+
if (error instanceof Error) {
|
|
348
|
+
if (
|
|
349
|
+
error.cause &&
|
|
350
|
+
typeof error.cause === 'object' &&
|
|
351
|
+
'response' in error.cause
|
|
352
|
+
) {
|
|
353
|
+
throw error;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (attempt >= maxRetries) {
|
|
357
|
+
zenodo.logger?.error(
|
|
358
|
+
`All ${maxRetries + 1} attempts failed for ${method} ${url}`,
|
|
359
|
+
);
|
|
360
|
+
throw error;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const delay = fetchOptions.useExponentialBackoff
|
|
364
|
+
? fetchOptions.baseDelay * 2 ** attempt
|
|
365
|
+
: fetchOptions.baseDelay;
|
|
366
|
+
|
|
367
|
+
zenodo.logger?.warn(
|
|
368
|
+
`Network error: ${error.message}. Waiting ${delay}ms before retry (attempt ${attempt + 1}/${maxRetries + 1})`,
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
await sleep(delay);
|
|
372
|
+
return retryFetch(zenodo, url, requestOptions, fetchOptions, attempt + 1);
|
|
373
|
+
} else {
|
|
374
|
+
throw error;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Fetch data from the Zenodo API with retry logic.
|
|
381
|
+
* @param zenodo - The Zenodo instance containing the base URL and access token.
|
|
382
|
+
* @param options - The options for the fetch request.
|
|
383
|
+
* @returns The response from the Zenodo API.
|
|
384
|
+
*/
|
|
385
|
+
export async function fetchZenodo(
|
|
386
|
+
zenodo: Zenodo,
|
|
387
|
+
options: FetchZenodoOptions,
|
|
388
|
+
): Promise<Response> {
|
|
389
|
+
const {
|
|
390
|
+
body,
|
|
391
|
+
route = 'deposit/depositions',
|
|
392
|
+
method = 'GET',
|
|
393
|
+
contentType = body instanceof FormData ? undefined : 'application/json',
|
|
394
|
+
expectedStatus = 200,
|
|
395
|
+
searchParams,
|
|
396
|
+
maxRetries = 3,
|
|
397
|
+
baseDelay = 1000,
|
|
398
|
+
useExponentialBackoff = true,
|
|
399
|
+
respectRateLimit = true,
|
|
400
|
+
} = options;
|
|
401
|
+
|
|
402
|
+
let url: string;
|
|
403
|
+
if (searchParams) {
|
|
404
|
+
const urlSearchParams = new URLSearchParams(searchParams);
|
|
405
|
+
url =
|
|
406
|
+
zenodo.baseURL +
|
|
407
|
+
route +
|
|
408
|
+
(urlSearchParams ? `?${urlSearchParams.toString()}` : '');
|
|
409
|
+
} else {
|
|
410
|
+
url = zenodo.baseURL + route;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const headers = new Headers();
|
|
414
|
+
if (zenodo.accessToken) {
|
|
415
|
+
headers.set('Authorization', `Bearer ${zenodo.accessToken}`);
|
|
416
|
+
}
|
|
417
|
+
if (contentType) {
|
|
418
|
+
headers.set('Content-Type', contentType);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const requestOptions: RequestInit = {
|
|
422
|
+
method,
|
|
423
|
+
headers,
|
|
424
|
+
body,
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
const fetchOptionsWithDefaults = {
|
|
428
|
+
expectedStatus,
|
|
429
|
+
maxRetries,
|
|
430
|
+
baseDelay,
|
|
431
|
+
useExponentialBackoff,
|
|
432
|
+
respectRateLimit,
|
|
433
|
+
method,
|
|
434
|
+
contentType,
|
|
435
|
+
body,
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
return retryFetch(zenodo, url, requestOptions, fetchOptionsWithDefaults, 0);
|
|
439
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
interface ResponseStatusInfo {
|
|
2
|
+
code: number;
|
|
3
|
+
name: string;
|
|
4
|
+
description: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const responseStatuses: Record<number, ResponseStatusInfo> = {
|
|
8
|
+
200: {
|
|
9
|
+
code: 200,
|
|
10
|
+
name: 'OK',
|
|
11
|
+
description:
|
|
12
|
+
'Request succeeded. Response included. Usually sent for GET/PUT/PATCH requests.',
|
|
13
|
+
},
|
|
14
|
+
201: {
|
|
15
|
+
code: 201,
|
|
16
|
+
name: 'Created',
|
|
17
|
+
description:
|
|
18
|
+
'Request succeeded. Response included. Usually sent for POST requests.',
|
|
19
|
+
},
|
|
20
|
+
202: {
|
|
21
|
+
code: 202,
|
|
22
|
+
name: 'Accepted',
|
|
23
|
+
description:
|
|
24
|
+
'Request succeeded. Response included. Usually sent for POST requests, where background processing is needed to fulfill the request.',
|
|
25
|
+
},
|
|
26
|
+
204: {
|
|
27
|
+
code: 204,
|
|
28
|
+
name: 'No Content',
|
|
29
|
+
description:
|
|
30
|
+
'Request succeeded. No response included. Usually sent for DELETE requests.',
|
|
31
|
+
},
|
|
32
|
+
400: {
|
|
33
|
+
code: 400,
|
|
34
|
+
name: 'Bad Request',
|
|
35
|
+
description: 'Request failed. Error response included.',
|
|
36
|
+
},
|
|
37
|
+
401: {
|
|
38
|
+
code: 401,
|
|
39
|
+
name: 'Unauthorized',
|
|
40
|
+
description:
|
|
41
|
+
'Request failed, due to an invalid access token. Error response included.',
|
|
42
|
+
},
|
|
43
|
+
403: {
|
|
44
|
+
code: 403,
|
|
45
|
+
name: 'Forbidden',
|
|
46
|
+
description:
|
|
47
|
+
'Request failed, due to missing authorization (e.g. deleting an already submitted upload or missing scopes for your access token). Error response included.',
|
|
48
|
+
},
|
|
49
|
+
404: {
|
|
50
|
+
code: 404,
|
|
51
|
+
name: 'Not Found',
|
|
52
|
+
description:
|
|
53
|
+
'Request failed, due to the resource not being found. Error response included.',
|
|
54
|
+
},
|
|
55
|
+
405: {
|
|
56
|
+
code: 405,
|
|
57
|
+
name: 'Method Not Allowed',
|
|
58
|
+
description:
|
|
59
|
+
'Request failed, due to unsupported HTTP method. Error response included.',
|
|
60
|
+
},
|
|
61
|
+
409: {
|
|
62
|
+
code: 409,
|
|
63
|
+
name: 'Conflict',
|
|
64
|
+
description:
|
|
65
|
+
'Request failed, due to the current state of the resource (e.g. edit a deposition which is not fully integrated). Error response included.',
|
|
66
|
+
},
|
|
67
|
+
415: {
|
|
68
|
+
code: 415,
|
|
69
|
+
name: 'Unsupported Media Type',
|
|
70
|
+
description:
|
|
71
|
+
'Request failed, due to missing or invalid request header Content-Type. Error response included.',
|
|
72
|
+
},
|
|
73
|
+
429: {
|
|
74
|
+
code: 429,
|
|
75
|
+
name: 'Too Many Requests',
|
|
76
|
+
description:
|
|
77
|
+
'Request failed, due to rate limiting. Error response included.',
|
|
78
|
+
},
|
|
79
|
+
500: {
|
|
80
|
+
code: 500,
|
|
81
|
+
name: 'Internal Server Error',
|
|
82
|
+
description:
|
|
83
|
+
'Request failed, due to an internal server error. Error response NOT included. Don’t worry, Zenodo admins have been notified and will be dealing with the problem ASAP.',
|
|
84
|
+
},
|
|
85
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { FromSchema } from 'json-schema-to-ts';
|
|
2
|
+
|
|
3
|
+
import { zenodoMetadataSchema } from './ZenodoMetadataSchema.ts';
|
|
4
|
+
|
|
5
|
+
export const zenodoDepositionSchema = {
|
|
6
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
7
|
+
title: 'Deposition',
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
created: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
format: 'date-time',
|
|
13
|
+
description: 'Creation time of deposition (ISO8601 format).',
|
|
14
|
+
},
|
|
15
|
+
modified: {
|
|
16
|
+
type: 'string',
|
|
17
|
+
format: 'date-time',
|
|
18
|
+
description: 'Last modification time of deposition (ISO8601 format).',
|
|
19
|
+
},
|
|
20
|
+
doi: {
|
|
21
|
+
type: 'string',
|
|
22
|
+
description:
|
|
23
|
+
'Digital Object Identifier (DOI). Present only for published depositions.',
|
|
24
|
+
},
|
|
25
|
+
doi_url: {
|
|
26
|
+
type: 'string',
|
|
27
|
+
format: 'uri',
|
|
28
|
+
description:
|
|
29
|
+
'Persistent link to your published deposition. Present only for published depositions.',
|
|
30
|
+
},
|
|
31
|
+
files: {
|
|
32
|
+
type: 'array',
|
|
33
|
+
description: 'A list of deposition files resources.',
|
|
34
|
+
items: {
|
|
35
|
+
type: 'object',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
id: {
|
|
39
|
+
type: 'integer',
|
|
40
|
+
description: 'Deposition identifier.',
|
|
41
|
+
},
|
|
42
|
+
metadata: zenodoMetadataSchema,
|
|
43
|
+
|
|
44
|
+
owner: {
|
|
45
|
+
type: 'integer',
|
|
46
|
+
description: 'User identifier of the owner of the deposition.',
|
|
47
|
+
},
|
|
48
|
+
record_id: {
|
|
49
|
+
type: 'integer',
|
|
50
|
+
description: 'Record identifier. Present only for published depositions.',
|
|
51
|
+
},
|
|
52
|
+
record_url: {
|
|
53
|
+
type: 'string',
|
|
54
|
+
format: 'uri',
|
|
55
|
+
description:
|
|
56
|
+
'URL to public version of record for this deposition. Present only for published depositions.',
|
|
57
|
+
},
|
|
58
|
+
state: {
|
|
59
|
+
type: 'string',
|
|
60
|
+
enum: ['inprogress', 'done', 'error', 'unsubmitted'],
|
|
61
|
+
description: 'State of deposition: inprogress, done, or error.',
|
|
62
|
+
},
|
|
63
|
+
submitted: {
|
|
64
|
+
type: 'boolean',
|
|
65
|
+
description:
|
|
66
|
+
'True if the deposition has been published, False otherwise.',
|
|
67
|
+
},
|
|
68
|
+
title: {
|
|
69
|
+
type: 'string',
|
|
70
|
+
description:
|
|
71
|
+
'Title of deposition (automatically set from metadata). Defaults to empty string.',
|
|
72
|
+
},
|
|
73
|
+
conceptrecid: {
|
|
74
|
+
type: 'string',
|
|
75
|
+
description: 'Concept record ID.',
|
|
76
|
+
},
|
|
77
|
+
links: {
|
|
78
|
+
type: 'object',
|
|
79
|
+
description: 'Links related to the deposition.',
|
|
80
|
+
},
|
|
81
|
+
conceptdoi: {
|
|
82
|
+
type: 'string',
|
|
83
|
+
description: 'Concept DOI.',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
required: [],
|
|
87
|
+
additionalProperties: false,
|
|
88
|
+
} as const;
|
|
89
|
+
|
|
90
|
+
export type ZenodoDeposition = FromSchema<typeof zenodoDepositionSchema>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { FromSchema } from 'json-schema-to-ts';
|
|
2
|
+
|
|
3
|
+
export const zenodoFileSchema = {
|
|
4
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
5
|
+
title: 'Zenodo File',
|
|
6
|
+
type: 'object',
|
|
7
|
+
properties: {
|
|
8
|
+
id: {
|
|
9
|
+
type: 'string',
|
|
10
|
+
},
|
|
11
|
+
filename: {
|
|
12
|
+
type: 'string',
|
|
13
|
+
},
|
|
14
|
+
filesize: {
|
|
15
|
+
type: 'number',
|
|
16
|
+
},
|
|
17
|
+
checksum: {
|
|
18
|
+
type: 'string',
|
|
19
|
+
},
|
|
20
|
+
links: {
|
|
21
|
+
type: 'object',
|
|
22
|
+
properties: {
|
|
23
|
+
download: {
|
|
24
|
+
type: 'string',
|
|
25
|
+
format: 'uri',
|
|
26
|
+
},
|
|
27
|
+
self: {
|
|
28
|
+
type: 'string',
|
|
29
|
+
format: 'uri',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
required: ['download', 'self'],
|
|
33
|
+
additionalProperties: false,
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
required: ['id', 'filename', 'filesize', 'checksum', 'links'],
|
|
37
|
+
additionalProperties: false,
|
|
38
|
+
} as const;
|
|
39
|
+
|
|
40
|
+
export type ZenodoFileType = FromSchema<typeof zenodoFileSchema>;
|