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.
Files changed (100) hide show
  1. package/README.md +12 -0
  2. package/lib/Zenodo.d.ts +66 -7
  3. package/lib/Zenodo.d.ts.map +1 -1
  4. package/lib/Zenodo.js +145 -29
  5. package/lib/Zenodo.js.map +1 -1
  6. package/lib/ZenodoAuthenticationStates.d.ts +22 -0
  7. package/lib/ZenodoAuthenticationStates.d.ts.map +1 -0
  8. package/lib/ZenodoAuthenticationStates.js +18 -0
  9. package/lib/ZenodoAuthenticationStates.js.map +1 -0
  10. package/lib/ZenodoFile.d.ts +22 -33
  11. package/lib/ZenodoFile.d.ts.map +1 -1
  12. package/lib/ZenodoFile.js +3 -19
  13. package/lib/ZenodoFile.js.map +1 -1
  14. package/lib/fetchZenodo.d.ts +45 -2
  15. package/lib/fetchZenodo.d.ts.map +1 -1
  16. package/lib/fetchZenodo.js +297 -15
  17. package/lib/fetchZenodo.js.map +1 -1
  18. package/lib/index.d.ts +2 -1
  19. package/lib/index.d.ts.map +1 -1
  20. package/lib/index.js +2 -17
  21. package/lib/index.js.map +1 -1
  22. package/{lib-esm/depositions → lib/records}/ListDepositionsOptions.d.ts +4 -2
  23. package/lib/records/ListDepositionsOptions.d.ts.map +1 -0
  24. package/lib/records/ListDepositionsOptions.js.map +1 -0
  25. package/lib/records/Record.d.ts +34 -0
  26. package/lib/records/Record.d.ts.map +1 -0
  27. package/lib/records/Record.js +225 -0
  28. package/lib/records/Record.js.map +1 -0
  29. package/lib/records/RecordType.d.ts +513 -0
  30. package/lib/records/RecordType.d.ts.map +1 -0
  31. package/lib/records/RecordType.js +2 -0
  32. package/lib/records/RecordType.js.map +1 -0
  33. package/lib/records/RequestType.d.ts +37 -0
  34. package/lib/records/RequestType.d.ts.map +1 -0
  35. package/lib/records/RequestType.js +2 -0
  36. package/lib/records/RequestType.js.map +1 -0
  37. package/lib/responseStatuses.js +1 -4
  38. package/lib/responseStatuses.js.map +1 -1
  39. package/lib/utilities/zipFiles.d.ts +9 -0
  40. package/lib/utilities/zipFiles.d.ts.map +1 -0
  41. package/lib/utilities/zipFiles.js +28 -0
  42. package/lib/utilities/zipFiles.js.map +1 -0
  43. package/package.json +28 -22
  44. package/src/Zenodo.ts +180 -26
  45. package/src/ZenodoAuthenticationStates.ts +23 -0
  46. package/src/ZenodoFile.ts +24 -18
  47. package/src/fetchZenodo.ts +491 -18
  48. package/src/index.ts +3 -1
  49. package/src/{depositions → records}/ListDepositionsOptions.ts +4 -2
  50. package/src/records/Record.ts +298 -0
  51. package/src/records/RecordType.ts +479 -0
  52. package/src/records/RequestType.ts +37 -0
  53. package/src/utilities/zipFiles.ts +36 -0
  54. package/lib/depositions/Deposition.d.ts +0 -14
  55. package/lib/depositions/Deposition.d.ts.map +0 -1
  56. package/lib/depositions/Deposition.js +0 -55
  57. package/lib/depositions/Deposition.js.map +0 -1
  58. package/lib/depositions/ListDepositionsOptions.d.ts +0 -32
  59. package/lib/depositions/ListDepositionsOptions.d.ts.map +0 -1
  60. package/lib/depositions/ListDepositionsOptions.js +0 -3
  61. package/lib/depositions/ListDepositionsOptions.js.map +0 -1
  62. package/lib/depositions/depositionSchema.d.ts +0 -982
  63. package/lib/depositions/depositionSchema.d.ts.map +0 -1
  64. package/lib/depositions/depositionSchema.js +0 -141
  65. package/lib/depositions/depositionSchema.js.map +0 -1
  66. package/lib-esm/Zenodo.d.ts +0 -19
  67. package/lib-esm/Zenodo.d.ts.map +0 -1
  68. package/lib-esm/Zenodo.js +0 -51
  69. package/lib-esm/Zenodo.js.map +0 -1
  70. package/lib-esm/ZenodoFile.d.ts +0 -45
  71. package/lib-esm/ZenodoFile.d.ts.map +0 -1
  72. package/lib-esm/ZenodoFile.js +0 -33
  73. package/lib-esm/ZenodoFile.js.map +0 -1
  74. package/lib-esm/depositions/Deposition.d.ts +0 -14
  75. package/lib-esm/depositions/Deposition.d.ts.map +0 -1
  76. package/lib-esm/depositions/Deposition.js +0 -51
  77. package/lib-esm/depositions/Deposition.js.map +0 -1
  78. package/lib-esm/depositions/ListDepositionsOptions.d.ts.map +0 -1
  79. package/lib-esm/depositions/ListDepositionsOptions.js.map +0 -1
  80. package/lib-esm/depositions/depositionSchema.d.ts +0 -982
  81. package/lib-esm/depositions/depositionSchema.d.ts.map +0 -1
  82. package/lib-esm/depositions/depositionSchema.js +0 -138
  83. package/lib-esm/depositions/depositionSchema.js.map +0 -1
  84. package/lib-esm/fetchZenodo.d.ts +0 -12
  85. package/lib-esm/fetchZenodo.d.ts.map +0 -1
  86. package/lib-esm/fetchZenodo.js +0 -31
  87. package/lib-esm/fetchZenodo.js.map +0 -1
  88. package/lib-esm/index.d.ts +0 -2
  89. package/lib-esm/index.d.ts.map +0 -1
  90. package/lib-esm/index.js +0 -2
  91. package/lib-esm/index.js.map +0 -1
  92. package/lib-esm/responseStatuses.d.ts +0 -8
  93. package/lib-esm/responseStatuses.d.ts.map +0 -1
  94. package/lib-esm/responseStatuses.js +0 -68
  95. package/lib-esm/responseStatuses.js.map +0 -1
  96. package/src/__tests__/Zenodo.test.ts +0 -79
  97. package/src/__tests__/getConfig.ts +0 -9
  98. package/src/depositions/Deposition.ts +0 -60
  99. package/src/depositions/depositionSchema.ts +0 -154
  100. /package/{lib-esm/depositions → lib/records}/ListDepositionsOptions.js +0 -0
