arocapi 1.1.0 → 1.2.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 (127) hide show
  1. package/README.md +334 -8
  2. package/dist/app.d.ts +19 -1
  3. package/dist/app.js +35 -28
  4. package/dist/app.js.map +1 -1
  5. package/dist/app.test.js +124 -0
  6. package/dist/app.test.js.map +1 -0
  7. package/dist/index.dev.js +146 -0
  8. package/dist/index.dev.js.map +1 -0
  9. package/dist/routes/crate.d.ts +7 -0
  10. package/dist/routes/crate.js +99 -0
  11. package/dist/routes/crate.js.map +1 -0
  12. package/dist/routes/crate.test.d.ts +1 -0
  13. package/dist/routes/crate.test.js +360 -0
  14. package/dist/routes/crate.test.js.map +1 -0
  15. package/dist/routes/entities.d.ts +6 -1
  16. package/dist/routes/entities.js +51 -39
  17. package/dist/routes/entities.js.map +1 -1
  18. package/dist/routes/entities.test.d.ts +1 -0
  19. package/dist/routes/entities.test.js +245 -0
  20. package/dist/routes/entities.test.js.map +1 -0
  21. package/dist/routes/entity.d.ts +6 -1
  22. package/dist/routes/entity.js +28 -7
  23. package/dist/routes/entity.js.map +1 -1
  24. package/dist/routes/entity.test.d.ts +1 -0
  25. package/dist/routes/entity.test.js +109 -0
  26. package/dist/routes/entity.test.js.map +1 -0
  27. package/dist/routes/file.d.ts +7 -0
  28. package/dist/routes/file.js +111 -0
  29. package/dist/routes/file.js.map +1 -0
  30. package/dist/routes/file.test.d.ts +1 -0
  31. package/dist/routes/file.test.js +319 -0
  32. package/dist/routes/file.test.js.map +1 -0
  33. package/dist/routes/files.d.ts +8 -0
  34. package/dist/routes/files.js +61 -0
  35. package/dist/routes/files.js.map +1 -0
  36. package/dist/routes/files.test.d.ts +1 -0
  37. package/dist/routes/files.test.js +244 -0
  38. package/dist/routes/files.test.js.map +1 -0
  39. package/dist/routes/search.d.ts +6 -1
  40. package/dist/routes/search.js +43 -17
  41. package/dist/routes/search.js.map +1 -1
  42. package/dist/routes/search.test.d.ts +1 -0
  43. package/dist/routes/search.test.js +698 -0
  44. package/dist/routes/search.test.js.map +1 -0
  45. package/dist/test/helpers/fastify.d.ts +9 -0
  46. package/dist/test/helpers/fastify.js +24 -0
  47. package/dist/test/helpers/fastify.js.map +1 -0
  48. package/dist/test/integration.setup.d.ts +58 -0
  49. package/dist/test/integration.setup.js +181 -0
  50. package/dist/test/integration.setup.js.map +1 -0
  51. package/dist/test/integration.test.d.ts +1 -0
  52. package/dist/test/integration.test.js +337 -0
  53. package/dist/test/integration.test.js.map +1 -0
  54. package/dist/transformers/default.d.ts +45 -0
  55. package/dist/transformers/default.js +46 -0
  56. package/dist/transformers/default.js.map +1 -0
  57. package/dist/transformers/default.test.d.ts +1 -0
  58. package/dist/transformers/default.test.js +177 -0
  59. package/dist/transformers/default.test.js.map +1 -0
  60. package/dist/transformers/transformer.test.d.ts +1 -0
  61. package/dist/transformers/transformer.test.js +176 -0
  62. package/dist/transformers/transformer.test.js.map +1 -0
  63. package/dist/types/fileHandlers.d.ts +41 -0
  64. package/dist/types/fileHandlers.js +2 -0
  65. package/dist/types/fileHandlers.js.map +1 -0
  66. package/dist/types/transformers.d.ts +10 -0
  67. package/dist/types/transformers.js +2 -0
  68. package/dist/types/transformers.js.map +1 -0
  69. package/dist/utils/errors.d.ts +23 -0
  70. package/dist/utils/errors.js +21 -0
  71. package/dist/utils/errors.js.map +1 -0
  72. package/dist/utils/errors.test.d.ts +1 -0
  73. package/dist/utils/errors.test.js +66 -0
  74. package/dist/utils/errors.test.js.map +1 -0
  75. package/package.json +34 -25
  76. package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +10 -0
  77. package/prisma/migrations/20250923032753_api_refactor/migration.sql +39 -0
  78. package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +8 -0
  79. package/prisma/migrations/20251020140329_add_meta_column/migration.sql +2 -0
  80. package/prisma/migrations/20251027010713_add_file_table/migration.sql +17 -0
  81. package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +2 -0
  82. package/prisma/models/entity.prisma +16 -12
  83. package/prisma/models/file.prisma +20 -0
  84. package/prisma.config.ts +6 -1
  85. package/src/__snapshots__/app.test.ts.snap +8 -0
  86. package/src/app.test.ts +158 -0
  87. package/src/app.ts +97 -35
  88. package/src/index.dev.ts +196 -0
  89. package/src/reset.d.ts +1 -0
  90. package/src/routes/__snapshots__/entities.test.ts.snap +68 -0
  91. package/src/routes/__snapshots__/entity.test.ts.snap +66 -0
  92. package/src/routes/__snapshots__/search.test.ts.snap +139 -0
  93. package/src/routes/crate.test.ts +441 -0
  94. package/src/routes/crate.ts +141 -0
  95. package/src/routes/entities.test.ts +284 -0
  96. package/src/routes/entities.ts +64 -45
  97. package/src/routes/entity.test.ts +132 -0
  98. package/src/routes/entity.ts +57 -22
  99. package/src/routes/file.test.ts +399 -0
  100. package/src/routes/file.ts +153 -0
  101. package/src/routes/files.test.ts +303 -0
  102. package/src/routes/files.ts +87 -0
  103. package/src/routes/search.test.ts +807 -0
  104. package/src/routes/search.ts +66 -23
  105. package/src/test/__snapshots__/integration.test.ts.snap +18 -0
  106. package/src/test/helpers/fastify.ts +39 -0
  107. package/src/test/integration.setup.ts +200 -0
  108. package/src/test/integration.test.ts +411 -0
  109. package/src/transformers/default.test.ts +199 -0
  110. package/src/transformers/default.ts +165 -0
  111. package/src/transformers/transformer.test.ts +233 -0
  112. package/src/types/fileHandlers.ts +65 -0
  113. package/src/types/transformers.ts +52 -0
  114. package/src/utils/errors.test.ts +75 -0
  115. package/src/utils/errors.ts +45 -0
  116. package/dist/index.js +0 -25
  117. package/dist/index.js.map +0 -1
  118. package/dist/scripts/loadEntities.js +0 -170
  119. package/dist/scripts/loadEntities.js.map +0 -1
  120. package/dist/type.d.ts +0 -6
  121. package/dist/type.js +0 -2
  122. package/dist/type.js.map +0 -1
  123. package/src/index.ts +0 -35
  124. package/src/scripts/loadEntities.ts +0 -223
  125. package/src/type.ts +0 -7
  126. /package/dist/{scripts/loadEntities.d.ts → app.test.d.ts} +0 -0
  127. /package/dist/{index.d.ts → index.dev.d.ts} +0 -0
