zenodo 3.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.
Files changed (96) hide show
  1. package/lib/Zenodo.d.ts +18 -31
  2. package/lib/Zenodo.d.ts.map +1 -1
  3. package/lib/Zenodo.js +29 -44
  4. package/lib/Zenodo.js.map +1 -1
  5. package/lib/ZenodoAuthenticationStates.d.ts +6 -5
  6. package/lib/ZenodoAuthenticationStates.d.ts.map +1 -1
  7. package/lib/ZenodoAuthenticationStates.js +3 -5
  8. package/lib/ZenodoAuthenticationStates.js.map +1 -1
  9. package/lib/ZenodoFile.d.ts +26 -1
  10. package/lib/ZenodoFile.d.ts.map +1 -1
  11. package/lib/ZenodoFile.js +2 -3
  12. package/lib/ZenodoFile.js.map +1 -1
  13. package/lib/fetchZenodo.d.ts +2 -2
  14. package/lib/fetchZenodo.d.ts.map +1 -1
  15. package/lib/fetchZenodo.js +94 -11
  16. package/lib/fetchZenodo.js.map +1 -1
  17. package/lib/index.d.ts +0 -1
  18. package/lib/index.d.ts.map +1 -1
  19. package/lib/index.js +0 -1
  20. package/lib/index.js.map +1 -1
  21. package/lib/records/ListDepositionsOptions.d.ts.map +1 -0
  22. package/lib/records/ListDepositionsOptions.js.map +1 -0
  23. package/lib/records/Record.d.ts +34 -0
  24. package/lib/records/Record.d.ts.map +1 -0
  25. package/lib/records/Record.js +225 -0
  26. package/lib/records/Record.js.map +1 -0
  27. package/lib/records/RecordType.d.ts +513 -0
  28. package/lib/records/RecordType.d.ts.map +1 -0
  29. package/lib/records/RecordType.js +2 -0
  30. package/lib/records/RecordType.js.map +1 -0
  31. package/lib/records/RequestType.d.ts +37 -0
  32. package/lib/records/RequestType.d.ts.map +1 -0
  33. package/lib/records/RequestType.js +2 -0
  34. package/lib/records/RequestType.js.map +1 -0
  35. package/lib/utilities/zipFiles.js +1 -1
  36. package/lib/utilities/zipFiles.js.map +1 -1
  37. package/package.json +5 -5
  38. package/src/Zenodo.ts +41 -57
  39. package/src/ZenodoAuthenticationStates.ts +6 -5
  40. package/src/ZenodoFile.ts +24 -4
  41. package/src/fetchZenodo.ts +105 -18
  42. package/src/index.ts +0 -2
  43. package/src/records/Record.ts +298 -0
  44. package/src/records/RecordType.ts +479 -0
  45. package/src/records/RequestType.ts +37 -0
  46. package/src/utilities/zipFiles.ts +1 -1
  47. package/lib/depositions/Deposition.d.ts +0 -98
  48. package/lib/depositions/Deposition.d.ts.map +0 -1
  49. package/lib/depositions/Deposition.js +0 -224
  50. package/lib/depositions/Deposition.js.map +0 -1
  51. package/lib/depositions/ListDepositionsOptions.d.ts.map +0 -1
  52. package/lib/depositions/ListDepositionsOptions.js.map +0 -1
  53. package/lib/depositions/Record.d.ts +0 -10
  54. package/lib/depositions/Record.d.ts.map +0 -1
  55. package/lib/depositions/Record.js +0 -19
  56. package/lib/depositions/Record.js.map +0 -1
  57. package/lib/utilities/ZenodoDepositionSchema.d.ts +0 -473
  58. package/lib/utilities/ZenodoDepositionSchema.d.ts.map +0 -1
  59. package/lib/utilities/ZenodoDepositionSchema.js +0 -80
  60. package/lib/utilities/ZenodoDepositionSchema.js.map +0 -1
  61. package/lib/utilities/ZenodoFileSchema.d.ts +0 -39
  62. package/lib/utilities/ZenodoFileSchema.d.ts.map +0 -1
  63. package/lib/utilities/ZenodoFileSchema.js +0 -37
  64. package/lib/utilities/ZenodoFileSchema.js.map +0 -1
  65. package/lib/utilities/ZenodoMetadataSchema.d.ts +0 -400
  66. package/lib/utilities/ZenodoMetadataSchema.d.ts.map +0 -1
  67. package/lib/utilities/ZenodoMetadataSchema.js +0 -602
  68. package/lib/utilities/ZenodoMetadataSchema.js.map +0 -1
  69. package/lib/utilities/ZenodoRecordSchema.d.ts +0 -544
  70. package/lib/utilities/ZenodoRecordSchema.d.ts.map +0 -1
  71. package/lib/utilities/ZenodoRecordSchema.js +0 -70
  72. package/lib/utilities/ZenodoRecordSchema.js.map +0 -1
  73. package/lib/utilities/ZenodoReviewSchema.d.ts +0 -112
  74. package/lib/utilities/ZenodoReviewSchema.d.ts.map +0 -1
  75. package/lib/utilities/ZenodoReviewSchema.js +0 -80
  76. package/lib/utilities/ZenodoReviewSchema.js.map +0 -1
  77. package/lib/utilities/licensesEnum.d.ts +0 -4
  78. package/lib/utilities/licensesEnum.d.ts.map +0 -1
  79. package/lib/utilities/licensesEnum.js +0 -449
  80. package/lib/utilities/licensesEnum.js.map +0 -1
  81. package/lib/utilities/schemaValidation.d.ts +0 -50
  82. package/lib/utilities/schemaValidation.d.ts.map +0 -1
  83. package/lib/utilities/schemaValidation.js +0 -124
  84. package/lib/utilities/schemaValidation.js.map +0 -1
  85. package/src/depositions/Deposition.ts +0 -272
  86. package/src/depositions/Record.ts +0 -25
  87. package/src/utilities/ZenodoDepositionSchema.ts +0 -90
  88. package/src/utilities/ZenodoFileSchema.ts +0 -40
  89. package/src/utilities/ZenodoMetadataSchema.ts +0 -608
  90. package/src/utilities/ZenodoRecordSchema.ts +0 -75
  91. package/src/utilities/ZenodoReviewSchema.ts +0 -84
  92. package/src/utilities/licensesEnum.ts +0 -448
  93. package/src/utilities/schemaValidation.ts +0 -150
  94. /package/lib/{depositions → records}/ListDepositionsOptions.d.ts +0 -0
  95. /package/lib/{depositions → records}/ListDepositionsOptions.js +0 -0
  96. /package/src/{depositions → records}/ListDepositionsOptions.ts +0 -0