package/package.json CHANGED
@@ -1,30 +1,30 @@
1
1
  {
2
2
  "name": "zenodo",
3
- "version": "2.0.0",
3
+ "version": "4.0.0",
4
4
  "description": "Node.js library to access the Zenodo API",
5
- "main": "./lib/index.js",
6
- "module": "./lib-esm/index.js",
7
- "types": "./lib/index.d.ts",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": "./lib/index.js"
8
+ },
8
9
  "files": [
9
10
  "src",
10
- "lib",
11
- "lib-esm"
11
+ "lib"
12
12
  ],
13
13
  "scripts": {
14
- "build": "npm run tsc-esm && cheminfo-build --entry lib-esm/index.js --root Zenodo",
14
+ "build": "npm run tsc && cheminfo-build --entry lib/index.js --root Zenodo",
15
15
  "build-watch": "fswatch -o src | xargs -n1 -I{} npm run build",
16
16
  "check-types": "tsc --noEmit",
17
- "clean": "rimraf lib lib-esm",
17
+ "clean": "rimraf lib",
18
18
  "eslint": "eslint src",
19
19
  "eslint-fix": "npm run eslint -- --fix",
20
20
  "prepack": "npm run tsc",
21
21
  "prettier": "prettier --check src",
22
22
  "prettier-write": "prettier --write src",
23
- "test": "npm run test-only && npm run eslint && npm run prettier && npm run check-types",
23
+ "test": "npm run test-only && npm run eslint && npm run prettier && npm run check-types && npm run verify-build",
24
24
  "test-only": "vitest run --coverage",
25
- "tsc": "npm run clean && npm run tsc-cjs && npm run tsc-esm",
26
- "tsc-cjs": "tsc --project tsconfig.cjs.json",
27
- "tsc-esm": "tsc --project tsconfig.esm.json"
25
+ "tsc": "npm run clean && npm run tsc-build",
26
+ "tsc-build": "tsc --project tsconfig.build.json",
27
+ "verify-build": "npm run tsc && node lib/index.js"
28
28
  },
29
29
  "repository": {
30
30
  "type": "git",
@@ -37,18 +37,24 @@
37
37
  "url": "https://github.com/cheminfo/zenodo/issues"
38
38
  },
