arocapi 5.0.0-beta.1 → 5.0.0-beta.3
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 +37 -802
- package/dist/app.d.ts +22 -9
- package/dist/app.js +46 -30
- package/dist/app.js.map +1 -1
- package/dist/app.test.js +41 -56
- package/dist/app.test.js.map +1 -1
- package/dist/errorHandling.test.d.ts +1 -0
- package/dist/errorHandling.test.js +222 -0
- package/dist/errorHandling.test.js.map +1 -0
- package/dist/index.dev.js +4 -3
- package/dist/index.dev.js.map +1 -1
- package/dist/routes/capabilities.d.ts +7 -0
- package/dist/routes/capabilities.js +13 -0
- package/dist/routes/capabilities.js.map +1 -0
- package/dist/routes/capabilities.test.d.ts +1 -0
- package/dist/routes/capabilities.test.js +81 -0
- package/dist/routes/capabilities.test.js.map +1 -0
- package/dist/routes/crate.test.js +67 -19
- package/dist/routes/crate.test.js.map +1 -1
- package/dist/routes/entities.d.ts +5 -2
- package/dist/routes/entities.js +31 -49
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entities.test.js +16 -12
- package/dist/routes/entities.test.js.map +1 -1
- package/dist/routes/entity.d.ts +7 -2
- package/dist/routes/entity.js +14 -29
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/entity.test.js +19 -13
- package/dist/routes/entity.test.js.map +1 -1
- package/dist/routes/file.d.ts +6 -0
- package/dist/routes/file.js +67 -59
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/file.test.js +99 -22
- package/dist/routes/file.test.js.map +1 -1
- package/dist/routes/files.d.ts +5 -2
- package/dist/routes/files.js +24 -39
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/files.test.js +44 -21
- package/dist/routes/files.test.js.map +1 -1
- package/dist/routes/metadata.d.ts +6 -0
- package/dist/routes/metadata.js +49 -54
- package/dist/routes/metadata.js.map +1 -1
- package/dist/routes/search.d.ts +8 -5
- package/dist/routes/search.js +95 -101
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.js +92 -39
- package/dist/routes/search.test.js.map +1 -1
- package/dist/test/helpers/fastify.d.ts +1 -1
- package/dist/test/helpers/fastify.js +2 -3
- package/dist/test/helpers/fastify.js.map +1 -1
- package/dist/test/integration.setup.d.ts +19 -1
- package/dist/test/integration.setup.js +15 -7
- package/dist/test/integration.setup.js.map +1 -1
- package/dist/test/integration.test.js +368 -2
- package/dist/test/integration.test.js.map +1 -1
- package/dist/transformers/default.d.ts +10 -18
- package/dist/transformers/default.js +41 -15
- package/dist/transformers/default.js.map +1 -1
- package/dist/transformers/default.test.js +109 -47
- package/dist/transformers/default.test.js.map +1 -1
- package/dist/transformers/transformer.test.js +12 -34
- package/dist/transformers/transformer.test.js.map +1 -1
- package/dist/types/access.d.ts +9 -0
- package/dist/types/access.js +2 -0
- package/dist/types/access.js.map +1 -0
- package/dist/types/context.d.ts +6 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/fileHandlers.d.ts +5 -9
- package/dist/types/transformers.d.ts +4 -10
- package/dist/types/visibility.d.ts +5 -0
- package/dist/types/visibility.js +2 -0
- package/dist/types/visibility.js.map +1 -0
- package/dist/utils/access.d.ts +17 -0
- package/dist/utils/access.js +41 -0
- package/dist/utils/access.js.map +1 -0
- package/dist/utils/errors.d.ts +19 -10
- package/dist/utils/errors.js +44 -8
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/errors.test.js +14 -1
- package/dist/utils/errors.test.js.map +1 -1
- package/dist/utils/queryBuilder.d.ts +12 -0
- package/dist/utils/queryBuilder.js +9 -0
- package/dist/utils/queryBuilder.js.map +1 -1
- package/dist/utils/validation.d.ts +2 -0
- package/dist/utils/validation.js +8 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/visibility.d.ts +18 -0
- package/dist/utils/visibility.js +16 -0
- package/dist/utils/visibility.js.map +1 -0
- package/package.json +34 -33
- package/src/app.test.ts +45 -66
- package/src/app.ts +92 -58
- package/src/errorHandling.test.ts +288 -0
- package/src/index.dev.ts +6 -4
- package/src/routes/__snapshots__/capabilities.test.ts.snap +34 -0
- package/src/routes/__snapshots__/search.test.ts.snap +2 -2
- package/src/routes/capabilities.test.ts +102 -0
- package/src/routes/capabilities.ts +22 -0
- package/src/routes/crate.test.ts +80 -19
- package/src/routes/entities.test.ts +16 -12
- package/src/routes/entities.ts +40 -59
- package/src/routes/entity.test.ts +19 -13
- package/src/routes/entity.ts +22 -35
- package/src/routes/file.test.ts +117 -22
- package/src/routes/file.ts +84 -62
- package/src/routes/files.test.ts +52 -26
- package/src/routes/files.ts +31 -47
- package/src/routes/metadata.ts +64 -58
- package/src/routes/search.test.ts +95 -39
- package/src/routes/search.ts +120 -125
- package/src/test/helpers/fastify.ts +2 -3
- package/src/test/integration.setup.ts +18 -7
- package/src/test/integration.test.ts +472 -1
- package/src/transformers/default.test.ts +139 -47
- package/src/transformers/default.ts +103 -83
- package/src/transformers/transformer.test.ts +18 -44
- package/src/types/access.ts +20 -0
- package/src/types/context.ts +15 -0
- package/src/types/fileHandlers.ts +5 -13
- package/src/types/transformers.ts +4 -32
- package/src/types/visibility.ts +18 -0
- package/src/utils/access.ts +80 -0
- package/src/utils/errors.test.ts +16 -1
- package/src/utils/errors.ts +68 -11
- package/src/utils/queryBuilder.ts +25 -0
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
package/src/app.ts
CHANGED
|
@@ -1,30 +1,35 @@
|
|
|
1
1
|
import sensible from '@fastify/sensible';
|
|
2
2
|
import type { Client } from '@opensearch-project/opensearch';
|
|
3
|
-
import type {
|
|
4
|
-
import { hasZodFastifySchemaValidationErrors, serializerCompiler, validatorCompiler } from 'fastify-type-provider-zod';
|
|
3
|
+
import type { FastifyPluginAsync } from 'fastify';
|
|
5
4
|
import type { PrismaClient } from './generated/prisma/client.js';
|
|
5
|
+
import capabilities from './routes/capabilities.js';
|
|
6
6
|
import entities from './routes/entities.js';
|
|
7
7
|
import entity from './routes/entity.js';
|
|
8
8
|
import file from './routes/file.js';
|
|
9
9
|
import files from './routes/files.js';
|
|
10
10
|
import metadata from './routes/metadata.js';
|
|
11
11
|
import search from './routes/search.js';
|
|
12
|
+
import type { AccessFunction } from './types/access.js';
|
|
13
|
+
import type { IsAuthenticated } from './types/context.js';
|
|
12
14
|
import type { FileHandler, RoCrateHandler } from './types/fileHandlers.js';
|
|
13
|
-
import type {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
FileTransformer,
|
|
18
|
-
} from './types/transformers.js';
|
|
19
|
-
import { createValidationError } from './utils/errors.js';
|
|
20
|
-
import type { QueryBuilderOptions } from './utils/queryBuilder.js';
|
|
15
|
+
import type { EntityTransformer, FileTransformer } from './types/transformers.js';
|
|
16
|
+
import type { EntityVisibilityFilter, SearchVisibilityFilter } from './types/visibility.js';
|
|
17
|
+
import { notFoundHandler } from './utils/errors.js';
|
|
18
|
+
import type { FacetCapability, FilterCapability, QueryBuilderOptions, SearchCapability } from './utils/queryBuilder.js';
|
|
21
19
|
import { OpensearchQueryBuilder } from './utils/queryBuilder.js';
|
|
20
|
+
import { setupValidation } from './utils/validation.js';
|
|
22
21
|
|
|
23
|
-
export type {
|
|
24
|
-
|
|
22
|
+
export type {
|
|
23
|
+
AuthorisedEntity,
|
|
24
|
+
AuthorisedFile,
|
|
25
|
+
BaseEntity,
|
|
26
|
+
StandardEntity,
|
|
27
|
+
StandardFile,
|
|
28
|
+
} from './transformers/default.js';
|
|
29
|
+
export type { AccessFunction, AccessInfo } from './types/access.js';
|
|
30
|
+
export type { IsAuthenticated, RequestContext } from './types/context.js';
|
|
25
31
|
export type {
|
|
26
32
|
FileHandler,
|
|
27
|
-
FileHandlerContext,
|
|
28
33
|
FileMetadata,
|
|
29
34
|
FilePathResult,
|
|
30
35
|
FileRedirectResult,
|
|
@@ -36,38 +41,35 @@ export type {
|
|
|
36
41
|
HeadRoCrateHandler,
|
|
37
42
|
RoCrateHandler,
|
|
38
43
|
} from './types/fileHandlers.js';
|
|
39
|
-
export type {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
export type { EntityTransformer, FileTransformer } from './types/transformers.js';
|
|
45
|
+
export type { EntityVisibilityFilter, SearchVisibilityFilter } from './types/visibility.js';
|
|
46
|
+
export type { LicenceAccessOptions } from './utils/access.js';
|
|
47
|
+
export { AllPublicAccess, LicenceAccess } from './utils/access.js';
|
|
48
|
+
export type { ErrorCode, StandardErrorResponse } from './utils/errors.js';
|
|
49
|
+
export {
|
|
50
|
+
createAuthenticationRequiredError,
|
|
51
|
+
createError,
|
|
52
|
+
createForbiddenError,
|
|
53
|
+
createInternalError,
|
|
54
|
+
createNotFoundError,
|
|
55
|
+
createValidationError,
|
|
56
|
+
ERROR_CODES,
|
|
57
|
+
errorHandler,
|
|
58
|
+
notFoundHandler,
|
|
59
|
+
} from './utils/errors.js';
|
|
60
|
+
export { AllVisibleEntityFilter, AllVisibleSearchFilter } from './utils/visibility.js';
|
|
61
|
+
export type { FacetCapability, FilterCapability, QueryBuilderOptions, SearchCapability };
|
|
47
62
|
export { OpensearchQueryBuilder };
|
|
48
63
|
|
|
49
|
-
const
|
|
50
|
-
fastify.setValidatorCompiler(validatorCompiler);
|
|
51
|
-
fastify.setSerializerCompiler(serializerCompiler);
|
|
52
|
-
|
|
53
|
-
fastify.setErrorHandler((error, _req, reply) => {
|
|
54
|
-
/* c8 ignore else -- non-Zod errors are caught by route try/catch in practice -- @preserve */
|
|
55
|
-
if (hasZodFastifySchemaValidationErrors(error)) {
|
|
56
|
-
const violations = error.validation.map((v) => ({
|
|
57
|
-
field: v.instancePath,
|
|
58
|
-
message: v.message,
|
|
59
|
-
}));
|
|
60
|
-
|
|
61
|
-
return reply.code(400).send(createValidationError('The request parameters are invalid', violations));
|
|
62
|
-
}
|
|
64
|
+
const DOCS_URL = 'https://arocapi.crate-works.org';
|
|
63
65
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
66
|
+
const REMOVED_OPTIONS: Record<string, { replacement: string; docs: string }> = {
|
|
67
|
+
accessTransformer: { replacement: 'access', docs: `${DOCS_URL}/configuration/access/` },
|
|
68
|
+
fileAccessTransformer: { replacement: 'access', docs: `${DOCS_URL}/configuration/access/` },
|
|
69
|
+
hiddenResponse: {
|
|
70
|
+
replacement: 'promptLoginForHidden and isAuthenticated',
|
|
71
|
+
docs: `${DOCS_URL}/configuration/visibility/#promptloginforhidden`,
|
|
72
|
+
},
|
|
71
73
|
};
|
|
72
74
|
|
|
73
75
|
export type Options = {
|
|
@@ -75,12 +77,15 @@ export type Options = {
|
|
|
75
77
|
opensearch: Client;
|
|
76
78
|
queryBuilderClass?: typeof OpensearchQueryBuilder;
|
|
77
79
|
queryBuilderOptions?: QueryBuilderOptions;
|
|
78
|
-
|
|
80
|
+
access: AccessFunction;
|
|
79
81
|
entityTransformers?: EntityTransformer[];
|
|
80
|
-
fileAccessTransformer: FileAccessTransformer;
|
|
81
82
|
fileTransformers?: FileTransformer[];
|
|
82
83
|
fileHandler: FileHandler;
|
|
83
84
|
roCrateHandler: RoCrateHandler;
|
|
85
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
86
|
+
searchVisibilityFilter: SearchVisibilityFilter;
|
|
87
|
+
isAuthenticated?: IsAuthenticated;
|
|
88
|
+
promptLoginForHidden?: boolean;
|
|
84
89
|
};
|
|
85
90
|
const app: FastifyPluginAsync<Options> = async (fastify, options) => {
|
|
86
91
|
const {
|
|
@@ -88,12 +93,15 @@ const app: FastifyPluginAsync<Options> = async (fastify, options) => {
|
|
|
88
93
|
opensearch,
|
|
89
94
|
queryBuilderClass,
|
|
90
95
|
queryBuilderOptions,
|
|
91
|
-
|
|
96
|
+
access,
|
|
92
97
|
entityTransformers,
|
|
93
|
-
fileAccessTransformer,
|
|
94
98
|
fileTransformers,
|
|
95
99
|
fileHandler,
|
|
96
100
|
roCrateHandler,
|
|
101
|
+
entityVisibilityFilter,
|
|
102
|
+
searchVisibilityFilter,
|
|
103
|
+
isAuthenticated,
|
|
104
|
+
promptLoginForHidden = false,
|
|
97
105
|
} = options;
|
|
98
106
|
|
|
99
107
|
if (!prisma) {
|
|
@@ -104,12 +112,14 @@ const app: FastifyPluginAsync<Options> = async (fastify, options) => {
|
|
|
104
112
|
throw new Error('OpenSearch client is required');
|
|
105
113
|
}
|
|
106
114
|
|
|
107
|
-
|
|
108
|
-
|
|
115
|
+
for (const [removed, { replacement, docs }] of Object.entries(REMOVED_OPTIONS)) {
|
|
116
|
+
if ((options as Record<string, unknown>)[removed] !== undefined) {
|
|
117
|
+
throw new Error(`${removed} has been replaced by ${replacement}, see ${docs}`);
|
|
118
|
+
}
|
|
109
119
|
}
|
|
110
120
|
|
|
111
|
-
if (!
|
|
112
|
-
throw new Error('
|
|
121
|
+
if (!access) {
|
|
122
|
+
throw new Error('access is required');
|
|
113
123
|
}
|
|
114
124
|
|
|
115
125
|
if (!fileHandler) {
|
|
@@ -120,21 +130,45 @@ const app: FastifyPluginAsync<Options> = async (fastify, options) => {
|
|
|
120
130
|
throw new Error('roCrateHandler is required');
|
|
121
131
|
}
|
|
122
132
|
|
|
133
|
+
if (!entityVisibilityFilter) {
|
|
134
|
+
throw new Error('entityVisibilityFilter is required');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (!searchVisibilityFilter) {
|
|
138
|
+
throw new Error('searchVisibilityFilter is required');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (promptLoginForHidden && !isAuthenticated) {
|
|
142
|
+
throw new Error('promptLoginForHidden requires isAuthenticated');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const refusal = { isAuthenticated, promptLoginForHidden };
|
|
146
|
+
|
|
123
147
|
fastify.register(sensible);
|
|
124
148
|
setupValidation(fastify);
|
|
125
149
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
150
|
+
// Fastify only gives a plugin its own not-found handler when registered with its own prefix
|
|
151
|
+
const { prefix } = options as { prefix?: string };
|
|
152
|
+
if (prefix && prefix !== '/') {
|
|
153
|
+
fastify.setNotFoundHandler(notFoundHandler);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const queryBuilder = new (queryBuilderClass ?? OpensearchQueryBuilder)(queryBuilderOptions);
|
|
157
|
+
|
|
158
|
+
fastify.register(capabilities, { queryBuilder });
|
|
159
|
+
fastify.register(entities, { prisma, access, entityTransformers, entityVisibilityFilter });
|
|
160
|
+
fastify.register(entity, { prisma, access, entityTransformers, entityVisibilityFilter, refusal });
|
|
161
|
+
fastify.register(files, { prisma, access, fileTransformers, entityVisibilityFilter });
|
|
162
|
+
fastify.register(file, { prisma, fileHandler, access, entityVisibilityFilter, refusal });
|
|
163
|
+
fastify.register(metadata, { prisma, roCrateHandler, access, entityVisibilityFilter, refusal });
|
|
131
164
|
fastify.register(search, {
|
|
132
165
|
prisma,
|
|
133
166
|
opensearch,
|
|
134
|
-
|
|
167
|
+
access,
|
|
135
168
|
entityTransformers,
|
|
136
|
-
|
|
137
|
-
|
|
169
|
+
queryBuilder,
|
|
170
|
+
entityVisibilityFilter,
|
|
171
|
+
searchVisibilityFilter,
|
|
138
172
|
});
|
|
139
173
|
};
|
|
140
174
|
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import sensible from '@fastify/sensible';
|
|
2
|
+
import Fastify from 'fastify';
|
|
3
|
+
import { serializerCompiler, validatorCompiler, type ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
4
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
import arocapi, {
|
|
8
|
+
AllPublicAccess,
|
|
9
|
+
AllVisibleEntityFilter,
|
|
10
|
+
AllVisibleSearchFilter,
|
|
11
|
+
errorHandler,
|
|
12
|
+
notFoundHandler,
|
|
13
|
+
} from './app.js';
|
|
14
|
+
import { opensearch, prisma } from './test/helpers/fastify.js';
|
|
15
|
+
|
|
16
|
+
const arocapiOptions = {
|
|
17
|
+
prisma,
|
|
18
|
+
opensearch,
|
|
19
|
+
access: AllPublicAccess,
|
|
20
|
+
fileHandler: { get: vi.fn(), head: vi.fn() },
|
|
21
|
+
roCrateHandler: { get: vi.fn(), head: vi.fn() },
|
|
22
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
23
|
+
searchVisibilityFilter: AllVisibleSearchFilter,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const internalErrorBody = { error: { code: 'INTERNAL_ERROR', message: 'Internal server error' } };
|
|
27
|
+
const notFoundBody = { error: { code: 'NOT_FOUND', message: 'The requested resource was not found' } };
|
|
28
|
+
const message = 'Something specific went wrong';
|
|
29
|
+
|
|
30
|
+
const buildConsumer = () => {
|
|
31
|
+
const logs: Record<string, unknown>[] = [];
|
|
32
|
+
const fastify = Fastify({
|
|
33
|
+
logger: {
|
|
34
|
+
level: 'info',
|
|
35
|
+
stream: { write: (line: string) => logs.push(JSON.parse(line) as Record<string, unknown>) },
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
fastify.setValidatorCompiler(validatorCompiler);
|
|
40
|
+
fastify.setSerializerCompiler(serializerCompiler);
|
|
41
|
+
fastify.setErrorHandler(errorHandler);
|
|
42
|
+
fastify.setNotFoundHandler(notFoundHandler);
|
|
43
|
+
|
|
44
|
+
return { fastify, logs };
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
describe('Error handling for consumer routes', () => {
|
|
48
|
+
it('hides unexpected errors behind a generic 500 and logs them', async () => {
|
|
49
|
+
const { fastify, logs } = buildConsumer();
|
|
50
|
+
fastify.get('/boom', async () => {
|
|
51
|
+
throw new Error('db password is hunter2');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const response = await fastify.inject({ method: 'GET', url: '/boom' });
|
|
55
|
+
|
|
56
|
+
expect(response.statusCode).toBe(500);
|
|
57
|
+
expect(response.json()).toEqual(internalErrorBody);
|
|
58
|
+
expect(
|
|
59
|
+
logs.some((l) => l.level === 50 && (l.err as { message: string }).message === 'db password is hunter2'),
|
|
60
|
+
).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it.each([
|
|
64
|
+
[400, 400, { error: { code: 'VALIDATION_ERROR', message } }],
|
|
65
|
+
[401, 401, { error: { code: 'UNAUTHORIZED', message } }],
|
|
66
|
+
[403, 403, { error: { code: 'FORBIDDEN', message } }],
|
|
67
|
+
[404, 404, { error: { code: 'NOT_FOUND', message } }],
|
|
68
|
+
[405, 405, { error: { code: 'VALIDATION_ERROR', message } }],
|
|
69
|
+
[409, 409, { error: { code: 'CONFLICT', message } }],
|
|
70
|
+
[413, 413, { error: { code: 'VALIDATION_ERROR', message } }],
|
|
71
|
+
[416, 416, { error: { code: 'RANGE_NOT_SATISFIABLE', message } }],
|
|
72
|
+
[422, 422, { error: { code: 'VALIDATION_ERROR', message } }],
|
|
73
|
+
[429, 429, { error: { code: 'RATE_LIMIT_EXCEEDED', message } }],
|
|
74
|
+
[503, 500, internalErrorBody],
|
|
75
|
+
])('answers a thrown %i http error with %i in the spec shape', async (thrown, status, body) => {
|
|
76
|
+
const { fastify } = buildConsumer();
|
|
77
|
+
await fastify.register(sensible);
|
|
78
|
+
fastify.get('/fail', async () => {
|
|
79
|
+
throw fastify.httpErrors.createError(thrown, message);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const response = await fastify.inject({ method: 'GET', url: '/fail' });
|
|
83
|
+
|
|
84
|
+
expect(response.statusCode).toBe(status);
|
|
85
|
+
expect(response.json()).toEqual(body);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('keeps headers carried by the error', async () => {
|
|
89
|
+
const { fastify } = buildConsumer();
|
|
90
|
+
await fastify.register(sensible);
|
|
91
|
+
fastify.get('/fail', async () => {
|
|
92
|
+
throw fastify.httpErrors.createError(401, message, { headers: { 'www-authenticate': 'Bearer' } });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const response = await fastify.inject({ method: 'GET', url: '/fail' });
|
|
96
|
+
|
|
97
|
+
expect(response.statusCode).toBe(401);
|
|
98
|
+
expect(response.headers['www-authenticate']).toBe('Bearer');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('hides a library error with a 4xx status behind a generic 500 and logs it with its stack', async () => {
|
|
102
|
+
const { fastify, logs } = buildConsumer();
|
|
103
|
+
const raw = 'index_not_found_exception: no such index [items-v2]';
|
|
104
|
+
fastify.get('/fail', async () => {
|
|
105
|
+
throw Object.assign(new Error(raw), { name: 'ResponseError', statusCode: 404, meta: { statusCode: 404 } });
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const response = await fastify.inject({ method: 'GET', url: '/fail' });
|
|
109
|
+
|
|
110
|
+
expect(response.statusCode).toBe(500);
|
|
111
|
+
expect(response.json()).toEqual(internalErrorBody);
|
|
112
|
+
expect(response.body).not.toContain('items-v2');
|
|
113
|
+
const logged = logs.find((l) => l.level === 50)?.err as { message: string; stack: string } | undefined;
|
|
114
|
+
expect(logged?.message).toBe(raw);
|
|
115
|
+
expect(logged?.stack).toContain(raw);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('keeps the status and message of a custom error marked expose', async () => {
|
|
119
|
+
class ConflictError extends Error {
|
|
120
|
+
statusCode = 409;
|
|
121
|
+
expose = true;
|
|
122
|
+
}
|
|
123
|
+
const { fastify } = buildConsumer();
|
|
124
|
+
fastify.get('/fail', async () => {
|
|
125
|
+
throw new ConflictError(message);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const response = await fastify.inject({ method: 'GET', url: '/fail' });
|
|
129
|
+
|
|
130
|
+
expect(response.statusCode).toBe(409);
|
|
131
|
+
expect(response.json()).toEqual({ error: { code: 'CONFLICT', message } });
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it.each([
|
|
135
|
+
['an invalid JSON body', { 'content-type': 'application/json' }, '{"broken', 400],
|
|
136
|
+
['an oversized body', { 'content-type': 'application/json' }, JSON.stringify({ big: 'x'.repeat(200) }), 413],
|
|
137
|
+
['an unsupported media type', { 'content-type': 'application/x-unknown' }, 'whatever', 415],
|
|
138
|
+
])("keeps Fastify's own client error for %s", async (_label, headers, payload, status) => {
|
|
139
|
+
const { fastify } = buildConsumer();
|
|
140
|
+
fastify.post('/things', { bodyLimit: 100 }, async () => ({}));
|
|
141
|
+
|
|
142
|
+
const response = await fastify.inject({ method: 'POST', url: '/things', headers, payload });
|
|
143
|
+
|
|
144
|
+
expect(response.statusCode).toBe(status);
|
|
145
|
+
expect(response.json().error.code).toBe('VALIDATION_ERROR');
|
|
146
|
+
expect(response.json().error.message).not.toBe('');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('hides errors that carry only a status behind a generic 500', async () => {
|
|
150
|
+
const { fastify } = buildConsumer();
|
|
151
|
+
fastify.get('/fail', async () => {
|
|
152
|
+
throw Object.assign(new Error(message), { status: 403 });
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const response = await fastify.inject({ method: 'GET', url: '/fail' });
|
|
156
|
+
|
|
157
|
+
expect(response.statusCode).toBe(500);
|
|
158
|
+
expect(response.json()).toEqual(internalErrorBody);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('maps a thrown plain object marked expose with no message', async () => {
|
|
162
|
+
const { fastify } = buildConsumer();
|
|
163
|
+
fastify.get('/fail', async () => {
|
|
164
|
+
throw { statusCode: 404, expose: true };
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const response = await fastify.inject({ method: 'GET', url: '/fail' });
|
|
168
|
+
|
|
169
|
+
expect(response.statusCode).toBe(404);
|
|
170
|
+
expect(response.json()).toEqual({ error: { code: 'NOT_FOUND', message: '' } });
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('hides an error with a non-string code behind a generic 500', async () => {
|
|
174
|
+
const { fastify } = buildConsumer();
|
|
175
|
+
fastify.get('/fail', async () => {
|
|
176
|
+
throw Object.assign(new Error(message), { code: 14, statusCode: 400 });
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
const response = await fastify.inject({ method: 'GET', url: '/fail' });
|
|
180
|
+
|
|
181
|
+
expect(response.statusCode).toBe(500);
|
|
182
|
+
expect(response.json()).toEqual(internalErrorBody);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('hides a thrown plain object with a status behind a generic 500', async () => {
|
|
186
|
+
const { fastify } = buildConsumer();
|
|
187
|
+
fastify.get('/fail', async () => {
|
|
188
|
+
throw { statusCode: 404 };
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const response = await fastify.inject({ method: 'GET', url: '/fail' });
|
|
192
|
+
|
|
193
|
+
expect(response.statusCode).toBe(500);
|
|
194
|
+
expect(response.json()).toEqual(internalErrorBody);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it.each([null, undefined])('answers a thrown %s with a generic 500', async (thrown) => {
|
|
198
|
+
const { fastify } = buildConsumer();
|
|
199
|
+
fastify.get('/fail', async () => {
|
|
200
|
+
throw thrown;
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const response = await fastify.inject({ method: 'GET', url: '/fail' });
|
|
204
|
+
|
|
205
|
+
expect(response.statusCode).toBe(500);
|
|
206
|
+
expect(response.json()).toEqual(internalErrorBody);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('answers Zod validation failures with VALIDATION_ERROR violations', async () => {
|
|
210
|
+
const { fastify } = buildConsumer();
|
|
211
|
+
fastify
|
|
212
|
+
.withTypeProvider<ZodTypeProvider>()
|
|
213
|
+
.get('/things', { schema: { querystring: z.object({ limit: z.coerce.number().int() }) } }, async () => ({}));
|
|
214
|
+
|
|
215
|
+
const response = await fastify.inject({ method: 'GET', url: '/things?limit=abc' });
|
|
216
|
+
|
|
217
|
+
expect(response.statusCode).toBe(400);
|
|
218
|
+
expect(response.json()).toMatchObject({
|
|
219
|
+
error: {
|
|
220
|
+
code: 'VALIDATION_ERROR',
|
|
221
|
+
message: 'The request parameters are invalid',
|
|
222
|
+
details: { violations: [{ field: '/limit' }] },
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('answers unknown routes with NOT_FOUND', async () => {
|
|
228
|
+
const { fastify } = buildConsumer();
|
|
229
|
+
|
|
230
|
+
const response = await fastify.inject({ method: 'GET', url: '/nowhere' });
|
|
231
|
+
|
|
232
|
+
expect(response.statusCode).toBe(404);
|
|
233
|
+
expect(response.json()).toEqual(notFoundBody);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it.each([
|
|
237
|
+
['without a prefix', undefined, '/entities'],
|
|
238
|
+
['under a prefix', '/api', '/api/entities'],
|
|
239
|
+
])('answers arocapi routes registered %s in the spec shape', async (_label, prefix, url) => {
|
|
240
|
+
const { fastify } = buildConsumer();
|
|
241
|
+
await fastify.register(arocapi, { ...arocapiOptions, prefix });
|
|
242
|
+
|
|
243
|
+
const invalid = await fastify.inject({ method: 'GET', url: `${url}?limit=abc` });
|
|
244
|
+
const unknown = await fastify.inject({ method: 'GET', url: `${url}/nowhere/else` });
|
|
245
|
+
|
|
246
|
+
expect(invalid.statusCode).toBe(400);
|
|
247
|
+
expect(invalid.json().error.code).toBe('VALIDATION_ERROR');
|
|
248
|
+
expect(unknown.statusCode).toBe(404);
|
|
249
|
+
expect(unknown.json().error.code).toBe('NOT_FOUND');
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('leaves 404s to a prefixed consumer scope that mounts arocapi without its own prefix', async () => {
|
|
253
|
+
const { fastify } = buildConsumer();
|
|
254
|
+
await fastify.register(
|
|
255
|
+
async (api) => {
|
|
256
|
+
api.setNotFoundHandler((_request, reply) => reply.code(404).send({ scope: 'api' }));
|
|
257
|
+
await api.register(arocapi, arocapiOptions);
|
|
258
|
+
},
|
|
259
|
+
{ prefix: '/api' },
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
const response = await fastify.inject({ method: 'GET', url: '/api/nowhere' });
|
|
263
|
+
|
|
264
|
+
expect(response.statusCode).toBe(404);
|
|
265
|
+
expect(response.json()).toEqual({ scope: 'api' });
|
|
266
|
+
});
|
|
267
|
+
it("leaves the consumer's root 404s alone when arocapi is registered with prefix '/'", async () => {
|
|
268
|
+
const fastify = Fastify({ logger: false });
|
|
269
|
+
fastify.setNotFoundHandler((_request, reply) => reply.code(404).send({ scope: 'root' }));
|
|
270
|
+
await fastify.register(arocapi, { ...arocapiOptions, prefix: '/' });
|
|
271
|
+
|
|
272
|
+
const response = await fastify.inject({ method: 'GET', url: '/nowhere' });
|
|
273
|
+
|
|
274
|
+
expect(response.json()).toEqual({ scope: 'root' });
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
describe('Error handling for arocapi on its own', () => {
|
|
279
|
+
it('answers unknown paths under its prefix with NOT_FOUND', async () => {
|
|
280
|
+
const fastify = Fastify({ logger: false });
|
|
281
|
+
await fastify.register(arocapi, { ...arocapiOptions, prefix: '/api' });
|
|
282
|
+
|
|
283
|
+
const response = await fastify.inject({ method: 'GET', url: '/api/nowhere' });
|
|
284
|
+
|
|
285
|
+
expect(response.statusCode).toBe(404);
|
|
286
|
+
expect(response.json()).toEqual(notFoundBody);
|
|
287
|
+
});
|
|
288
|
+
});
|
package/src/index.dev.ts
CHANGED
|
@@ -5,8 +5,9 @@ import { PrismaMariaDb } from '@prisma/adapter-mariadb';
|
|
|
5
5
|
import Fastify from 'fastify';
|
|
6
6
|
|
|
7
7
|
import arocapi, {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
AllPublicAccess,
|
|
9
|
+
AllVisibleEntityFilter,
|
|
10
|
+
AllVisibleSearchFilter,
|
|
10
11
|
type EntityTransformer,
|
|
11
12
|
type FileHandler,
|
|
12
13
|
type RoCrateHandler,
|
|
@@ -184,9 +185,10 @@ fastify.register(arocapi, {
|
|
|
184
185
|
opensearch,
|
|
185
186
|
fileHandler,
|
|
186
187
|
roCrateHandler,
|
|
187
|
-
|
|
188
|
+
access: AllPublicAccess,
|
|
188
189
|
entityTransformers: [oniTransformer],
|
|
189
|
-
|
|
190
|
+
entityVisibilityFilter: AllVisibleEntityFilter,
|
|
191
|
+
searchVisibilityFilter: AllVisibleSearchFilter,
|
|
190
192
|
});
|
|
191
193
|
|
|
192
194
|
const start = async () => {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`Capabilities Route > GET /capabilities > should declare the default capabilities 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"apiVersion": "0.4.0",
|
|
6
|
+
"deposit": {
|
|
7
|
+
"supported": false,
|
|
8
|
+
},
|
|
9
|
+
"extensions": {},
|
|
10
|
+
"search": {
|
|
11
|
+
"facets": {
|
|
12
|
+
"communicationMode": {},
|
|
13
|
+
"entityType": {},
|
|
14
|
+
"inLanguage": {},
|
|
15
|
+
"mediaType": {},
|
|
16
|
+
},
|
|
17
|
+
"filters": {
|
|
18
|
+
"communicationMode": {
|
|
19
|
+
"type": "string",
|
|
20
|
+
},
|
|
21
|
+
"entityType": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
},
|
|
24
|
+
"inLanguage": {
|
|
25
|
+
"type": "string",
|
|
26
|
+
},
|
|
27
|
+
"mediaType": {
|
|
28
|
+
"type": "string",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
"tombstonePolicy": "404",
|
|
33
|
+
}
|
|
34
|
+
`;
|
|
@@ -32,7 +32,7 @@ exports[`Search Route > POST /search > should handle id explicitly set to undefi
|
|
|
32
32
|
{
|
|
33
33
|
"error": {
|
|
34
34
|
"code": "INTERNAL_ERROR",
|
|
35
|
-
"message": "
|
|
35
|
+
"message": "Internal server error",
|
|
36
36
|
},
|
|
37
37
|
}
|
|
38
38
|
`;
|
|
@@ -41,7 +41,7 @@ exports[`Search Route > POST /search > should handle opensearch errors 1`] = `
|
|
|
41
41
|
{
|
|
42
42
|
"error": {
|
|
43
43
|
"code": "INTERNAL_ERROR",
|
|
44
|
-
"message": "
|
|
44
|
+
"message": "Internal server error",
|
|
45
45
|
},
|
|
46
46
|
}
|
|
47
47
|
`;
|