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.
Files changed (128) hide show
  1. package/README.md +37 -802
  2. package/dist/app.d.ts +22 -9
  3. package/dist/app.js +46 -30
  4. package/dist/app.js.map +1 -1
  5. package/dist/app.test.js +41 -56
  6. package/dist/app.test.js.map +1 -1
  7. package/dist/errorHandling.test.d.ts +1 -0
  8. package/dist/errorHandling.test.js +222 -0
  9. package/dist/errorHandling.test.js.map +1 -0
  10. package/dist/index.dev.js +4 -3
  11. package/dist/index.dev.js.map +1 -1
  12. package/dist/routes/capabilities.d.ts +7 -0
  13. package/dist/routes/capabilities.js +13 -0
  14. package/dist/routes/capabilities.js.map +1 -0
  15. package/dist/routes/capabilities.test.d.ts +1 -0
  16. package/dist/routes/capabilities.test.js +81 -0
  17. package/dist/routes/capabilities.test.js.map +1 -0
  18. package/dist/routes/crate.test.js +67 -19
  19. package/dist/routes/crate.test.js.map +1 -1
  20. package/dist/routes/entities.d.ts +5 -2
  21. package/dist/routes/entities.js +31 -49
  22. package/dist/routes/entities.js.map +1 -1
  23. package/dist/routes/entities.test.js +16 -12
  24. package/dist/routes/entities.test.js.map +1 -1
  25. package/dist/routes/entity.d.ts +7 -2
  26. package/dist/routes/entity.js +14 -29
  27. package/dist/routes/entity.js.map +1 -1
  28. package/dist/routes/entity.test.js +19 -13
  29. package/dist/routes/entity.test.js.map +1 -1
  30. package/dist/routes/file.d.ts +6 -0
  31. package/dist/routes/file.js +67 -59
  32. package/dist/routes/file.js.map +1 -1
  33. package/dist/routes/file.test.js +99 -22
  34. package/dist/routes/file.test.js.map +1 -1
  35. package/dist/routes/files.d.ts +5 -2
  36. package/dist/routes/files.js +24 -39
  37. package/dist/routes/files.js.map +1 -1
  38. package/dist/routes/files.test.js +44 -21
  39. package/dist/routes/files.test.js.map +1 -1
  40. package/dist/routes/metadata.d.ts +6 -0
  41. package/dist/routes/metadata.js +49 -54
  42. package/dist/routes/metadata.js.map +1 -1
  43. package/dist/routes/search.d.ts +8 -5
  44. package/dist/routes/search.js +95 -101
  45. package/dist/routes/search.js.map +1 -1
  46. package/dist/routes/search.test.js +92 -39
  47. package/dist/routes/search.test.js.map +1 -1
  48. package/dist/test/helpers/fastify.d.ts +1 -1
  49. package/dist/test/helpers/fastify.js +2 -3
  50. package/dist/test/helpers/fastify.js.map +1 -1
  51. package/dist/test/integration.setup.d.ts +19 -1
  52. package/dist/test/integration.setup.js +15 -7
  53. package/dist/test/integration.setup.js.map +1 -1
  54. package/dist/test/integration.test.js +368 -2
  55. package/dist/test/integration.test.js.map +1 -1
  56. package/dist/transformers/default.d.ts +10 -18
  57. package/dist/transformers/default.js +41 -15
  58. package/dist/transformers/default.js.map +1 -1
  59. package/dist/transformers/default.test.js +109 -47
  60. package/dist/transformers/default.test.js.map +1 -1
  61. package/dist/transformers/transformer.test.js +12 -34
  62. package/dist/transformers/transformer.test.js.map +1 -1
  63. package/dist/types/access.d.ts +9 -0
  64. package/dist/types/access.js +2 -0
  65. package/dist/types/access.js.map +1 -0
  66. package/dist/types/context.d.ts +6 -0
  67. package/dist/types/context.js +2 -0
  68. package/dist/types/context.js.map +1 -0
  69. package/dist/types/fileHandlers.d.ts +5 -9
  70. package/dist/types/transformers.d.ts +4 -10
  71. package/dist/types/visibility.d.ts +5 -0
  72. package/dist/types/visibility.js +2 -0
  73. package/dist/types/visibility.js.map +1 -0
  74. package/dist/utils/access.d.ts +17 -0
  75. package/dist/utils/access.js +41 -0
  76. package/dist/utils/access.js.map +1 -0
  77. package/dist/utils/errors.d.ts +19 -10
  78. package/dist/utils/errors.js +44 -8
  79. package/dist/utils/errors.js.map +1 -1
  80. package/dist/utils/errors.test.js +14 -1
  81. package/dist/utils/errors.test.js.map +1 -1
  82. package/dist/utils/queryBuilder.d.ts +12 -0
  83. package/dist/utils/queryBuilder.js +9 -0
  84. package/dist/utils/queryBuilder.js.map +1 -1
  85. package/dist/utils/validation.d.ts +2 -0
  86. package/dist/utils/validation.js +8 -0
  87. package/dist/utils/validation.js.map +1 -0
  88. package/dist/utils/visibility.d.ts +18 -0
  89. package/dist/utils/visibility.js +16 -0
  90. package/dist/utils/visibility.js.map +1 -0
  91. package/package.json +34 -33
  92. package/src/app.test.ts +45 -66
  93. package/src/app.ts +92 -58
  94. package/src/errorHandling.test.ts +288 -0
  95. package/src/index.dev.ts +6 -4
  96. package/src/routes/__snapshots__/capabilities.test.ts.snap +34 -0
  97. package/src/routes/__snapshots__/search.test.ts.snap +2 -2
  98. package/src/routes/capabilities.test.ts +102 -0
  99. package/src/routes/capabilities.ts +22 -0
  100. package/src/routes/crate.test.ts +80 -19
  101. package/src/routes/entities.test.ts +16 -12
  102. package/src/routes/entities.ts +40 -59
  103. package/src/routes/entity.test.ts +19 -13
  104. package/src/routes/entity.ts +22 -35
  105. package/src/routes/file.test.ts +117 -22
  106. package/src/routes/file.ts +84 -62
  107. package/src/routes/files.test.ts +52 -26
  108. package/src/routes/files.ts +31 -47
  109. package/src/routes/metadata.ts +64 -58
  110. package/src/routes/search.test.ts +95 -39
  111. package/src/routes/search.ts +120 -125
  112. package/src/test/helpers/fastify.ts +2 -3
  113. package/src/test/integration.setup.ts +18 -7
  114. package/src/test/integration.test.ts +472 -1
  115. package/src/transformers/default.test.ts +139 -47
  116. package/src/transformers/default.ts +103 -83
  117. package/src/transformers/transformer.test.ts +18 -44
  118. package/src/types/access.ts +20 -0
  119. package/src/types/context.ts +15 -0
  120. package/src/types/fileHandlers.ts +5 -13
  121. package/src/types/transformers.ts +4 -32
  122. package/src/types/visibility.ts +18 -0
  123. package/src/utils/access.ts +80 -0
  124. package/src/utils/errors.test.ts +16 -1
  125. package/src/utils/errors.ts +68 -11
  126. package/src/utils/queryBuilder.ts +25 -0
  127. package/src/utils/validation.ts +11 -0
  128. package/src/utils/visibility.ts +56 -0
