arocapi 1.1.0 → 1.2.2
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/LICENSE.md +5 -0
- package/README.md +459 -36
- package/dist/app.d.ts +19 -1
- package/dist/app.js +37 -29
- package/dist/app.js.map +1 -1
- package/dist/app.test.js +124 -0
- package/dist/app.test.js.map +1 -0
- package/dist/index.dev.js +146 -0
- package/dist/index.dev.js.map +1 -0
- package/dist/routes/crate.d.ts +7 -0
- package/dist/routes/crate.js +101 -0
- package/dist/routes/crate.js.map +1 -0
- package/dist/routes/crate.test.d.ts +1 -0
- package/dist/routes/crate.test.js +360 -0
- package/dist/routes/crate.test.js.map +1 -0
- package/dist/routes/entities.d.ts +6 -1
- package/dist/routes/entities.js +52 -39
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entities.test.d.ts +1 -0
- package/dist/routes/entities.test.js +245 -0
- package/dist/routes/entities.test.js.map +1 -0
- package/dist/routes/entity.d.ts +6 -1
- package/dist/routes/entity.js +30 -8
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/entity.test.d.ts +1 -0
- package/dist/routes/entity.test.js +109 -0
- package/dist/routes/entity.test.js.map +1 -0
- package/dist/routes/file.d.ts +7 -0
- package/dist/routes/file.js +113 -0
- package/dist/routes/file.js.map +1 -0
- package/dist/routes/file.test.d.ts +1 -0
- package/dist/routes/file.test.js +319 -0
- package/dist/routes/file.test.js.map +1 -0
- package/dist/routes/files.d.ts +8 -0
- package/dist/routes/files.js +62 -0
- package/dist/routes/files.js.map +1 -0
- package/dist/routes/files.test.d.ts +1 -0
- package/dist/routes/files.test.js +244 -0
- package/dist/routes/files.test.js.map +1 -0
- package/dist/routes/search.d.ts +6 -1
- package/dist/routes/search.js +44 -19
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.d.ts +1 -0
- package/dist/routes/search.test.js +698 -0
- package/dist/routes/search.test.js.map +1 -0
- package/dist/test/helpers/fastify.d.ts +9 -0
- package/dist/test/helpers/fastify.js +24 -0
- package/dist/test/helpers/fastify.js.map +1 -0
- package/dist/test/integration.setup.d.ts +58 -0
- package/dist/test/integration.setup.js +181 -0
- package/dist/test/integration.setup.js.map +1 -0
- package/dist/test/integration.test.d.ts +1 -0
- package/dist/test/integration.test.js +337 -0
- package/dist/test/integration.test.js.map +1 -0
- package/dist/transformers/default.d.ts +45 -0
- package/dist/transformers/default.js +46 -0
- package/dist/transformers/default.js.map +1 -0
- package/dist/transformers/default.test.d.ts +1 -0
- package/dist/transformers/default.test.js +177 -0
- package/dist/transformers/default.test.js.map +1 -0
- package/dist/transformers/transformer.test.d.ts +1 -0
- package/dist/transformers/transformer.test.js +176 -0
- package/dist/transformers/transformer.test.js.map +1 -0
- package/dist/types/fileHandlers.d.ts +41 -0
- package/dist/types/fileHandlers.js +2 -0
- package/dist/types/fileHandlers.js.map +1 -0
- package/dist/types/transformers.d.ts +10 -0
- package/dist/types/transformers.js +2 -0
- package/dist/types/transformers.js.map +1 -0
- package/dist/utils/errors.d.ts +23 -0
- package/dist/utils/errors.js +21 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/errors.test.d.ts +1 -0
- package/dist/utils/errors.test.js +66 -0
- package/dist/utils/errors.test.js.map +1 -0
- package/package.json +35 -28
- package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +10 -0
- package/prisma/migrations/20250923032753_api_refactor/migration.sql +39 -0
- package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +8 -0
- package/prisma/migrations/20251020140329_add_meta_column/migration.sql +2 -0
- package/prisma/migrations/20251027010713_add_file_table/migration.sql +17 -0
- package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +2 -0
- package/prisma/models/entity.prisma +16 -12
- package/prisma/models/file.prisma +20 -0
- package/prisma.config.ts +6 -1
- package/src/__snapshots__/app.test.ts.snap +8 -0
- package/src/app.test.ts +158 -0
- package/src/app.ts +99 -36
- package/src/index.dev.ts +196 -0
- package/src/reset.d.ts +1 -0
- package/src/routes/__snapshots__/entities.test.ts.snap +68 -0
- package/src/routes/__snapshots__/entity.test.ts.snap +57 -0
- package/src/routes/__snapshots__/search.test.ts.snap +139 -0
- package/src/routes/crate.test.ts +441 -0
- package/src/routes/crate.ts +145 -0
- package/src/routes/entities.test.ts +284 -0
- package/src/routes/entities.ts +69 -48
- package/src/routes/entity.test.ts +132 -0
- package/src/routes/entity.ts +59 -22
- package/src/routes/file.test.ts +399 -0
- package/src/routes/file.ts +157 -0
- package/src/routes/files.test.ts +303 -0
- package/src/routes/files.ts +89 -0
- package/src/routes/search.test.ts +807 -0
- package/src/routes/search.ts +67 -25
- package/src/test/__snapshots__/integration.test.ts.snap +18 -0
- package/src/test/helpers/fastify.ts +39 -0
- package/src/test/integration.setup.ts +200 -0
- package/src/test/integration.test.ts +411 -0
- package/src/transformers/default.test.ts +199 -0
- package/src/transformers/default.ts +165 -0
- package/src/transformers/transformer.test.ts +233 -0
- package/src/types/fileHandlers.ts +65 -0
- package/src/types/transformers.ts +52 -0
- package/src/utils/errors.test.ts +75 -0
- package/src/utils/errors.ts +45 -0
- package/dist/index.js +0 -25
- package/dist/index.js.map +0 -1
- package/dist/scripts/loadEntities.js +0 -170
- package/dist/scripts/loadEntities.js.map +0 -1
- package/dist/type.d.ts +0 -6
- package/dist/type.js +0 -2
- package/dist/type.js.map +0 -1
- package/src/index.ts +0 -35
- package/src/scripts/loadEntities.ts +0 -223
- package/src/type.ts +0 -7
- /package/dist/{scripts/loadEntities.d.ts → app.test.d.ts} +0 -0
- /package/dist/{index.d.ts → index.dev.d.ts} +0 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { Entity, File } from '../generated/prisma/client.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Standard entity shape - output of base transformation
|
|
5
|
+
* Does not include access information
|
|
6
|
+
*/
|
|
7
|
+
export type StandardEntity = {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
memberOf: string | null;
|
|
12
|
+
rootCollection: string | null;
|
|
13
|
+
metadataLicenseId: string;
|
|
14
|
+
contentLicenseId: string;
|
|
15
|
+
} & ({ entityType: string; fileId?: never } | { entityType: 'http://schema.org/MediaObject'; fileId: string });
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Access information for an entity
|
|
19
|
+
*/
|
|
20
|
+
type AccessInfo = {
|
|
21
|
+
metadata: boolean;
|
|
22
|
+
content: boolean;
|
|
23
|
+
contentAuthorizationUrl?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Authorised entity - includes access information
|
|
28
|
+
* This is the output of the access transformer
|
|
29
|
+
*/
|
|
30
|
+
export type AuthorisedEntity = StandardEntity & {
|
|
31
|
+
access: AccessInfo;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Access information for a file
|
|
36
|
+
* Files only control content access - metadata is always accessible
|
|
37
|
+
*/
|
|
38
|
+
type FileAccessInfo = {
|
|
39
|
+
content: boolean;
|
|
40
|
+
contentAuthorizationUrl?: string;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Standard file shape - output of base file transformation
|
|
45
|
+
* Does not include access information
|
|
46
|
+
* Files only have contentLicenseId (no metadataLicenseId)
|
|
47
|
+
*/
|
|
48
|
+
export type StandardFile = {
|
|
49
|
+
id: string;
|
|
50
|
+
filename: string;
|
|
51
|
+
mediaType: string;
|
|
52
|
+
size: number;
|
|
53
|
+
memberOf: string;
|
|
54
|
+
rootCollection: string;
|
|
55
|
+
contentLicenseId: string;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Authorised file - includes access information
|
|
60
|
+
* This is the output of the file access transformer
|
|
61
|
+
* File metadata is always accessible - only content access is controlled
|
|
62
|
+
*/
|
|
63
|
+
export type AuthorisedFile = StandardFile & {
|
|
64
|
+
access: FileAccessInfo;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Base entity transformer - always applied first
|
|
69
|
+
* Transforms raw database entity to standard entity shape (without access)
|
|
70
|
+
*/
|
|
71
|
+
export const baseEntityTransformer = (entity: Entity): StandardEntity => {
|
|
72
|
+
const base: StandardEntity = {
|
|
73
|
+
id: entity.rocrateId,
|
|
74
|
+
name: entity.name,
|
|
75
|
+
description: entity.description,
|
|
76
|
+
entityType: entity.entityType,
|
|
77
|
+
memberOf: entity.memberOf,
|
|
78
|
+
rootCollection: entity.rootCollection,
|
|
79
|
+
metadataLicenseId: entity.metadataLicenseId,
|
|
80
|
+
contentLicenseId: entity.contentLicenseId,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
if (base.entityType === ('http://schema.org/MediaObject' as const)) {
|
|
84
|
+
if (!entity.fileId) {
|
|
85
|
+
return base;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
...base,
|
|
90
|
+
entityType: base.entityType,
|
|
91
|
+
fileId: entity.fileId,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return base;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* All Public Access Transformer - grants full access to metadata and content
|
|
100
|
+
*
|
|
101
|
+
* WARNING: This transformer makes ALL content publicly accessible without restrictions.
|
|
102
|
+
* Only use this for fully public datasets where no access control is needed.
|
|
103
|
+
*
|
|
104
|
+
* For repositories with restricted content, implement a custom accessTransformer
|
|
105
|
+
* that checks user permissions and licenses.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* await server.register(arocapi, {
|
|
110
|
+
* prisma,
|
|
111
|
+
* opensearch,
|
|
112
|
+
* accessTransformer: AllPublicAccessTransformer, // Explicit choice for public data
|
|
113
|
+
* });
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export const AllPublicAccessTransformer = (entity: StandardEntity): AuthorisedEntity => ({
|
|
117
|
+
...entity,
|
|
118
|
+
access: {
|
|
119
|
+
metadata: true,
|
|
120
|
+
content: true,
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Base file transformer - always applied first
|
|
126
|
+
* Transforms raw database file to standard file shape (without access)
|
|
127
|
+
*/
|
|
128
|
+
export const baseFileTransformer = (file: File): StandardFile => ({
|
|
129
|
+
id: file.fileId,
|
|
130
|
+
filename: file.filename,
|
|
131
|
+
mediaType: file.mediaType,
|
|
132
|
+
size: Number(file.size),
|
|
133
|
+
memberOf: file.memberOf,
|
|
134
|
+
rootCollection: file.rootCollection,
|
|
135
|
+
contentLicenseId: file.contentLicenseId,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* All Public File Access Transformer - grants full access to file content
|
|
140
|
+
*
|
|
141
|
+
* WARNING: This transformer makes ALL file content publicly accessible without restrictions.
|
|
142
|
+
* Only use this for fully public datasets where no access control is needed.
|
|
143
|
+
*
|
|
144
|
+
* For repositories with restricted content, implement a custom fileAccessTransformer
|
|
145
|
+
* that checks user permissions and licenses.
|
|
146
|
+
*
|
|
147
|
+
* Note: File metadata (filename, size, mediaType, etc.) is always accessible.
|
|
148
|
+
* This transformer only controls content access.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* await server.register(arocapi, {
|
|
153
|
+
* prisma,
|
|
154
|
+
* opensearch,
|
|
155
|
+
* accessTransformer: AllPublicAccessTransformer,
|
|
156
|
+
* fileAccessTransformer: AllPublicFileAccessTransformer, // Explicit choice for public data
|
|
157
|
+
* });
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
export const AllPublicFileAccessTransformer = (file: StandardFile): AuthorisedFile => ({
|
|
161
|
+
...file,
|
|
162
|
+
access: {
|
|
163
|
+
content: true,
|
|
164
|
+
},
|
|
165
|
+
});
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import type { FastifyInstance, FastifyRequest } from 'fastify';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import type { AccessTransformer, EntityTransformer, TransformerContext } from '../types/transformers.js';
|
|
4
|
+
import {
|
|
5
|
+
AllPublicAccessTransformer,
|
|
6
|
+
type AuthorisedEntity,
|
|
7
|
+
baseEntityTransformer,
|
|
8
|
+
type StandardEntity,
|
|
9
|
+
} from './default.js';
|
|
10
|
+
|
|
11
|
+
describe('Entity Transformers', () => {
|
|
12
|
+
const mockContext: TransformerContext = {
|
|
13
|
+
request: {} as FastifyRequest,
|
|
14
|
+
fastify: {} as FastifyInstance,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const mockStandardEntity: StandardEntity = {
|
|
18
|
+
id: 'http://example.com/entity/123',
|
|
19
|
+
name: 'Test Entity',
|
|
20
|
+
description: 'A test entity',
|
|
21
|
+
entityType: 'http://schema.org/Person',
|
|
22
|
+
memberOf: 'http://example.com/collection',
|
|
23
|
+
rootCollection: 'http://example.com/root',
|
|
24
|
+
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
25
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const mockAuthorisedEntity: AuthorisedEntity = {
|
|
29
|
+
...mockStandardEntity,
|
|
30
|
+
access: {
|
|
31
|
+
metadata: true,
|
|
32
|
+
content: true,
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
describe('baseEntityTransformer', () => {
|
|
37
|
+
it('should transform raw entity to standard shape', () => {
|
|
38
|
+
const rawEntity = {
|
|
39
|
+
id: 1,
|
|
40
|
+
rocrateId: 'http://example.com/entity/123',
|
|
41
|
+
name: 'Test Entity',
|
|
42
|
+
description: 'A test entity',
|
|
43
|
+
entityType: 'http://schema.org/Person',
|
|
44
|
+
fileId: null,
|
|
45
|
+
memberOf: 'http://example.com/collection',
|
|
46
|
+
rootCollection: 'http://example.com/root',
|
|
47
|
+
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
48
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
49
|
+
rocrate: {},
|
|
50
|
+
meta: {},
|
|
51
|
+
createdAt: new Date(),
|
|
52
|
+
updatedAt: new Date(),
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const result = baseEntityTransformer(rawEntity);
|
|
56
|
+
|
|
57
|
+
expect(result).toEqual(mockStandardEntity);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('AllPublicAccessTransformer', () => {
|
|
62
|
+
it('should add full access to standard entity', () => {
|
|
63
|
+
const result = AllPublicAccessTransformer(mockStandardEntity);
|
|
64
|
+
|
|
65
|
+
expect(result).toEqual({
|
|
66
|
+
...mockStandardEntity,
|
|
67
|
+
access: {
|
|
68
|
+
metadata: true,
|
|
69
|
+
content: true,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should grant metadata and content access', () => {
|
|
75
|
+
const result = AllPublicAccessTransformer(mockStandardEntity);
|
|
76
|
+
|
|
77
|
+
expect(result.access.metadata).toBe(true);
|
|
78
|
+
expect(result.access.content).toBe(true);
|
|
79
|
+
expect(result.access.contentAuthorizationUrl).toBeUndefined();
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('Custom transformers', () => {
|
|
84
|
+
it('should allow custom transformer to add computed fields', () => {
|
|
85
|
+
const customTransformer: EntityTransformer<AuthorisedEntity, { id: string; displayName: string; uri: string }> = (
|
|
86
|
+
entity,
|
|
87
|
+
) => ({
|
|
88
|
+
id: entity.id,
|
|
89
|
+
displayName: entity.name.toUpperCase(),
|
|
90
|
+
uri: entity.id,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const result = customTransformer(mockAuthorisedEntity, mockContext);
|
|
94
|
+
|
|
95
|
+
expect(result).toEqual({
|
|
96
|
+
id: 'http://example.com/entity/123',
|
|
97
|
+
displayName: 'TEST ENTITY',
|
|
98
|
+
uri: 'http://example.com/entity/123',
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should support async transformers for fetching related data', async () => {
|
|
103
|
+
const asyncTransformer: EntityTransformer<
|
|
104
|
+
AuthorisedEntity,
|
|
105
|
+
{ id: string; name: string; relatedData: string }
|
|
106
|
+
> = async (entity) => {
|
|
107
|
+
// Simulate async operation
|
|
108
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
id: entity.id,
|
|
112
|
+
name: entity.name,
|
|
113
|
+
relatedData: 'fetched-data',
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const result = await asyncTransformer(mockAuthorisedEntity, mockContext);
|
|
118
|
+
|
|
119
|
+
expect(result).toEqual({
|
|
120
|
+
id: 'http://example.com/entity/123',
|
|
121
|
+
name: 'Test Entity',
|
|
122
|
+
relatedData: 'fetched-data',
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('should allow transformer to access context', async () => {
|
|
127
|
+
const contextAwareTransformer: EntityTransformer<
|
|
128
|
+
AuthorisedEntity,
|
|
129
|
+
{
|
|
130
|
+
id: string;
|
|
131
|
+
name: string;
|
|
132
|
+
requestUrl: string;
|
|
133
|
+
}
|
|
134
|
+
> = async (entity, context) => ({
|
|
135
|
+
id: entity.id,
|
|
136
|
+
name: entity.name,
|
|
137
|
+
requestUrl: context.request.url || 'unknown',
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const contextWithUrl: TransformerContext = {
|
|
141
|
+
request: { url: '/test-url' } as FastifyRequest,
|
|
142
|
+
fastify: {} as FastifyInstance,
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const result = await contextAwareTransformer(mockAuthorisedEntity, contextWithUrl);
|
|
146
|
+
|
|
147
|
+
expect(result).toEqual({
|
|
148
|
+
id: 'http://example.com/entity/123',
|
|
149
|
+
name: 'Test Entity',
|
|
150
|
+
requestUrl: '/test-url',
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('should support transformer pipeline', async () => {
|
|
155
|
+
const addDisplayName: EntityTransformer<AuthorisedEntity, AuthorisedEntity & { displayName: string }> = (
|
|
156
|
+
entity,
|
|
157
|
+
) => ({
|
|
158
|
+
...entity,
|
|
159
|
+
displayName: `${entity.name} [${entity.entityType.split('/').pop()}]`,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const addUpperCase: EntityTransformer<
|
|
163
|
+
AuthorisedEntity & { displayName: string },
|
|
164
|
+
AuthorisedEntity & { displayName: string; upperName: string }
|
|
165
|
+
> = (entity) => ({
|
|
166
|
+
...entity,
|
|
167
|
+
upperName: entity.name.toUpperCase(),
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Simulate pipeline
|
|
171
|
+
// biome-ignore lint/suspicious/noExplicitAny: fine in tests
|
|
172
|
+
let result: any = mockAuthorisedEntity;
|
|
173
|
+
result = await addDisplayName(result, mockContext);
|
|
174
|
+
result = await addUpperCase(result, mockContext);
|
|
175
|
+
|
|
176
|
+
expect(result).toMatchObject({
|
|
177
|
+
id: 'http://example.com/entity/123',
|
|
178
|
+
name: 'Test Entity',
|
|
179
|
+
displayName: 'Test Entity [Person]',
|
|
180
|
+
upperName: 'TEST ENTITY',
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should demonstrate full pipeline: base -> access -> custom', async () => {
|
|
185
|
+
const rawEntity = {
|
|
186
|
+
id: 1,
|
|
187
|
+
rocrateId: 'http://example.com/entity/123',
|
|
188
|
+
name: 'Test Entity',
|
|
189
|
+
description: 'A test entity',
|
|
190
|
+
entityType: 'http://schema.org/Person',
|
|
191
|
+
fileId: null,
|
|
192
|
+
memberOf: 'http://example.com/collection',
|
|
193
|
+
rootCollection: 'http://example.com/root',
|
|
194
|
+
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
195
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
196
|
+
rocrate: {},
|
|
197
|
+
meta: {},
|
|
198
|
+
createdAt: new Date(),
|
|
199
|
+
updatedAt: new Date(),
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const customAccessTransformer: AccessTransformer = (entity) => ({
|
|
203
|
+
...entity,
|
|
204
|
+
access: {
|
|
205
|
+
metadata: true,
|
|
206
|
+
content: false,
|
|
207
|
+
contentAuthorizationUrl: 'https://example.com/auth',
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const addMetadata: EntityTransformer<AuthorisedEntity, AuthorisedEntity & { processed: boolean }> = (entity) => ({
|
|
212
|
+
...entity,
|
|
213
|
+
processed: true,
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// Full pipeline
|
|
217
|
+
const standard = baseEntityTransformer(rawEntity);
|
|
218
|
+
const authorised = await customAccessTransformer(standard, mockContext);
|
|
219
|
+
const final = await addMetadata(authorised, mockContext);
|
|
220
|
+
|
|
221
|
+
expect(final).toMatchObject({
|
|
222
|
+
id: 'http://example.com/entity/123',
|
|
223
|
+
name: 'Test Entity',
|
|
224
|
+
access: {
|
|
225
|
+
metadata: true,
|
|
226
|
+
content: false,
|
|
227
|
+
contentAuthorizationUrl: 'https://example.com/auth',
|
|
228
|
+
},
|
|
229
|
+
processed: true,
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Readable } from 'node:stream';
|
|
2
|
+
import type { FastifyInstance, FastifyRequest } from 'fastify';
|
|
3
|
+
import type { Entity, File } from '../generated/prisma/client.js';
|
|
4
|
+
|
|
5
|
+
export type FileHandlerContext = {
|
|
6
|
+
request: FastifyRequest;
|
|
7
|
+
fastify: FastifyInstance;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type FileMetadata = {
|
|
11
|
+
contentType: string;
|
|
12
|
+
contentLength: number;
|
|
13
|
+
etag?: string;
|
|
14
|
+
lastModified?: Date;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type FileRedirectResult = {
|
|
18
|
+
type: 'redirect';
|
|
19
|
+
url: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type FileStreamResult = {
|
|
23
|
+
type: 'stream';
|
|
24
|
+
stream: Readable;
|
|
25
|
+
metadata: FileMetadata;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type FilePathResult = {
|
|
29
|
+
type: 'file';
|
|
30
|
+
path: string; // Absolute file path on disk
|
|
31
|
+
metadata: FileMetadata;
|
|
32
|
+
accelPath?: string; // Optional nginx internal path for X-Accel-Redirect
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type FileResult = FileRedirectResult | FileStreamResult | FilePathResult;
|
|
36
|
+
|
|
37
|
+
export type GetFileHandler = (
|
|
38
|
+
file: File,
|
|
39
|
+
context: FileHandlerContext,
|
|
40
|
+
) => Promise<FileResult | false> | FileResult | false;
|
|
41
|
+
|
|
42
|
+
export type HeadFileHandler = (
|
|
43
|
+
file: File,
|
|
44
|
+
context: FileHandlerContext,
|
|
45
|
+
) => Promise<FileMetadata | false> | FileMetadata | false;
|
|
46
|
+
|
|
47
|
+
export type FileHandler = {
|
|
48
|
+
get: GetFileHandler;
|
|
49
|
+
head: HeadFileHandler;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export type GetRoCrateHandler = (
|
|
53
|
+
entity: Entity,
|
|
54
|
+
context: FileHandlerContext,
|
|
55
|
+
) => Promise<FileResult | false> | FileResult | false;
|
|
56
|
+
|
|
57
|
+
export type HeadRoCrateHandler = (
|
|
58
|
+
entity: Entity,
|
|
59
|
+
context: FileHandlerContext,
|
|
60
|
+
) => Promise<FileMetadata | false> | FileMetadata | false;
|
|
61
|
+
|
|
62
|
+
export type RoCrateHandler = {
|
|
63
|
+
get: GetRoCrateHandler;
|
|
64
|
+
head: HeadRoCrateHandler;
|
|
65
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { FastifyInstance, FastifyRequest } from 'fastify';
|
|
2
|
+
import type { AuthorisedEntity, AuthorisedFile, StandardEntity, StandardFile } from '../transformers/default.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Context provided to entity transformers
|
|
6
|
+
*/
|
|
7
|
+
export type TransformerContext = {
|
|
8
|
+
request: FastifyRequest;
|
|
9
|
+
fastify: FastifyInstance;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Access transformer function - required
|
|
14
|
+
* Transforms StandardEntity to AuthorisedEntity by adding access information
|
|
15
|
+
*/
|
|
16
|
+
export type AccessTransformer = (
|
|
17
|
+
entity: StandardEntity,
|
|
18
|
+
context: TransformerContext,
|
|
19
|
+
) => Promise<AuthorisedEntity> | AuthorisedEntity;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Entity transformer function
|
|
23
|
+
* Receives AuthorisedEntity and transforms it further
|
|
24
|
+
* Transformers are applied as a pipeline, with each transformer receiving
|
|
25
|
+
* the output of the previous one
|
|
26
|
+
*/
|
|
27
|
+
export type EntityTransformer<TInput = AuthorisedEntity, TOutput = TInput> = (
|
|
28
|
+
entity: TInput,
|
|
29
|
+
context: TransformerContext,
|
|
30
|
+
) => Promise<TOutput> | TOutput;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* File access transformer function - required
|
|
34
|
+
* Transforms StandardFile to AuthorisedFile by adding access information
|
|
35
|
+
* File metadata (filename, size, mediaType, etc.) is always accessible
|
|
36
|
+
* Only content access is controlled (access.content)
|
|
37
|
+
*/
|
|
38
|
+
export type FileAccessTransformer = (
|
|
39
|
+
file: StandardFile,
|
|
40
|
+
context: TransformerContext,
|
|
41
|
+
) => Promise<AuthorisedFile> | AuthorisedFile;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* File transformer function
|
|
45
|
+
* Receives AuthorisedFile and transforms it further
|
|
46
|
+
* Transformers are applied as a pipeline, with each transformer receiving
|
|
47
|
+
* the output of the previous one
|
|
48
|
+
*/
|
|
49
|
+
export type FileTransformer<TInput = AuthorisedFile, TOutput = TInput> = (
|
|
50
|
+
file: TInput,
|
|
51
|
+
context: TransformerContext,
|
|
52
|
+
) => Promise<TOutput> | TOutput;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { 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('createNotFoundError', () => {
|
|
33
|
+
it('should create a not found error with message', () => {
|
|
34
|
+
const message = 'Entity not found';
|
|
35
|
+
const error = createNotFoundError(message);
|
|
36
|
+
|
|
37
|
+
expect(error).toEqual({
|
|
38
|
+
error: {
|
|
39
|
+
code: 'NOT_FOUND',
|
|
40
|
+
message,
|
|
41
|
+
details: undefined,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should create a not found error with message and entityId', () => {
|
|
47
|
+
const message = 'Entity not found';
|
|
48
|
+
const entityId = 'http://example.com/entity/123';
|
|
49
|
+
const error = createNotFoundError(message, entityId);
|
|
50
|
+
|
|
51
|
+
expect(error).toEqual({
|
|
52
|
+
error: {
|
|
53
|
+
code: 'NOT_FOUND',
|
|
54
|
+
message,
|
|
55
|
+
details: {
|
|
56
|
+
entityId,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should handle undefined entityId', () => {
|
|
63
|
+
const message = 'Entity not found';
|
|
64
|
+
const error = createNotFoundError(message, undefined);
|
|
65
|
+
|
|
66
|
+
expect(error).toEqual({
|
|
67
|
+
error: {
|
|
68
|
+
code: 'NOT_FOUND',
|
|
69
|
+
message,
|
|
70
|
+
details: undefined,
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const ERROR_CODES = {
|
|
2
|
+
VALIDATION_ERROR: 'VALIDATION_ERROR',
|
|
3
|
+
NOT_FOUND: 'NOT_FOUND',
|
|
4
|
+
RATE_LIMIT_EXCEEDED: 'RATE_LIMIT_EXCEEDED',
|
|
5
|
+
INTERNAL_ERROR: 'INTERNAL_ERROR',
|
|
6
|
+
INVALID_REQUEST: 'INVALID_REQUEST',
|
|
7
|
+
INVALID_ENTITY_TYPE: 'INVALID_ENTITY_TYPE',
|
|
8
|
+
} as const;
|
|
9
|
+
|
|
10
|
+
type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
|
|
11
|
+
|
|
12
|
+
type ErrorDetails = {
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type StandardErrorResponse = {
|
|
17
|
+
error: {
|
|
18
|
+
code: ErrorCode;
|
|
19
|
+
message: string;
|
|
20
|
+
details?: ErrorDetails;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const createErrorResponse = (code: ErrorCode, message: string, details?: ErrorDetails): StandardErrorResponse => ({
|
|
25
|
+
error: {
|
|
26
|
+
code,
|
|
27
|
+
message,
|
|
28
|
+
details,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export const createValidationError = (message: string, issues: string[]): StandardErrorResponse =>
|
|
33
|
+
createErrorResponse(ERROR_CODES.VALIDATION_ERROR, message, { issues });
|
|
34
|
+
|
|
35
|
+
export const createNotFoundError = (message: string, entityId?: string): StandardErrorResponse =>
|
|
36
|
+
createErrorResponse(ERROR_CODES.NOT_FOUND, message, entityId ? { entityId } : undefined);
|
|
37
|
+
|
|
38
|
+
// export const createRateLimitError = (retryAfter: number): StandardErrorResponse =>
|
|
39
|
+
// createErrorResponse(ERROR_CODES.RATE_LIMIT_EXCEEDED, 'Rate limit exceeded. Please retry after the specified time.', {
|
|
40
|
+
// retryAfter,
|
|
41
|
+
// });
|
|
42
|
+
|
|
43
|
+
export const createInternalError = (message = 'Internal server error'): StandardErrorResponse => {
|
|
44
|
+
return createErrorResponse(ERROR_CODES.INTERNAL_ERROR, message);
|
|
45
|
+
};
|
package/dist/index.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { Client } from '@opensearch-project/opensearch';
|
|
2
|
-
import Fastify from 'fastify';
|
|
3
|
-
import arocapi from './app.js';
|
|
4
|
-
import { PrismaClient } from './generated/prisma/client.js';
|
|
5
|
-
const prisma = new PrismaClient();
|
|
6
|
-
if (!process.env.OPENSEARCH_URL) {
|
|
7
|
-
throw new Error('OPENSEARCH_URL environment variable is not set');
|
|
8
|
-
}
|
|
9
|
-
const opensearchUrl = process.env.OPENSEARCH_URL;
|
|
10
|
-
const opensearch = new Client({ node: opensearchUrl });
|
|
11
|
-
const fastify = Fastify({
|
|
12
|
-
logger: true,
|
|
13
|
-
});
|
|
14
|
-
fastify.register(arocapi, { prisma, opensearch });
|
|
15
|
-
const start = async () => {
|
|
16
|
-
try {
|
|
17
|
-
await fastify.listen({ port: 3000 });
|
|
18
|
-
}
|
|
19
|
-
catch (err) {
|
|
20
|
-
fastify.log.error(err);
|
|
21
|
-
process.exit(1);
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
start();
|
|
25
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AACxD,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,OAAO,OAAO,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAS5D,MAAM,MAAM,GAAG,IAAI,YAAY,EAAE,CAAC;AAElC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;IAChC,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;AACpE,CAAC;AACD,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;AACjD,MAAM,UAAU,GAAG,IAAI,MAAM,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC,CAAC;AAEvD,MAAM,OAAO,GAAG,OAAO,CAAC;IACtB,MAAM,EAAE,IAAI;CACb,CAAC,CAAC;AACH,OAAO,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC;AAElD,MAAM,KAAK,GAAG,KAAK,IAAI,EAAE;IACvB,IAAI,CAAC;QACH,MAAM,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IACvC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACvB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC,CAAC;AACF,KAAK,EAAE,CAAC"}
|