package/src/Zenodo.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  import type { Logger } from 'cheminfo-types';
2
+ import { recursiveRemoveEmptyAndNull } from 'ml-spectra-processing';
2
3
 
3
4
  import { ZenodoAuthenticationStates } from './ZenodoAuthenticationStates.ts';
4
5
  import type { ZenodoAuthenticationStatesType } from './ZenodoAuthenticationStates.ts';
5
- import { Deposition } from './depositions/Deposition.ts';
6
- import type { ListDepositionsOptions } from './depositions/ListDepositionsOptions.ts';
7
- import { Record } from './depositions/Record.ts';
8
6
  import { fetchZenodo } from './fetchZenodo.ts';
9
- import type { ZenodoMetadata } from './utilities/ZenodoMetadataSchema.ts';
10
- import type { ZenodoReview } from './utilities/ZenodoReviewSchema.ts';
7
+ import type { ListDepositionsOptions } from './records/ListDepositionsOptions.ts';
8
+ import { Record } from './records/Record.ts';
9
+ import type { ZenodoMetadata, Identifier } from './records/RecordType.ts';
10
+ import type { ZenodoReview } from './records/RequestType.ts';
11
11
 
12
12
  interface ZenodoOptions {
13
13
  accessToken: string;
@@ -15,7 +15,7 @@ interface ZenodoOptions {
15
15
  logger?: Logger;
16
16
  }
17
17
 
18
- interface PublicRecordOptions {
18
+ export interface PublicRecordOptions {
19
19
  isPublished?: boolean;
20
20
  }
21
21
 
@@ -45,7 +45,7 @@ export class Zenodo {
45
45
  static async create(options: ZenodoOptions): Promise<Zenodo> {
46
46
  const zenodo = new Zenodo(options);
47
47
  const response = await fetchZenodo(zenodo, {
48
- route: 'deposit/depositions',
48
+ route: 'user/records',
49
49
  expectedStatus: 200,
50
50
  });
51
51
  if (!response.ok) {
@@ -62,7 +62,7 @@ export class Zenodo {
62
62
  * @returns true if authentication is successful, false otherwise.
63
63
  */
64
64
  async verifyAuthentication(): Promise<boolean> {
65
- const url = `${this.baseURL}deposit/depositions`;
65
+ const url = `${this.baseURL}user/records`;
66
66
  const headers = new Headers();
67
67
 
68
68
  if (this.accessToken) {
@@ -97,13 +97,12 @@ export class Zenodo {
97
97
  }
98
98
 
99
99
  /**
100
- * List all depositions
101
- * @param options - options for listing depositions
102
- * @returns an array of deposition objects
100
+ * Lists all records in the Zenodo instance.
101
+ * @param options - options for listing records
102
+ * @description Lists all records in the Zenodo instance.
103
+ * @returns An array of Record objects representing the records in the Zenodo instance.
103
104
  */
104
- async listDepositions(
105
- options: ListDepositionsOptions = {},
106
- ): Promise<Deposition[]> {
105
+ async listRecords(options: ListDepositionsOptions = {}): Promise<Record[]> {
107
106
  // all the values must be string
108
107
  const optionsWithStrings = Object.fromEntries(
109
108
  Object.entries(options).map(([key, value]) => {
@@ -115,48 +114,31 @@ export class Zenodo {
115
114
  );
116
115
 
117
116
  const response = await fetchZenodo(this, {
118
- route: 'deposit/depositions',
117
+ route: 'user/records',
119
118
  searchParams: optionsWithStrings,
120
119
  });
121
- const depositions = (await response.json()) as unknown[];
122
- this.logger?.info(`Listed ${depositions.length} depositions`);
123
- return depositions.map(
124
- (deposition: unknown) => new Deposition(this, deposition),
125
- );
120
+ const records = (await response.json()) as { hits: { hits: unknown[] } };
121
+ this.logger?.info(`Listed ${records.hits.hits.length} records`);
122
+ return records.hits.hits.map((record: unknown) => new Record(this, record));
126
123
  }
127
124
 
128
125
  /**
129
- * Create a new deposition
130
- * @param metadata - the metadata for the new deposition
126
+ * Creates a new record in the Zenodo instance.
127
+ * @param metadata - the metadata for the new record
131
128
  * @throws {Error} If the metadata is invalid or the request fails
132
- * @description Creates a new deposition with the provided metadata.
133
- * @returns The created deposition object
129
+ * @returns The created record object
134
130
  */
135
- async createDeposition(metadata: ZenodoMetadata): Promise<Deposition> {
131
+ async createRecord(metadata: ZenodoMetadata): Promise<Record> {
132
+ recursiveRemoveEmptyAndNull(metadata);
136
133
  const response = await fetchZenodo(this, {
137
- route: 'deposit/depositions',
134
+ route: 'records',
138
135
  expectedStatus: 201,
139
136
  method: 'POST',
140
137
  body: JSON.stringify({ metadata }),
141
138
  });
142
- const deposition = new Deposition(this, await response.json());
143
- this.logger?.info(`Created deposition ${deposition.value.id}`);
144
- return deposition;
145
- }
146
-
147
- /**
148
- * Retrieve a deposition by its ID
149
- * @param id - the deposition id
150
- * @throws {Error} If the deposition does not exist or the ID is undefined
151
- * @returns The retrieved deposition object
152
- */
153
- async retrieveDeposition(id: number): Promise<Deposition> {
154
- const response = await fetchZenodo(this, {
155
- route: `deposit/depositions/${id}`,
156
- });
157
- const deposition = await response.json();
158
- this.logger?.info(`Retrieved deposition ${id}`);
159
- return new Deposition(this, deposition);
139
+ const record = new Record(this, await response.json());
140
+ this.logger?.info(`Created record ${record.value.id}`);
141
+ return record;
160
142
  }
161
143
 
162
144
  /**
@@ -167,7 +149,7 @@ export class Zenodo {
167
149
  * @returns The public deposition record
168
150
  */
169
151
  async retrieveRecord(
170
- id: number,
152
+ id: Identifier,
171
153
  options: PublicRecordOptions = {},
172
154
  ): Promise<Record> {
173
155
  const { isPublished = false } = options;
@@ -186,7 +168,7 @@ export class Zenodo {
186
168
  * @returns An object containing the total number of requests and the list of requests
187
169
  */
188
170
  async retrieveRequests(
189
- depositionId?: number,
171
+ depositionId?: Identifier,
190
172
  ): Promise<{ hits: { total: number; hits: ZenodoReview[] } }> {
191
173
  const response = await fetchZenodo(this, {
192
174
  route: `requests/`,
@@ -208,7 +190,7 @@ export class Zenodo {
208
190
  * @param id - the deposition id
209
191
  * @returns unvalidated array of deposition versions
210
192
  */
211
- async retrieveVersions(id: number): Promise<unknown[]> {
193
+ async retrieveVersions(id: Identifier): Promise<Record[]> {
212
194
  const response = await fetchZenodo(this, {
213
195
  route: `records/${id}/versions`,
214
196
  });
@@ -218,21 +200,23 @@ export class Zenodo {
218
200
  this.logger?.info(
219
201
  `Retrieved ${versions?.hits.total} versions for deposition ${id}`,
220
202
  );
221
-
222
- return versions?.hits.hits || [];
203
+ const records = versions.hits.hits.map(
204
+ (version) => new Record(this, version),
205
+ );
206
+ return records;
223
207
  }
224
208
 
225
- /**
226
- * Deletes a deposition.
227
- * @param id - the deposition id
228
- * @throws {Error} If the deposition does not exist or the ID is undefined
229
- */
230
- async deleteDeposition(id: number): Promise<void> {
209
+ async deleteRecord(
210
+ id: Identifier,
211
+ options: PublicRecordOptions = {},
212
+ ): Promise<void> {
213
+ const { isPublished = false } = options;
214
+ const route = isPublished ? `records/${id}` : `records/${id}/draft`;
231
215
  await fetchZenodo(this, {
232
216
  method: 'DELETE',
233
- route: `deposit/depositions/${id}`,
217
+ route,
234
218
  expectedStatus: 204,
235
219
  });
236
- this.logger?.info(`Deleted deposition ${id}`);
220
+ this.logger?.info(`Deleted record ${id}`);
237
221
  }
238
222
  }
@@ -1,11 +1,6 @@
1
1
  /**
2
2
  * Represents the different states of Zenodo authentication for retry logic.
3
3
  *
4
- * States:
5
- * - NOT_TRIED: Authentication verification has not been attempted yet
6
- * - FAILED: Authentication verification has failed
7
- * - SUCCEEDED: Authentication verification has succeeded
8
- *
9
4
  * Usage in retry mechanism (src/services/zenodoService.ts):
10
5
  * The Zenodo API sometimes returns 401 Unauthorized or 403 Forbidden due to temporary issues.
11
6
  * - If SUCCEEDED: Access token is valid, safe to retry the request
@@ -13,10 +8,16 @@
13
8
  * - If NOT_TRIED: Can attempt authentication once to determine next steps
14
9
  */
15
10
  export const ZenodoAuthenticationStates = {
11
+ /** Authentication verification has not been attempted yet. */
16
12
  NOT_TRIED: 0,
13
+ /** Authentication verification has failed (token is invalid/expired). */
17
14
  FAILED: 1,
15
+ /** Authentication verification has succeeded (token is valid). */
18
16
  SUCCEEDED: 2,
19
17
  } as const;
20
18
 
19
+ /**
20
+ * Type representing the authentication state numeric codes.
21
+ */
21
22
  export type ZenodoAuthenticationStatesType =
22
23
  (typeof ZenodoAuthenticationStates)[keyof typeof ZenodoAuthenticationStates];
package/src/ZenodoFile.ts CHANGED
@@ -1,18 +1,38 @@
1
1
  import type { Zenodo } from './Zenodo.ts';
2
- import type { ZenodoFileType } from './utilities/ZenodoFileSchema.ts';
3
- import { validateZenodoFile } from './utilities/schemaValidation.ts';
2
+
3
+ interface ZenodoFileType {
4
+ created: string;
5
+ updated: string;
6
+ mimetype: string;
7
+ version_id: string;
8
+ file_id: string;
9
+ bucket_id: string;
10
+ metadata: object;
11
+ access: { hidden: boolean };
12
+ links: {
13
+ self: string;
14
+ content: string;
15
+ commit: string;
16
+ };
17
+ key: string;
18
+ transfer: { type: string };
19
+ status: string;
20
+ checksum: string;
21
+ size: number;
22
+ storage_class: string;
23
+ }
4
24
 
5
25
  export class ZenodoFile {
6
26
  private zenodo: Zenodo;
7
27
  public value: ZenodoFileType;
8
28
 
9
29
  constructor(zenodo: Zenodo, file: unknown) {
10
- this.value = validateZenodoFile(file);
30
+ this.value = file as ZenodoFileType;
11
31
  this.zenodo = zenodo;
12
32
  }
13
33
 
14
34
  async getContentResponse() {
15
- const link = this.value.links.download;
35
+ const link = this.value.links.content;
16
36
  const response = await fetch(link, {
17
37
  headers: {
18
38
  Authorization: `Bearer ${this.zenodo.accessToken}`,
@@ -5,7 +5,7 @@ import { responseStatuses } from './responseStatuses.ts';
5
5
  interface FetchZenodoOptions {
6
6
  /**
7
7
  * The route to append to the Zenodo base URL.
8
- * @default 'deposit/depositions'
8
+ * @default 'user/records'
9
9
  */
10
10
  route?: string;
11
11
  /**
@@ -25,7 +25,7 @@ interface FetchZenodoOptions {
25
25
  */
26
26
  expectedStatus?: number;
27
27
  searchParams?: Record<string, string>;
28
- body?: string | FormData;
28
+ body?: string | File;
29
29
  /**
30
30
  * Maximum number of retry attempts.
31
31
  * @default 3
@@ -196,17 +196,35 @@ async function performFetchAttempt(
196
196
  `Attempt ${attempt + 1} for ${requestOptions.method} ${url}`,
197
197
  );
198
198
 
199
- const response = await fetch(url, requestOptions);
199
+ try {
200
+ const response = await fetch(url, requestOptions);
200
201
 
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}`,
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}`,
206
213
  );
207
- }
208
214
 
209
- return response;
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
+ }
210
228
  }
211
229
 
212
230
  /**
@@ -225,20 +243,84 @@ async function handleFailedResponse(
225
243
  url: string,
226
244
  method: string,
227
245
  contentType: string | undefined,
228
- body: string | FormData | undefined,
246
+ body: string | File | undefined,
229
247
  ): Promise<Error> {
230
- const errorMessage =
248
+ const baseErrorMessage =
231
249
  responseStatuses[response.status]?.description || response.statusText;
232
-
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
+ }
233
294
  if (!(await shouldRetry(response.status, zenodo))) {
234
295
  zenodo.logger?.error(
235
- `Non-retryable error fetching ${url} with ${method}: ${errorMessage}`,
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'}`,
236
308
  );
237
309
  }
238
-
239
- return new Error(errorMessage, {
240
- cause: { url, method, contentType, body, response },
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
+ },
241
322
  });
323
+ return error;
242
324
  }
243
325
 
244
326
  /**
@@ -388,7 +470,7 @@ export async function fetchZenodo(
388
470
  ): Promise<Response> {
389
471
  const {
390
472
  body,
391
- route = 'deposit/depositions',
473
+ route = 'user/records',
392
474
  method = 'GET',
393
475
  contentType = body instanceof FormData ? undefined : 'application/json',
394
476
  expectedStatus = 200,
@@ -417,6 +499,11 @@ export async function fetchZenodo(
417
499
  if (contentType) {
418
500
  headers.set('Content-Type', contentType);
419
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
+ }
420
507
 
421
508
  const requestOptions: RequestInit = {
422
509
  method,
package/src/index.ts CHANGED
@@ -1,5 +1,3 @@
1
1
  export * from './Zenodo.ts';
2
2
 
3
- export * from './utilities/schemaValidation.ts';
4
-
5
3
  export * from './utilities/zipFiles.ts';