@@ -0,0 +1,196 @@
1
+ import { createReadStream, existsSync, readFileSync, statSync } from 'node:fs';
2
+ import { Readable } from 'node:stream';
3
+ import { Client } from '@opensearch-project/opensearch';
4
+ import Fastify from 'fastify';
5
+
6
+ import arocapi, {
7
+ AllPublicAccessTransformer,
8
+ AllPublicFileAccessTransformer,
9
+ type EntityTransformer,
10
+ type FileHandler,
11
+ type RoCrateHandler,
12
+ } from './app.js';
13
+ import type { Entity, File } from './generated/prisma/client.js';
14
+ import { PrismaClient } from './generated/prisma/client.js';
15
+
16
+ declare module 'fastify' {
17
+ interface FastifyInstance {
18
+ prisma: PrismaClient;
19
+ opensearch: Client;
20
+ }
21
+ }
22
+
23
+ const prisma = new PrismaClient();
24
+
25
+ if (!process.env.OPENSEARCH_URL) {
26
+ throw new Error('OPENSEARCH_URL environment variable is not set');
27
+ }
28
+ const opensearchUrl = process.env.OPENSEARCH_URL;
29
+ const opensearch = new Client({ node: opensearchUrl });
30
+
31
+ const fileHandler: FileHandler = {
32
+ head: async (file: File) => {
33
+ const filePath = (file.meta as { storagePath: string }).storagePath;
34
+
35
+ if (!existsSync(filePath)) {
36
+ return false;
37
+ }
38
+
39
+ const stats = statSync(filePath);
40
+
41
+ return {
42
+ contentType: file.mediaType,
43
+ contentLength: Number(file.size),
44
+ lastModified: stats.mtime,
45
+ };
46
+ },
47
+ get: async (file: File) => {
48
+ const filePath = (file.meta as { storagePath: string }).storagePath;
49
+
50
+ if (!existsSync(filePath)) {
51
+ return false;
52
+ }
53
+
54
+ const stats = statSync(filePath);
55
+
56
+ return {
57
+ type: 'stream',
58
+ stream: createReadStream(filePath),
59
+ metadata: {
60
+ contentType: file.mediaType,
61
+ contentLength: Number(file.size),
62
+ lastModified: stats.mtime,
63
+ },
64
+ };
65
+ },
66
+ };
67
+
68
+ const transformRoCrate = (filePath: string, remapRootTo: string): { rocrate: unknown; jsonString: string } | false => {
69
+ const fileContents = readFileSync(filePath, 'utf-8');
70
+ const rocrate = JSON.parse(fileContents) as {
71
+ '@context'?: unknown;
72
+ '@graph'?: Array<{ '@id': string; [key: string]: unknown }>;
73
+ [key: string]: unknown;
74
+ };
75
+
76
+ const graph = rocrate['@graph'] || [];
77
+ const rootNode = graph.find((node) => node['@id'] === 'ro-crate-metadata.json') as
78
+ | { about: { '@id': string } }
79
+ | undefined;
80
+
81
+ if (!rootNode) {
82
+ return false;
83
+ }
84
+
85
+ rootNode.about['@id'] = remapRootTo;
86
+
87
+ const jsonString = JSON.stringify(rocrate);
88
+
89
+ return { rocrate, jsonString };
90
+ };
91
+
92
+ const roCrateHandler: RoCrateHandler = {
93
+ head: async (entity: Entity) => {
94
+ const meta = entity.meta as { storagePath: string; remapRootTo: string };
95
+ const filePath = meta.storagePath;
96
+
97
+ if (!existsSync(filePath)) {
98
+ return false;
99
+ }
100
+
101
+ if (meta.remapRootTo) {
102
+ const result = transformRoCrate(filePath, meta.remapRootTo);
103
+
104
+ if (!result) {
105
+ return false;
106
+ }
107
+
108
+ return {
109
+ contentType: 'application/ld+json',
110
+ contentLength: Buffer.byteLength(result.jsonString),
111
+ lastModified: statSync(filePath).mtime,
112
+ };
113
+ }
114
+
115
+ const stats = statSync(filePath);
116
+
117
+ return {
118
+ contentType: 'application/ld+json',
119
+ contentLength: stats.size,
120
+ lastModified: stats.mtime,
121
+ };
122
+ },
123
+ get: async (entity: Entity) => {
124
+ const meta = entity.meta as { storagePath: string; remapRootTo: string };
125
+ const filePath = meta.storagePath;
126
+
127
+ if (!existsSync(filePath)) {
128
+ return false;
129
+ }
130
+
131
+ if (meta.remapRootTo) {
132
+ const result = transformRoCrate(filePath, meta.remapRootTo);
133
+
134
+ if (!result) {
135
+ return false;
136
+ }
137
+
138
+ return {
139
+ type: 'stream',
140
+ stream: Readable.from(result.jsonString),
141
+ metadata: {
142
+ contentType: 'application/ld+json',
143
+ contentLength: Buffer.byteLength(result.jsonString),
144
+ lastModified: statSync(filePath).mtime,
145
+ },
146
+ };
147
+ }
148
+
149
+ const stats = statSync(filePath);
150
+
151
+ return {
152
+ type: 'stream',
153
+ stream: createReadStream(filePath),
154
+ metadata: {
155
+ contentType: 'application/ld+json',
156
+ contentLength: stats.size,
157
+ lastModified: stats.mtime,
158
+ },
159
+ };
160
+ },
161
+ };
162
+
163
+ const oniTransformer: EntityTransformer = (entity, { request: _r, fastify: _f }) => {
164
+ return {
165
+ ...entity,
166
+ accessControl: 'Public',
167
+ counts: {
168
+ collections: 0,
169
+ objects: 0,
170
+ files: 0,
171
+ },
172
+ };
173
+ };
174
+
175
+ const fastify = Fastify({
176
+ logger: true,
177
+ });
178
+ fastify.register(arocapi, {
179
+ prisma,
180
+ opensearch,
181
+ fileHandler,
182
+ roCrateHandler,
183
+ accessTransformer: AllPublicAccessTransformer,
184
+ entityTransformers: [oniTransformer],
185
+ fileAccessTransformer: AllPublicFileAccessTransformer,
186
+ });
187
+
188
+ const start = async () => {
189
+ try {
190
+ await fastify.listen({ port: 9000 });
191
+ } catch (err) {
192
+ fastify.log.error(err);
193
+ process.exit(1);
194
+ }
195
+ };
196
+ start();
package/src/reset.d.ts ADDED
@@ -0,0 +1 @@
1
+ import '@total-typescript/ts-reset';
@@ -0,0 +1,68 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`Entities Route > GET /entities > should apply custom entity transformers 1`] = `
4
+ {
5
+ "entities": [
6
+ {
7
+ "access": {
8
+ "content": true,
9
+ "metadata": true,
10
+ },
11
+ "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
12
+ "description": "First test entity",
13
+ "entityType": "http://pcdm.org/models#Collection",
14
+ "id": "http://example.com/entity/1",
15
+ "memberOf": null,
16
+ "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
17
+ "name": "Test Entity 1",
18
+ "rootCollection": null,
19
+ "tested": true,
20
+ },
21
+ ],
22
+ }
23
+ `;
24
+
25
+ exports[`Entities Route > GET /entities > should return 500 when database error occurs 1`] = `
26
+ {
27
+ "error": {
28
+ "code": "INTERNAL_ERROR",
29
+ "message": "Internal server error",
30
+ },
31
+ }
32
+ `;
33
+
34
+ exports[`Entities Route > GET /entities > should return entities with default pagination 1`] = `
35
+ {
36
+ "entities": [
37
+ {
38
+ "access": {
39
+ "content": true,
40
+ "metadata": true,
41
+ },
42
+ "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
43
+ "description": "First test entity",
44
+ "entityType": "http://pcdm.org/models#Collection",
45
+ "id": "http://example.com/entity/1",
46
+ "memberOf": null,
47
+ "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
48
+ "name": "Test Entity 1",
49
+ "rootCollection": null,
50
+ },
51
+ {
52
+ "access": {
53
+ "content": true,
54
+ "metadata": true,
55
+ },
56
+ "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
57
+ "description": "Second test entity",
58
+ "entityType": "http://pcdm.org/models#Object",
59
+ "id": "http://example.com/entity/2",
60
+ "memberOf": "http://example.com/entity/1",
61
+ "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
62
+ "name": "Test Entity 2",
63
+ "rootCollection": "http://example.com/entity/1",
64
+ },
65
+ ],
66
+ "total": 2,
67
+ }
68
+ `;
@@ -0,0 +1,66 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`Entity Route > GET /entity/:id > should apply custom entity transformers 1`] = `
4
+ {
5
+ "access": {
6
+ "content": true,
7
+ "metadata": true,
8
+ },
9
+ "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
10
+ "description": "A test entity",
11
+ "entityType": "http://pcdm.org/models#Collection",
12
+ "id": "http://example.com/entity/123",
13
+ "memberOf": null,
14
+ "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
15
+ "name": "Test Entity",
16
+ "rootCollection": null,
17
+ "tested": true,
18
+ }
19
+ `;
20
+
21
+ exports[`Entity Route > GET /entity/:id > should return 404 when entity not found 1`] = `
22
+ {
23
+ "error": {
24
+ "code": "NOT_FOUND",
25
+ "details": {
26
+ "entityId": "http://example.com/entity/nonexistent",
27
+ },
28
+ "message": "The requested entity was not found",
29
+ },
30
+ }
31
+ `;
32
+
33
+ exports[`Entity Route > GET /entity/:id > should return 500 when database error occurs 1`] = `
34
+ {
35
+ "error": {
36
+ "code": "INTERNAL_ERROR",
37
+ "message": "Internal server error",
38
+ },
39
+ }
40
+ `;
41
+
42
+ exports[`Entity Route > GET /entity/:id > should return entity when found 1`] = `
43
+ {
44
+ "access": {
45
+ "content": true,
46
+ "metadata": true,
47
+ },
48
+ "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
49
+ "description": "A test entity",
50
+ "entityType": "http://schema.org/Person",
51
+ "id": "http://example.com/entity/123",
52
+ "memberOf": null,
53
+ "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
54
+ "name": "Test Entity",
55
+ "rootCollection": null,
56
+ }
57
+ `;
58
+
59
+ exports[`Entity Route > GET /entity/:id > should validate ID parameter format 1`] = `
60
+ {
61
+ "code": "FST_ERR_VALIDATION",
62
+ "error": "Bad Request",
63
+ "message": "params/id Invalid URI format",
64
+ "statusCode": 400,
65
+ }
66
+ `;
@@ -0,0 +1,139 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`Search Route > POST /search > should apply custom entity transformers 1`] = `
4
+ {
5
+ "entities": [
6
+ {
7
+ "access": {
8
+ "content": true,
9
+ "metadata": true,
10
+ },
11
+ "contentLicenseId": null,
12
+ "description": "A test entity",
13
+ "entityType": "http://pcdm.org/models#Collection",
14
+ "id": "http://example.com/entity/1",
15
+ "memberOf": null,
16
+ "metadataLicenseId": null,
17
+ "name": "Test Entity 1",
18
+ "rootCollection": null,
19
+ "searchExtra": {
20
+ "highlight": {},
21
+ "score": 1.5,
22
+ },
23
+ "tested": true,
24
+ },
25
+ ],
26
+ "searchTime": 10,
27
+ "total": 1,
28
+ }
29
+ `;
30
+
31
+ exports[`Search Route > POST /search > should handle opensearch errors 1`] = `
32
+ {
33
+ "error": {
34
+ "code": "INTERNAL_ERROR",
35
+ "message": "Search failed",
36
+ },
37
+ }
38
+ `;
39
+
40
+ exports[`Search Route > POST /search > should handle rocrateId explicitly set to undefined 1`] = `
41
+ {
42
+ "error": {
43
+ "code": "INTERNAL_ERROR",
44
+ "message": "Search failed",
45
+ },
46
+ }
47
+ `;
48
+
49
+ exports[`Search Route > POST /search > should perform basic search successfully 1`] = `
50
+ {
51
+ "entities": [
52
+ {
53
+ "access": {
54
+ "content": true,
55
+ "metadata": true,
56
+ },
57
+ "contentLicenseId": null,
58
+ "description": "A test entity",
59
+ "entityType": "http://pcdm.org/models#Collection",
60
+ "id": "http://example.com/entity/1",
61
+ "memberOf": null,
62
+ "metadataLicenseId": null,
63
+ "name": "Test Entity 1",
64
+ "rootCollection": null,
65
+ "searchExtra": {
66
+ "highlight": {
67
+ "name": [
68
+ "<em>Test</em> Entity 1",
69
+ ],
70
+ },
71
+ "score": 1.5,
72
+ },
73
+ },
74
+ {
75
+ "access": {
76
+ "content": true,
77
+ "metadata": true,
78
+ },
79
+ "contentLicenseId": null,
80
+ "description": "Another test entity",
81
+ "entityType": "http://pcdm.org/models#Object",
82
+ "id": "http://example.com/entity/2",
83
+ "memberOf": null,
84
+ "metadataLicenseId": null,
85
+ "name": "Test Entity 2",
86
+ "rootCollection": null,
87
+ "searchExtra": {
88
+ "highlight": {
89
+ "description": [
90
+ "Another <em>test</em> entity",
91
+ ],
92
+ },
93
+ "score": 1.2,
94
+ },
95
+ },
96
+ ],
97
+ "facets": {
98
+ "entityType": [
99
+ {
100
+ "count": 1,
101
+ "name": "http://pcdm.org/models#Collection",
102
+ },
103
+ {
104
+ "count": 1,
105
+ "name": "http://pcdm.org/models#Object",
106
+ },
107
+ ],
108
+ },
109
+ "searchTime": 10,
110
+ "total": 2,
111
+ }
112
+ `;
113
+
114
+ exports[`Search Route > POST /search > should skip entities not found in database and log warning 1`] = `
115
+ {
116
+ "entities": [
117
+ {
118
+ "access": {
119
+ "content": true,
120
+ "metadata": true,
121
+ },
122
+ "contentLicenseId": null,
123
+ "description": "A test entity",
124
+ "entityType": "http://pcdm.org/models#Collection",
125
+ "id": "http://example.com/entity/1",
126
+ "memberOf": null,
127
+ "metadataLicenseId": null,
128
+ "name": "Test Entity 1",
129
+ "rootCollection": null,
130
+ "searchExtra": {
131
+ "highlight": {},
132
+ "score": 1.5,
133
+ },
134
+ },
135
+ ],
136
+ "searchTime": 10,
137
+ "total": 2,
138
+ }
139
+ `;