arocapi 5.0.0-beta.5 → 5.0.0-beta.7

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 (205) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +2 -2
  3. package/dist/app.d.ts +22 -17
  4. package/dist/app.js +22 -16
  5. package/dist/app.js.map +1 -1
  6. package/dist/routes/capabilities.d.ts +2 -2
  7. package/dist/routes/capabilities.js +2 -2
  8. package/dist/routes/capabilities.js.map +1 -1
  9. package/dist/routes/entities.js +8 -9
  10. package/dist/routes/entities.js.map +1 -1
  11. package/dist/routes/file.d.ts +2 -2
  12. package/dist/routes/file.js +25 -72
  13. package/dist/routes/file.js.map +1 -1
  14. package/dist/routes/metadata.d.ts +5 -5
  15. package/dist/routes/metadata.js +22 -61
  16. package/dist/routes/metadata.js.map +1 -1
  17. package/dist/routes/search.d.ts +2 -4
  18. package/dist/routes/search.js +75 -97
  19. package/dist/routes/search.js.map +1 -1
  20. package/dist/search/advanced.d.ts +8 -0
  21. package/dist/search/advanced.js +198 -0
  22. package/dist/search/advanced.js.map +1 -0
  23. package/dist/search/backend.d.ts +95 -0
  24. package/dist/search/backend.js +5 -0
  25. package/dist/search/backend.js.map +1 -0
  26. package/dist/search/fields.d.ts +63 -0
  27. package/dist/search/fields.js +203 -0
  28. package/dist/search/fields.js.map +1 -0
  29. package/dist/search/opensearch/advanced.d.ts +4 -0
  30. package/dist/search/opensearch/advanced.js +28 -0
  31. package/dist/search/opensearch/advanced.js.map +1 -0
  32. package/dist/search/opensearch/opensearch.d.ts +16 -0
  33. package/dist/search/opensearch/opensearch.js +187 -0
  34. package/dist/search/opensearch/opensearch.js.map +1 -0
  35. package/dist/search/opensearch/visibility.d.ts +3 -0
  36. package/dist/search/opensearch/visibility.js +27 -0
  37. package/dist/search/opensearch/visibility.js.map +1 -0
  38. package/dist/search/postgres/advanced.d.ts +7 -0
  39. package/dist/search/postgres/advanced.js +54 -0
  40. package/dist/search/postgres/advanced.js.map +1 -0
  41. package/dist/search/postgres/facets.d.ts +2 -0
  42. package/dist/search/postgres/facets.js +27 -0
  43. package/dist/search/postgres/facets.js.map +1 -0
  44. package/dist/search/postgres/geo.d.ts +6 -0
  45. package/dist/search/postgres/geo.js +42 -0
  46. package/dist/search/postgres/geo.js.map +1 -0
  47. package/dist/search/postgres/postgres.d.ts +19 -0
  48. package/dist/search/postgres/postgres.js +157 -0
  49. package/dist/search/postgres/postgres.js.map +1 -0
  50. package/dist/search/postgres/values.d.ts +8 -0
  51. package/dist/search/postgres/values.js +20 -0
  52. package/dist/search/postgres/values.js.map +1 -0
  53. package/dist/search/postgres/visibility.d.ts +4 -0
  54. package/dist/search/postgres/visibility.js +38 -0
  55. package/dist/search/postgres/visibility.js.map +1 -0
  56. package/dist/search/requestFilters.d.ts +39 -0
  57. package/dist/search/requestFilters.js +125 -0
  58. package/dist/search/requestFilters.js.map +1 -0
  59. package/dist/search/visibility.d.ts +51 -0
  60. package/dist/search/visibility.js +100 -0
  61. package/dist/search/visibility.js.map +1 -0
  62. package/dist/storage/backend.d.ts +58 -0
  63. package/dist/storage/backend.js +2 -0
  64. package/dist/storage/backend.js.map +1 -0
  65. package/dist/storage/deliver.d.ts +12 -0
  66. package/dist/storage/deliver.js +34 -0
  67. package/dist/storage/deliver.js.map +1 -0
  68. package/dist/storage/directory/directory.d.ts +6 -0
  69. package/dist/storage/directory/directory.js +24 -0
  70. package/dist/storage/directory/directory.js.map +1 -0
  71. package/dist/storage/entity-meta/entity-meta.d.ts +5 -0
  72. package/dist/storage/entity-meta/entity-meta.js +14 -0
  73. package/dist/storage/entity-meta/entity-meta.js.map +1 -0
  74. package/dist/storage/location.d.ts +20 -0
  75. package/dist/storage/location.js +40 -0
  76. package/dist/storage/location.js.map +1 -0
  77. package/dist/storage/ocfl/ocfl.d.ts +7 -0
  78. package/dist/storage/ocfl/ocfl.js +79 -0
  79. package/dist/storage/ocfl/ocfl.js.map +1 -0
  80. package/dist/storage/ocfl/ocflFs.d.ts +7 -0
  81. package/dist/storage/ocfl/ocflFs.js +33 -0
  82. package/dist/storage/ocfl/ocflFs.js.map +1 -0
  83. package/dist/storage/results.d.ts +7 -0
  84. package/dist/storage/results.js +22 -0
  85. package/dist/storage/results.js.map +1 -0
  86. package/dist/storage/s3/s3.d.ts +10 -0
  87. package/dist/storage/s3/s3.js +47 -0
  88. package/dist/storage/s3/s3.js.map +1 -0
  89. package/dist/types/visibility.d.ts +3 -2
  90. package/dist/utils/visibility.js +1 -1
  91. package/dist/utils/visibility.js.map +1 -1
  92. package/package.json +66 -26
  93. package/prisma/models/entity.prisma +9 -8
  94. package/prisma/models/entitySearch.prisma +21 -0
  95. package/prisma/models/file.prisma +3 -3
  96. package/src/app.ts +85 -48
  97. package/src/routes/capabilities.ts +4 -4
  98. package/src/routes/entities.ts +10 -9
  99. package/src/routes/file.ts +23 -99
  100. package/src/routes/metadata.ts +24 -86
  101. package/src/routes/search.ts +104 -118
  102. package/src/search/advanced.ts +258 -0
  103. package/src/search/backend.ts +91 -0
  104. package/src/search/fields.ts +320 -0
  105. package/src/search/opensearch/advanced.ts +36 -0
  106. package/src/search/opensearch/opensearch.ts +290 -0
  107. package/src/search/opensearch/visibility.ts +38 -0
  108. package/src/search/postgres/advanced.ts +76 -0
  109. package/src/search/postgres/facets.ts +34 -0
  110. package/src/search/postgres/geo.ts +58 -0
  111. package/src/search/postgres/postgres.ts +232 -0
  112. package/src/search/postgres/values.ts +33 -0
  113. package/src/search/postgres/visibility.ts +54 -0
  114. package/src/search/requestFilters.ts +178 -0
  115. package/src/search/visibility.ts +193 -0
  116. package/src/storage/backend.ts +75 -0
  117. package/src/storage/deliver.ts +65 -0
  118. package/src/storage/directory/directory.ts +39 -0
  119. package/src/storage/entity-meta/entity-meta.ts +25 -0
  120. package/src/storage/location.ts +85 -0
  121. package/src/storage/ocfl/ocfl.ts +129 -0
  122. package/src/storage/ocfl/ocflFs.ts +67 -0
  123. package/src/storage/results.ts +32 -0
  124. package/src/storage/s3/s3.ts +79 -0
  125. package/src/types/visibility.ts +6 -3
  126. package/src/utils/visibility.ts +1 -1
  127. package/dist/app.test.d.ts +0 -1
  128. package/dist/app.test.js +0 -67
  129. package/dist/app.test.js.map +0 -1
  130. package/dist/errorHandling.test.d.ts +0 -1
  131. package/dist/errorHandling.test.js +0 -222
  132. package/dist/errorHandling.test.js.map +0 -1
  133. package/dist/index.dev.d.ts +0 -1
  134. package/dist/index.dev.js +0 -157
  135. package/dist/index.dev.js.map +0 -1
  136. package/dist/routes/capabilities.test.d.ts +0 -1
  137. package/dist/routes/capabilities.test.js +0 -81
  138. package/dist/routes/capabilities.test.js.map +0 -1
  139. package/dist/routes/crate.test.d.ts +0 -1
  140. package/dist/routes/crate.test.js +0 -390
  141. package/dist/routes/crate.test.js.map +0 -1
  142. package/dist/routes/entities.test.d.ts +0 -1
  143. package/dist/routes/entities.test.js +0 -277
  144. package/dist/routes/entities.test.js.map +0 -1
  145. package/dist/routes/entity.test.d.ts +0 -1
  146. package/dist/routes/entity.test.js +0 -136
  147. package/dist/routes/entity.test.js.map +0 -1
  148. package/dist/routes/file.test.d.ts +0 -1
  149. package/dist/routes/file.test.js +0 -435
  150. package/dist/routes/file.test.js.map +0 -1
  151. package/dist/routes/files.test.d.ts +0 -1
  152. package/dist/routes/files.test.js +0 -245
  153. package/dist/routes/files.test.js.map +0 -1
  154. package/dist/routes/search.test.d.ts +0 -1
  155. package/dist/routes/search.test.js +0 -833
  156. package/dist/routes/search.test.js.map +0 -1
  157. package/dist/test/helpers/fastify.d.ts +0 -9
  158. package/dist/test/helpers/fastify.js +0 -17
  159. package/dist/test/helpers/fastify.js.map +0 -1
  160. package/dist/test/integration.setup.d.ts +0 -47
  161. package/dist/test/integration.setup.js +0 -195
  162. package/dist/test/integration.setup.js.map +0 -1
  163. package/dist/test/integration.test.d.ts +0 -1
  164. package/dist/test/integration.test.js +0 -762
  165. package/dist/test/integration.test.js.map +0 -1
  166. package/dist/transformers/default.test.d.ts +0 -1
  167. package/dist/transformers/default.test.js +0 -225
  168. package/dist/transformers/default.test.js.map +0 -1
  169. package/dist/transformers/transformer.test.d.ts +0 -1
  170. package/dist/transformers/transformer.test.js +0 -165
  171. package/dist/transformers/transformer.test.js.map +0 -1
  172. package/dist/types/fileHandlers.d.ts +0 -37
  173. package/dist/types/fileHandlers.js +0 -2
  174. package/dist/types/fileHandlers.js.map +0 -1
  175. package/dist/utils/errors.test.d.ts +0 -1
  176. package/dist/utils/errors.test.js +0 -79
  177. package/dist/utils/errors.test.js.map +0 -1
  178. package/dist/utils/queryBuilder.d.ts +0 -47
  179. package/dist/utils/queryBuilder.js +0 -107
  180. package/dist/utils/queryBuilder.js.map +0 -1
  181. package/prisma.config.ts +0 -9
  182. package/src/app.test.ts +0 -91
  183. package/src/errorHandling.test.ts +0 -288
  184. package/src/index.dev.ts +0 -202
  185. package/src/reset.d.ts +0 -1
  186. package/src/routes/__snapshots__/capabilities.test.ts.snap +0 -34
  187. package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
  188. package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
  189. package/src/routes/__snapshots__/search.test.ts.snap +0 -165
  190. package/src/routes/capabilities.test.ts +0 -102
  191. package/src/routes/crate.test.ts +0 -484
  192. package/src/routes/entities.test.ts +0 -324
  193. package/src/routes/entity.test.ts +0 -166
  194. package/src/routes/file.test.ts +0 -538
  195. package/src/routes/files.test.ts +0 -304
  196. package/src/routes/search.test.ts +0 -959
  197. package/src/test/__snapshots__/integration.test.ts.snap +0 -18
  198. package/src/test/helpers/fastify.ts +0 -26
  199. package/src/test/integration.setup.ts +0 -220
  200. package/src/test/integration.test.ts +0 -953
  201. package/src/transformers/default.test.ts +0 -276
  202. package/src/transformers/transformer.test.ts +0 -226
  203. package/src/types/fileHandlers.ts +0 -57
  204. package/src/utils/errors.test.ts +0 -90
  205. package/src/utils/queryBuilder.ts +0 -157