39
39
  "homepage": "https://github.com/cheminfo/zenodo#readme",
40
+ "dependencies": {
41
+ "@zip.js/zip.js": "^2.7.62",
42
+ "delay": "^6.0.0",
43
+ "ml-spectra-processing": "^14.16.0",
44
+ "orcid-utils": "^1.2.2"
45
+ },
40
46
  "devDependencies": {
41
- "@types/node": "^22.13.4",
42
- "@vitest/coverage-v8": "^3.0.5",
47
+ "@types/node": "^24.0.3",
48
+ "@vitest/coverage-v8": "^3.2.3",
49
+ "@zakodium/tsconfig": "^1.0.2",
43
50
  "cheminfo-build": "^1.2.1",
44
- "dotenv": "^16.4.7",
45
- "eslint": "^9.20.1",
46
- "eslint-config-cheminfo-typescript": "^17.0.0",
47
- "prettier": "^3.5.1",
51
+ "cheminfo-types": "^1.8.1",
52
+ "dotenv": "^16.5.0",
53
+ "eslint": "^9.29.0",
54
+ "eslint-config-cheminfo-typescript": "^18.0.1",
55
+ "fifo-logger": "^1.0.0",
56
+ "prettier": "^3.5.3",
48
57
  "rimraf": "^6.0.1",
49
- "vitest": "^3.0.5"
50
- },
51
- "dependencies": {
52
- "zod": "^3.24.2"
58
+ "vitest": "^3.2.3"
53
59
  }
54
60
  }
package/src/Zenodo.ts CHANGED
@@ -1,68 +1,222 @@
1
- import { Deposition } from './depositions/Deposition';
2
- import type { ListDepositionsOptions } from './depositions/ListDepositionsOptions';
3
- import type { DepositionMetadata } from './depositions/depositionSchema';
4
- import { fetchZenodo } from './fetchZenodo';
1
+ import type { Logger } from 'cheminfo-types';
2
+ import { recursiveRemoveEmptyAndNull } from 'ml-spectra-processing';
3
+
4
+ import { ZenodoAuthenticationStates } from './ZenodoAuthenticationStates.ts';
5
+ import type { ZenodoAuthenticationStatesType } from './ZenodoAuthenticationStates.ts';
6
+ import { fetchZenodo } from './fetchZenodo.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';
5
11
 
6
12
  interface ZenodoOptions {
7
13
  accessToken: string;
8
14
  host?: string;
15
+ logger?: Logger;
16
+ }
17
+
18
+ export interface PublicRecordOptions {
19
+ isPublished?: boolean;
9
20
  }
