arocapi 3.0.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -17
- package/dist/app.d.ts +2 -8
- package/dist/app.js +15 -33
- 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 +1 -1
- package/dist/index.dev.js.map +1 -1
- package/dist/routes/crate.d.ts +2 -0
- package/dist/routes/crate.js +5 -11
- package/dist/routes/crate.js.map +1 -1
- package/dist/routes/crate.test.js +1 -1
- package/dist/routes/crate.test.js.map +1 -1
- package/dist/routes/entities.d.ts +2 -0
- package/dist/routes/entities.js +6 -12
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entities.test.js +2 -1
- package/dist/routes/entities.test.js.map +1 -1
- package/dist/routes/entity.d.ts +2 -0
- package/dist/routes/entity.js +5 -11
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/entity.test.js +2 -1
- package/dist/routes/entity.test.js.map +1 -1
- package/dist/routes/file.d.ts +2 -0
- package/dist/routes/file.js +5 -11
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/file.test.js +1 -1
- package/dist/routes/file.test.js.map +1 -1
- package/dist/routes/files.d.ts +2 -0
- package/dist/routes/files.js +6 -12
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/files.test.js +9 -2
- package/dist/routes/files.test.js.map +1 -1
- package/dist/routes/search.d.ts +4 -0
- package/dist/routes/search.js +6 -12
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.js +3 -1
- 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.js +23 -0
- 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/package.json +1 -2
- package/src/app.test.ts +5 -44
- package/src/app.ts +18 -45
- package/src/index.dev.ts +1 -8
- package/src/routes/crate.test.ts +1 -1
- package/src/routes/crate.ts +7 -11
- package/src/routes/entities.test.ts +2 -1
- package/src/routes/entities.ts +9 -13
- package/src/routes/entity.test.ts +2 -1
- package/src/routes/entity.ts +7 -11
- package/src/routes/file.test.ts +1 -1
- package/src/routes/file.ts +7 -11
- package/src/routes/files.test.ts +10 -3
- package/src/routes/files.ts +9 -13
- package/src/routes/search.test.ts +3 -1
- package/src/routes/search.ts +11 -11
- package/src/test/helpers/fastify.ts +0 -12
- package/src/test/integration.setup.ts +25 -0
- package/src/test/integration.test.ts +72 -1
- package/src/__snapshots__/app.test.ts.snap +0 -8
package/src/routes/entities.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 } from '../utils/errors.js';
|
|
@@ -23,12 +24,13 @@ const querySchema = z.object({
|
|
|
23
24
|
});
|
|
24
25
|
|
|
25
26
|
type EntitiesRouteOptions = {
|
|
27
|
+
prisma: PrismaClient;
|
|
26
28
|
accessTransformer: AccessTransformer;
|
|
27
29
|
entityTransformers?: EntityTransformer[];
|
|
28
30
|
};
|
|
29
31
|
|
|
30
32
|
const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts) => {
|
|
31
|
-
const { accessTransformer, entityTransformers = [] } = opts;
|
|
33
|
+
const { prisma, accessTransformer, entityTransformers = [] } = opts;
|
|
32
34
|
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
33
35
|
'/entities',
|
|
34
36
|
{
|
|
@@ -40,7 +42,7 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
|
|
|
40
42
|
const { memberOf, entityType, limit, offset, sort, order } = request.query;
|
|
41
43
|
|
|
42
44
|
try {
|
|
43
|
-
const where: NonNullable<Parameters<typeof
|
|
45
|
+
const where: NonNullable<Parameters<typeof prisma.entity.findMany>[0]>['where'] = {};
|
|
44
46
|
|
|
45
47
|
if (memberOf) {
|
|
46
48
|
where.memberOf = memberOf;
|
|
@@ -53,7 +55,7 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
|
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
const [dbEntities, total] = await Promise.all([
|
|
56
|
-
|
|
58
|
+
prisma.entity.findMany({
|
|
57
59
|
where,
|
|
58
60
|
include: { file: { select: { id: true } } },
|
|
59
61
|
orderBy: {
|
|
@@ -62,11 +64,11 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
|
|
|
62
64
|
skip: offset,
|
|
63
65
|
take: limit,
|
|
64
66
|
}),
|
|
65
|
-
|
|
67
|
+
prisma.entity.count({ where }),
|
|
66
68
|
]);
|
|
67
69
|
|
|
68
70
|
// Resolve memberOf and rootCollection references
|
|
69
|
-
const refMap = await resolveEntityReferences(dbEntities,
|
|
71
|
+
const refMap = await resolveEntityReferences(dbEntities, prisma);
|
|
70
72
|
|
|
71
73
|
// Apply transformers to each entity: base -> access -> additional
|
|
72
74
|
const entities = await Promise.all(
|
|
@@ -77,17 +79,11 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
|
|
|
77
79
|
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
78
80
|
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
79
81
|
};
|
|
80
|
-
const authorisedEntity = await accessTransformer(standardEntity, {
|
|
81
|
-
request,
|
|
82
|
-
fastify,
|
|
83
|
-
});
|
|
82
|
+
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
84
83
|
|
|
85
84
|
let result = authorisedEntity;
|
|
86
85
|
for (const transformer of entityTransformers) {
|
|
87
|
-
result = await transformer(result, {
|
|
88
|
-
request,
|
|
89
|
-
fastify,
|
|
90
|
-
});
|
|
86
|
+
result = await transformer(result, { request, fastify });
|
|
91
87
|
}
|
|
92
88
|
|
|
93
89
|
return result;
|
|
@@ -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 () => {
|
|
@@ -96,6 +96,7 @@ describe('Entity Route', () => {
|
|
|
96
96
|
|
|
97
97
|
await fastifyBefore();
|
|
98
98
|
await fastify.register(entityRoute, {
|
|
99
|
+
prisma,
|
|
99
100
|
accessTransformer: AllPublicAccessTransformer,
|
|
100
101
|
entityTransformers: [customTransformer],
|
|
101
102
|
});
|
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,7 +29,7 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
|
|
|
27
29
|
const { id } = request.params;
|
|
28
30
|
|
|
29
31
|
try {
|
|
30
|
-
const entity = await
|
|
32
|
+
const entity = await prisma.entity.findUnique({
|
|
31
33
|
where: { id },
|
|
32
34
|
include: { file: { select: { id: true } } },
|
|
33
35
|
});
|
|
@@ -37,7 +39,7 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
|
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
// Resolve memberOf and rootCollection references
|
|
40
|
-
const refMap = await resolveEntityReferences([entity],
|
|
42
|
+
const refMap = await resolveEntityReferences([entity], prisma);
|
|
41
43
|
|
|
42
44
|
const base = baseEntityTransformer(entity);
|
|
43
45
|
const standardEntity = {
|
|
@@ -45,17 +47,11 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
|
|
|
45
47
|
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
46
48
|
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
47
49
|
};
|
|
48
|
-
const authorisedEntity = await accessTransformer(standardEntity, {
|
|
49
|
-
request,
|
|
50
|
-
fastify,
|
|
51
|
-
});
|
|
50
|
+
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
52
51
|
|
|
53
52
|
let result = authorisedEntity;
|
|
54
53
|
for (const transformer of entityTransformers) {
|
|
55
|
-
result = await transformer(result, {
|
|
56
|
-
request,
|
|
57
|
-
fastify,
|
|
58
|
-
});
|
|
54
|
+
result = await transformer(result, { request, fastify });
|
|
59
55
|
}
|
|
60
56
|
|
|
61
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
|
|
package/src/routes/file.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { createReadStream } from 'node:fs';
|
|
|
2
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';
|
|
7
8
|
import { setFileHeaders } from '../utils/headers.js';
|
|
@@ -17,11 +18,12 @@ const querySchema = z.object({
|
|
|
17
18
|
});
|
|
18
19
|
|
|
19
20
|
type FileRouteOptions = {
|
|
21
|
+
prisma: PrismaClient;
|
|
20
22
|
fileHandler: FileHandler;
|
|
21
23
|
};
|
|
22
24
|
|
|
23
25
|
const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
24
|
-
const { fileHandler } = opts;
|
|
26
|
+
const { prisma, fileHandler } = opts;
|
|
25
27
|
|
|
26
28
|
fastify.withTypeProvider<ZodTypeProvider>().head(
|
|
27
29
|
'/file/:id',
|
|
@@ -34,7 +36,7 @@ const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
|
34
36
|
const { id } = request.params;
|
|
35
37
|
|
|
36
38
|
try {
|
|
37
|
-
const file = await
|
|
39
|
+
const file = await prisma.file.findUnique({
|
|
38
40
|
where: { id },
|
|
39
41
|
});
|
|
40
42
|
|
|
@@ -42,10 +44,7 @@ const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
|
42
44
|
return reply.code(404).send(createNotFoundError('The requested file was not found', id));
|
|
43
45
|
}
|
|
44
46
|
|
|
45
|
-
const metadata: FileMetadata | false = await fileHandler.head(file, {
|
|
46
|
-
request,
|
|
47
|
-
fastify,
|
|
48
|
-
});
|
|
47
|
+
const metadata: FileMetadata | false = await fileHandler.head(file, { request, fastify });
|
|
49
48
|
|
|
50
49
|
if (!metadata) {
|
|
51
50
|
return reply.code(404).send(createNotFoundError('The requested file metadata was not found', id));
|
|
@@ -75,7 +74,7 @@ const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
|
75
74
|
const { id } = request.params;
|
|
76
75
|
|
|
77
76
|
try {
|
|
78
|
-
const file = await
|
|
77
|
+
const file = await prisma.file.findUnique({
|
|
79
78
|
where: { id },
|
|
80
79
|
});
|
|
81
80
|
|
|
@@ -83,10 +82,7 @@ const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
|
83
82
|
return reply.code(404).send(createNotFoundError('The requested file was not found', id));
|
|
84
83
|
}
|
|
85
84
|
|
|
86
|
-
const result = await fileHandler.get(file, {
|
|
87
|
-
request,
|
|
88
|
-
fastify,
|
|
89
|
-
});
|
|
85
|
+
const result = await fileHandler.get(file, { request, fastify });
|
|
90
86
|
|
|
91
87
|
if (!result) {
|
|
92
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 () => {
|
|
@@ -70,11 +70,16 @@ describe('Files Route', () => {
|
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
expect(response.statusCode).toBe(200);
|
|
73
|
-
const body = JSON.parse(response.body) as { total: number };
|
|
73
|
+
const body = JSON.parse(response.body) as { total: number; files: unknown[] };
|
|
74
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
|
+
});
|
|
75
80
|
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
76
81
|
expect.objectContaining({
|
|
77
|
-
where: { entity: {
|
|
82
|
+
where: { entity: { memberOf: 'http://example.com/collection/1' } },
|
|
78
83
|
}),
|
|
79
84
|
);
|
|
80
85
|
});
|
|
@@ -208,6 +213,7 @@ describe('Files Route', () => {
|
|
|
208
213
|
});
|
|
209
214
|
|
|
210
215
|
await fastify.register(filesRoute, {
|
|
216
|
+
prisma,
|
|
211
217
|
fileAccessTransformer: AllPublicFileAccessTransformer,
|
|
212
218
|
fileTransformers: [customTransformer],
|
|
213
219
|
});
|
|
@@ -245,6 +251,7 @@ describe('Files Route', () => {
|
|
|
245
251
|
});
|
|
246
252
|
|
|
247
253
|
await fastify.register(filesRoute, {
|
|
254
|
+
prisma,
|
|
248
255
|
fileAccessTransformer: customFileAccessTransformer,
|
|
249
256
|
});
|
|
250
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,14 +34,14 @@ 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.entity = {
|
|
40
|
+
where.entity = { memberOf };
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
const [dbFiles, total] = await Promise.all([
|
|
42
|
-
|
|
44
|
+
prisma.file.findMany({
|
|
43
45
|
where,
|
|
44
46
|
orderBy: {
|
|
45
47
|
[sort]: order,
|
|
@@ -47,24 +49,18 @@ const files: FastifyPluginAsync<FilesRouteOptions> = async (fastify, opts) => {
|
|
|
47
49
|
skip: offset,
|
|
48
50
|
take: limit,
|
|
49
51
|
}),
|
|
50
|
-
|
|
52
|
+
prisma.file.count({ where }),
|
|
51
53
|
]);
|
|
52
54
|
|
|
53
55
|
// Apply transformers to each entity: base -> access -> additional
|
|
54
56
|
const filesWithAccess = await Promise.all(
|
|
55
57
|
dbFiles.map(async (dbFile) => {
|
|
56
58
|
const standardFile = baseFileTransformer(dbFile);
|
|
57
|
-
const authorisedFile = await fileAccessTransformer(standardFile, {
|
|
58
|
-
request,
|
|
59
|
-
fastify,
|
|
60
|
-
});
|
|
59
|
+
const authorisedFile = await fileAccessTransformer(standardFile, { request, fastify });
|
|
61
60
|
|
|
62
61
|
let result = authorisedFile;
|
|
63
62
|
for (const transformer of fileTransformers) {
|
|
64
|
-
result = await transformer(result, {
|
|
65
|
-
request,
|
|
66
|
-
fastify,
|
|
67
|
-
});
|
|
63
|
+
result = await transformer(result, { request, fastify });
|
|
68
64
|
}
|
|
69
65
|
|
|
70
66
|
return result;
|
|
@@ -8,7 +8,7 @@ import searchRoute from './search.js';
|
|
|
8
8
|
describe('Search Route', () => {
|
|
9
9
|
beforeEach(async () => {
|
|
10
10
|
await fastifyBefore();
|
|
11
|
-
await fastify.register(searchRoute, { accessTransformer: AllPublicAccessTransformer });
|
|
11
|
+
await fastify.register(searchRoute, { prisma, opensearch, accessTransformer: AllPublicAccessTransformer });
|
|
12
12
|
});
|
|
13
13
|
|
|
14
14
|
afterEach(async () => {
|
|
@@ -611,6 +611,8 @@ describe('Search Route', () => {
|
|
|
611
611
|
|
|
612
612
|
await fastifyBefore();
|
|
613
613
|
await fastify.register(searchRoute, {
|
|
614
|
+
prisma,
|
|
615
|
+
opensearch,
|
|
614
616
|
accessTransformer: AllPublicAccessTransformer,
|
|
615
617
|
entityTransformers: [customTransformer],
|
|
616
618
|
});
|
package/src/routes/search.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import type { Client } from '@opensearch-project/opensearch';
|
|
1
2
|
import type { MultiBucketAggregateBaseFiltersBucket } from '@opensearch-project/opensearch/api/_types/_common.aggregations.js';
|
|
2
3
|
import type { Search_Request } from '@opensearch-project/opensearch/api/index.js';
|
|
3
4
|
import type { FastifyPluginAsync } from 'fastify';
|
|
4
5
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
5
6
|
import { z } from 'zod/v4';
|
|
7
|
+
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
6
8
|
import { baseEntityTransformer, resolveEntityReferences } from '../transformers/default.js';
|
|
7
9
|
import type { AccessTransformer, EntityTransformer } from '../types/transformers.js';
|
|
8
10
|
import { createInternalError, createInvalidRequestError } from '../utils/errors.js';
|
|
@@ -32,6 +34,8 @@ const searchParamsSchema = z.object({
|
|
|
32
34
|
});
|
|
33
35
|
|
|
34
36
|
type SearchRouteOptions = {
|
|
37
|
+
prisma: PrismaClient;
|
|
38
|
+
opensearch: Client;
|
|
35
39
|
accessTransformer: AccessTransformer;
|
|
36
40
|
entityTransformers?: EntityTransformer[];
|
|
37
41
|
queryBuilderClass?: typeof OpensearchQueryBuilder;
|
|
@@ -40,6 +44,8 @@ type SearchRouteOptions = {
|
|
|
40
44
|
|
|
41
45
|
const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) => {
|
|
42
46
|
const {
|
|
47
|
+
prisma,
|
|
48
|
+
opensearch,
|
|
43
49
|
accessTransformer,
|
|
44
50
|
entityTransformers = [],
|
|
45
51
|
queryBuilderClass = OpensearchQueryBuilder,
|
|
@@ -74,7 +80,7 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
|
|
|
74
80
|
},
|
|
75
81
|
};
|
|
76
82
|
fastify.log.debug(opensearchQuery);
|
|
77
|
-
const response = await
|
|
83
|
+
const response = await opensearch.search(opensearchQuery);
|
|
78
84
|
|
|
79
85
|
if (!response.body?.hits?.hits) {
|
|
80
86
|
throw new Error('Invalid search response: missing hits data');
|
|
@@ -82,7 +88,7 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
|
|
|
82
88
|
|
|
83
89
|
const entityIds = response.body.hits.hits.map((hit) => hit._source?.id as string | undefined).filter(Boolean);
|
|
84
90
|
|
|
85
|
-
const dbEntities = await
|
|
91
|
+
const dbEntities = await prisma.entity.findMany({
|
|
86
92
|
where: {
|
|
87
93
|
id: {
|
|
88
94
|
in: entityIds,
|
|
@@ -94,7 +100,7 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
|
|
|
94
100
|
const entityMap = new Map(dbEntities.map((entity) => [entity.id, entity]));
|
|
95
101
|
|
|
96
102
|
// Resolve memberOf and rootCollection references
|
|
97
|
-
const refMap = await resolveEntityReferences(dbEntities,
|
|
103
|
+
const refMap = await resolveEntityReferences(dbEntities, prisma);
|
|
98
104
|
|
|
99
105
|
const entities = await Promise.all(
|
|
100
106
|
response.body.hits.hits.map(async (hit) => {
|
|
@@ -115,17 +121,11 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
|
|
|
115
121
|
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
116
122
|
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
117
123
|
};
|
|
118
|
-
const authorisedEntity = await accessTransformer(standardEntity, {
|
|
119
|
-
request,
|
|
120
|
-
fastify,
|
|
121
|
-
});
|
|
124
|
+
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
122
125
|
|
|
123
126
|
let result = authorisedEntity;
|
|
124
127
|
for (const transformer of entityTransformers) {
|
|
125
|
-
result = await transformer(result, {
|
|
126
|
-
request,
|
|
127
|
-
fastify,
|
|
128
|
-
});
|
|
128
|
+
result = await transformer(result, { request, fastify });
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
// Add search-specific metadata
|
|
@@ -10,23 +10,11 @@ export let fastify: FastifyInstance;
|
|
|
10
10
|
export const prisma = mockDeep<PrismaClient>();
|
|
11
11
|
export const opensearch = mockDeep<Client>();
|
|
12
12
|
|
|
13
|
-
// @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
|
|
14
|
-
prisma.getter = undefined;
|
|
15
|
-
// @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
|
|
16
|
-
prisma.setter = undefined;
|
|
17
|
-
|
|
18
|
-
// @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
|
|
19
|
-
opensearch.getter = undefined;
|
|
20
|
-
// @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
|
|
21
|
-
opensearch.setter = undefined;
|
|
22
|
-
|
|
23
13
|
export const fastifyBefore = async () => {
|
|
24
14
|
mockReset(prisma);
|
|
25
15
|
mockReset(opensearch);
|
|
26
16
|
|
|
27
17
|
fastify = Fastify({ logger: false });
|
|
28
|
-
fastify.decorate('prisma', prisma);
|
|
29
|
-
fastify.decorate('opensearch', opensearch);
|
|
30
18
|
|
|
31
19
|
fastify.setValidatorCompiler(validatorCompiler);
|
|
32
20
|
fastify.setSerializerCompiler(serializerCompiler);
|
|
@@ -149,6 +149,31 @@ export async function seedTestData() {
|
|
|
149
149
|
data: testEntities,
|
|
150
150
|
});
|
|
151
151
|
|
|
152
|
+
const testFiles = [
|
|
153
|
+
{
|
|
154
|
+
id: 'http://example.com/entity/4',
|
|
155
|
+
filename: 'test-audio.wav',
|
|
156
|
+
mediaType: 'audio/wav',
|
|
157
|
+
size: BigInt(2048),
|
|
158
|
+
meta: {},
|
|
159
|
+
createdAt: new Date(),
|
|
160
|
+
updatedAt: new Date(),
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
id: 'http://example.com/entity/5',
|
|
164
|
+
filename: 'collection-metadata.csv',
|
|
165
|
+
mediaType: 'text/csv',
|
|
166
|
+
size: BigInt(512),
|
|
167
|
+
meta: {},
|
|
168
|
+
createdAt: new Date(),
|
|
169
|
+
updatedAt: new Date(),
|
|
170
|
+
},
|
|
171
|
+
];
|
|
172
|
+
|
|
173
|
+
await prisma.file.createMany({
|
|
174
|
+
data: testFiles,
|
|
175
|
+
});
|
|
176
|
+
|
|
152
177
|
await opensearch.indices.create({
|
|
153
178
|
index: 'entities',
|
|
154
179
|
body: {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
-
import type { AuthorisedEntity } from '../transformers/default.js';
|
|
2
|
+
import type { AuthorisedEntity, AuthorisedFile } from '../transformers/default.js';
|
|
3
3
|
import type { StandardErrorResponse } from '../utils/errors.js';
|
|
4
4
|
import {
|
|
5
5
|
cleanupTestData,
|
|
@@ -235,6 +235,77 @@ describe('Integration Tests', () => {
|
|
|
235
235
|
});
|
|
236
236
|
});
|
|
237
237
|
|
|
238
|
+
describe('GET /files', () => {
|
|
239
|
+
it('should return all files', async () => {
|
|
240
|
+
const app = getTestApp();
|
|
241
|
+
|
|
242
|
+
const response = await app.inject({
|
|
243
|
+
method: 'GET',
|
|
244
|
+
url: '/files',
|
|
245
|
+
});
|
|
246
|
+
const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };
|
|
247
|
+
|
|
248
|
+
expect(response.statusCode).toBe(200);
|
|
249
|
+
expect(body.total).toBe(2);
|
|
250
|
+
expect(body.files).toHaveLength(2);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('should filter files by memberOf (Object parent)', async () => {
|
|
254
|
+
const app = getTestApp();
|
|
255
|
+
|
|
256
|
+
const response = await app.inject({
|
|
257
|
+
method: 'GET',
|
|
258
|
+
url: '/files',
|
|
259
|
+
query: {
|
|
260
|
+
memberOf: 'http://example.com/entity/2',
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };
|
|
264
|
+
|
|
265
|
+
expect(response.statusCode).toBe(200);
|
|
266
|
+
expect(body.total).toBe(1);
|
|
267
|
+
expect(body.files).toHaveLength(1);
|
|
268
|
+
expect(body.files[0].id).toBe('http://example.com/entity/4');
|
|
269
|
+
expect(body.files[0].filename).toBe('test-audio.wav');
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('should filter files by memberOf (Collection parent)', async () => {
|
|
273
|
+
const app = getTestApp();
|
|
274
|
+
|
|
275
|
+
const response = await app.inject({
|
|
276
|
+
method: 'GET',
|
|
277
|
+
url: '/files',
|
|
278
|
+
query: {
|
|
279
|
+
memberOf: 'http://example.com/entity/1',
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };
|
|
283
|
+
|
|
284
|
+
expect(response.statusCode).toBe(200);
|
|
285
|
+
expect(body.total).toBe(1);
|
|
286
|
+
expect(body.files).toHaveLength(1);
|
|
287
|
+
expect(body.files[0].id).toBe('http://example.com/entity/5');
|
|
288
|
+
expect(body.files[0].filename).toBe('collection-metadata.csv');
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it('should return empty list when memberOf matches no entity', async () => {
|
|
292
|
+
const app = getTestApp();
|
|
293
|
+
|
|
294
|
+
const response = await app.inject({
|
|
295
|
+
method: 'GET',
|
|
296
|
+
url: '/files',
|
|
297
|
+
query: {
|
|
298
|
+
memberOf: 'http://example.com/entity/does-not-exist',
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };
|
|
302
|
+
|
|
303
|
+
expect(response.statusCode).toBe(200);
|
|
304
|
+
expect(body.total).toBe(0);
|
|
305
|
+
expect(body.files).toHaveLength(0);
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
238
309
|
describe('POST /search', () => {
|
|
239
310
|
it('should perform basic search', async () => {
|
|
240
311
|
const app = getTestApp();
|