@@ -1,22 +1,5 @@
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;
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: TransformerContext,
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: TransformerContext,
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
+ };
@@ -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';
@@ -1,13 +1,19 @@
1
- const ERROR_CODES = {
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 createErrorResponse = (code: ErrorCode, message: string, details?: ErrorDetails): StandardErrorResponse => ({
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: string;
40
+ message?: string;
35
41
  value?: unknown;
36
42
  };
37
43
 
38
44
  export const createValidationError = (message: string, violations: ValidationViolation[]): StandardErrorResponse =>
39
- createErrorResponse(ERROR_CODES.VALIDATION_ERROR, message, { violations });
45
+ createError(ERROR_CODES.VALIDATION_ERROR, message, { violations });
40
46
 
41
47
  export const createNotFoundError = (message: string, entityId?: string): StandardErrorResponse =>
42
- createErrorResponse(ERROR_CODES.NOT_FOUND, message, entityId ? { entityId } : undefined);
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
- export const createInvalidRequestError = (message: string): StandardErrorResponse =>
45
- createErrorResponse(ERROR_CODES.INVALID_REQUEST, message);
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
- export const createInternalError = (message = 'Internal server error'): StandardErrorResponse => {
48
- return createErrorResponse(ERROR_CODES.INTERNAL_ERROR, message);
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
+ });