21
+
10
22
  export class Zenodo {
11
23
  host: string;
12
24
  accessToken: string;
13
25
  baseURL: string;
26
+ logger?: Logger;
27
+ authenticationState: ZenodoAuthenticationStatesType;
14
28
 
15
29
  constructor(options: ZenodoOptions) {
16
- const { accessToken, host = 'sandbox.zenodo.org' } = options;
30
+ const { accessToken, host = 'sandbox.zenodo.org', logger } = options;
17
31
  this.host = host;
18
32
  this.baseURL = `https://${host}/api/`;
19
- if (!accessToken) {
20
- throw new Error('accessToken is required');
21
- }
33
+ this.logger = logger;
22
34
  this.accessToken = accessToken;
35
+ this.authenticationState = ZenodoAuthenticationStates.NOT_TRIED;
23
36
  }
24
37
 
25
- async listDepositions(
26
- options: ListDepositionsOptions = {},
27
- ): Promise<Deposition[]> {
38
+ /**
39
+ * Create a new Zenodo instance.
40
+ * This method authenticates the user and initializes a new Zenodo instance.
41
+ * @param options - the options for creating a Zenodo instance
42
+ * @throws {Error} If the authentication fails
43
+ * @returns a new Zenodo instance
44
+ */
45
+ static async create(options: ZenodoOptions): Promise<Zenodo> {
46
+ const zenodo = new Zenodo(options);
47
+ const response = await fetchZenodo(zenodo, {
48
+ route: 'user/records',
49
+ expectedStatus: 200,
50
+ });
51
+ if (!response.ok) {
52
+ throw new Error(`Failed to authenticate: ${response.statusText}`);
53
+ }
54
+ zenodo.logger?.info('Authenticated successfully');
55
+ zenodo.authenticationState = ZenodoAuthenticationStates.SUCCEEDED;
56
+ return zenodo;
57
+ }
58
+
59
+ /**
60
+ * Verify the authentication state of the Zenodo instance.
61
+ * This method checks if the access token is valid by making a request to the Zenodo API.
62
+ * @returns true if authentication is successful, false otherwise.
63
+ */
64
+ async verifyAuthentication(): Promise<boolean> {
65
+ const url = `${this.baseURL}user/records`;
66
+ const headers = new Headers();
67
+
68
+ if (this.accessToken) {
69
+ headers.set('Authorization', `Bearer ${this.accessToken}`);
70
+ }
71
+
72
+ // can't use fetchZenodo to avoid circular dependency
73
+ try {
74
+ const response = await fetch(url, {
75
+ method: 'GET',
76
+ headers,
77
+ });
78
+
79
+ if (response.status === 200) {
80
+ this.authenticationState = ZenodoAuthenticationStates.SUCCEEDED;
81
+ this.logger?.info('Authentication verified successfully');
82
+ return true;
83
+ } else {
84
+ this.authenticationState = ZenodoAuthenticationStates.FAILED;
85
+ this.logger?.warn(
86
+ `Authentication failed with status ${response.status}`,
87
+ );
88
+ return false;
89
+ }
90
+ } catch (error) {
91
+ this.authenticationState = ZenodoAuthenticationStates.FAILED;
92
+ this.logger?.error(
93
+ `Authentication verification failed: ${String(error)}`,
94
+ );
95
+ return false;
96
+ }
97
+ }
98
+
99
+ /**
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.
104
+ */
105
+ async listRecords(options: ListDepositionsOptions = {}): Promise<Record[]> {
28
106
  // all the values must be string
29
107
  const optionsWithStrings = Object.fromEntries(
30
- Object.entries(options).map(([key, value]) => [key, String(value)]),
108
+ Object.entries(options).map(([key, value]) => {
109
+ if (key === 'allVersions') {
110
+ return ['all_versions', String(value)];
111
+ }
112
+ return [key, String(value)];
113
+ }),
31
114
  );
115
+
32
116
  const response = await fetchZenodo(this, {
33
- route: 'deposit/depositions',
117
+ route: 'user/records',
34
118
  searchParams: optionsWithStrings,
35
119
  });
36
- const depositions = (await response.json()) as unknown[];
37
- return depositions.map(
38
- (deposition: unknown) => new Deposition(this, deposition),
39
- );
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));
40
123
  }
41
124
 
