arocapi 5.0.0-beta.4 → 5.0.0-beta.6
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 +1 -1
- package/README.md +3 -3
- package/dist/app.d.ts +19 -13
- package/dist/app.js +13 -10
- package/dist/app.js.map +1 -1
- package/dist/routes/capabilities.d.ts +2 -2
- package/dist/routes/capabilities.js +2 -2
- package/dist/routes/capabilities.js.map +1 -1
- package/dist/routes/entities.js +8 -9
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/file.js +5 -1
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/search.d.ts +2 -4
- package/dist/routes/search.js +75 -97
- package/dist/routes/search.js.map +1 -1
- package/dist/search/advanced.d.ts +8 -0
- package/dist/search/advanced.js +198 -0
- package/dist/search/advanced.js.map +1 -0
- package/dist/search/backend.d.ts +95 -0
- package/dist/search/backend.js +5 -0
- package/dist/search/backend.js.map +1 -0
- package/dist/search/fields.d.ts +63 -0
- package/dist/search/fields.js +203 -0
- package/dist/search/fields.js.map +1 -0
- package/dist/search/opensearch/advanced.d.ts +4 -0
- package/dist/search/opensearch/advanced.js +28 -0
- package/dist/search/opensearch/advanced.js.map +1 -0
- package/dist/search/opensearch/opensearch.d.ts +16 -0
- package/dist/search/opensearch/opensearch.js +187 -0
- package/dist/search/opensearch/opensearch.js.map +1 -0
- package/dist/search/opensearch/visibility.d.ts +3 -0
- package/dist/search/opensearch/visibility.js +27 -0
- package/dist/search/opensearch/visibility.js.map +1 -0
- package/dist/search/postgres/advanced.d.ts +7 -0
- package/dist/search/postgres/advanced.js +54 -0
- package/dist/search/postgres/advanced.js.map +1 -0
- package/dist/search/postgres/facets.d.ts +2 -0
- package/dist/search/postgres/facets.js +27 -0
- package/dist/search/postgres/facets.js.map +1 -0
- package/dist/search/postgres/geo.d.ts +6 -0
- package/dist/search/postgres/geo.js +42 -0
- package/dist/search/postgres/geo.js.map +1 -0
- package/dist/search/postgres/postgres.d.ts +19 -0
- package/dist/search/postgres/postgres.js +157 -0
- package/dist/search/postgres/postgres.js.map +1 -0
- package/dist/search/postgres/values.d.ts +8 -0
- package/dist/search/postgres/values.js +20 -0
- package/dist/search/postgres/values.js.map +1 -0
- package/dist/search/postgres/visibility.d.ts +4 -0
- package/dist/search/postgres/visibility.js +38 -0
- package/dist/search/postgres/visibility.js.map +1 -0
- package/dist/search/requestFilters.d.ts +39 -0
- package/dist/search/requestFilters.js +125 -0
- package/dist/search/requestFilters.js.map +1 -0
- package/dist/search/visibility.d.ts +51 -0
- package/dist/search/visibility.js +100 -0
- package/dist/search/visibility.js.map +1 -0
- package/dist/types/visibility.d.ts +3 -2
- package/dist/utils/visibility.js +1 -1
- package/dist/utils/visibility.js.map +1 -1
- package/package.json +46 -26
- package/prisma/models/entity.prisma +9 -8
- package/prisma/models/entitySearch.prisma +21 -0
- package/prisma/models/file.prisma +3 -3
- package/src/app.ts +53 -24
- package/src/routes/capabilities.ts +4 -4
- package/src/routes/entities.ts +10 -9
- package/src/routes/file.ts +5 -1
- package/src/routes/search.ts +104 -118
- package/src/search/advanced.ts +258 -0
- package/src/search/backend.ts +91 -0
- package/src/search/fields.ts +320 -0
- package/src/search/opensearch/advanced.ts +36 -0
- package/src/search/opensearch/opensearch.ts +290 -0
- package/src/search/opensearch/visibility.ts +38 -0
- package/src/search/postgres/advanced.ts +76 -0
- package/src/search/postgres/facets.ts +34 -0
- package/src/search/postgres/geo.ts +58 -0
- package/src/search/postgres/postgres.ts +232 -0
- package/src/search/postgres/values.ts +33 -0
- package/src/search/postgres/visibility.ts +54 -0
- package/src/search/requestFilters.ts +178 -0
- package/src/search/visibility.ts +193 -0
- package/src/types/visibility.ts +6 -3
- package/src/utils/visibility.ts +1 -1
- package/dist/app.test.d.ts +0 -1
- package/dist/app.test.js +0 -67
- package/dist/app.test.js.map +0 -1
- package/dist/errorHandling.test.d.ts +0 -1
- package/dist/errorHandling.test.js +0 -222
- package/dist/errorHandling.test.js.map +0 -1
- package/dist/index.dev.d.ts +0 -1
- package/dist/index.dev.js +0 -157
- package/dist/index.dev.js.map +0 -1
- package/dist/routes/capabilities.test.d.ts +0 -1
- package/dist/routes/capabilities.test.js +0 -81
- package/dist/routes/capabilities.test.js.map +0 -1
- package/dist/routes/crate.test.d.ts +0 -1
- package/dist/routes/crate.test.js +0 -390
- package/dist/routes/crate.test.js.map +0 -1
- package/dist/routes/entities.test.d.ts +0 -1
- package/dist/routes/entities.test.js +0 -277
- package/dist/routes/entities.test.js.map +0 -1
- package/dist/routes/entity.test.d.ts +0 -1
- package/dist/routes/entity.test.js +0 -136
- package/dist/routes/entity.test.js.map +0 -1
- package/dist/routes/file.test.d.ts +0 -1
- package/dist/routes/file.test.js +0 -425
- package/dist/routes/file.test.js.map +0 -1
- package/dist/routes/files.test.d.ts +0 -1
- package/dist/routes/files.test.js +0 -245
- package/dist/routes/files.test.js.map +0 -1
- package/dist/routes/search.test.d.ts +0 -1
- package/dist/routes/search.test.js +0 -833
- package/dist/routes/search.test.js.map +0 -1
- package/dist/test/helpers/fastify.d.ts +0 -9
- package/dist/test/helpers/fastify.js +0 -17
- package/dist/test/helpers/fastify.js.map +0 -1
- package/dist/test/integration.setup.d.ts +0 -47
- package/dist/test/integration.setup.js +0 -195
- package/dist/test/integration.setup.js.map +0 -1
- package/dist/test/integration.test.d.ts +0 -1
- package/dist/test/integration.test.js +0 -762
- package/dist/test/integration.test.js.map +0 -1
- package/dist/transformers/default.test.d.ts +0 -1
- package/dist/transformers/default.test.js +0 -225
- package/dist/transformers/default.test.js.map +0 -1
- package/dist/transformers/transformer.test.d.ts +0 -1
- package/dist/transformers/transformer.test.js +0 -165
- package/dist/transformers/transformer.test.js.map +0 -1
- package/dist/utils/errors.test.d.ts +0 -1
- package/dist/utils/errors.test.js +0 -79
- package/dist/utils/errors.test.js.map +0 -1
- package/dist/utils/queryBuilder.d.ts +0 -47
- package/dist/utils/queryBuilder.js +0 -107
- package/dist/utils/queryBuilder.js.map +0 -1
- package/prisma.config.ts +0 -9
- package/src/app.test.ts +0 -91
- package/src/errorHandling.test.ts +0 -288
- package/src/index.dev.ts +0 -202
- package/src/reset.d.ts +0 -1
- package/src/routes/__snapshots__/capabilities.test.ts.snap +0 -34
- package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
- package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
- package/src/routes/__snapshots__/search.test.ts.snap +0 -165
- package/src/routes/capabilities.test.ts +0 -102
- package/src/routes/crate.test.ts +0 -484
- package/src/routes/entities.test.ts +0 -324
- package/src/routes/entity.test.ts +0 -166
- package/src/routes/file.test.ts +0 -526
- package/src/routes/files.test.ts +0 -304
- package/src/routes/search.test.ts +0 -959
- package/src/test/__snapshots__/integration.test.ts.snap +0 -18
- package/src/test/helpers/fastify.ts +0 -26
- package/src/test/integration.setup.ts +0 -220
- package/src/test/integration.test.ts +0 -953
- package/src/transformers/default.test.ts +0 -276
- package/src/transformers/transformer.test.ts +0 -226
- package/src/utils/errors.test.ts +0 -90
- package/src/utils/queryBuilder.ts +0 -157
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
export class OpensearchQueryBuilder {
|
|
2
|
-
aggregations;
|
|
3
|
-
constructor(opts) {
|
|
4
|
-
this.aggregations =
|
|
5
|
-
opts?.aggregations ||
|
|
6
|
-
Object.fromEntries(['inLanguage', 'mediaType', 'communicationMode', 'entityType'].map((name) => [
|
|
7
|
-
name,
|
|
8
|
-
{ terms: { field: `${name}.keyword`, size: 20 } },
|
|
9
|
-
]));
|
|
10
|
-
}
|
|
11
|
-
buildQuery(searchType, query, filters, boundingBox) {
|
|
12
|
-
const must = [];
|
|
13
|
-
const filter = [];
|
|
14
|
-
if (searchType === 'basic') {
|
|
15
|
-
must.push({
|
|
16
|
-
multi_match: {
|
|
17
|
-
query,
|
|
18
|
-
fields: ['name^2', 'description'],
|
|
19
|
-
type: 'best_fields',
|
|
20
|
-
fuzziness: 'AUTO',
|
|
21
|
-
zero_terms_query: 'all',
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
must.push({
|
|
27
|
-
query_string: {
|
|
28
|
-
query,
|
|
29
|
-
fields: ['name^2', 'description'],
|
|
30
|
-
default_operator: 'AND',
|
|
31
|
-
},
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
if (filters) {
|
|
35
|
-
Object.entries(filters).forEach(([field, values]) => {
|
|
36
|
-
filter.push({
|
|
37
|
-
terms: {
|
|
38
|
-
[field]: values,
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
if (boundingBox) {
|
|
44
|
-
filter.push({
|
|
45
|
-
geo_bounding_box: {
|
|
46
|
-
location: {
|
|
47
|
-
top_left: {
|
|
48
|
-
lat: boundingBox.topRight.lat,
|
|
49
|
-
lon: boundingBox.bottomLeft.lng,
|
|
50
|
-
},
|
|
51
|
-
bottom_right: {
|
|
52
|
-
lat: boundingBox.bottomLeft.lat,
|
|
53
|
-
lon: boundingBox.topRight.lng,
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
return {
|
|
60
|
-
bool: {
|
|
61
|
-
must,
|
|
62
|
-
filter,
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
buildAggregations(geohashPrecision, boundingBox) {
|
|
67
|
-
const aggs = { ...this.aggregations };
|
|
68
|
-
if (geohashPrecision && boundingBox) {
|
|
69
|
-
aggs.geohash_grid = {
|
|
70
|
-
geohash_grid: {
|
|
71
|
-
field: 'location',
|
|
72
|
-
precision: geohashPrecision,
|
|
73
|
-
bounds: {
|
|
74
|
-
top_left: {
|
|
75
|
-
lat: boundingBox.topRight.lat,
|
|
76
|
-
lon: boundingBox.bottomLeft.lng,
|
|
77
|
-
},
|
|
78
|
-
bottom_right: {
|
|
79
|
-
lat: boundingBox.bottomLeft.lat,
|
|
80
|
-
lon: boundingBox.topRight.lng,
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
return aggs;
|
|
87
|
-
}
|
|
88
|
-
searchCapabilities() {
|
|
89
|
-
const fields = Object.entries(this.aggregations)
|
|
90
|
-
.filter(([, aggregation]) => 'terms' in aggregation)
|
|
91
|
-
.map(([name]) => name);
|
|
92
|
-
return {
|
|
93
|
-
filters: Object.fromEntries(fields.map((name) => [name, { type: 'string' }])),
|
|
94
|
-
facets: Object.fromEntries(fields.map((name) => [name, {}])),
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
buildSort(sort, order) {
|
|
98
|
-
if (sort === 'relevance') {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
if (sort === 'name') {
|
|
102
|
-
return [{ 'name.keyword': order }];
|
|
103
|
-
}
|
|
104
|
-
return [{ [sort]: order }];
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
//# sourceMappingURL=queryBuilder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"queryBuilder.js","sourceRoot":"","sources":["../../src/utils/queryBuilder.ts"],"names":[],"mappings":"AAmCA,MAAM,OAAO,sBAAsB;IACjC,YAAY,CAAe;IAE3B,YAAY,IAA0B;QACpC,IAAI,CAAC,YAAY;YACf,IAAI,EAAE,YAAY;gBAClB,MAAM,CAAC,WAAW,CAChB,CAAC,YAAY,EAAE,WAAW,EAAE,mBAAmB,EAAE,YAAY,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;oBAC3E,IAAI;oBACJ,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,GAAG,IAAI,UAAU,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE;iBAClD,CAAC,CACH,CAAC;IACN,CAAC;IAED,UAAU,CAAC,UAAkB,EAAE,KAAa,EAAE,OAAkC,EAAE,WAAyB;QACzG,MAAM,I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AED,SAAS,CAAC,IAAY,EAAE,KAAgB;QACtC,IAAI,IAAI,KAAK,WAAW,EAAE,CAAC;YACzB,OAAO;QACT,CAAC;QAED,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;YACpB,OAAO,CAAC,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC,CAAC;QACrC,CAAC;QAED,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;IAC7B,CAAC;CACF"}
|
package/prisma.config.ts
DELETED
package/src/app.test.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import Fastify from 'fastify';
|
|
2
|
-
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
-
import { mockReset } from 'vitest-mock-extended';
|
|
4
|
-
|
|
5
|
-
import type { Options } from './app.js';
|
|
6
|
-
import app, { AllPublicAccess, AllVisibleEntityFilter, AllVisibleSearchFilter } from './app.js';
|
|
7
|
-
|
|
8
|
-
import { opensearch, prisma } from './test/helpers/fastify.js';
|
|
9
|
-
|
|
10
|
-
describe('Entity Route', () => {
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
mockReset(prisma);
|
|
13
|
-
mockReset(opensearch);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
const requiredOptions = (): Options => ({
|
|
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
|
-
describe('App Registration', () => {
|
|
27
|
-
it.each([
|
|
28
|
-
['prisma', 'Prisma client is required'],
|
|
29
|
-
['opensearch', 'OpenSearch client is required'],
|
|
30
|
-
['access', 'access is required'],
|
|
31
|
-
['fileHandler', 'fileHandler is required'],
|
|
32
|
-
['roCrateHandler', 'roCrateHandler is required'],
|
|
33
|
-
['entityVisibilityFilter', 'entityVisibilityFilter is required'],
|
|
34
|
-
['searchVisibilityFilter', 'searchVisibilityFilter is required'],
|
|
35
|
-
] as const)('should handle missing %s', async (option, message) => {
|
|
36
|
-
const fastify = Fastify({ logger: false });
|
|
37
|
-
const { [option]: _omitted, ...options } = requiredOptions();
|
|
38
|
-
|
|
39
|
-
// @ts-expect-error we are testing missing options
|
|
40
|
-
fastify.register(app, options);
|
|
41
|
-
|
|
42
|
-
await expect(() => fastify.ready()).rejects.toThrowError(message);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it.each([
|
|
46
|
-
['accessTransformer', 'access', 'https://arocapi.crate-works.org/configuration/access/'],
|
|
47
|
-
['fileAccessTransformer', 'access', 'https://arocapi.crate-works.org/configuration/access/'],
|
|
48
|
-
[
|
|
49
|
-
'hiddenResponse',
|
|
50
|
-
'promptLoginForHidden and isAuthenticated',
|
|
51
|
-
'https://arocapi.crate-works.org/configuration/visibility/#promptloginforhidden',
|
|
52
|
-
],
|
|
53
|
-
])('should reject the removed %s option', async (option, replacement, docs) => {
|
|
54
|
-
const fastify = Fastify({ logger: false });
|
|
55
|
-
|
|
56
|
-
fastify.register(app, { ...requiredOptions(), [option]: 'notFound' });
|
|
57
|
-
|
|
58
|
-
await expect(() => fastify.ready()).rejects.toThrowError(
|
|
59
|
-
`${option} has been replaced by ${replacement}, see ${docs}`,
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should reject promptLoginForHidden without isAuthenticated', async () => {
|
|
64
|
-
const fastify = Fastify({ logger: false });
|
|
65
|
-
|
|
66
|
-
fastify.register(app, { ...requiredOptions(), promptLoginForHidden: true });
|
|
67
|
-
|
|
68
|
-
await expect(() => fastify.ready()).rejects.toThrowError('promptLoginForHidden requires isAuthenticated');
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('should ignore a removed option left undefined', async () => {
|
|
72
|
-
const fastify = Fastify({ logger: false });
|
|
73
|
-
|
|
74
|
-
await fastify.register(app, { ...requiredOptions(), accessTransformer: undefined } as Options);
|
|
75
|
-
|
|
76
|
-
await expect(fastify.ready()).resolves.toBeDefined();
|
|
77
|
-
|
|
78
|
-
await fastify.close();
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('should register successfully with all required options', async () => {
|
|
82
|
-
const fastify = Fastify({ logger: false });
|
|
83
|
-
|
|
84
|
-
await fastify.register(app, requiredOptions());
|
|
85
|
-
|
|
86
|
-
await expect(fastify.ready()).resolves.toBeDefined();
|
|
87
|
-
|
|
88
|
-
await fastify.close();
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
});
|
|
@@ -1,288 +0,0 @@
|
|
|
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
|
-
});
|