arocapi 4.0.1 → 5.0.0-beta.10
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 +37 -781
- package/dist/app.d.ts +38 -21
- package/dist/app.js +63 -45
- package/dist/app.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/entities.d.ts +5 -2
- package/dist/routes/entities.js +38 -58
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entity.d.ts +7 -2
- package/dist/routes/entity.js +14 -30
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/file.d.ts +8 -2
- package/dist/routes/file.js +38 -73
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/files.d.ts +5 -2
- package/dist/routes/files.js +25 -40
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/metadata.d.ts +15 -0
- package/dist/routes/metadata.js +38 -0
- package/dist/routes/metadata.js.map +1 -0
- package/dist/routes/search.d.ts +8 -7
- package/dist/routes/search.js +91 -120
- 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/storage/backend.d.ts +58 -0
- package/dist/storage/backend.js +2 -0
- package/dist/storage/backend.js.map +1 -0
- package/dist/storage/deliver.d.ts +12 -0
- package/dist/storage/deliver.js +34 -0
- package/dist/storage/deliver.js.map +1 -0
- package/dist/storage/directory/directory.d.ts +6 -0
- package/dist/storage/directory/directory.js +24 -0
- package/dist/storage/directory/directory.js.map +1 -0
- package/dist/storage/entity-meta/entity-meta.d.ts +5 -0
- package/dist/storage/entity-meta/entity-meta.js +14 -0
- package/dist/storage/entity-meta/entity-meta.js.map +1 -0
- package/dist/storage/location.d.ts +19 -0
- package/dist/storage/location.js +36 -0
- package/dist/storage/location.js.map +1 -0
- package/dist/storage/ocfl/ocfl.d.ts +6 -0
- package/dist/storage/ocfl/ocfl.js +45 -0
- package/dist/storage/ocfl/ocfl.js.map +1 -0
- package/dist/storage/results.d.ts +7 -0
- package/dist/storage/results.js +22 -0
- package/dist/storage/results.js.map +1 -0
- package/dist/storage/s3/s3.d.ts +10 -0
- package/dist/storage/s3/s3.js +47 -0
- package/dist/storage/s3/s3.js.map +1 -0
- package/dist/transformers/default.d.ts +11 -22
- package/dist/transformers/default.js +41 -15
- package/dist/transformers/default.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/transformers.d.ts +4 -10
- package/dist/types/visibility.d.ts +6 -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/headers.d.ts +1 -1
- package/dist/utils/headers.js +10 -1
- package/dist/utils/headers.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 +84 -45
- 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 +169 -102
- package/src/routes/capabilities.ts +22 -0
- package/src/routes/entities.ts +49 -68
- package/src/routes/entity.ts +22 -36
- package/src/routes/file.ts +54 -104
- package/src/routes/files.ts +32 -48
- package/src/routes/metadata.ts +67 -0
- package/src/routes/search.ts +132 -152
- 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/storage/backend.ts +75 -0
- package/src/storage/deliver.ts +65 -0
- package/src/storage/directory/directory.ts +39 -0
- package/src/storage/entity-meta/entity-meta.ts +25 -0
- package/src/storage/location.ts +78 -0
- package/src/storage/ocfl/ocfl.ts +71 -0
- package/src/storage/results.ts +32 -0
- package/src/storage/s3/s3.ts +79 -0
- package/src/transformers/default.ts +115 -85
- package/src/types/access.ts +25 -0
- package/src/types/context.ts +15 -0
- package/src/types/transformers.ts +8 -34
- package/src/types/visibility.ts +21 -0
- package/src/utils/access.ts +80 -0
- package/src/utils/errors.ts +68 -11
- package/src/utils/headers.ts +22 -2
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
- package/dist/app.test.d.ts +0 -1
- package/dist/app.test.js +0 -89
- package/dist/app.test.js.map +0 -1
- package/dist/express.d.ts +0 -4
- package/dist/express.js +0 -14
- package/dist/express.js.map +0 -1
- package/dist/index.dev.d.ts +0 -1
- package/dist/index.dev.js +0 -156
- package/dist/index.dev.js.map +0 -1
- package/dist/routes/crate.d.ts +0 -9
- package/dist/routes/crate.js +0 -82
- package/dist/routes/crate.js.map +0 -1
- package/dist/routes/crate.test.d.ts +0 -1
- package/dist/routes/crate.test.js +0 -342
- 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 -273
- 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 -130
- 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 -315
- 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 -222
- 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 -780
- 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 -18
- package/dist/test/helpers/fastify.js.map +0 -1
- package/dist/test/integration.setup.d.ts +0 -29
- package/dist/test/integration.setup.js +0 -188
- 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 -396
- 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 -163
- 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 -187
- package/dist/transformers/transformer.test.js.map +0 -1
- package/dist/types/fileHandlers.d.ts +0 -41
- package/dist/types/fileHandlers.js +0 -2
- package/dist/types/fileHandlers.js.map +0 -1
- package/dist/utils/errors.test.d.ts +0 -1
- package/dist/utils/errors.test.js +0 -66
- package/dist/utils/errors.test.js.map +0 -1
- package/dist/utils/queryBuilder.d.ts +0 -35
- package/dist/utils/queryBuilder.js +0 -98
- package/dist/utils/queryBuilder.js.map +0 -1
- package/prisma.config.ts +0 -9
- package/src/app.test.ts +0 -119
- package/src/express.ts +0 -21
- package/src/index.dev.ts +0 -200
- package/src/reset.d.ts +0 -1
- 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/crate.test.ts +0 -423
- package/src/routes/crate.ts +0 -123
- package/src/routes/entities.test.ts +0 -320
- package/src/routes/entity.test.ts +0 -160
- package/src/routes/file.test.ts +0 -395
- package/src/routes/files.test.ts +0 -278
- package/src/routes/search.test.ts +0 -903
- package/src/test/__snapshots__/integration.test.ts.snap +0 -18
- package/src/test/helpers/fastify.ts +0 -27
- package/src/test/integration.setup.ts +0 -210
- package/src/test/integration.test.ts +0 -482
- package/src/transformers/default.test.ts +0 -184
- package/src/transformers/transformer.test.ts +0 -252
- package/src/types/fileHandlers.ts +0 -65
- package/src/utils/errors.test.ts +0 -75
- package/src/utils/queryBuilder.ts +0 -132
package/src/app.ts
CHANGED
|
@@ -1,146 +1,213 @@
|
|
|
1
|
-
import cors from '@fastify/cors';
|
|
2
1
|
import sensible from '@fastify/sensible';
|
|
3
|
-
import type {
|
|
4
|
-
import type { FastifyInstance, FastifyPluginAsync } from 'fastify';
|
|
5
|
-
import { hasZodFastifySchemaValidationErrors, serializerCompiler, validatorCompiler } from 'fastify-type-provider-zod';
|
|
2
|
+
import type { FastifyPluginAsync } from 'fastify';
|
|
6
3
|
import type { PrismaClient } from './generated/prisma/client.js';
|
|
7
|
-
import
|
|
4
|
+
import capabilities from './routes/capabilities.js';
|
|
8
5
|
import entities from './routes/entities.js';
|
|
9
6
|
import entity from './routes/entity.js';
|
|
10
7
|
import file from './routes/file.js';
|
|
11
8
|
import files from './routes/files.js';
|
|
9
|
+
import metadata from './routes/metadata.js';
|
|
12
10
|
import search from './routes/search.js';
|
|
13
|
-
import type {
|
|
14
|
-
import type {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
} from './types/
|
|
20
|
-
import {
|
|
21
|
-
import
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export type { AuthorisedEntity, AuthorisedFile, StandardEntity, StandardFile } from './transformers/default.js';
|
|
25
|
-
export { AllPublicAccessTransformer, AllPublicFileAccessTransformer } from './transformers/default.js';
|
|
11
|
+
import type { SearchBackend } from './search/backend.js';
|
|
12
|
+
import type { SearchVisibilityWhere } from './search/visibility.js';
|
|
13
|
+
import type { StorageBackend } from './storage/backend.js';
|
|
14
|
+
import type { AccessFunction } from './types/access.js';
|
|
15
|
+
import type { IsAuthenticated, RequestContext } from './types/context.js';
|
|
16
|
+
import type { EntityTransformer, FileTransformer } from './types/transformers.js';
|
|
17
|
+
import type { EntityVisibilityFilter } from './types/visibility.js';
|
|
18
|
+
import { notFoundHandler } from './utils/errors.js';
|
|
19
|
+
import { setupValidation } from './utils/validation.js';
|
|
20
|
+
|
|
26
21
|
export type {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
} from './types/fileHandlers.js';
|
|
22
|
+
AdvancedQuery,
|
|
23
|
+
BackendSearchCapability,
|
|
24
|
+
BoundingBox,
|
|
25
|
+
FacetBucket,
|
|
26
|
+
RangeBound,
|
|
27
|
+
RangeFilter,
|
|
28
|
+
SearchBackend,
|
|
29
|
+
SearchExtra,
|
|
30
|
+
SearchRequest,
|
|
31
|
+
SearchResult,
|
|
32
|
+
} from './search/backend.js';
|
|
33
|
+
export { SearchQueryError } from './search/backend.js';
|
|
40
34
|
export type {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
} from './
|
|
47
|
-
export
|
|
48
|
-
export {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
35
|
+
FieldDeclaration,
|
|
36
|
+
FieldDeclarations,
|
|
37
|
+
FieldType,
|
|
38
|
+
GeoPoint,
|
|
39
|
+
SearchDocument,
|
|
40
|
+
} from './search/fields.js';
|
|
41
|
+
export { capabilitiesFromFields } from './search/fields.js';
|
|
42
|
+
export type { SearchVisibilityWhere, VisibilityCondition } from './search/visibility.js';
|
|
43
|
+
export { checkSearchVisibility } from './search/visibility.js';
|
|
44
|
+
export type {
|
|
45
|
+
ContentContext,
|
|
46
|
+
ContentDelivery,
|
|
47
|
+
ContentSource,
|
|
48
|
+
DeliveryHeaders,
|
|
49
|
+
DirectoryStorageMeta,
|
|
50
|
+
JsonDelivery,
|
|
51
|
+
MetadataDelivery,
|
|
52
|
+
MetadataSource,
|
|
53
|
+
OcflLocation,
|
|
54
|
+
OcflStorageMeta,
|
|
55
|
+
PathDelivery,
|
|
56
|
+
RedirectDelivery,
|
|
57
|
+
S3StorageMeta,
|
|
58
|
+
StorageBackend,
|
|
59
|
+
StorageLocate,
|
|
60
|
+
StreamDelivery,
|
|
61
|
+
} from './storage/backend.js';
|
|
62
|
+
export type {
|
|
63
|
+
AuthorisedEntity,
|
|
64
|
+
AuthorisedFile,
|
|
65
|
+
BaseEntity,
|
|
66
|
+
EntityRow,
|
|
67
|
+
FileRow,
|
|
68
|
+
StandardEntity,
|
|
69
|
+
StandardFile,
|
|
70
|
+
} from './transformers/default.js';
|
|
71
|
+
export type { AccessFunction, AccessInfo } from './types/access.js';
|
|
72
|
+
export type { IsAuthenticated, RequestContext } from './types/context.js';
|
|
73
|
+
export type { EntityTransformer, FileTransformer } from './types/transformers.js';
|
|
74
|
+
export type { EntityVisibilityFilter, SearchVisibilityFilter } from './types/visibility.js';
|
|
75
|
+
export type { LicenceAccessOptions } from './utils/access.js';
|
|
76
|
+
export { AllPublicAccess, LicenceAccess } from './utils/access.js';
|
|
77
|
+
export type { ErrorCode, StandardErrorResponse } from './utils/errors.js';
|
|
78
|
+
export {
|
|
79
|
+
createAuthenticationRequiredError,
|
|
80
|
+
createError,
|
|
81
|
+
createForbiddenError,
|
|
82
|
+
createInternalError,
|
|
83
|
+
createNotFoundError,
|
|
84
|
+
createValidationError,
|
|
85
|
+
ERROR_CODES,
|
|
86
|
+
errorHandler,
|
|
87
|
+
notFoundHandler,
|
|
88
|
+
} from './utils/errors.js';
|
|
89
|
+
export { AllVisibleEntityFilter, AllVisibleSearchFilter } from './utils/visibility.js';
|
|
64
90
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
91
|
+
const DOCS_URL = 'https://arocapi.crate-works.org';
|
|
92
|
+
|
|
93
|
+
const REMOVED_OPTIONS: Record<string, { replacement: string; docs: string }> = {
|
|
94
|
+
accessTransformer: { replacement: 'access', docs: `${DOCS_URL}/configuration/access/` },
|
|
95
|
+
fileAccessTransformer: { replacement: 'access', docs: `${DOCS_URL}/configuration/access/` },
|
|
96
|
+
hiddenResponse: {
|
|
97
|
+
replacement: 'promptLoginForHidden and isAuthenticated',
|
|
98
|
+
docs: `${DOCS_URL}/configuration/visibility/#promptloginforhidden`,
|
|
99
|
+
},
|
|
72
100
|
};
|
|
73
101
|
|
|
102
|
+
type PrismaMethod = (args: never) => PromiseLike<unknown>;
|
|
103
|
+
type PrismaDelegate = Record<'findMany' | 'findFirst' | 'count', PrismaMethod>;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The Prisma client arocapi needs: any Prisma 7 client generated from a schema that includes the arocapi models,
|
|
107
|
+
* including one with extra models, a `$extends` client or the client inside `$transaction`
|
|
108
|
+
*/
|
|
109
|
+
export type ArocapiPrismaClient = { entity: PrismaDelegate; file: PrismaDelegate };
|
|
110
|
+
|
|
74
111
|
export type Options = {
|
|
75
|
-
prisma:
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
queryBuilderOptions?: QueryBuilderOptions;
|
|
79
|
-
disableCors?: boolean;
|
|
80
|
-
accessTransformer: AccessTransformer;
|
|
112
|
+
prisma: ArocapiPrismaClient;
|
|
113
|
+
search: SearchBackend;
|
|
114
|
+
access: AccessFunction;
|
|
81
115
|
entityTransformers?: EntityTransformer[];
|
|
82
|
-
fileAccessTransformer: FileAccessTransformer;
|
|
83
116
|
fileTransformers?: FileTransformer[];
|
|
84
|
-
|
|
85
|
-
|
|
117
|
+
/** Where Metadata and Content come from */
|
|
118
|
+
storage: StorageBackend;
|
|
119
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
120
|
+
/** A `SearchVisibilityFilter<typeof fields>` for the search backend's declared fields */
|
|
121
|
+
searchVisibilityFilter: (context: RequestContext) => Promise<SearchVisibilityWhere> | SearchVisibilityWhere;
|
|
122
|
+
isAuthenticated?: IsAuthenticated;
|
|
123
|
+
promptLoginForHidden?: boolean;
|
|
86
124
|
};
|
|
87
125
|
const app: FastifyPluginAsync<Options> = async (fastify, options) => {
|
|
88
126
|
const {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
queryBuilderClass,
|
|
92
|
-
queryBuilderOptions,
|
|
93
|
-
disableCors = false,
|
|
94
|
-
accessTransformer,
|
|
127
|
+
search: searchBackend,
|
|
128
|
+
access,
|
|
95
129
|
entityTransformers,
|
|
96
|
-
fileAccessTransformer,
|
|
97
130
|
fileTransformers,
|
|
98
|
-
|
|
99
|
-
|
|
131
|
+
storage,
|
|
132
|
+
entityVisibilityFilter,
|
|
133
|
+
searchVisibilityFilter,
|
|
134
|
+
isAuthenticated,
|
|
135
|
+
promptLoginForHidden = false,
|
|
100
136
|
} = options;
|
|
101
137
|
|
|
102
|
-
if (!prisma) {
|
|
138
|
+
if (!options.prisma) {
|
|
103
139
|
throw new Error('Prisma client is required');
|
|
104
140
|
}
|
|
105
141
|
|
|
106
|
-
if (
|
|
107
|
-
throw new Error('
|
|
142
|
+
if (typeof options.prisma.entity?.findMany !== 'function' || typeof options.prisma.file?.findMany !== 'function') {
|
|
143
|
+
throw new Error('prisma must be a Prisma 7 client generated from a schema that includes the arocapi models');
|
|
108
144
|
}
|
|
109
145
|
|
|
110
|
-
|
|
111
|
-
|
|
146
|
+
// Consumers generate their own client, whose delegate generics never match ours; the routes need the full type
|
|
147
|
+
const prisma = options.prisma as PrismaClient;
|
|
148
|
+
|
|
149
|
+
if (!searchBackend) {
|
|
150
|
+
throw new Error('search is required');
|
|
112
151
|
}
|
|
113
152
|
|
|
114
|
-
|
|
115
|
-
|
|
153
|
+
for (const [removed, { replacement, docs }] of Object.entries(REMOVED_OPTIONS)) {
|
|
154
|
+
if ((options as Record<string, unknown>)[removed] !== undefined) {
|
|
155
|
+
throw new Error(`${removed} has been replaced by ${replacement}, see ${docs}`);
|
|
156
|
+
}
|
|
116
157
|
}
|
|
117
158
|
|
|
118
|
-
if (!
|
|
119
|
-
throw new Error('
|
|
159
|
+
if (!access) {
|
|
160
|
+
throw new Error('access is required');
|
|
120
161
|
}
|
|
121
162
|
|
|
122
|
-
if (!
|
|
123
|
-
throw new Error('
|
|
163
|
+
if (!storage) {
|
|
164
|
+
throw new Error('storage is required');
|
|
124
165
|
}
|
|
125
166
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
fastify.register(cors);
|
|
167
|
+
if (!storage.metadata) {
|
|
168
|
+
throw new Error('storage.metadata is required');
|
|
129
169
|
}
|
|
170
|
+
|
|
171
|
+
if (!storage.content) {
|
|
172
|
+
throw new Error('storage.content is required');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (!entityVisibilityFilter) {
|
|
176
|
+
throw new Error('entityVisibilityFilter is required');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (!searchVisibilityFilter) {
|
|
180
|
+
throw new Error('searchVisibilityFilter is required');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (promptLoginForHidden && !isAuthenticated) {
|
|
184
|
+
throw new Error('promptLoginForHidden requires isAuthenticated');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const refusal = { isAuthenticated, promptLoginForHidden };
|
|
188
|
+
|
|
189
|
+
fastify.register(sensible);
|
|
130
190
|
setupValidation(fastify);
|
|
131
191
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
192
|
+
// Fastify only gives a plugin its own not-found handler when registered with its own prefix
|
|
193
|
+
const { prefix } = options as { prefix?: string };
|
|
194
|
+
if (prefix && prefix !== '/') {
|
|
195
|
+
fastify.setNotFoundHandler(notFoundHandler);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
fastify.register(capabilities, { search: searchBackend });
|
|
199
|
+
fastify.register(entities, { prisma, access, entityTransformers, entityVisibilityFilter });
|
|
200
|
+
fastify.register(entity, { prisma, access, entityTransformers, entityVisibilityFilter, refusal });
|
|
201
|
+
fastify.register(files, { prisma, access, fileTransformers, entityVisibilityFilter });
|
|
202
|
+
fastify.register(file, { prisma, content: storage.content, access, entityVisibilityFilter, refusal });
|
|
203
|
+
fastify.register(metadata, { prisma, metadata: storage.metadata, access, entityVisibilityFilter, refusal });
|
|
137
204
|
fastify.register(search, {
|
|
138
205
|
prisma,
|
|
139
|
-
|
|
140
|
-
|
|
206
|
+
search: searchBackend,
|
|
207
|
+
access,
|
|
141
208
|
entityTransformers,
|
|
142
|
-
|
|
143
|
-
|
|
209
|
+
entityVisibilityFilter,
|
|
210
|
+
searchVisibilityFilter,
|
|
144
211
|
});
|
|
145
212
|
};
|
|
146
213
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { FastifyPluginAsync } from 'fastify';
|
|
2
|
+
import type { SearchBackend } from '../search/backend.js';
|
|
3
|
+
|
|
4
|
+
const SPEC_VERSION = '0.5.0';
|
|
5
|
+
|
|
6
|
+
type CapabilitiesRouteOptions = {
|
|
7
|
+
search: SearchBackend;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const capabilities: FastifyPluginAsync<CapabilitiesRouteOptions> = async (fastify, opts) => {
|
|
11
|
+
const document = {
|
|
12
|
+
apiVersion: SPEC_VERSION,
|
|
13
|
+
deposit: { supported: false },
|
|
14
|
+
tombstonePolicy: '404',
|
|
15
|
+
extensions: {},
|
|
16
|
+
search: opts.search.capabilities,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
fastify.get('/capabilities', async () => document);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default capabilities;
|
package/src/routes/entities.ts
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import type { FastifyPluginAsync } from 'fastify';
|
|
2
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
3
|
-
import { z } from 'zod
|
|
3
|
+
import { z } from 'zod';
|
|
4
4
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
5
|
+
import { applyTransformers, authoriseEntities } from '../transformers/default.js';
|
|
6
|
+
import type { AccessFunction } from '../types/access.js';
|
|
7
|
+
import type { EntityTransformer } from '../types/transformers.js';
|
|
8
|
+
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
8
9
|
|
|
9
10
|
const querySchema = z.object({
|
|
10
11
|
memberOf: z.url().optional(),
|
|
11
12
|
entityType: z
|
|
12
|
-
.string()
|
|
13
|
-
.
|
|
14
|
-
.transform((val) =>
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
.union([z.string(), z.array(z.string())])
|
|
14
|
+
.default([])
|
|
15
|
+
.transform((val) =>
|
|
16
|
+
[val]
|
|
17
|
+
.flat()
|
|
18
|
+
.map((value) => value.trim())
|
|
19
|
+
.filter(Boolean),
|
|
20
|
+
)
|
|
21
|
+
.pipe(z.array(z.url())),
|
|
20
22
|
limit: z.coerce.number().int().min(1).max(1000).default(100),
|
|
21
23
|
offset: z.coerce.number().int().min(0).default(0),
|
|
22
24
|
sort: z.enum(['id', 'name', 'createdAt', 'updatedAt']).default('id'),
|
|
@@ -25,12 +27,13 @@ const querySchema = z.object({
|
|
|
25
27
|
|
|
26
28
|
type EntitiesRouteOptions = {
|
|
27
29
|
prisma: PrismaClient;
|
|
28
|
-
|
|
30
|
+
access: AccessFunction;
|
|
29
31
|
entityTransformers?: EntityTransformer[];
|
|
32
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
30
33
|
};
|
|
31
34
|
|
|
32
35
|
const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts) => {
|
|
33
|
-
const { prisma,
|
|
36
|
+
const { prisma, access, entityTransformers = [], entityVisibilityFilter } = opts;
|
|
34
37
|
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
35
38
|
'/entities',
|
|
36
39
|
{
|
|
@@ -38,68 +41,46 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
|
|
|
38
41
|
querystring: querySchema,
|
|
39
42
|
},
|
|
40
43
|
},
|
|
41
|
-
async (request
|
|
44
|
+
async (request) => {
|
|
42
45
|
const { memberOf, entityType, limit, offset, sort, order } = request.query;
|
|
43
46
|
|
|
44
|
-
|
|
45
|
-
const where: NonNullable<Parameters<typeof prisma.entity.findMany>[0]>['where'] = {};
|
|
46
|
-
|
|
47
|
-
if (memberOf) {
|
|
48
|
-
where.memberOf = memberOf;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (entityType && entityType.length > 0) {
|
|
52
|
-
where.entityType = {
|
|
53
|
-
in: entityType,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
47
|
+
const where: NonNullable<Parameters<typeof prisma.entity.findMany>[0]>['where'] = {};
|
|
56
48
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
include: { file: { select: { id: true } } },
|
|
61
|
-
orderBy: {
|
|
62
|
-
[sort]: order,
|
|
63
|
-
},
|
|
64
|
-
skip: offset,
|
|
65
|
-
take: limit,
|
|
66
|
-
}),
|
|
67
|
-
prisma.entity.count({ where }),
|
|
68
|
-
]);
|
|
69
|
-
|
|
70
|
-
// Resolve memberOf and rootCollection references
|
|
71
|
-
const refMap = await resolveEntityReferences(dbEntities, prisma);
|
|
49
|
+
if (memberOf) {
|
|
50
|
+
where.memberOf = memberOf;
|
|
51
|
+
}
|
|
72
52
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
...base,
|
|
79
|
-
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
80
|
-
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
81
|
-
};
|
|
82
|
-
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
53
|
+
if (entityType.length > 0) {
|
|
54
|
+
where.entityType = {
|
|
55
|
+
in: entityType,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
83
58
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
59
|
+
const context = { request, fastify };
|
|
60
|
+
const visibilityWhere = await entityVisibilityFilter(context);
|
|
61
|
+
const visibleWhere = { AND: [where, visibilityWhere] };
|
|
88
62
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
63
|
+
const [dbEntities, total] = await Promise.all([
|
|
64
|
+
prisma.entity.findMany({
|
|
65
|
+
where: visibleWhere,
|
|
66
|
+
orderBy: {
|
|
67
|
+
[sort]: order,
|
|
68
|
+
},
|
|
69
|
+
skip: offset,
|
|
70
|
+
take: limit,
|
|
71
|
+
}),
|
|
72
|
+
prisma.entity.count({ where: visibleWhere }),
|
|
73
|
+
]);
|
|
92
74
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
} catch (error) {
|
|
98
|
-
const err = error as Error;
|
|
99
|
-
fastify.log.error(`Database error: ${err.message}`);
|
|
75
|
+
const authorisedEntities = await authoriseEntities(dbEntities, prisma, access, context);
|
|
76
|
+
const entities = await Promise.all(
|
|
77
|
+
authorisedEntities.map((entity, i) => applyTransformers(entity, entityTransformers, context, dbEntities[i])),
|
|
78
|
+
);
|
|
100
79
|
|
|
101
|
-
|
|
102
|
-
|
|
80
|
+
return {
|
|
81
|
+
total,
|
|
82
|
+
entities,
|
|
83
|
+
};
|
|
103
84
|
},
|
|
104
85
|
);
|
|
105
86
|
};
|
package/src/routes/entity.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { FastifyPluginAsync } from 'fastify';
|
|
2
2
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
3
|
-
import { z } from 'zod
|
|
3
|
+
import { z } from 'zod';
|
|
4
4
|
import type { PrismaClient } from '../generated/prisma/client.js';
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
7
|
-
import {
|
|
5
|
+
import { applyTransformers, authoriseEntities } from '../transformers/default.js';
|
|
6
|
+
import type { AccessFunction } from '../types/access.js';
|
|
7
|
+
import type { EntityTransformer } from '../types/transformers.js';
|
|
8
|
+
import type { EntityVisibilityFilter } from '../types/visibility.js';
|
|
9
|
+
import { createEntityNotVisibleResponder, type RefusalOptions } from '../utils/visibility.js';
|
|
8
10
|
|
|
9
11
|
const paramsSchema = z.object({
|
|
10
12
|
id: z.url(),
|
|
@@ -12,12 +14,16 @@ const paramsSchema = z.object({
|
|
|
12
14
|
|
|
13
15
|
type EntityRouteOptions = {
|
|
14
16
|
prisma: PrismaClient;
|
|
15
|
-
|
|
17
|
+
access: AccessFunction;
|
|
16
18
|
entityTransformers?: EntityTransformer[];
|
|
19
|
+
entityVisibilityFilter: EntityVisibilityFilter;
|
|
20
|
+
refusal: RefusalOptions;
|
|
17
21
|
};
|
|
18
22
|
|
|
19
23
|
const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) => {
|
|
20
|
-
const { prisma,
|
|
24
|
+
const { prisma, access, entityTransformers = [], entityVisibilityFilter, refusal } = opts;
|
|
25
|
+
const sendEntityNotVisible = createEntityNotVisibleResponder(prisma, refusal);
|
|
26
|
+
|
|
21
27
|
fastify.withTypeProvider<ZodTypeProvider>().get(
|
|
22
28
|
'/entity/:id',
|
|
23
29
|
{
|
|
@@ -28,39 +34,19 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
|
|
|
28
34
|
async (request, reply) => {
|
|
29
35
|
const { id } = request.params;
|
|
30
36
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!entity) {
|
|
38
|
-
return reply.code(404).send(createNotFoundError('The requested entity was not found', id));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Resolve memberOf and rootCollection references
|
|
42
|
-
const refMap = await resolveEntityReferences([entity], prisma);
|
|
37
|
+
const context = { request, fastify };
|
|
38
|
+
const visibilityWhere = await entityVisibilityFilter(context);
|
|
39
|
+
const entity = await prisma.entity.findFirst({
|
|
40
|
+
where: { AND: [{ id }, visibilityWhere] },
|
|
41
|
+
});
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
48
|
-
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
49
|
-
};
|
|
50
|
-
const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
|
|
51
|
-
|
|
52
|
-
let result = authorisedEntity;
|
|
53
|
-
for (const transformer of entityTransformers) {
|
|
54
|
-
result = await transformer(result, { request, fastify });
|
|
55
|
-
}
|
|
43
|
+
if (!entity) {
|
|
44
|
+
return sendEntityNotVisible(reply, id, context);
|
|
45
|
+
}
|
|
56
46
|
|
|
57
|
-
|
|
58
|
-
} catch (error) {
|
|
59
|
-
const err = error as Error;
|
|
60
|
-
fastify.log.error(`Database error: ${err.message}`);
|
|
47
|
+
const [authorisedEntity] = await authoriseEntities([entity], prisma, access, context);
|
|
61
48
|
|
|
62
|
-
|
|
63
|
-
}
|
|
49
|
+
return applyTransformers(authorisedEntity, entityTransformers, context, entity);
|
|
64
50
|
},
|
|
65
51
|
);
|
|
66
52
|
};
|