42
- async createDeposition(metadata: DepositionMetadata): Promise<Deposition> {
125
+ /**
126
+ * Creates a new record in the Zenodo instance.
127
+ * @param metadata - the metadata for the new record
128
+ * @throws {Error} If the metadata is invalid or the request fails
129
+ * @returns The created record object
130
+ */
131
+ async createRecord(metadata: ZenodoMetadata): Promise<Record> {
132
+ recursiveRemoveEmptyAndNull(metadata);
43
133
  const response = await fetchZenodo(this, {
44
- route: 'deposit/depositions',
134
+ route: 'records',
45
135
  expectedStatus: 201,
46
136
  method: 'POST',
47
137
  body: JSON.stringify({ metadata }),
48
138
  });
49
- const deposition = new Deposition(this, await response.json());
50
- return deposition;
139
+ const record = new Record(this, await response.json());
140
+ this.logger?.info(`Created record ${record.value.id}`);
141
+ return record;
51
142
  }
52
143
 
53
- async retrieveDeposition(id: number): Promise<Deposition> {
144
+ /**
145
+ * Retrieve a public deposition record
146
+ * @param id - the deposition id
147
+ * @param options - additional options for the request
148
+ * @throws {Error} If the deposition does not exist or the ID is undefined
149
+ * @returns The public deposition record
150
+ */
151
+ async retrieveRecord(
152
+ id: Identifier,
153
+ options: PublicRecordOptions = {},
154
+ ): Promise<Record> {
155
+ const { isPublished = false } = options;
156
+ const route = isPublished ? `records/${id}` : `records/${id}/draft`;
54
157
  const response = await fetchZenodo(this, {
55
- route: `deposit/depositions/${id}`,
158
+ route,
56
159
  });
57
160
  const deposition = await response.json();
58
- return new Deposition(this, deposition);
161
+ this.logger?.info(`Retrieved public deposition ${id}`);
162
+ return new Record(this, deposition);
163
+ }
164
+
165
+ /**
166
+ * List files in a deposition
167
+ * @param depositionId - the deposition id to retrieve requests for
168
+ * @returns An object containing the total number of requests and the list of requests
169
+ */
170
+ async retrieveRequests(
171
+ depositionId?: Identifier,
172
+ ): Promise<{ hits: { total: number; hits: ZenodoReview[] } }> {
173
+ const response = await fetchZenodo(this, {
174
+ route: `requests/`,
175
+ searchParams: {
176
+ q: String(depositionId),
177
+ },
178
+ });
179
+ const requests = (await response.json()) as {
180
+ hits: { total: number; hits: ZenodoReview[] };
181
+ };
182
+ this.logger?.info(
183
+ `Retrieved ${requests.hits.total} requests for deposition ${depositionId}`,
184
+ );
185
+ return requests;
186
+ }
187
+
188
+ /**
189
+ * Retrieve all versions of a deposition
190
+ * @param id - the deposition id
191
+ * @returns unvalidated array of deposition versions
192
+ */
193
+ async retrieveVersions(id: Identifier): Promise<Record[]> {
194
+ const response = await fetchZenodo(this, {
195
+ route: `records/${id}/versions`,
196
+ });
197
+ const versions = (await response.json()) as {
198
+ hits: { total: number; hits: unknown[] };
199
+ };
200
+ this.logger?.info(
201
+ `Retrieved ${versions?.hits.total} versions for deposition ${id}`,
202
+ );
203
+ const records = versions.hits.hits.map(
204
+ (version) => new Record(this, version),
205
+ );
206
+ return records;
59
207
  }
60
208
 
61
- 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`;
62
215
  await fetchZenodo(this, {
63
216
  method: 'DELETE',
64
- route: `deposit/depositions/${id}`,
217
+ route,
65
218
  expectedStatus: 204,
66
219
  });
220
+ this.logger?.info(`Deleted record ${id}`);
67
221
  }
68
222
  }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Represents the different states of Zenodo authentication for retry logic.
3
+ *
4
+ * Usage in retry mechanism (src/services/zenodoService.ts):
5
+ * The Zenodo API sometimes returns 401 Unauthorized or 403 Forbidden due to temporary issues.
6
+ * - If SUCCEEDED: Access token is valid, safe to retry the request
7
+ * - If FAILED: Access token is invalid/expired, do not retry
8
+ * - If NOT_TRIED: Can attempt authentication once to determine next steps
9
+ */
10
+ export const ZenodoAuthenticationStates = {
11
+ /** Authentication verification has not been attempted yet. */
12
+ NOT_TRIED: 0,
13
+ /** Authentication verification has failed (token is invalid/expired). */
14
+ FAILED: 1,
15
+ /** Authentication verification has succeeded (token is valid). */
16
+ SUCCEEDED: 2,
17
+ } as const;
18
+
19
+ /**
20
+ * Type representing the authentication state numeric codes.
21
+ */
22
+ export type ZenodoAuthenticationStatesType =
23
+ (typeof ZenodoAuthenticationStates)[keyof typeof ZenodoAuthenticationStates];
package/src/ZenodoFile.ts CHANGED
@@ -1,32 +1,38 @@
1
- import { z } from 'zod';
1
+ import type { Zenodo } from './Zenodo.ts';
2
2
 
3
- import type { Zenodo } from './Zenodo';
4
-
5
- // Define the Zod schema
6
- export const zenodoFileSchema = z.object({
7
- id: z.string(),
8
- filename: z.string(),
9
- filesize: z.number(),
10
- checksum: z.string(),
11
- links: z.object({
12
- download: z.string().url(),
13
- self: z.string().url(),
14
- }),
15
- });
16
-
17
- type ZenodoFileType = z.infer<typeof zenodoFileSchema>;
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
+ }
18
24
 
19
25
  export class ZenodoFile {
20
26
  private zenodo: Zenodo;
21
27
  public value: ZenodoFileType;
22
28
 
23
29
  constructor(zenodo: Zenodo, file: unknown) {
24
- this.value = zenodoFileSchema.parse(file);
30
+ this.value = file as ZenodoFileType;
25
31
  this.zenodo = zenodo;
26
32
  }
27
33
 
28
34
  async getContentResponse() {
29
- const link = this.value.links.download;
35
+ const link = this.value.links.content;
30
36
  const response = await fetch(link, {
31
37
  headers: {
32
38
  Authorization: `Bearer ${this.zenodo.accessToken}`,