arocapi 1.0.5 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/README.md +597 -29
  2. package/dist/app.d.ts +27 -6
  3. package/dist/app.js +76 -17
  4. package/dist/app.js.map +1 -1
  5. package/dist/app.test.js +124 -0
  6. package/dist/app.test.js.map +1 -0
  7. package/dist/express.d.ts +3 -2
  8. package/dist/express.js +11 -11
  9. package/dist/express.js.map +1 -1
  10. package/dist/index.dev.d.ts +8 -0
  11. package/dist/index.dev.js +146 -0
  12. package/dist/index.dev.js.map +1 -0
  13. package/dist/routes/crate.d.ts +7 -0
  14. package/dist/routes/crate.js +99 -0
  15. package/dist/routes/crate.js.map +1 -0
  16. package/dist/routes/crate.test.d.ts +1 -0
  17. package/dist/routes/crate.test.js +360 -0
  18. package/dist/routes/crate.test.js.map +1 -0
  19. package/dist/routes/entities.d.ts +8 -0
  20. package/dist/routes/entities.js +75 -0
  21. package/dist/routes/entities.js.map +1 -0
  22. package/dist/routes/entities.test.d.ts +1 -0
  23. package/dist/routes/entities.test.js +245 -0
  24. package/dist/routes/entities.test.js.map +1 -0
  25. package/dist/routes/entity.d.ts +8 -0
  26. package/dist/routes/entity.js +45 -0
  27. package/dist/routes/entity.js.map +1 -0
  28. package/dist/routes/entity.test.d.ts +1 -0
  29. package/dist/routes/entity.test.js +109 -0
  30. package/dist/routes/entity.test.js.map +1 -0
  31. package/dist/routes/file.d.ts +7 -0
  32. package/dist/routes/file.js +111 -0
  33. package/dist/routes/file.js.map +1 -0
  34. package/dist/routes/file.test.d.ts +1 -0
  35. package/dist/routes/file.test.js +319 -0
  36. package/dist/routes/file.test.js.map +1 -0
  37. package/dist/routes/files.d.ts +8 -0
  38. package/dist/routes/files.js +61 -0
  39. package/dist/routes/files.js.map +1 -0
  40. package/dist/routes/files.test.d.ts +1 -0
  41. package/dist/routes/files.test.js +244 -0
  42. package/dist/routes/files.test.js.map +1 -0
  43. package/dist/routes/search.d.ts +8 -0
  44. package/dist/routes/{search/index.js → search.js} +45 -19
  45. package/dist/routes/search.js.map +1 -0
  46. package/dist/routes/search.test.d.ts +1 -0
  47. package/dist/routes/search.test.js +698 -0
  48. package/dist/routes/search.test.js.map +1 -0
  49. package/dist/test/helpers/fastify.d.ts +9 -0
  50. package/dist/test/helpers/fastify.js +24 -0
  51. package/dist/test/helpers/fastify.js.map +1 -0
  52. package/dist/test/integration.setup.d.ts +58 -0
  53. package/dist/test/integration.setup.js +181 -0
  54. package/dist/test/integration.setup.js.map +1 -0
  55. package/dist/test/integration.test.d.ts +1 -0
  56. package/dist/test/integration.test.js +337 -0
  57. package/dist/test/integration.test.js.map +1 -0
  58. package/dist/transformers/default.d.ts +45 -0
  59. package/dist/transformers/default.js +46 -0
  60. package/dist/transformers/default.js.map +1 -0
  61. package/dist/transformers/default.test.d.ts +1 -0
  62. package/dist/transformers/default.test.js +177 -0
  63. package/dist/transformers/default.test.js.map +1 -0
  64. package/dist/transformers/transformer.test.d.ts +1 -0
  65. package/dist/transformers/transformer.test.js +176 -0
  66. package/dist/transformers/transformer.test.js.map +1 -0
  67. package/dist/types/fileHandlers.d.ts +41 -0
  68. package/dist/types/fileHandlers.js +2 -0
  69. package/dist/types/fileHandlers.js.map +1 -0
  70. package/dist/types/transformers.d.ts +10 -0
  71. package/dist/types/transformers.js +2 -0
  72. package/dist/types/transformers.js.map +1 -0
  73. package/dist/utils/errors.d.ts +23 -0
  74. package/dist/utils/errors.js +21 -0
  75. package/dist/utils/errors.js.map +1 -0
  76. package/dist/utils/errors.test.d.ts +1 -0
  77. package/dist/utils/errors.test.js +66 -0
  78. package/dist/utils/errors.test.js.map +1 -0
  79. package/package.json +34 -25
  80. package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +10 -0
  81. package/prisma/migrations/20250923032753_api_refactor/migration.sql +39 -0
  82. package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +8 -0
  83. package/prisma/migrations/20251020140329_add_meta_column/migration.sql +2 -0
  84. package/prisma/migrations/20251027010713_add_file_table/migration.sql +17 -0
  85. package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +2 -0
  86. package/prisma/models/entity.prisma +16 -12
  87. package/prisma/models/file.prisma +20 -0
  88. package/prisma/schema.prisma +1 -20
  89. package/prisma.config.ts +6 -7
  90. package/src/__snapshots__/app.test.ts.snap +8 -0
  91. package/src/app.test.ts +158 -0
  92. package/src/app.ts +150 -30
  93. package/src/express.ts +12 -11
  94. package/src/index.dev.ts +196 -0
  95. package/src/reset.d.ts +1 -0
  96. package/src/routes/__snapshots__/entities.test.ts.snap +68 -0
  97. package/src/routes/__snapshots__/entity.test.ts.snap +66 -0
  98. package/src/routes/__snapshots__/search.test.ts.snap +139 -0
  99. package/src/routes/crate.test.ts +441 -0
  100. package/src/routes/crate.ts +141 -0
  101. package/src/routes/entities.test.ts +284 -0
  102. package/src/routes/entities.ts +101 -0
  103. package/src/routes/entity.test.ts +132 -0
  104. package/src/routes/entity.ts +63 -0
  105. package/src/routes/file.test.ts +399 -0
  106. package/src/routes/file.ts +153 -0
  107. package/src/routes/files.test.ts +303 -0
  108. package/src/routes/files.ts +87 -0
  109. package/src/routes/search.test.ts +807 -0
  110. package/src/routes/{search/index.ts → search.ts} +67 -24
  111. package/src/test/__snapshots__/integration.test.ts.snap +18 -0
  112. package/src/test/helpers/fastify.ts +39 -0
  113. package/src/test/integration.setup.ts +200 -0
  114. package/src/test/integration.test.ts +411 -0
  115. package/src/transformers/default.test.ts +199 -0
  116. package/src/transformers/default.ts +165 -0
  117. package/src/transformers/transformer.test.ts +233 -0
  118. package/src/types/fileHandlers.ts +65 -0
  119. package/src/types/transformers.ts +52 -0
  120. package/src/utils/errors.test.ts +75 -0
  121. package/src/utils/errors.ts +45 -0
  122. package/dist/plugins/cors.d.ts +0 -3
  123. package/dist/plugins/cors.js +0 -6
  124. package/dist/plugins/cors.js.map +0 -1
  125. package/dist/plugins/opensearch.d.ts +0 -9
  126. package/dist/plugins/opensearch.js +0 -22
  127. package/dist/plugins/opensearch.js.map +0 -1
  128. package/dist/plugins/prisma.d.ts +0 -8
  129. package/dist/plugins/prisma.js +0 -11
  130. package/dist/plugins/prisma.js.map +0 -1
  131. package/dist/plugins/sensible.d.ts +0 -3
  132. package/dist/plugins/sensible.js +0 -6
  133. package/dist/plugins/sensible.js.map +0 -1
  134. package/dist/plugins/support.d.ts +0 -9
  135. package/dist/plugins/support.js +0 -5
  136. package/dist/plugins/support.js.map +0 -1
  137. package/dist/plugins/validation.d.ts +0 -3
  138. package/dist/plugins/validation.js +0 -32
  139. package/dist/plugins/validation.js.map +0 -1
  140. package/dist/routes/entities/index.d.ts +0 -3
  141. package/dist/routes/entities/index.js +0 -63
  142. package/dist/routes/entities/index.js.map +0 -1
  143. package/dist/routes/entity/index.d.ts +0 -3
  144. package/dist/routes/entity/index.js +0 -24
  145. package/dist/routes/entity/index.js.map +0 -1
  146. package/dist/routes/search/index.d.ts +0 -3
  147. package/dist/routes/search/index.js.map +0 -1
  148. package/dist/scripts/loadEntities.js +0 -170
  149. package/dist/scripts/loadEntities.js.map +0 -1
  150. package/dist/type.d.ts +0 -6
  151. package/dist/type.js +0 -2
  152. package/dist/type.js.map +0 -1
  153. package/src/plugins/README.md +0 -16
  154. package/src/plugins/cors.ts +0 -11
  155. package/src/plugins/opensearch.ts +0 -32
  156. package/src/plugins/prisma.ts +0 -23
  157. package/src/plugins/sensible.ts +0 -11
  158. package/src/plugins/support.ts +0 -20
  159. package/src/plugins/validation.ts +0 -48
  160. package/src/routes/README.md +0 -29
  161. package/src/routes/entities/index.ts +0 -83
  162. package/src/routes/entity/index.ts +0 -28
  163. package/src/scripts/loadEntities.ts +0 -223
  164. package/src/type.ts +0 -7
  165. /package/dist/{scripts/loadEntities.d.ts → app.test.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
+ };
@@ -1,3 +0,0 @@
1
- import { type FastifyCorsOptions } from '@fastify/cors';
2
- declare const _default: import("fastify").FastifyPluginCallback<FastifyCorsOptions, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
3
- export default _default;
@@ -1,6 +0,0 @@
1
- import cors from '@fastify/cors';
2
- import fp from 'fastify-plugin';
3
- export default fp(async (fastify) => {
4
- fastify.register(cors);
5
- });
6
- //# sourceMappingURL=cors.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cors.js","sourceRoot":"","sources":["../../src/plugins/cors.ts"],"names":[],"mappings":"AAAA,OAAO,IAAiC,MAAM,eAAe,CAAC;AAC9D,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAOhC,eAAe,EAAE,CAAqB,KAAK,EAAE,OAAO,EAAE,EAAE;IACtD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AACzB,CAAC,CAAC,CAAC"}
@@ -1,9 +0,0 @@
1
- import { Client } from '@opensearch-project/opensearch';
2
- import type { FastifyPluginAsync } from 'fastify';
3
- declare module 'fastify' {
4
- interface FastifyInstance {
5
- opensearch: Client;
6
- }
7
- }
8
- declare const _default: import("fastify").FastifyPluginCallback<FastifyPluginAsync, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
9
- export default _default;
@@ -1,22 +0,0 @@
1
- import { Client } from '@opensearch-project/opensearch';
2
- import fp from 'fastify-plugin';
3
- export default fp(async (fastify) => {
4
- if (!process.env.OPENSEARCH_URL) {
5
- throw new Error('OPENSEARCH_URL environment variable is not set');
6
- }
7
- const opensearchUrl = process.env.OPENSEARCH_URL;
8
- const client = new Client({
9
- node: opensearchUrl,
10
- });
11
- try {
12
- await client.ping();
13
- fastify.log.info(`Connected to OpenSearch at ${opensearchUrl}`);
14
- }
15
- catch (error) {
16
- fastify.log.error(`Failed to connect to OpenSearch: ${error}`);
17
- throw error;
18
- }
19
- fastify.decorate('opensearch', client);
20
- fastify.addHook('onClose', () => client.close());
21
- });
22
- //# sourceMappingURL=opensearch.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"opensearch.js","sourceRoot":"","sources":["../../src/plugins/opensearch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAExD,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAQhC,eAAe,EAAE,CAAqB,KAAK,EAAE,OAAO,EAAE,EAAE;IACtD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IACpE,CAAC;IACD,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;IAEjD,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC;QACxB,IAAI,EAAE,aAAa;KACpB,CAAC,CAAC;IAEH,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;QACpB,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,8BAA8B,aAAa,EAAE,CAAC,CAAC;IAClE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,oCAAoC,KAAK,EAAE,CAAC,CAAC;QAC/D,MAAM,KAAK,CAAC;IACd,CAAC;IAED,OAAO,CAAC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IAEvC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;AACnD,CAAC,CAAC,CAAC"}