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
|
@@ -1,22 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
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;
|
|
1
|
+
import type { AuthorisedEntity, AuthorisedFile } from '../transformers/default.js';
|
|
2
|
+
import type { RequestContext } from './context.js';
|
|
20
3
|
|
|
21
4
|
/**
|
|
22
5
|
* Entity transformer function
|
|
@@ -26,20 +9,9 @@ export type AccessTransformer = (
|
|
|
26
9
|
*/
|
|
27
10
|
export type EntityTransformer<TInput = AuthorisedEntity, TOutput = TInput> = (
|
|
28
11
|
entity: TInput,
|
|
29
|
-
context:
|
|
12
|
+
context: RequestContext,
|
|
30
13
|
) => Promise<TOutput> | TOutput;
|
|
31
14
|
|
|
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
15
|
/**
|
|
44
16
|
* File transformer function
|
|
45
17
|
* Receives AuthorisedFile and transforms it further
|
|
@@ -48,5 +20,5 @@ export type FileAccessTransformer = (
|
|
|
48
20
|
*/
|
|
49
21
|
export type FileTransformer<TInput = AuthorisedFile, TOutput = TInput> = (
|
|
50
22
|
file: TInput,
|
|
51
|
-
context:
|
|
23
|
+
context: RequestContext,
|
|
52
24
|
) => Promise<TOutput> | TOutput;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { QueryContainer } from '@opensearch-project/opensearch/api/_types/_common.query_dsl.js';
|
|
2
|
+
import type { Prisma } from '../generated/prisma/client.js';
|
|
3
|
+
import type { RequestContext } from './context.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Entity visibility filter - required
|
|
7
|
+
* Returns a Prisma condition matching the entities visible to this request
|
|
8
|
+
*/
|
|
9
|
+
export type EntityVisibilityFilter = (
|
|
10
|
+
context: RequestContext,
|
|
11
|
+
) => Promise<Prisma.EntityWhereInput> | Prisma.EntityWhereInput;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Search visibility filter - required
|
|
15
|
+
* Returns an OpenSearch query matching the entities visible to this request
|
|
16
|
+
* Must agree with the entity visibility filter
|
|
17
|
+
*/
|
|
18
|
+
export type SearchVisibilityFilter = (context: RequestContext) => Promise<QueryContainer> | QueryContainer;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { FastifyReply, FastifyRequest } from 'fastify';
|
|
2
|
+
import { decideAccess, type EntityWithFile } from '../transformers/default.js';
|
|
3
|
+
import type { AccessFunction } from '../types/access.js';
|
|
4
|
+
import type { IsAuthenticated, RequestContext } from '../types/context.js';
|
|
5
|
+
import { sendRefusal } from './errors.js';
|
|
6
|
+
import { isAnonymous } from './visibility.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* All Public Access - every request may read every visible entity's Metadata and Content
|
|
10
|
+
*
|
|
11
|
+
* WARNING: Only use this for fully public repositories with no access control.
|
|
12
|
+
*/
|
|
13
|
+
export const AllPublicAccess: AccessFunction = () => ({ metadata: true, content: true });
|
|
14
|
+
|
|
15
|
+
export type LicenceAccessOptions = {
|
|
16
|
+
hasLicence: (licenceId: string, context: RequestContext) => Promise<boolean> | boolean;
|
|
17
|
+
authorisationUrl?: (licenceId: string) => string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Licence Access - Metadata access needs the entity's metadata licence, Content access its content licence
|
|
22
|
+
*
|
|
23
|
+
* Each licence is checked at most once per request, so a listing sharing a few licences costs a few checks.
|
|
24
|
+
*/
|
|
25
|
+
export const LicenceAccess = ({ hasLicence, authorisationUrl }: LicenceAccessOptions): AccessFunction => {
|
|
26
|
+
const checksByRequest = new WeakMap<FastifyRequest, Map<string, Promise<boolean>>>();
|
|
27
|
+
|
|
28
|
+
const check = (licenceId: string, context: RequestContext) => {
|
|
29
|
+
let checks = checksByRequest.get(context.request);
|
|
30
|
+
if (!checks) {
|
|
31
|
+
checks = new Map();
|
|
32
|
+
checksByRequest.set(context.request, checks);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let held = checks.get(licenceId);
|
|
36
|
+
if (!held) {
|
|
37
|
+
held = Promise.resolve(hasLicence(licenceId, context));
|
|
38
|
+
checks.set(licenceId, held);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return held;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return async ({ metadataLicenseId, contentLicenseId }, context) => {
|
|
45
|
+
const [metadata, content] = await Promise.all([
|
|
46
|
+
check(metadataLicenseId, context),
|
|
47
|
+
check(contentLicenseId, context),
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
metadata,
|
|
52
|
+
content,
|
|
53
|
+
...(!metadata && authorisationUrl && { metadataAuthorizationUrl: authorisationUrl(metadataLicenseId) }),
|
|
54
|
+
...(!content && authorisationUrl && { contentAuthorizationUrl: authorisationUrl(contentLicenseId) }),
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
type AccessGuardOptions = {
|
|
60
|
+
access: AccessFunction;
|
|
61
|
+
isAuthenticated?: IsAuthenticated;
|
|
62
|
+
level: 'metadata' | 'content';
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Build a check that refuses a request when a visible entity's Metadata or Content isn't accessible to it
|
|
67
|
+
* Returns whether the request was denied (not the reply: replies are thenable, so awaiting one loses it)
|
|
68
|
+
*/
|
|
69
|
+
export const createAccessGuard =
|
|
70
|
+
({ access, isAuthenticated, level }: AccessGuardOptions) =>
|
|
71
|
+
async (reply: FastifyReply, entity: EntityWithFile, context: RequestContext) => {
|
|
72
|
+
const [entityAccess] = await decideAccess([entity], access, context);
|
|
73
|
+
if (entityAccess[level]) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
sendRefusal(reply, await isAnonymous(isAuthenticated, context), entity.id);
|
|
78
|
+
|
|
79
|
+
return true;
|
|
80
|
+
};
|
package/src/utils/errors.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { createInternalError, createNotFoundError } from './errors.js';
|
|
2
|
+
import { createForbiddenError, createInternalError, createNotFoundError } from './errors.js';
|
|
3
3
|
|
|
4
4
|
describe('Error Utilities', () => {
|
|
5
5
|
describe('createInternalError', () => {
|
|
@@ -29,6 +29,21 @@ describe('Error Utilities', () => {
|
|
|
29
29
|
});
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
+
describe('createForbiddenError', () => {
|
|
33
|
+
it('should create a forbidden error with message and entityId', () => {
|
|
34
|
+
const entityId = 'http://example.com/entity/123';
|
|
35
|
+
const error = createForbiddenError('No access', entityId);
|
|
36
|
+
|
|
37
|
+
expect(error).toEqual({
|
|
38
|
+
error: {
|
|
39
|
+
code: 'FORBIDDEN',
|
|
40
|
+
message: 'No access',
|
|
41
|
+
details: { entityId },
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
32
47
|
describe('createNotFoundError', () => {
|
|
33
48
|
it('should create a not found error with message', () => {
|
|
34
49
|
const message = 'Entity not found';
|
package/src/utils/errors.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
import type { FastifyError, FastifyReply, FastifyRequest } from 'fastify';
|
|
2
|
+
import { hasZodFastifySchemaValidationErrors } from 'fastify-type-provider-zod';
|
|
3
|
+
|
|
4
|
+
export const ERROR_CODES = {
|
|
2
5
|
VALIDATION_ERROR: 'VALIDATION_ERROR',
|
|
3
6
|
NOT_FOUND: 'NOT_FOUND',
|
|
7
|
+
UNAUTHORIZED: 'UNAUTHORIZED',
|
|
8
|
+
FORBIDDEN: 'FORBIDDEN',
|
|
9
|
+
CONFLICT: 'CONFLICT',
|
|
10
|
+
RANGE_NOT_SATISFIABLE: 'RANGE_NOT_SATISFIABLE',
|
|
4
11
|
RATE_LIMIT_EXCEEDED: 'RATE_LIMIT_EXCEEDED',
|
|
5
12
|
INTERNAL_ERROR: 'INTERNAL_ERROR',
|
|
6
|
-
INVALID_REQUEST: 'INVALID_REQUEST',
|
|
7
13
|
INVALID_ENTITY_TYPE: 'INVALID_ENTITY_TYPE',
|
|
8
14
|
} as const;
|
|
9
15
|
|
|
10
|
-
type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
|
|
16
|
+
export type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
|
|
11
17
|
|
|
12
18
|
type ErrorDetails = {
|
|
13
19
|
[key: string]: unknown;
|
|
@@ -21,7 +27,7 @@ export type StandardErrorResponse = {
|
|
|
21
27
|
};
|
|
22
28
|
};
|
|
23
29
|
|
|
24
|
-
const
|
|
30
|
+
export const createError = (code: ErrorCode, message: string, details?: ErrorDetails): StandardErrorResponse => ({
|
|
25
31
|
error: {
|
|
26
32
|
code,
|
|
27
33
|
message,
|
|
@@ -31,19 +37,70 @@ const createErrorResponse = (code: ErrorCode, message: string, details?: ErrorDe
|
|
|
31
37
|
|
|
32
38
|
type ValidationViolation = {
|
|
33
39
|
field: string;
|
|
34
|
-
message
|
|
40
|
+
message?: string;
|
|
35
41
|
value?: unknown;
|
|
36
42
|
};
|
|
37
43
|
|
|
38
44
|
export const createValidationError = (message: string, violations: ValidationViolation[]): StandardErrorResponse =>
|
|
39
|
-
|
|
45
|
+
createError(ERROR_CODES.VALIDATION_ERROR, message, { violations });
|
|
40
46
|
|
|
41
47
|
export const createNotFoundError = (message: string, entityId?: string): StandardErrorResponse =>
|
|
42
|
-
|
|
48
|
+
createError(ERROR_CODES.NOT_FOUND, message, entityId ? { entityId } : undefined);
|
|
49
|
+
|
|
50
|
+
export const createAuthenticationRequiredError = (message: string, entityId: string): StandardErrorResponse =>
|
|
51
|
+
createError(ERROR_CODES.UNAUTHORIZED, message, { entityId });
|
|
52
|
+
|
|
53
|
+
export const createForbiddenError = (message: string, entityId: string): StandardErrorResponse =>
|
|
54
|
+
createError(ERROR_CODES.FORBIDDEN, message, { entityId });
|
|
55
|
+
|
|
56
|
+
export const createInternalError = (message = 'Internal server error'): StandardErrorResponse =>
|
|
57
|
+
createError(ERROR_CODES.INTERNAL_ERROR, message);
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Refuse a request for a resource: 401 if it is anonymous, so the client can prompt a login, otherwise 403
|
|
61
|
+
*/
|
|
62
|
+
export const sendRefusal = (reply: FastifyReply, anonymous: boolean, id: string) =>
|
|
63
|
+
anonymous
|
|
64
|
+
? reply.code(401).send(createAuthenticationRequiredError('Authentication is required to access this resource', id))
|
|
65
|
+
: reply.code(403).send(createForbiddenError('You do not have permission to access this resource', id));
|
|
66
|
+
|
|
67
|
+
const ERROR_CODE_BY_STATUS: Record<number, ErrorCode> = {
|
|
68
|
+
401: ERROR_CODES.UNAUTHORIZED,
|
|
69
|
+
403: ERROR_CODES.FORBIDDEN,
|
|
70
|
+
404: ERROR_CODES.NOT_FOUND,
|
|
71
|
+
409: ERROR_CODES.CONFLICT,
|
|
72
|
+
416: ERROR_CODES.RANGE_NOT_SATISFIABLE,
|
|
73
|
+
429: ERROR_CODES.RATE_LIMIT_EXCEEDED,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const errorHandler = (error: unknown, request: FastifyRequest, reply: FastifyReply) => {
|
|
77
|
+
if (hasZodFastifySchemaValidationErrors(error)) {
|
|
78
|
+
const violations = error.validation.map((v) => ({
|
|
79
|
+
field: v.instancePath,
|
|
80
|
+
message: v.message,
|
|
81
|
+
}));
|
|
82
|
+
|
|
83
|
+
return reply.code(400).send(createValidationError('The request parameters are invalid', violations));
|
|
84
|
+
}
|
|
43
85
|
|
|
44
|
-
|
|
45
|
-
|
|
86
|
+
const { statusCode, status, message, headers, expose, code } = (error ?? {}) as Partial<
|
|
87
|
+
FastifyError & { status: number; headers: Record<string, string>; expose: boolean }
|
|
88
|
+
>;
|
|
89
|
+
const httpStatus = statusCode ?? status;
|
|
90
|
+
const trusted = expose === true || (typeof code === 'string' && code.startsWith('FST_'));
|
|
91
|
+
if (trusted && httpStatus && httpStatus >= 400 && httpStatus < 500) {
|
|
92
|
+
const errorCode = ERROR_CODE_BY_STATUS[httpStatus] ?? ERROR_CODES.VALIDATION_ERROR;
|
|
46
93
|
|
|
47
|
-
|
|
48
|
-
|
|
94
|
+
return reply
|
|
95
|
+
.code(httpStatus)
|
|
96
|
+
.headers(headers ?? {})
|
|
97
|
+
.send(createError(errorCode, message ?? ''));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
request.log.error({ err: error });
|
|
101
|
+
|
|
102
|
+
return reply.code(500).send(createInternalError());
|
|
49
103
|
};
|
|
104
|
+
|
|
105
|
+
export const notFoundHandler = (_request: FastifyRequest, reply: FastifyReply) =>
|
|
106
|
+
reply.code(404).send(createNotFoundError('The requested resource was not found'));
|
|
@@ -19,6 +19,20 @@ export type QueryBuilderOptions = {
|
|
|
19
19
|
aggregations?: Aggregations;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
+
export type FilterCapability = {
|
|
23
|
+
type: 'string' | 'date' | 'number' | 'boolean';
|
|
24
|
+
label?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type FacetCapability = {
|
|
28
|
+
label?: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type SearchCapability = {
|
|
32
|
+
filters: Record<string, FilterCapability>;
|
|
33
|
+
facets: Record<string, FacetCapability>;
|
|
34
|
+
};
|
|
35
|
+
|
|
22
36
|
export class OpensearchQueryBuilder {
|
|
23
37
|
aggregations: Aggregations;
|
|
24
38
|
|
|
@@ -118,6 +132,17 @@ export class OpensearchQueryBuilder {
|
|
|
118
132
|
return aggs;
|
|
119
133
|
}
|
|
120
134
|
|
|
135
|
+
searchCapabilities(): SearchCapability {
|
|
136
|
+
const fields = Object.entries(this.aggregations)
|
|
137
|
+
.filter(([, aggregation]) => 'terms' in aggregation)
|
|
138
|
+
.map(([name]) => name);
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
filters: Object.fromEntries(fields.map((name) => [name, { type: 'string' }])),
|
|
142
|
+
facets: Object.fromEntries(fields.map((name) => [name, {}])),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
121
146
|
buildSort(sort: string, order: SortOrder) {
|
|
122
147
|
if (sort === 'relevance') {
|
|
123
148
|
return;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { FastifyInstance } from 'fastify';
|
|
2
|
+
import { serializerCompiler, validatorCompiler } from 'fastify-type-provider-zod';
|
|
3
|
+
|
|
4
|
+
import { errorHandler } from './errors.js';
|
|
5
|
+
|
|
6
|
+
export const setupValidation = (fastify: FastifyInstance) => {
|
|
7
|
+
fastify.setValidatorCompiler(validatorCompiler);
|
|
8
|
+
fastify.setSerializerCompiler(serializerCompiler);
|
|
9
|
+
|
|
10
|
+
fastify.setErrorHandler(errorHandler);
|
|
11
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { FastifyReply } from 'fastify';
|
|
2
|
+
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
3
|
+
import type { IsAuthenticated, RequestContext } from '../types/context.js';
|
|
4
|
+
import type { EntityVisibilityFilter, SearchVisibilityFilter } from '../types/visibility.js';
|
|
5
|
+
import { createNotFoundError, sendRefusal } from './errors.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* All Visible Entity Filter - every entity is visible to every request
|
|
9
|
+
*
|
|
10
|
+
* Use together with AllVisibleSearchFilter for repositories with no hidden entities.
|
|
11
|
+
*/
|
|
12
|
+
export const AllVisibleEntityFilter: EntityVisibilityFilter = () => ({});
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* All Visible Search Filter - every indexed entity is visible to every request
|
|
16
|
+
*
|
|
17
|
+
* Use together with AllVisibleEntityFilter for repositories with no hidden entities.
|
|
18
|
+
*/
|
|
19
|
+
export const AllVisibleSearchFilter: SearchVisibilityFilter = () => ({ match_all: {} });
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Whether a request has no credentials; without isAuthenticated the repository has no logins, so no request is
|
|
23
|
+
*/
|
|
24
|
+
export const isAnonymous = async (isAuthenticated: IsAuthenticated | undefined, context: RequestContext) =>
|
|
25
|
+
isAuthenticated ? !(await isAuthenticated(context)) : false;
|
|
26
|
+
|
|
27
|
+
export type RefusalOptions = {
|
|
28
|
+
promptLoginForHidden: boolean;
|
|
29
|
+
isAuthenticated?: IsAuthenticated;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
type NotVisibleResponderOptions = RefusalOptions & {
|
|
33
|
+
notFoundMessage: string;
|
|
34
|
+
exists: (id: string) => Promise<boolean>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Build a reply for a resource that is missing or hidden from this request: 404, except that with the login
|
|
39
|
+
* prompt an anonymous request for a hidden resource gets 401
|
|
40
|
+
*/
|
|
41
|
+
export const createNotVisibleResponder =
|
|
42
|
+
({ promptLoginForHidden, isAuthenticated, notFoundMessage, exists }: NotVisibleResponderOptions) =>
|
|
43
|
+
async (reply: FastifyReply, id: string, context: RequestContext) => {
|
|
44
|
+
if (promptLoginForHidden && (await isAnonymous(isAuthenticated, context)) && (await exists(id))) {
|
|
45
|
+
return sendRefusal(reply, true, id);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return reply.code(404).send(createNotFoundError(notFoundMessage, id));
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const createEntityNotVisibleResponder = (prisma: PrismaClient, refusal: RefusalOptions) =>
|
|
52
|
+
createNotVisibleResponder({
|
|
53
|
+
...refusal,
|
|
54
|
+
notFoundMessage: 'The requested entity was not found',
|
|
55
|
+
exists: async (id) => (await prisma.entity.count({ where: { id } })) > 0,
|
|
56
|
+
});
|