arocapi 2.1.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.
- package/README.md +70 -26
- package/dist/app.d.ts +3 -9
- package/dist/app.js +20 -34
- package/dist/app.js.map +1 -1
- package/dist/app.test.js +5 -40
- package/dist/app.test.js.map +1 -1
- package/dist/index.dev.d.ts +1 -8
- package/dist/index.dev.js +12 -2
- package/dist/index.dev.js.map +1 -1
- package/dist/routes/crate.d.ts +2 -0
- package/dist/routes/crate.js +8 -27
- package/dist/routes/crate.js.map +1 -1
- package/dist/routes/crate.test.js +21 -23
- package/dist/routes/crate.test.js.map +1 -1
- package/dist/routes/entities.d.ts +2 -0
- package/dist/routes/entities.js +16 -20
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entities.test.js +20 -19
- package/dist/routes/entities.test.js.map +1 -1
- package/dist/routes/entity.d.ts +2 -0
- package/dist/routes/entity.js +7 -14
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/entity.test.js +13 -17
- package/dist/routes/entity.test.js.map +1 -1
- package/dist/routes/file.d.ts +2 -0
- package/dist/routes/file.js +8 -27
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/file.test.js +19 -23
- package/dist/routes/file.test.js.map +1 -1
- package/dist/routes/files.d.ts +2 -0
- package/dist/routes/files.js +15 -20
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/files.test.js +13 -35
- package/dist/routes/files.test.js.map +1 -1
- package/dist/routes/search.d.ts +4 -0
- package/dist/routes/search.js +21 -24
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.js +56 -24
- package/dist/routes/search.test.js.map +1 -1
- package/dist/test/helpers/fastify.js +0 -6
- package/dist/test/helpers/fastify.js.map +1 -1
- package/dist/test/integration.setup.d.ts +2 -4
- package/dist/test/integration.setup.js +41 -21
- package/dist/test/integration.setup.js.map +1 -1
- package/dist/test/integration.test.js +59 -0
- package/dist/test/integration.test.js.map +1 -1
- package/dist/transformers/default.d.ts +11 -18
- package/dist/transformers/default.js +5 -18
- package/dist/transformers/default.js.map +1 -1
- package/dist/transformers/default.test.js +6 -15
- package/dist/transformers/default.test.js.map +1 -1
- package/dist/transformers/transformer.test.js +2 -6
- package/dist/transformers/transformer.test.js.map +1 -1
- package/dist/utils/errors.d.ts +7 -1
- package/dist/utils/errors.js +2 -1
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/headers.d.ts +7 -0
- package/dist/utils/headers.js +11 -0
- package/dist/utils/headers.js.map +1 -0
- package/dist/utils/queryBuilder.d.ts +2 -2
- package/dist/utils/queryBuilder.js +2 -3
- package/dist/utils/queryBuilder.js.map +1 -1
- package/package.json +24 -24
- package/prisma/models/entity.prisma +17 -13
- package/prisma/models/file.prisma +9 -13
- package/prisma.config.ts +0 -2
- package/src/app.test.ts +5 -44
- package/src/app.ts +25 -47
- package/src/index.dev.ts +13 -9
- package/src/routes/__snapshots__/search.test.ts.snap +2 -2
- package/src/routes/crate.test.ts +21 -23
- package/src/routes/crate.ts +11 -33
- package/src/routes/entities.test.ts +23 -19
- package/src/routes/entities.ts +19 -23
- package/src/routes/entity.test.ts +15 -17
- package/src/routes/entity.ts +9 -14
- package/src/routes/file.test.ts +19 -23
- package/src/routes/file.ts +11 -33
- package/src/routes/files.test.ts +14 -39
- package/src/routes/files.ts +18 -23
- package/src/routes/search.test.ts +65 -26
- package/src/routes/search.ts +30 -23
- package/src/test/helpers/fastify.ts +0 -12
- package/src/test/integration.setup.ts +44 -21
- package/src/test/integration.test.ts +72 -1
- package/src/transformers/default.test.ts +10 -20
- package/src/transformers/default.ts +14 -27
- package/src/transformers/transformer.test.ts +4 -6
- package/src/utils/errors.ts +10 -6
- package/src/utils/headers.ts +16 -0
- package/src/utils/queryBuilder.ts +3 -5
- package/prisma/migrations/20250515034400_entities/migration.sql +0 -14
- package/prisma/migrations/20250515050009_extend_entity_fields/migration.sql +0 -7
- package/prisma/migrations/20250804225438_add_record_type/migration.sql +0 -8
- package/prisma/migrations/20250805040110_add_rocrate_field/migration.sql +0 -2
- package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +0 -10
- package/prisma/migrations/20250923032753_api_refactor/migration.sql +0 -39
- package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +0 -8
- package/prisma/migrations/20251020140329_add_meta_column/migration.sql +0 -2
- package/prisma/migrations/20251027010713_add_file_table/migration.sql +0 -17
- package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +0 -2
- package/prisma/migrations/20251211025510_remove_rocrate_column/migration.sql +0 -8
- package/prisma/migrations/migration_lock.toml +0 -3
- package/prisma/schema.prisma +0 -17
- package/src/__snapshots__/app.test.ts.snap +0 -8
|
@@ -8,7 +8,7 @@ import entityRoute from './entity.js';
|
|
|
8
8
|
describe('Entity Route', () => {
|
|
9
9
|
beforeEach(async () => {
|
|
10
10
|
await fastifyBefore();
|
|
11
|
-
await fastify.register(entityRoute, { accessTransformer: AllPublicAccessTransformer });
|
|
11
|
+
await fastify.register(entityRoute, { prisma, accessTransformer: AllPublicAccessTransformer });
|
|
12
12
|
});
|
|
13
13
|
|
|
14
14
|
afterEach(async () => {
|
|
@@ -18,12 +18,11 @@ describe('Entity Route', () => {
|
|
|
18
18
|
describe('GET /entity/:id', () => {
|
|
19
19
|
it('should return entity when found', async () => {
|
|
20
20
|
const mockEntity = {
|
|
21
|
-
id:
|
|
22
|
-
rocrateId: 'http://example.com/entity/123',
|
|
21
|
+
id: 'http://example.com/entity/123',
|
|
23
22
|
name: 'Test Entity',
|
|
24
23
|
description: 'A test entity',
|
|
25
24
|
entityType: 'http://schema.org/Person',
|
|
26
|
-
|
|
25
|
+
|
|
27
26
|
memberOf: null,
|
|
28
27
|
rootCollection: null,
|
|
29
28
|
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
@@ -33,7 +32,7 @@ describe('Entity Route', () => {
|
|
|
33
32
|
meta: {},
|
|
34
33
|
};
|
|
35
34
|
|
|
36
|
-
prisma.entity.
|
|
35
|
+
prisma.entity.findUnique.mockResolvedValue(mockEntity);
|
|
37
36
|
|
|
38
37
|
const response = await fastify.inject({
|
|
39
38
|
method: 'GET',
|
|
@@ -43,16 +42,16 @@ describe('Entity Route', () => {
|
|
|
43
42
|
|
|
44
43
|
expect(response.statusCode).toBe(200);
|
|
45
44
|
expect(body).toMatchSnapshot();
|
|
46
|
-
expect(prisma.entity.
|
|
45
|
+
expect(prisma.entity.findUnique).toHaveBeenCalledWith({
|
|
47
46
|
where: {
|
|
48
|
-
|
|
47
|
+
id: 'http://example.com/entity/123',
|
|
49
48
|
},
|
|
49
|
+
include: { file: { select: { id: true } } },
|
|
50
50
|
});
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
it('should return 404 when entity not found', async () => {
|
|
54
|
-
|
|
55
|
-
prisma.entity.findFirst.mockResolvedValue(null);
|
|
54
|
+
prisma.entity.findUnique.mockResolvedValue(null);
|
|
56
55
|
|
|
57
56
|
const response = await fastify.inject({
|
|
58
57
|
method: 'GET',
|
|
@@ -65,7 +64,7 @@ describe('Entity Route', () => {
|
|
|
65
64
|
});
|
|
66
65
|
|
|
67
66
|
it('should return 500 when database error occurs', async () => {
|
|
68
|
-
prisma.entity.
|
|
67
|
+
prisma.entity.findUnique.mockRejectedValue(new Error('Database connection failed'));
|
|
69
68
|
|
|
70
69
|
const response = await fastify.inject({
|
|
71
70
|
method: 'GET',
|
|
@@ -97,13 +96,13 @@ describe('Entity Route', () => {
|
|
|
97
96
|
|
|
98
97
|
await fastifyBefore();
|
|
99
98
|
await fastify.register(entityRoute, {
|
|
99
|
+
prisma,
|
|
100
100
|
accessTransformer: AllPublicAccessTransformer,
|
|
101
101
|
entityTransformers: [customTransformer],
|
|
102
102
|
});
|
|
103
103
|
|
|
104
104
|
const mockEntity = {
|
|
105
|
-
id:
|
|
106
|
-
rocrateId: 'http://example.com/entity/123',
|
|
105
|
+
id: 'http://example.com/entity/123',
|
|
107
106
|
name: 'Test Entity',
|
|
108
107
|
description: 'A test entity',
|
|
109
108
|
entityType: 'http://pcdm.org/models#Collection',
|
|
@@ -116,7 +115,7 @@ describe('Entity Route', () => {
|
|
|
116
115
|
};
|
|
117
116
|
|
|
118
117
|
// @ts-expect-error TS is looking at the wronf function signature
|
|
119
|
-
prisma.entity.
|
|
118
|
+
prisma.entity.findUnique.mockResolvedValue(mockEntity);
|
|
120
119
|
|
|
121
120
|
const response = await fastify.inject({
|
|
122
121
|
method: 'GET',
|
|
@@ -130,12 +129,11 @@ describe('Entity Route', () => {
|
|
|
130
129
|
|
|
131
130
|
it('should return null for memberOf/rootCollection when parent entity not found', async () => {
|
|
132
131
|
const mockEntity = {
|
|
133
|
-
id:
|
|
134
|
-
rocrateId: 'http://example.com/entity/123',
|
|
132
|
+
id: 'http://example.com/entity/123',
|
|
135
133
|
name: 'Test Entity',
|
|
136
134
|
description: 'A test entity',
|
|
137
135
|
entityType: 'http://pcdm.org/models#Object',
|
|
138
|
-
|
|
136
|
+
|
|
139
137
|
memberOf: 'http://example.com/entity/deleted',
|
|
140
138
|
rootCollection: 'http://example.com/entity/deleted',
|
|
141
139
|
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
@@ -146,7 +144,7 @@ describe('Entity Route', () => {
|
|
|
146
144
|
};
|
|
147
145
|
|
|
148
146
|
// First call returns the entity, second call (for reference resolution) returns empty
|
|
149
|
-
prisma.entity.
|
|
147
|
+
prisma.entity.findUnique.mockResolvedValue(mockEntity);
|
|
150
148
|
prisma.entity.findMany.mockResolvedValue([]);
|
|
151
149
|
|
|
152
150
|
const response = await fastify.inject({
|
package/src/routes/entity.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { FastifyPluginAsync } from 'fastify';
|
|
2
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
3
3
|
import { z } from 'zod/v4';
|
|
4
|
+
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
4
5
|
import { baseEntityTransformer, resolveEntityReferences } from '../transformers/default.js';
|
|
5
6
|
import type { AccessTransformer, EntityTransformer } from '../types/transformers.js';
|
|
6
7
|
import { createInternalError, createNotFoundError } from '../utils/errors.js';
|
|
@@ -10,12 +11,13 @@ const paramsSchema = z.object({
|
|
|
10
11
|
});
|
|
11
12
|
|
|
12
13
|
type EntityRouteOptions = {
|
|
14
|
+
prisma: PrismaClient;
|
|
13
15
|
accessTransformer: AccessTransformer;
|
|
14
16
|
entityTransformers?: EntityTransformer[];
|
|
15
17
|
};
|
|
16
18
|
|
|
17
19
|
const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) => {
|
|
18
|
-
const { accessTransformer, entityTransformers = [] } = opts;
|
|
20
|
+
const { prisma, accessTransformer, entityTransformers = [] } = opts;
|
|
19
21
|
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
20
22
|
'/entity/:id',
|
|
21
23
|
{
|
|
@@ -27,10 +29,9 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
|
|
|
27
29
|
const { id } = request.params;
|
|
28
30
|
|
|
29
31
|
try {
|
|
30
|
-
const entity = await
|
|
31
|
-
where: {
|
|
32
|
-
|
|
33
|
-
},
|
|
32
|
+
const entity = await prisma.entity.findUnique({
|
|
33
|
+
where: { id },
|
|
34
|
+
include: { file: { select: { id: true } } },
|
|
34
35
|
});
|
|
35
36
|
|
|
36
37
|
if (!entity) {
|
|
@@ -38,7 +39,7 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
|
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
// Resolve memberOf and rootCollection references
|
|
41
|
-
const refMap = await resolveEntityReferences([entity],
|
|
42
|
+
const refMap = await resolveEntityReferences([entity], prisma);
|
|
42
43
|
|
|
43
44
|
const base = baseEntityTransformer(entity);
|
|
44
45
|
const standardEntity = {
|
|
@@ -46,17 +47,11 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
|
|
|
46
47
|
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
47
48
|
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
48
49
|
};
|
|
49
|
-
const authorisedEntity = await accessTransformer(standardEntity, {
|
|
50
|
-
request,
|
|
51
|
-
fastify,
|
|
52
|
-
});
|
|
50
|
+
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
53
51
|
|
|
54
52
|
let result = authorisedEntity;
|
|
55
53
|
for (const transformer of entityTransformers) {
|
|
56
|
-
result = await transformer(result, {
|
|
57
|
-
request,
|
|
58
|
-
fastify,
|
|
59
|
-
});
|
|
54
|
+
result = await transformer(result, { request, fastify });
|
|
60
55
|
}
|
|
61
56
|
|
|
62
57
|
return result;
|
package/src/routes/file.test.ts
CHANGED
|
@@ -18,7 +18,7 @@ describe('File Route', () => {
|
|
|
18
18
|
|
|
19
19
|
beforeEach(async () => {
|
|
20
20
|
await fastifyBefore();
|
|
21
|
-
await fastify.register(fileRoute, { fileHandler: mockFileHandler });
|
|
21
|
+
await fastify.register(fileRoute, { prisma, fileHandler: mockFileHandler });
|
|
22
22
|
vi.clearAllMocks();
|
|
23
23
|
});
|
|
24
24
|
|
|
@@ -27,14 +27,10 @@ describe('File Route', () => {
|
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
const mockFile = {
|
|
30
|
-
id:
|
|
31
|
-
fileId: 'http://example.com/file/test.wav',
|
|
30
|
+
id: 'http://example.com/file/test.wav',
|
|
32
31
|
filename: 'test.wav',
|
|
33
32
|
mediaType: 'audio/wav',
|
|
34
33
|
size: BigInt(1024),
|
|
35
|
-
memberOf: 'http://example.com/collection',
|
|
36
|
-
rootCollection: 'http://example.com/collection',
|
|
37
|
-
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
38
34
|
meta: { storagePath: '/data/files/test.wav' },
|
|
39
35
|
createdAt: new Date(),
|
|
40
36
|
updatedAt: new Date(),
|
|
@@ -42,7 +38,7 @@ describe('File Route', () => {
|
|
|
42
38
|
|
|
43
39
|
describe('GET /file/:id', () => {
|
|
44
40
|
it('should stream file content successfully', async () => {
|
|
45
|
-
prisma.file.
|
|
41
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
46
42
|
|
|
47
43
|
const mockStream = Readable.from(['test file content']);
|
|
48
44
|
const mockResult: FileResult = {
|
|
@@ -79,7 +75,7 @@ describe('File Route', () => {
|
|
|
79
75
|
});
|
|
80
76
|
|
|
81
77
|
it('should handle attachment disposition', async () => {
|
|
82
|
-
prisma.file.
|
|
78
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
83
79
|
|
|
84
80
|
const mockStream = Readable.from(['test']);
|
|
85
81
|
const mockResult: FileResult = {
|
|
@@ -102,7 +98,7 @@ describe('File Route', () => {
|
|
|
102
98
|
});
|
|
103
99
|
|
|
104
100
|
it('should use custom filename when provided', async () => {
|
|
105
|
-
prisma.file.
|
|
101
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
106
102
|
|
|
107
103
|
const mockStream = Readable.from(['test']);
|
|
108
104
|
const mockResult: FileResult = {
|
|
@@ -125,7 +121,7 @@ describe('File Route', () => {
|
|
|
125
121
|
});
|
|
126
122
|
|
|
127
123
|
it('should handle redirect response with noRedirect=false (default)', async () => {
|
|
128
|
-
prisma.file.
|
|
124
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
129
125
|
|
|
130
126
|
const mockResult: FileResult = {
|
|
131
127
|
type: 'redirect',
|
|
@@ -143,7 +139,7 @@ describe('File Route', () => {
|
|
|
143
139
|
});
|
|
144
140
|
|
|
145
141
|
it('should handle redirect response with noRedirect=true', async () => {
|
|
146
|
-
prisma.file.
|
|
142
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
147
143
|
|
|
148
144
|
const mockResult: FileResult = {
|
|
149
145
|
type: 'redirect',
|
|
@@ -162,7 +158,7 @@ describe('File Route', () => {
|
|
|
162
158
|
});
|
|
163
159
|
|
|
164
160
|
it('should handle file path response without nginx X-Accel-Redirect', async () => {
|
|
165
|
-
prisma.file.
|
|
161
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
166
162
|
|
|
167
163
|
const mockStream = Readable.from(['file content']);
|
|
168
164
|
vi.mocked(createReadStream).mockReturnValue(mockStream as never);
|
|
@@ -190,7 +186,7 @@ describe('File Route', () => {
|
|
|
190
186
|
});
|
|
191
187
|
|
|
192
188
|
it('should handle file path response with nginx X-Accel-Redirect', async () => {
|
|
193
|
-
prisma.file.
|
|
189
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
194
190
|
|
|
195
191
|
const mockResult: FileResult = {
|
|
196
192
|
type: 'file',
|
|
@@ -218,7 +214,7 @@ describe('File Route', () => {
|
|
|
218
214
|
});
|
|
219
215
|
|
|
220
216
|
it('should return 404 when entity not found', async () => {
|
|
221
|
-
prisma.
|
|
217
|
+
prisma.file.findUnique.mockResolvedValue(null);
|
|
222
218
|
|
|
223
219
|
const response = await fastify.inject({
|
|
224
220
|
method: 'GET',
|
|
@@ -233,7 +229,7 @@ describe('File Route', () => {
|
|
|
233
229
|
});
|
|
234
230
|
|
|
235
231
|
it('should return 500 when database error occurs', async () => {
|
|
236
|
-
prisma.file.
|
|
232
|
+
prisma.file.findUnique.mockRejectedValue(new Error('Database connection failed'));
|
|
237
233
|
|
|
238
234
|
const response = await fastify.inject({
|
|
239
235
|
method: 'GET',
|
|
@@ -246,7 +242,7 @@ describe('File Route', () => {
|
|
|
246
242
|
});
|
|
247
243
|
|
|
248
244
|
it('should return 500 when fileHandler throws error', async () => {
|
|
249
|
-
prisma.file.
|
|
245
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
250
246
|
vi.mocked(mockFileHandler.get).mockRejectedValue(new Error('File not found in storage'));
|
|
251
247
|
|
|
252
248
|
const response = await fastify.inject({
|
|
@@ -271,7 +267,7 @@ describe('File Route', () => {
|
|
|
271
267
|
});
|
|
272
268
|
|
|
273
269
|
it('should validate disposition parameter', async () => {
|
|
274
|
-
prisma.file.
|
|
270
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
275
271
|
|
|
276
272
|
const response = await fastify.inject({
|
|
277
273
|
method: 'GET',
|
|
@@ -284,7 +280,7 @@ describe('File Route', () => {
|
|
|
284
280
|
});
|
|
285
281
|
|
|
286
282
|
it('should return 404 when get handler returns false', async () => {
|
|
287
|
-
prisma.file.
|
|
283
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
288
284
|
vi.mocked(mockFileHandler.get).mockResolvedValue(false);
|
|
289
285
|
|
|
290
286
|
const response = await fastify.inject({
|
|
@@ -301,7 +297,7 @@ describe('File Route', () => {
|
|
|
301
297
|
|
|
302
298
|
describe('HEAD /file/:id', () => {
|
|
303
299
|
it('should return headers without body using head handler', async () => {
|
|
304
|
-
prisma.file.
|
|
300
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
305
301
|
|
|
306
302
|
const mockMetadata = {
|
|
307
303
|
contentType: 'audio/wav',
|
|
@@ -332,7 +328,7 @@ describe('File Route', () => {
|
|
|
332
328
|
});
|
|
333
329
|
|
|
334
330
|
it('should return 404 when file not found', async () => {
|
|
335
|
-
prisma.file.
|
|
331
|
+
prisma.file.findUnique.mockResolvedValue(null);
|
|
336
332
|
|
|
337
333
|
const response = await fastify.inject({
|
|
338
334
|
method: 'HEAD',
|
|
@@ -346,7 +342,7 @@ describe('File Route', () => {
|
|
|
346
342
|
});
|
|
347
343
|
|
|
348
344
|
it('should return 500 when head handler throws error', async () => {
|
|
349
|
-
prisma.file.
|
|
345
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
350
346
|
vi.mocked(mockFileHandler.head).mockRejectedValue(new Error('Failed to get metadata'));
|
|
351
347
|
|
|
352
348
|
const response = await fastify.inject({
|
|
@@ -360,7 +356,7 @@ describe('File Route', () => {
|
|
|
360
356
|
});
|
|
361
357
|
|
|
362
358
|
it('should return 404 when head handler returns false', async () => {
|
|
363
|
-
prisma.file.
|
|
359
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
364
360
|
vi.mocked(mockFileHandler.head).mockResolvedValue(false);
|
|
365
361
|
|
|
366
362
|
const response = await fastify.inject({
|
|
@@ -377,7 +373,7 @@ describe('File Route', () => {
|
|
|
377
373
|
|
|
378
374
|
describe('GET /file/:id - exhaustiveness check', () => {
|
|
379
375
|
it('should handle unexpected file result type', async () => {
|
|
380
|
-
prisma.file.
|
|
376
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
381
377
|
|
|
382
378
|
// Mock an invalid result type to test exhaustiveness check
|
|
383
379
|
const invalidResult = {
|
package/src/routes/file.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { createReadStream } from 'node:fs';
|
|
2
|
-
import type { FastifyPluginAsync
|
|
2
|
+
import type { FastifyPluginAsync } from 'fastify';
|
|
3
3
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
4
4
|
import { z } from 'zod/v4';
|
|
5
|
+
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
5
6
|
import type { FileHandler, FileMetadata } from '../types/fileHandlers.js';
|
|
6
7
|
import { createInternalError, createNotFoundError } from '../utils/errors.js';
|
|
8
|
+
import { setFileHeaders } from '../utils/headers.js';
|
|
7
9
|
|
|
8
10
|
const paramsSchema = z.object({
|
|
9
11
|
id: z.url(),
|
|
@@ -16,26 +18,12 @@ const querySchema = z.object({
|
|
|
16
18
|
});
|
|
17
19
|
|
|
18
20
|
type FileRouteOptions = {
|
|
21
|
+
prisma: PrismaClient;
|
|
19
22
|
fileHandler: FileHandler;
|
|
20
23
|
};
|
|
21
24
|
|
|
22
|
-
const setFileHeaders = (
|
|
23
|
-
reply: FastifyReply,
|
|
24
|
-
metadata: { contentType: string; contentLength: number; etag?: string; lastModified?: Date },
|
|
25
|
-
) => {
|
|
26
|
-
reply.header('Content-Type', metadata.contentType);
|
|
27
|
-
reply.header('Content-Length', metadata.contentLength.toString());
|
|
28
|
-
|
|
29
|
-
if (metadata.etag) {
|
|
30
|
-
reply.header('ETag', metadata.etag);
|
|
31
|
-
}
|
|
32
|
-
if (metadata.lastModified) {
|
|
33
|
-
reply.header('Last-Modified', metadata.lastModified.toUTCString());
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
25
|
const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
38
|
-
const { fileHandler } = opts;
|
|
26
|
+
const { prisma, fileHandler } = opts;
|
|
39
27
|
|
|
40
28
|
fastify.withTypeProvider<ZodTypeProvider>().head(
|
|
41
29
|
'/file/:id',
|
|
@@ -48,20 +36,15 @@ const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
|
48
36
|
const { id } = request.params;
|
|
49
37
|
|
|
50
38
|
try {
|
|
51
|
-
const file = await
|
|
52
|
-
where: {
|
|
53
|
-
fileId: id,
|
|
54
|
-
},
|
|
39
|
+
const file = await prisma.file.findUnique({
|
|
40
|
+
where: { id },
|
|
55
41
|
});
|
|
56
42
|
|
|
57
43
|
if (!file) {
|
|
58
44
|
return reply.code(404).send(createNotFoundError('The requested file was not found', id));
|
|
59
45
|
}
|
|
60
46
|
|
|
61
|
-
const metadata: FileMetadata | false = await fileHandler.head(file, {
|
|
62
|
-
request,
|
|
63
|
-
fastify,
|
|
64
|
-
});
|
|
47
|
+
const metadata: FileMetadata | false = await fileHandler.head(file, { request, fastify });
|
|
65
48
|
|
|
66
49
|
if (!metadata) {
|
|
67
50
|
return reply.code(404).send(createNotFoundError('The requested file metadata was not found', id));
|
|
@@ -91,20 +74,15 @@ const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
|
91
74
|
const { id } = request.params;
|
|
92
75
|
|
|
93
76
|
try {
|
|
94
|
-
const file = await
|
|
95
|
-
where: {
|
|
96
|
-
fileId: id,
|
|
97
|
-
},
|
|
77
|
+
const file = await prisma.file.findUnique({
|
|
78
|
+
where: { id },
|
|
98
79
|
});
|
|
99
80
|
|
|
100
81
|
if (!file) {
|
|
101
82
|
return reply.code(404).send(createNotFoundError('The requested file was not found', id));
|
|
102
83
|
}
|
|
103
84
|
|
|
104
|
-
const result = await fileHandler.get(file, {
|
|
105
|
-
request,
|
|
106
|
-
fastify,
|
|
107
|
-
});
|
|
85
|
+
const result = await fileHandler.get(file, { request, fastify });
|
|
108
86
|
|
|
109
87
|
if (!result) {
|
|
110
88
|
return reply.code(404).send(createNotFoundError('The requested file could not be retrieved', id));
|
package/src/routes/files.test.ts
CHANGED
|
@@ -8,7 +8,7 @@ import filesRoute from './files.js';
|
|
|
8
8
|
describe('Files Route', () => {
|
|
9
9
|
beforeEach(async () => {
|
|
10
10
|
await fastifyBefore();
|
|
11
|
-
await fastify.register(filesRoute, { fileAccessTransformer: AllPublicFileAccessTransformer });
|
|
11
|
+
await fastify.register(filesRoute, { prisma, fileAccessTransformer: AllPublicFileAccessTransformer });
|
|
12
12
|
});
|
|
13
13
|
|
|
14
14
|
afterEach(async () => {
|
|
@@ -16,28 +16,20 @@ describe('Files Route', () => {
|
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
const mockFile1 = {
|
|
19
|
-
id:
|
|
20
|
-
fileId: 'http://example.com/file1.wav',
|
|
19
|
+
id: 'http://example.com/file1.wav',
|
|
21
20
|
filename: 'file1.wav',
|
|
22
21
|
mediaType: 'audio/wav',
|
|
23
22
|
size: BigInt(1024),
|
|
24
|
-
memberOf: 'http://example.com/collection/1',
|
|
25
|
-
rootCollection: 'http://example.com/collection/1',
|
|
26
|
-
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
27
23
|
meta: {},
|
|
28
24
|
createdAt: new Date('2025-01-01'),
|
|
29
25
|
updatedAt: new Date('2025-01-01'),
|
|
30
26
|
};
|
|
31
27
|
|
|
32
28
|
const mockFile2 = {
|
|
33
|
-
id:
|
|
34
|
-
fileId: 'http://example.com/file2.txt',
|
|
29
|
+
id: 'http://example.com/file2.txt',
|
|
35
30
|
filename: 'file2.txt',
|
|
36
31
|
mediaType: 'text/plain',
|
|
37
32
|
size: BigInt(512),
|
|
38
|
-
memberOf: 'http://example.com/collection/1',
|
|
39
|
-
rootCollection: 'http://example.com/collection/1',
|
|
40
|
-
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
41
33
|
meta: {},
|
|
42
34
|
createdAt: new Date('2025-01-02'),
|
|
43
35
|
updatedAt: new Date('2025-01-02'),
|
|
@@ -62,14 +54,10 @@ describe('Files Route', () => {
|
|
|
62
54
|
filename: 'file1.wav',
|
|
63
55
|
mediaType: 'audio/wav',
|
|
64
56
|
size: 1024,
|
|
65
|
-
memberOf: 'http://example.com/collection/1',
|
|
66
|
-
rootCollection: 'http://example.com/collection/1',
|
|
67
|
-
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
68
57
|
access: {
|
|
69
58
|
content: true,
|
|
70
59
|
},
|
|
71
60
|
});
|
|
72
|
-
expect(body.files[0]).not.toHaveProperty('metadataLicenseId');
|
|
73
61
|
});
|
|
74
62
|
|
|
75
63
|
it('should filter files by memberOf', async () => {
|
|
@@ -82,11 +70,16 @@ describe('Files Route', () => {
|
|
|
82
70
|
});
|
|
83
71
|
|
|
84
72
|
expect(response.statusCode).toBe(200);
|
|
85
|
-
const body = JSON.parse(response.body) as { total: number };
|
|
73
|
+
const body = JSON.parse(response.body) as { total: number; files: unknown[] };
|
|
86
74
|
expect(body.total).toBe(1);
|
|
75
|
+
expect(body.files).toHaveLength(1);
|
|
76
|
+
expect(body.files[0]).toMatchObject({
|
|
77
|
+
id: 'http://example.com/file1.wav',
|
|
78
|
+
filename: 'file1.wav',
|
|
79
|
+
});
|
|
87
80
|
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
88
81
|
expect.objectContaining({
|
|
89
|
-
where: { memberOf: 'http://example.com/collection/1' },
|
|
82
|
+
where: { entity: { memberOf: 'http://example.com/collection/1' } },
|
|
90
83
|
}),
|
|
91
84
|
);
|
|
92
85
|
});
|
|
@@ -129,7 +122,7 @@ describe('Files Route', () => {
|
|
|
129
122
|
);
|
|
130
123
|
});
|
|
131
124
|
|
|
132
|
-
it('should support sorting by id
|
|
125
|
+
it('should support sorting by id directly', async () => {
|
|
133
126
|
prisma.file.findMany.mockResolvedValue([mockFile1, mockFile2]);
|
|
134
127
|
prisma.file.count.mockResolvedValue(2);
|
|
135
128
|
|
|
@@ -141,7 +134,7 @@ describe('Files Route', () => {
|
|
|
141
134
|
expect(response.statusCode).toBe(200);
|
|
142
135
|
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
143
136
|
expect.objectContaining({
|
|
144
|
-
orderBy: {
|
|
137
|
+
orderBy: { id: 'desc' },
|
|
145
138
|
}),
|
|
146
139
|
);
|
|
147
140
|
});
|
|
@@ -161,26 +154,6 @@ describe('Files Route', () => {
|
|
|
161
154
|
expect(body.files).toHaveLength(0);
|
|
162
155
|
});
|
|
163
156
|
|
|
164
|
-
it('should not include metadataLicenseId in response', async () => {
|
|
165
|
-
prisma.file.findMany.mockResolvedValue([mockFile1]);
|
|
166
|
-
prisma.file.count.mockResolvedValue(1);
|
|
167
|
-
|
|
168
|
-
const response = await fastify.inject({
|
|
169
|
-
method: 'GET',
|
|
170
|
-
url: '/files',
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
expect(response.statusCode).toBe(200);
|
|
174
|
-
const body = JSON.parse(response.body) as { files: Array<{ memberOf: string }> };
|
|
175
|
-
expect(body.files[0]).toMatchObject({
|
|
176
|
-
id: 'http://example.com/file1.wav',
|
|
177
|
-
filename: 'file1.wav',
|
|
178
|
-
memberOf: 'http://example.com/collection/1',
|
|
179
|
-
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
180
|
-
});
|
|
181
|
-
expect(body.files[0]).not.toHaveProperty('metadataLicenseId');
|
|
182
|
-
});
|
|
183
|
-
|
|
184
157
|
it('should return 500 when database error occurs', async () => {
|
|
185
158
|
prisma.file.findMany.mockRejectedValue(new Error('Database connection failed'));
|
|
186
159
|
|
|
@@ -240,6 +213,7 @@ describe('Files Route', () => {
|
|
|
240
213
|
});
|
|
241
214
|
|
|
242
215
|
await fastify.register(filesRoute, {
|
|
216
|
+
prisma,
|
|
243
217
|
fileAccessTransformer: AllPublicFileAccessTransformer,
|
|
244
218
|
fileTransformers: [customTransformer],
|
|
245
219
|
});
|
|
@@ -277,6 +251,7 @@ describe('Files Route', () => {
|
|
|
277
251
|
});
|
|
278
252
|
|
|
279
253
|
await fastify.register(filesRoute, {
|
|
254
|
+
prisma,
|
|
280
255
|
fileAccessTransformer: customFileAccessTransformer,
|
|
281
256
|
});
|
|
282
257
|
|
package/src/routes/files.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { FastifyPluginAsync } from 'fastify';
|
|
2
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
3
3
|
import { z } from 'zod/v4';
|
|
4
|
+
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
4
5
|
import { baseFileTransformer } from '../transformers/default.js';
|
|
5
6
|
import type { FileAccessTransformer, FileTransformer } from '../types/transformers.js';
|
|
6
7
|
import { createInternalError } from '../utils/errors.js';
|
|
@@ -14,12 +15,13 @@ const querySchema = z.object({
|
|
|
14
15
|
});
|
|
15
16
|
|
|
16
17
|
type FilesRouteOptions = {
|
|
18
|
+
prisma: PrismaClient;
|
|
17
19
|
fileAccessTransformer: FileAccessTransformer;
|
|
18
20
|
fileTransformers?: FileTransformer[];
|
|
19
21
|
};
|
|
20
22
|
|
|
21
23
|
const files: FastifyPluginAsync<FilesRouteOptions> = async (fastify, opts) => {
|
|
22
|
-
const { fileAccessTransformer, fileTransformers = [] } = opts;
|
|
24
|
+
const { prisma, fileAccessTransformer, fileTransformers = [] } = opts;
|
|
23
25
|
|
|
24
26
|
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
25
27
|
'/files',
|
|
@@ -32,40 +34,33 @@ const files: FastifyPluginAsync<FilesRouteOptions> = async (fastify, opts) => {
|
|
|
32
34
|
const { memberOf, limit, offset, sort, order } = request.query;
|
|
33
35
|
|
|
34
36
|
try {
|
|
35
|
-
const where: NonNullable<Parameters<typeof
|
|
37
|
+
const where: NonNullable<Parameters<typeof prisma.file.findMany>[0]>['where'] = {};
|
|
36
38
|
|
|
37
39
|
if (memberOf) {
|
|
38
|
-
where.
|
|
40
|
+
where.entity = { memberOf };
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const total = await fastify.prisma.file.count({ where });
|
|
43
|
+
const [dbFiles, total] = await Promise.all([
|
|
44
|
+
prisma.file.findMany({
|
|
45
|
+
where,
|
|
46
|
+
orderBy: {
|
|
47
|
+
[sort]: order,
|
|
48
|
+
},
|
|
49
|
+
skip: offset,
|
|
50
|
+
take: limit,
|
|
51
|
+
}),
|
|
52
|
+
prisma.file.count({ where }),
|
|
53
|
+
]);
|
|
53
54
|
|
|
54
55
|
// Apply transformers to each entity: base -> access -> additional
|
|
55
56
|
const filesWithAccess = await Promise.all(
|
|
56
57
|
dbFiles.map(async (dbFile) => {
|
|
57
58
|
const standardFile = baseFileTransformer(dbFile);
|
|
58
|
-
const authorisedFile = await fileAccessTransformer(standardFile, {
|
|
59
|
-
request,
|
|
60
|
-
fastify,
|
|
61
|
-
});
|
|
59
|
+
const authorisedFile = await fileAccessTransformer(standardFile, { request, fastify });
|
|
62
60
|
|
|
63
61
|
let result = authorisedFile;
|
|
64
62
|
for (const transformer of fileTransformers) {
|
|
65
|
-
result = await transformer(result, {
|
|
66
|
-
request,
|
|
67
|
-
fastify,
|
|
68
|
-
});
|
|
63
|
+
result = await transformer(result, { request, fastify });
|
|
69
64
|
}
|
|
70
65
|
|
|
71
66
|
return result;
|