@@ -1,276 +0,0 @@
1
- import type { FastifyInstance, FastifyRequest } from 'fastify';
2
- import { beforeEach, describe, expect, it, vi } from 'vitest';
3
- import { mockReset } from 'vitest-mock-extended';
4
- import type { Entity } from '../generated/prisma/client.js';
5
- import { prisma } from '../test/helpers/fastify.js';
6
- import type { AccessFunction } from '../types/access.js';
7
- import type { RequestContext } from '../types/context.js';
8
- import { AllPublicAccess } from '../utils/access.js';
9
- import { applyTransformers, authoriseEntities, authoriseFiles, baseEntityTransformer } from './default.js';
10
-
11
- const context: RequestContext = {
12
- request: {} as FastifyRequest,
13
- fastify: {} as FastifyInstance,
14
- };
15
-
16
- describe('baseEntityTransformer', () => {
17
- it('should transform entity to standard entity shape', () => {
18
- const entity: Entity & { file?: { id: string } | null } = {
19
- id: 'http://example.com/entity/123',
20
- name: 'Test Entity',
21
- description: 'A test entity description',
22
- entityType: 'http://pcdm.org/models#Collection',
23
- memberOf: 'http://example.com/parent',
24
- rootCollection: 'http://example.com/root',
25
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
26
- contentLicenseId: 'https://creativecommons.org/licenses/by-sa/4.0/',
27
- createdAt: new Date('2025-01-01'),
28
- updatedAt: new Date('2025-01-02'),
29
- meta: { test: 'data' },
30
- };
31
-
32
- const result = baseEntityTransformer(entity);
33
-
34
- expect(result).toEqual({
35
- id: 'http://example.com/entity/123',
36
- name: 'Test Entity',
37
- description: 'A test entity description',
38
- entityType: 'http://pcdm.org/models#Collection',
39
- memberOf: 'http://example.com/parent',
40
- rootCollection: 'http://example.com/root',
41
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
42
- contentLicenseId: 'https://creativecommons.org/licenses/by-sa/4.0/',
43
- });
44
- });
45
-
46
- it('should handle null memberOf and rootCollection', () => {
47
- const entity: Entity & { file?: { id: string } | null } = {
48
- id: 'http://example.com/collection',
49
- name: 'Top Collection',
50
- description: 'A top-level collection',
51
- entityType: 'http://pcdm.org/models#Collection',
52
- memberOf: null,
53
- rootCollection: null,
54
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
55
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
56
- createdAt: new Date(),
57
- updatedAt: new Date(),
58
- meta: null,
59
- };
60
-
61
- const result = baseEntityTransformer(entity);
62
-
63
- expect(result.memberOf).toBeNull();
64
- expect(result.rootCollection).toBeNull();
65
- });
66
-
67
- it('should exclude database-specific fields', () => {
68
- const entity: Entity & { file?: { id: string } | null } = {
69
- id: 'http://example.com/entity/456',
70
- name: 'Test',
71
- description: 'Test',
72
- entityType: 'http://pcdm.org/models#Object',
73
- memberOf: null,
74
- rootCollection: null,
75
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
76
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
77
- createdAt: new Date(),
78
- updatedAt: new Date(),
79
- meta: { storage: 'path' },
80
- };
81
-
82
- const result = baseEntityTransformer(entity);
83
-
84
- expect(result.id).toBe('http://example.com/entity/456');
85
- expect(result).not.toHaveProperty('createdAt');
86
- expect(result).not.toHaveProperty('updatedAt');
87
- expect(result).not.toHaveProperty('meta');
88
- expect(Object.keys(result)).toEqual([
89
- 'id',
90
- 'name',
91
- 'description',
92
- 'entityType',
93
- 'memberOf',
94
- 'rootCollection',
95
- 'metadataLicenseId',
96
- 'contentLicenseId',
97
- ]);
98
- });
99
-
100
- it('should handle File entity (MediaObject) with file relation', () => {
101
- const entity: Entity & { file?: { id: string } | null } = {
102
- id: 'http://example.com/file/audio.wav',
103
- name: 'Audio File',
104
- description: 'An audio recording',
105
- entityType: 'http://schema.org/MediaObject',
106
- memberOf: 'http://example.com/collection',
107
- rootCollection: 'http://example.com/collection',
108
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
109
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
110
- createdAt: new Date(),
111
- updatedAt: new Date(),
112
- meta: null,
113
- file: { id: 'http://example.com/files/audio.wav' },
114
- };
115
-
116
- const result = baseEntityTransformer(entity);
117
-
118
- expect(result).toEqual({
119
- id: 'http://example.com/file/audio.wav',
120
- name: 'Audio File',
121
- description: 'An audio recording',
122
- entityType: 'http://schema.org/MediaObject',
123
- memberOf: 'http://example.com/collection',
124
- rootCollection: 'http://example.com/collection',
125
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
126
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
127
- });
128
- });
129
- });
130
-
131
- describe('AllPublicAccess', () => {
132
- it('should grant access to metadata and content without authorisation URLs', () => {
133
- const baseEntity = {
134
- id: 'http://example.com/entity/123',
135
- name: 'Test Entity',
136
- description: 'A test entity',
137
- entityType: 'http://schema.org/MediaObject',
138
- memberOf: 'http://example.com/parent',
139
- rootCollection: 'http://example.com/root',
140
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
141
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
142
- };
143
-
144
- expect(AllPublicAccess(baseEntity, context)).toEqual({ metadata: true, content: true });
145
- });
146
- });
147
-
148
- const dbEntity = (id: string, overrides: Partial<Entity> = {}): Entity => ({
149
- id,
150
- name: `Name of ${id}`,
151
- description: '',
152
- entityType: 'http://schema.org/MediaObject',
153
- memberOf: null,
154
- rootCollection: null,
155
- metadataLicenseId: 'https://example.com/licence/metadata',
156
- contentLicenseId: 'https://example.com/licence/content',
157
- createdAt: new Date(),
158
- updatedAt: new Date(),
159
- meta: null,
160
- ...overrides,
161
- });
162
-
163
- describe('authoriseEntities', () => {
164
- beforeEach(() => {
165
- mockReset(prisma);
166
- });
167
-
168
- it('should pass the access function parent ids but return the Record with parent names', async () => {
169
- prisma.entity.findMany.mockResolvedValue([dbEntity('http://example.com/collection')]);
170
- const access = vi.fn<AccessFunction>(AllPublicAccess);
171
-
172
- const [entity] = await authoriseEntities(
173
- [dbEntity('http://example.com/item', { memberOf: 'http://example.com/collection' })],
174
- prisma,
175
- access,
176
- context,
177
- );
178
-
179
- expect(access).toHaveBeenCalledWith(
180
- expect.objectContaining({ memberOf: 'http://example.com/collection' }),
181
- context,
182
- );
183
- expect(entity.memberOf).toEqual({
184
- id: 'http://example.com/collection',
185
- name: 'Name of http://example.com/collection',
186
- });
187
- expect(entity.access).toEqual({ metadata: true, content: true });
188
- });
189
-
190
- it('should keep only the access keys the library defines', async () => {
191
- const access = vi.fn(async () => ({
192
- metadata: false,
193
- content: false,
194
- metadataAuthorizationUrl: 'https://example.com/request-metadata',
195
- contentAuthorizationUrl: 'https://example.com/request-content',
196
- licence: 'secret',
197
- }));
198
-
199
- const [entity] = await authoriseEntities([dbEntity('http://example.com/item')], prisma, access, context);
200
-
201
- expect(entity.access).toEqual({
202
- metadata: false,
203
- content: false,
204
- metadataAuthorizationUrl: 'https://example.com/request-metadata',
205
- contentAuthorizationUrl: 'https://example.com/request-content',
206
- });
207
- });
208
- });
209
-
210
- describe('authoriseFiles', () => {
211
- beforeEach(() => {
212
- mockReset(prisma);
213
- });
214
-
215
- const dbFile = (id: string) => ({
216
- id,
217
- filename: 'audio.wav',
218
- mediaType: 'audio/wav',
219
- size: BigInt(2048),
220
- meta: null,
221
- createdAt: new Date(),
222
- updatedAt: new Date(),
223
- entity: dbEntity(id),
224
- });
225
-
226
- it("should attach only the Content access of each file's entity", async () => {
227
- const access: AccessFunction = (entity) =>
228
- entity.id === 'http://example.com/restricted.wav'
229
- ? {
230
- metadata: false,
231
- content: false,
232
- metadataAuthorizationUrl: 'https://example.com/request-metadata',
233
- contentAuthorizationUrl: 'https://example.com/request-content',
234
- }
235
- : { metadata: false, content: true };
236
-
237
- const files = await authoriseFiles(
238
- [dbFile('http://example.com/restricted.wav'), dbFile('http://example.com/open.wav')],
239
- access,
240
- context,
241
- );
242
-
243
- expect(files).toEqual([
244
- {
245
- id: 'http://example.com/restricted.wav',
246
- filename: 'audio.wav',
247
- mediaType: 'audio/wav',
248
- size: 2048,
249
- access: { content: false, contentAuthorizationUrl: 'https://example.com/request-content' },
250
- },
251
- {
252
- id: 'http://example.com/open.wav',
253
- filename: 'audio.wav',
254
- mediaType: 'audio/wav',
255
- size: 2048,
256
- access: { content: true },
257
- },
258
- ]);
259
- });
260
- });
261
-
262
- describe('applyTransformers', () => {
263
- it('should chain transformers in order', async () => {
264
- const result = await applyTransformers(
265
- 'a',
266
- [(item) => `${item}b`, async (item, { request }) => `${item}c${request.url}`],
267
- { ...context, request: { url: '/d' } as FastifyRequest },
268
- );
269
-
270
- expect(result).toBe('abc/d');
271
- });
272
-
273
- it('should return the item unchanged without transformers', async () => {
274
- expect(await applyTransformers('a', [], context)).toBe('a');
275
- });
276
- });
@@ -1,226 +0,0 @@
1
- import type { FastifyInstance, FastifyRequest } from 'fastify';
2
- import { describe, expect, it } from 'vitest';
3
- import { prisma } from '../test/helpers/fastify.js';
4
- import type { AccessFunction } from '../types/access.js';
5
- import type { RequestContext } from '../types/context.js';
6
- import type { EntityTransformer } from '../types/transformers.js';
7
- import {
8
- type AuthorisedEntity,
9
- applyTransformers,
10
- authoriseEntities,
11
- type BaseEntity,
12
- baseEntityTransformer,
13
- type StandardEntity,
14
- } from './default.js';
15
-
16
- describe('Entity Transformers', () => {
17
- const mockContext: RequestContext = {
18
- request: {} as FastifyRequest,
19
- fastify: {} as FastifyInstance,
20
- };
21
-
22
- const mockStandardEntity: StandardEntity = {
23
- id: 'http://example.com/entity/123',
24
- name: 'Test Entity',
25
- description: 'A test entity',
26
- entityType: 'http://schema.org/Person',
27
- memberOf: { id: 'http://example.com/collection', name: 'Test Collection' },
28
- rootCollection: { id: 'http://example.com/root', name: 'Root Collection' },
29
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
30
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
31
- };
32
-
33
- const mockAuthorisedEntity: AuthorisedEntity = {
34
- ...mockStandardEntity,
35
- access: {
36
- metadata: true,
37
- content: true,
38
- },
39
- };
40
-
41
- describe('baseEntityTransformer', () => {
42
- it('should transform raw entity to base shape with unresolved references', () => {
43
- const rawEntity = {
44
- id: 'http://example.com/entity/123',
45
- name: 'Test Entity',
46
- description: 'A test entity',
47
- entityType: 'http://schema.org/Person',
48
-
49
- memberOf: 'http://example.com/collection',
50
- rootCollection: 'http://example.com/root',
51
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
52
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
53
- meta: {},
54
- createdAt: new Date(),
55
- updatedAt: new Date(),
56
- };
57
-
58
- const result = baseEntityTransformer(rawEntity);
59
-
60
- // baseEntityTransformer returns BaseEntity with string references
61
- const expectedBaseEntity: BaseEntity = {
62
- id: 'http://example.com/entity/123',
63
- name: 'Test Entity',
64
- description: 'A test entity',
65
- entityType: 'http://schema.org/Person',
66
- memberOf: 'http://example.com/collection',
67
- rootCollection: 'http://example.com/root',
68
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
69
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
70
- };
71
-
72
- expect(result).toEqual(expectedBaseEntity);
73
- });
74
- });
75
-
76
- describe('Custom transformers', () => {
77
- it('should allow custom transformer to add computed fields', () => {
78
- const customTransformer: EntityTransformer<AuthorisedEntity, { id: string; displayName: string; uri: string }> = (
79
- entity,
80
- ) => ({
81
- id: entity.id,
82
- displayName: entity.name.toUpperCase(),
83
- uri: entity.id,
84
- });
85
-
86
- const result = customTransformer(mockAuthorisedEntity, mockContext);
87
-
88
- expect(result).toEqual({
89
- id: 'http://example.com/entity/123',
90
- displayName: 'TEST ENTITY',
91
- uri: 'http://example.com/entity/123',
92
- });
93
- });
94
-
95
- it('should support async transformers for fetching related data', async () => {
96
- const asyncTransformer: EntityTransformer<
97
- AuthorisedEntity,
98
- { id: string; name: string; relatedData: string }
99
- > = async (entity) => {
100
- // Simulate async operation
101
- await new Promise((resolve) => setTimeout(resolve, 1));
102
-
103
- return {
104
- id: entity.id,
105
- name: entity.name,
106
- relatedData: 'fetched-data',
107
- };
108
- };
109
-
110
- const result = await asyncTransformer(mockAuthorisedEntity, mockContext);
111
-
112
- expect(result).toEqual({
113
- id: 'http://example.com/entity/123',
114
- name: 'Test Entity',
115
- relatedData: 'fetched-data',
116
- });
117
- });
118
-
119
- it('should allow transformer to access context', async () => {
120
- const contextAwareTransformer: EntityTransformer<
121
- AuthorisedEntity,
122
- {
123
- id: string;
124
- name: string;
125
- requestUrl: string;
126
- }
127
- > = async (entity, context) => ({
128
- id: entity.id,
129
- name: entity.name,
130
- requestUrl: context.request.url || 'unknown',
131
- });
132
-
133
- const contextWithUrl: RequestContext = {
134
- request: { url: '/test-url' } as FastifyRequest,
135
- fastify: {} as FastifyInstance,
136
- };
137
-
138
- const result = await contextAwareTransformer(mockAuthorisedEntity, contextWithUrl);
139
-
140
- expect(result).toEqual({
141
- id: 'http://example.com/entity/123',
142
- name: 'Test Entity',
143
- requestUrl: '/test-url',
144
- });
145
- });
146
-
147
- it('should support transformer pipeline', async () => {
148
- const addDisplayName: EntityTransformer<AuthorisedEntity, AuthorisedEntity & { displayName: string }> = (
149
- entity,
150
- ) => ({
151
- ...entity,
152
- displayName: `${entity.name} [${entity.entityType.split('/').pop()}]`,
153
- });
154
-
155
- const addUpperCase: EntityTransformer<
156
- AuthorisedEntity & { displayName: string },
157
- AuthorisedEntity & { displayName: string; upperName: string }
158
- > = (entity) => ({
159
- ...entity,
160
- upperName: entity.name.toUpperCase(),
161
- });
162
-
163
- // Simulate pipeline
164
- // biome-ignore lint/suspicious/noExplicitAny: fine in tests
165
- let result: any = mockAuthorisedEntity;
166
- result = await addDisplayName(result, mockContext);
167
- result = await addUpperCase(result, mockContext);
168
-
169
- expect(result).toMatchObject({
170
- id: 'http://example.com/entity/123',
171
- name: 'Test Entity',
172
- displayName: 'Test Entity [Person]',
173
- upperName: 'TEST ENTITY',
174
- });
175
- });
176
-
177
- it('should demonstrate full pipeline: base -> resolve -> access -> custom', async () => {
178
- const rawEntity = {
179
- id: 'http://example.com/entity/123',
180
- name: 'Test Entity',
181
- description: 'A test entity',
182
- entityType: 'http://schema.org/Person',
183
-
184
- memberOf: 'http://example.com/collection',
185
- rootCollection: 'http://example.com/root',
186
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
187
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
188
- meta: {},
189
- createdAt: new Date(),
190
- updatedAt: new Date(),
191
- };
192
-
193
- const access: AccessFunction = () => ({
194
- metadata: true,
195
- content: false,
196
- contentAuthorizationUrl: 'https://example.com/auth',
197
- });
198
-
199
- const addMetadata: EntityTransformer<AuthorisedEntity, AuthorisedEntity & { processed: boolean }> = (entity) => ({
200
- ...entity,
201
- processed: true,
202
- });
203
-
204
- prisma.entity.findMany.mockResolvedValue([
205
- { ...rawEntity, id: 'http://example.com/collection', name: 'Parent Collection' },
206
- { ...rawEntity, id: 'http://example.com/root', name: 'Root Collection' },
207
- ]);
208
-
209
- const [authorised] = await authoriseEntities([rawEntity], prisma, access, mockContext);
210
- const final = await applyTransformers(authorised, [addMetadata], mockContext);
211
-
212
- expect(final).toMatchObject({
213
- id: 'http://example.com/entity/123',
214
- name: 'Test Entity',
215
- memberOf: { id: 'http://example.com/collection', name: 'Parent Collection' },
216
- rootCollection: { id: 'http://example.com/root', name: 'Root Collection' },
217
- access: {
218
- metadata: true,
219
- content: false,
220
- contentAuthorizationUrl: 'https://example.com/auth',
221
- },
222
- processed: true,
223
- });
224
- });
225
- });
226
- });
@@ -1,57 +0,0 @@
1
- import type { Readable } from 'node:stream';
2
- import type { Entity, File } from '../generated/prisma/client.js';
3
- import type { RequestContext } from './context.js';
4
-
5
- export type FileMetadata = {
6
- contentType: string;
7
- contentLength: number;
8
- etag?: string;
9
- lastModified?: Date;
10
- };
11
-
12
- export type FileRedirectResult = {
13
- type: 'redirect';
14
- url: string;
15
- };
16
-
17
- export type FileStreamResult = {
18
- type: 'stream';
19
- stream: Readable | ReadableStream;
20
- metadata: FileMetadata;
21
- };
22
-
23
- export type FilePathResult = {
24
- type: 'file';
25
- path: string; // Absolute file path on disk
26
- metadata: FileMetadata;
27
- accelPath?: string; // Optional nginx internal path for X-Accel-Redirect
28
- };
29
-
30
- export type FileResult = FileRedirectResult | FileStreamResult | FilePathResult;
31
-
32
- export type GetFileHandler = (file: File, context: RequestContext) => Promise<FileResult | false> | FileResult | false;
33
-
34
- export type HeadFileHandler = (
35
- file: File,
36
- context: RequestContext,
37
- ) => Promise<FileMetadata | false> | FileMetadata | false;
38
-
39
- export type FileHandler = {
40
- get: GetFileHandler;
41
- head: HeadFileHandler;
42
- };
43
-
44
- export type GetRoCrateHandler = (
45
- entity: Entity,
46
- context: RequestContext,
47
- ) => Promise<FileResult | false> | FileResult | false;
48
-
49
- export type HeadRoCrateHandler = (
50
- entity: Entity,
51
- context: RequestContext,
52
- ) => Promise<FileMetadata | false> | FileMetadata | false;
53
-
54
- export type RoCrateHandler = {
55
- get: GetRoCrateHandler;
56
- head: HeadRoCrateHandler;
57
- };
@@ -1,90 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { createForbiddenError, createInternalError, createNotFoundError } from './errors.js';
3
-
4
- describe('Error Utilities', () => {
5
- describe('createInternalError', () => {
6
- it('should create a default internal error', () => {
7
- const error = createInternalError();
8
-
9
- expect(error).toEqual({
10
- error: {
11
- code: 'INTERNAL_ERROR',
12
- message: 'Internal server error',
13
- details: undefined,
14
- },
15
- });
16
- });
17
-
18
- it('should create an internal error with custom message', () => {
19
- const customMessage = 'Database connection failed';
20
- const error = createInternalError(customMessage);
21
-
22
- expect(error).toEqual({
23
- error: {
24
- code: 'INTERNAL_ERROR',
25
- message: customMessage,
26
- details: undefined,
27
- },
28
- });
29
- });
30
- });
31
-
32
- describe('createForbiddenError', () => {
33
- it('should create a forbidden error with message and entityId', () => {
34
- const entityId = 'http://example.com/entity/123';
35
- const error = createForbiddenError('No access', entityId);
36
-
37
- expect(error).toEqual({
38
- error: {
39
- code: 'FORBIDDEN',
40
- message: 'No access',
41
- details: { entityId },
42
- },
43
- });
44
- });
45
- });
46
-
47
- describe('createNotFoundError', () => {
48
- it('should create a not found error with message', () => {
49
- const message = 'Entity not found';
50
- const error = createNotFoundError(message);
51
-
52
- expect(error).toEqual({
53
- error: {
54
- code: 'NOT_FOUND',
55
- message,
56
- details: undefined,
57
- },
58
- });
59
- });
60
-
61
- it('should create a not found error with message and entityId', () => {
62
- const message = 'Entity not found';
63
- const entityId = 'http://example.com/entity/123';
64
- const error = createNotFoundError(message, entityId);
65
-
66
- expect(error).toEqual({
67
- error: {
68
- code: 'NOT_FOUND',
69
- message,
70
- details: {
71
- entityId,
72
- },
73
- },
74
- });
75
- });
76
-
77
- it('should handle undefined entityId', () => {
78
- const message = 'Entity not found';
79
- const error = createNotFoundError(message, undefined);
80
-
81
- expect(error).toEqual({
82
- error: {
83
- code: 'NOT_FOUND',
84
- message,
85
- details: undefined,
86
- },
87
- });
88
- });
89
- });
90
- });