zenodo 2.0.0 → 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 +12 -0
- package/lib/Zenodo.d.ts +76 -4
- package/lib/Zenodo.d.ts.map +1 -1
- package/lib/Zenodo.js +150 -19
- package/lib/Zenodo.js.map +1 -1
- 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 +2 -38
- package/lib/ZenodoFile.d.ts.map +1 -1
- package/lib/ZenodoFile.js +3 -18
- package/lib/ZenodoFile.js.map +1 -1
- package/lib/depositions/Deposition.d.ts +90 -6
- package/lib/depositions/Deposition.d.ts.map +1 -1
- package/lib/depositions/Deposition.js +198 -29
- package/lib/depositions/Deposition.js.map +1 -1
- package/lib/depositions/ListDepositionsOptions.d.ts +4 -2
- package/lib/depositions/ListDepositionsOptions.d.ts.map +1 -1
- package/lib/depositions/ListDepositionsOptions.js +1 -2
- 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 +44 -1
- package/lib/fetchZenodo.d.ts.map +1 -1
- package/lib/fetchZenodo.js +214 -15
- package/lib/fetchZenodo.js.map +1 -1
- package/lib/index.d.ts +3 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -17
- package/lib/index.js.map +1 -1
- package/lib/responseStatuses.js +1 -4
- package/lib/responseStatuses.js.map +1 -1
- 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 +28 -22
- package/src/Zenodo.ts +180 -10
- package/src/ZenodoAuthenticationStates.ts +22 -0
- package/src/ZenodoFile.ts +4 -18
- package/src/depositions/Deposition.ts +236 -24
- package/src/depositions/ListDepositionsOptions.ts +4 -2
- package/src/depositions/Record.ts +25 -0
- package/src/fetchZenodo.ts +402 -16
- package/src/index.ts +5 -1
- 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/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 +0 -32
- package/lib-esm/depositions/ListDepositionsOptions.d.ts.map +0 -1
- package/lib-esm/depositions/ListDepositionsOptions.js +0 -2
- 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/depositionSchema.ts +0 -154
|
@@ -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
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Zenodo } from '../Zenodo.ts';
|
|
2
|
+
import type { ZenodoRecord } from '../utilities/ZenodoRecordSchema.ts';
|
|
3
|
+
import { validateZenodoRecord } from '../utilities/schemaValidation.ts';
|
|
4
|
+
|
|
5
|
+
import type { Deposition } from './Deposition.ts';
|
|
6
|
+
|
|
7
|
+
export class Record {
|
|
8
|
+
public value: ZenodoRecord;
|
|
9
|
+
private zenodo: Zenodo;
|
|
10
|
+
|
|
11
|
+
constructor(zenodo: Zenodo, value: unknown) {
|
|
12
|
+
this.zenodo = zenodo;
|
|
13
|
+
this.value = validateZenodoRecord(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async getDeposition(): Promise<Deposition> {
|
|
17
|
+
if (this.value.id === undefined) {
|
|
18
|
+
throw new Error('Deposition ID is undefined');
|
|
19
|
+
}
|
|
20
|
+
if (this.zenodo.accessToken === undefined) {
|
|
21
|
+
throw new Error('Access token is required to retrieve a deposition');
|
|
22
|
+
}
|
|
23
|
+
return await this.zenodo.retrieveDeposition(this.value.id);
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/fetchZenodo.ts
CHANGED
|
@@ -1,16 +1,391 @@
|
|
|
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 'deposit/depositions'
|
|
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
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;
|
|
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
|
+
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> {
|
|
14
389
|
const {
|
|
15
390
|
body,
|
|
16
391
|
route = 'deposit/depositions',
|
|
@@ -18,9 +393,13 @@ export async function fetchZenodo(zenodo: Zenodo, options: FetchZenodoOptions) {
|
|
|
18
393
|
contentType = body instanceof FormData ? undefined : 'application/json',
|
|
19
394
|
expectedStatus = 200,
|
|
20
395
|
searchParams,
|
|
396
|
+
maxRetries = 3,
|
|
397
|
+
baseDelay = 1000,
|
|
398
|
+
useExponentialBackoff = true,
|
|
399
|
+
respectRateLimit = true,
|
|
21
400
|
} = options;
|
|
22
401
|
|
|
23
|
-
let url;
|
|
402
|
+
let url: string;
|
|
24
403
|
if (searchParams) {
|
|
25
404
|
const urlSearchParams = new URLSearchParams(searchParams);
|
|
26
405
|
url =
|
|
@@ -31,23 +410,30 @@ export async function fetchZenodo(zenodo: Zenodo, options: FetchZenodoOptions) {
|
|
|
31
410
|
url = zenodo.baseURL + route;
|
|
32
411
|
}
|
|
33
412
|
|
|
34
|
-
const headers = new Headers(
|
|
35
|
-
|
|
36
|
-
|
|
413
|
+
const headers = new Headers();
|
|
414
|
+
if (zenodo.accessToken) {
|
|
415
|
+
headers.set('Authorization', `Bearer ${zenodo.accessToken}`);
|
|
416
|
+
}
|
|
37
417
|
if (contentType) {
|
|
38
418
|
headers.set('Content-Type', contentType);
|
|
39
419
|
}
|
|
40
420
|
|
|
41
|
-
const
|
|
421
|
+
const requestOptions: RequestInit = {
|
|
42
422
|
method,
|
|
43
423
|
headers,
|
|
44
424
|
body,
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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);
|
|
53
439
|
}
|
package/src/index.ts
CHANGED
|
@@ -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>;
|