arocapi 5.0.0-beta.2 → 5.0.0-beta.4

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 (117) hide show
  1. package/README.md +37 -824
  2. package/dist/app.d.ts +20 -7
  3. package/dist/app.js +42 -28
  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.test.js +8 -8
  13. package/dist/routes/capabilities.test.js.map +1 -1
  14. package/dist/routes/crate.test.js +67 -19
  15. package/dist/routes/crate.test.js.map +1 -1
  16. package/dist/routes/entities.d.ts +5 -2
  17. package/dist/routes/entities.js +31 -49
  18. package/dist/routes/entities.js.map +1 -1
  19. package/dist/routes/entities.test.js +16 -12
  20. package/dist/routes/entities.test.js.map +1 -1
  21. package/dist/routes/entity.d.ts +7 -2
  22. package/dist/routes/entity.js +14 -29
  23. package/dist/routes/entity.js.map +1 -1
  24. package/dist/routes/entity.test.js +19 -13
  25. package/dist/routes/entity.test.js.map +1 -1
  26. package/dist/routes/file.d.ts +6 -0
  27. package/dist/routes/file.js +67 -59
  28. package/dist/routes/file.js.map +1 -1
  29. package/dist/routes/file.test.js +135 -25
  30. package/dist/routes/file.test.js.map +1 -1
  31. package/dist/routes/files.d.ts +5 -2
  32. package/dist/routes/files.js +24 -39
  33. package/dist/routes/files.js.map +1 -1
  34. package/dist/routes/files.test.js +44 -21
  35. package/dist/routes/files.test.js.map +1 -1
  36. package/dist/routes/metadata.d.ts +6 -0
  37. package/dist/routes/metadata.js +49 -54
  38. package/dist/routes/metadata.js.map +1 -1
  39. package/dist/routes/search.d.ts +6 -2
  40. package/dist/routes/search.js +95 -99
  41. package/dist/routes/search.js.map +1 -1
  42. package/dist/routes/search.test.js +85 -39
  43. package/dist/routes/search.test.js.map +1 -1
  44. package/dist/test/helpers/fastify.js +2 -3
  45. package/dist/test/helpers/fastify.js.map +1 -1
  46. package/dist/test/integration.setup.d.ts +18 -0
  47. package/dist/test/integration.setup.js +15 -7
  48. package/dist/test/integration.setup.js.map +1 -1
  49. package/dist/test/integration.test.js +368 -2
  50. package/dist/test/integration.test.js.map +1 -1
  51. package/dist/transformers/default.d.ts +10 -18
  52. package/dist/transformers/default.js +41 -15
  53. package/dist/transformers/default.js.map +1 -1
  54. package/dist/transformers/default.test.js +109 -47
  55. package/dist/transformers/default.test.js.map +1 -1
  56. package/dist/transformers/transformer.test.js +12 -34
  57. package/dist/transformers/transformer.test.js.map +1 -1
  58. package/dist/types/access.d.ts +9 -0
  59. package/dist/types/access.js +2 -0
  60. package/dist/types/access.js.map +1 -0
  61. package/dist/types/context.d.ts +6 -0
  62. package/dist/types/context.js +2 -0
  63. package/dist/types/context.js.map +1 -0
  64. package/dist/types/fileHandlers.d.ts +5 -9
  65. package/dist/types/transformers.d.ts +4 -10
  66. package/dist/types/visibility.d.ts +5 -0
  67. package/dist/types/visibility.js +2 -0
  68. package/dist/types/visibility.js.map +1 -0
  69. package/dist/utils/access.d.ts +17 -0
  70. package/dist/utils/access.js +41 -0
  71. package/dist/utils/access.js.map +1 -0
  72. package/dist/utils/errors.d.ts +13 -4
  73. package/dist/utils/errors.js +44 -8
  74. package/dist/utils/errors.js.map +1 -1
  75. package/dist/utils/errors.test.js +14 -1
  76. package/dist/utils/errors.test.js.map +1 -1
  77. package/dist/utils/validation.d.ts +2 -0
  78. package/dist/utils/validation.js +8 -0
  79. package/dist/utils/validation.js.map +1 -0
  80. package/dist/utils/visibility.d.ts +18 -0
  81. package/dist/utils/visibility.js +16 -0
  82. package/dist/utils/visibility.js.map +1 -0
  83. package/package.json +6 -4
  84. package/src/app.test.ts +45 -66
  85. package/src/app.ts +85 -54
  86. package/src/errorHandling.test.ts +288 -0
  87. package/src/index.dev.ts +6 -4
  88. package/src/routes/__snapshots__/search.test.ts.snap +2 -2
  89. package/src/routes/capabilities.test.ts +8 -8
  90. package/src/routes/crate.test.ts +80 -19
  91. package/src/routes/entities.test.ts +16 -12
  92. package/src/routes/entities.ts +40 -59
  93. package/src/routes/entity.test.ts +19 -13
  94. package/src/routes/entity.ts +22 -35
  95. package/src/routes/file.test.ts +156 -25
  96. package/src/routes/file.ts +84 -62
  97. package/src/routes/files.test.ts +52 -26
  98. package/src/routes/files.ts +31 -47
  99. package/src/routes/metadata.ts +64 -58
  100. package/src/routes/search.test.ts +88 -39
  101. package/src/routes/search.ts +123 -119
  102. package/src/test/helpers/fastify.ts +2 -3
  103. package/src/test/integration.setup.ts +18 -7
  104. package/src/test/integration.test.ts +472 -1
  105. package/src/transformers/default.test.ts +139 -47
  106. package/src/transformers/default.ts +103 -83
  107. package/src/transformers/transformer.test.ts +18 -44
  108. package/src/types/access.ts +20 -0
  109. package/src/types/context.ts +15 -0
  110. package/src/types/fileHandlers.ts +5 -13
  111. package/src/types/transformers.ts +4 -32
  112. package/src/types/visibility.ts +18 -0
  113. package/src/utils/access.ts +80 -0
  114. package/src/utils/errors.test.ts +16 -1
  115. package/src/utils/errors.ts +67 -10
  116. package/src/utils/validation.ts +11 -0
  117. package/src/utils/visibility.ts +56 -0
@@ -1,13 +1,15 @@
1
1
  import type { Client } from '@opensearch-project/opensearch';
2
2
  import type { MultiBucketAggregateBaseFiltersBucket } from '@opensearch-project/opensearch/api/_types/_common.aggregations.js';
3
- import type { Search_Request } from '@opensearch-project/opensearch/api/index.js';
3
+ import type { Search_Request, Search_Response } from '@opensearch-project/opensearch/api/index.js';
4
4
  import type { FastifyPluginAsync } from 'fastify';
5
5
  import type { ZodTypeProvider } from 'fastify-type-provider-zod';
6
6
  import { z } from 'zod';
7
7
  import type { PrismaClient } from '../generated/prisma/client.js';
8
- import { baseEntityTransformer, resolveEntityReferences } from '../transformers/default.js';
9
- import type { AccessTransformer, EntityTransformer } from '../types/transformers.js';
10
- import { createInternalError, createInvalidRequestError } from '../utils/errors.js';
8
+ import { applyTransformers, authoriseEntities } from '../transformers/default.js';
9
+ import type { AccessFunction } from '../types/access.js';
10
+ import type { EntityTransformer } from '../types/transformers.js';
11
+ import type { EntityVisibilityFilter, SearchVisibilityFilter } from '../types/visibility.js';
12
+ import { createError, ERROR_CODES } from '../utils/errors.js';
11
13
  import type { OpensearchQueryBuilder } from '../utils/queryBuilder.js';
12
14
 
13
15
  const boundingBoxSchema = z.object({
@@ -36,13 +38,23 @@ const searchParamsSchema = z.object({
36
38
  type SearchRouteOptions = {
37
39
  prisma: PrismaClient;
38
40
  opensearch: Client;
39
- accessTransformer: AccessTransformer;
41
+ access: AccessFunction;
40
42
  entityTransformers?: EntityTransformer[];
41
43
  queryBuilder: OpensearchQueryBuilder;
44
+ entityVisibilityFilter: EntityVisibilityFilter;
45
+ searchVisibilityFilter: SearchVisibilityFilter;
42
46
  };
43
47
 
44
48
  const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) => {
45
- const { prisma, opensearch, accessTransformer, entityTransformers = [], queryBuilder } = opts;
49
+ const {
50
+ prisma,
51
+ opensearch,
52
+ access,
53
+ entityTransformers = [],
54
+ queryBuilder,
55
+ entityVisibilityFilter,
56
+ searchVisibilityFilter,
57
+ } = opts;
46
58
  fastify.withTypeProvider<ZodTypeProvider>().post(
47
59
  '/search',
48
60
  {
@@ -53,138 +65,130 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
53
65
  async (request, reply) => {
54
66
  const { searchType, query, filters, boundingBox, geohashPrecision, limit, offset, sort, order } = request.body;
55
67
 
56
- try {
57
- const opensearchQuery: Search_Request = {
58
- index: 'entities',
59
- body: {
60
- query: queryBuilder.buildQuery(searchType, query, filters, boundingBox),
61
- aggs: queryBuilder.buildAggregations(geohashPrecision, boundingBox),
62
- highlight: {
63
- fields: {
64
- name: {},
65
- description: {},
66
- },
68
+ const context = { request, fastify };
69
+ const [visibilityWhere, visibilityQuery] = await Promise.all([
70
+ entityVisibilityFilter(context),
71
+ searchVisibilityFilter(context),
72
+ ]);
73
+
74
+ const opensearchQuery: Search_Request = {
75
+ index: 'entities',
76
+ body: {
77
+ query: {
78
+ bool: {
79
+ must: [queryBuilder.buildQuery(searchType, query, filters, boundingBox)],
80
+ filter: [visibilityQuery],
67
81
  },
68
- sort: queryBuilder.buildSort(sort, order),
69
- from: offset,
70
- size: limit,
71
82
  },
72
- };
73
- fastify.log.debug(opensearchQuery);
74
- const response = await opensearch.search(opensearchQuery);
75
-
76
- if (!response.body?.hits?.hits) {
77
- throw new Error('Invalid search response: missing hits data');
78
- }
79
-
80
- const entityIds = response.body.hits.hits.map((hit) => hit._source?.id as string | undefined).filter(Boolean);
81
-
82
- const dbEntities = await prisma.entity.findMany({
83
- where: {
84
- id: {
85
- in: entityIds,
83
+ aggs: queryBuilder.buildAggregations(geohashPrecision, boundingBox),
84
+ highlight: {
85
+ fields: {
86
+ name: {},
87
+ description: {},
86
88
  },
87
89
  },
88
- include: { file: { select: { id: true } } },
89
- });
90
+ sort: queryBuilder.buildSort(sort, order),
91
+ from: offset,
92
+ size: limit,
93
+ },
94
+ };
95
+ fastify.log.debug(opensearchQuery);
96
+ let response: Search_Response;
97
+ try {
98
+ response = await opensearch.search(opensearchQuery);
99
+ } catch (error) {
100
+ const { statusCode, message } = error as Error & { statusCode?: number };
101
+ // OpenSearch returns 400 for malformed queries (e.g. invalid query_string syntax)
102
+ if (statusCode !== 400) {
103
+ throw error;
104
+ }
90
105
 
91
- const entityMap = new Map(dbEntities.map((entity) => [entity.id, entity]));
106
+ fastify.log.warn(`Invalid search query: ${message}`);
92
107
 
93
- // Resolve memberOf and rootCollection references
94
- const refMap = await resolveEntityReferences(dbEntities, prisma);
108
+ return reply.code(422).send(createError(ERROR_CODES.VALIDATION_ERROR, message));
109
+ }
95
110
 
96
- const entities = await Promise.all(
97
- response.body.hits.hits.map(async (hit) => {
98
- if (!hit._source?.id) {
99
- throw new Error('Missing id in search hit');
100
- }
111
+ if (!response.body?.hits?.hits) {
112
+ throw new Error('Invalid search response: missing hits data');
113
+ }
101
114
 
102
- const dbEntity = entityMap.get(hit._source.id);
103
- if (!dbEntity) {
104
- fastify.log.warn(`Entity ${hit._source.id} found in OpenSearch but not in database`);
115
+ const entityIds = response.body.hits.hits.map((hit) => hit._source?.id as string | undefined).filter(Boolean);
105
116
 
106
- return null;
107
- }
117
+ const dbEntities = await prisma.entity.findMany({
118
+ where: {
119
+ AND: [{ id: { in: entityIds } }, visibilityWhere],
120
+ },
121
+ include: { file: { select: { id: true } } },
122
+ });
108
123
 
109
- const base = baseEntityTransformer(dbEntity);
110
- const standardEntity = {
111
- ...base,
112
- memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
113
- rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
114
- };
115
- const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
116
-
117
- let result = authorisedEntity;
118
- for (const transformer of entityTransformers) {
119
- result = await transformer(result, { request, fastify });
120
- }
124
+ const authorisedEntities = await authoriseEntities(dbEntities, prisma, access, context);
125
+ const entityMap = new Map(authorisedEntities.map((entity) => [entity.id, entity]));
121
126
 
122
- // Add search-specific metadata
123
- return {
124
- ...(result as Record<string, unknown>),
125
- searchExtra: {
126
- score: hit._score,
127
- highlight: hit.highlight,
128
- },
129
- };
130
- }),
131
- ).then((results) => results.filter(Boolean));
132
-
133
- const facets: Record<string, Array<{ name: string; count: number }>> = {};
134
- if (response.body.aggregations) {
135
- Object.keys(response.body.aggregations).forEach((key) => {
136
- if (key !== 'geohash_grid') {
137
- const agg = response.body.aggregations?.[key] as MultiBucketAggregateBaseFiltersBucket;
138
- if (agg?.buckets && Array.isArray(agg.buckets)) {
139
- facets[key] = agg.buckets.map((bucket) => ({
140
- name: bucket.key,
141
- count: bucket.doc_count,
142
- }));
143
- }
144
- }
145
- });
146
- }
127
+ const entities = await Promise.all(
128
+ response.body.hits.hits.map(async (hit) => {
129
+ if (!hit._source?.id) {
130
+ throw new Error('Missing id in search hit');
131
+ }
132
+
133
+ const authorisedEntity = entityMap.get(hit._source.id);
134
+ if (!authorisedEntity) {
135
+ fastify.log.warn(`Entity ${hit._source.id} found in OpenSearch but missing or not visible in database`);
147
136
 
148
- let geohashGrid: Record<string, number> | undefined;
149
- if (response.body.aggregations?.geohash_grid) {
150
- const geohashAgg = response.body.aggregations.geohash_grid as MultiBucketAggregateBaseFiltersBucket;
151
- if (geohashAgg?.buckets && Array.isArray(geohashAgg.buckets)) {
152
- geohashAgg.buckets.forEach((bucket) => {
153
- geohashGrid ||= {};
154
- geohashGrid[bucket.key] = bucket.doc_count;
155
- });
137
+ return null;
156
138
  }
157
- }
158
139
 
159
- /* v8 ignore next 3 -- Not sure how to force opensearch to hit the other path -- @preserve */
160
- const total =
161
- typeof response.body.hits.total === 'number'
162
- ? response.body.hits.total
163
- : response.body.hits.total?.value || 0;
164
-
165
- const result = {
166
- total,
167
- searchTime: response.body.took,
168
- entities,
169
- facets: Object.keys(facets).length > 0 ? facets : undefined,
170
- geohashGrid,
171
- };
172
-
173
- return result;
174
- } catch (error) {
175
- const err = error as Error;
140
+ const result = await applyTransformers(authorisedEntity, entityTransformers, context);
176
141
 
177
- // OpenSearch returns 400 for malformed queries (e.g. invalid query_string syntax)
178
- if ('statusCode' in err && (err as { statusCode: number }).statusCode === 400) {
179
- fastify.log.warn(`Invalid search query: ${err.message}`);
142
+ // Add search-specific metadata
143
+ return {
144
+ ...(result as Record<string, unknown>),
145
+ searchExtra: {
146
+ score: hit._score,
147
+ highlight: hit.highlight,
148
+ },
149
+ };
150
+ }),
151
+ ).then((results) => results.filter(Boolean));
152
+
153
+ const facets: Record<string, Array<{ name: string; count: number }>> = {};
154
+ if (response.body.aggregations) {
155
+ Object.keys(response.body.aggregations).forEach((key) => {
156
+ if (key !== 'geohash_grid') {
157
+ const agg = response.body.aggregations?.[key] as MultiBucketAggregateBaseFiltersBucket;
158
+ if (agg?.buckets && Array.isArray(agg.buckets)) {
159
+ facets[key] = agg.buckets.map((bucket) => ({
160
+ name: bucket.key,
161
+ count: bucket.doc_count,
162
+ }));
163
+ }
164
+ }
165
+ });
166
+ }
180
167
 
181
- return reply.code(422).send(createInvalidRequestError(err.message));
168
+ let geohashGrid: Record<string, number> | undefined;
169
+ if (response.body.aggregations?.geohash_grid) {
170
+ const geohashAgg = response.body.aggregations.geohash_grid as MultiBucketAggregateBaseFiltersBucket;
171
+ if (geohashAgg?.buckets && Array.isArray(geohashAgg.buckets)) {
172
+ geohashAgg.buckets.forEach((bucket) => {
173
+ geohashGrid ||= {};
174
+ geohashGrid[bucket.key] = bucket.doc_count;
175
+ });
182
176
  }
177
+ }
183
178
 
184
- fastify.log.error(`Search error: ${err.message}`);
179
+ /* v8 ignore next 2 -- Not sure how to force opensearch to hit the other path -- @preserve */
180
+ const total =
181
+ typeof response.body.hits.total === 'number' ? response.body.hits.total : response.body.hits.total?.value || 0;
185
182
 
186
- return reply.code(500).send(createInternalError('Search failed'));
187
- }
183
+ const result = {
184
+ total,
185
+ searchTime: response.body.took,
186
+ entities,
187
+ facets: Object.keys(facets).length > 0 ? facets : undefined,
188
+ geohashGrid,
189
+ };
190
+
191
+ return result;
188
192
  },
189
193
  );
190
194
  };
@@ -1,10 +1,10 @@
1
1
  import type { Client } from '@opensearch-project/opensearch';
2
2
  import type { FastifyInstance } from 'fastify';
3
3
  import Fastify from 'fastify';
4
- import { serializerCompiler, validatorCompiler } from 'fastify-type-provider-zod';
5
4
  import { mockDeep, mockReset } from 'vitest-mock-extended';
6
5
 
7
6
  import type { PrismaClient } from '../../generated/prisma/client.js';
7
+ import { setupValidation } from '../../utils/validation.js';
8
8
 
9
9
  export let fastify: FastifyInstance;
10
10
  export const prisma = mockDeep<PrismaClient>();
@@ -16,8 +16,7 @@ export const fastifyBefore = async () => {
16
16
 
17
17
  fastify = Fastify({ logger: false });
18
18
 
19
- fastify.setValidatorCompiler(validatorCompiler);
20
- fastify.setSerializerCompiler(serializerCompiler);
19
+ setupValidation(fastify);
21
20
 
22
21
  return fastify;
23
22
  };
@@ -2,7 +2,8 @@ import { Client } from '@opensearch-project/opensearch';
2
2
  import { PrismaMariaDb } from '@prisma/adapter-mariadb';
3
3
  import type { FastifyInstance } from 'fastify';
4
4
  import Fastify from 'fastify';
5
- import app, { AllPublicAccessTransformer, AllPublicFileAccessTransformer } from '../app.js';
5
+ import type { Options } from '../app.js';
6
+ import app, { AllPublicAccess, AllVisibleEntityFilter, AllVisibleSearchFilter } from '../app.js';
6
7
  import { PrismaClient } from '../generated/prisma/client.js';
7
8
 
8
9
  let fastify: FastifyInstance;
@@ -32,13 +33,18 @@ export async function setupIntegrationTests() {
32
33
  node: process.env.OPENSEARCH_URL,
33
34
  });
34
35
 
35
- fastify = Fastify({ logger: false });
36
+ fastify = await buildTestApp();
37
+ }
38
+
39
+ export async function buildTestApp(overrides: Partial<Options> = {}) {
40
+ const instance = Fastify({ logger: false });
36
41
 
37
- await fastify.register(app, {
42
+ await instance.register(app, {
38
43
  prisma,
39
44
  opensearch,
40
- accessTransformer: AllPublicAccessTransformer,
41
- fileAccessTransformer: AllPublicFileAccessTransformer,
45
+ access: AllPublicAccess,
46
+ entityVisibilityFilter: AllVisibleEntityFilter,
47
+ searchVisibilityFilter: AllVisibleSearchFilter,
42
48
  fileHandler: {
43
49
  get: async () => {
44
50
  throw new Error('File handler not implemented in integration tests');
@@ -55,9 +61,12 @@ export async function setupIntegrationTests() {
55
61
  throw new Error('RO-Crate head handler not implemented in integration tests');
56
62
  },
57
63
  },
64
+ ...overrides,
58
65
  });
59
66
 
60
- await fastify.ready();
67
+ await instance.ready();
68
+
69
+ return instance;
61
70
  }
62
71
 
63
72
  export async function teardownIntegrationTests() {
@@ -103,6 +112,7 @@ export async function seedTestData() {
103
112
  rootCollection: 'http://example.com/entity/1',
104
113
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
105
114
  contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
115
+ meta: { draft: true },
106
116
  createdAt: new Date(),
107
117
  updatedAt: new Date(),
108
118
  },
@@ -126,7 +136,7 @@ export async function seedTestData() {
126
136
  memberOf: 'http://example.com/entity/2',
127
137
  rootCollection: 'http://example.com/entity/1',
128
138
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
129
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
139
+ contentLicenseId: 'https://example.com/licences/restricted',
130
140
  createdAt: new Date(),
131
141
  updatedAt: new Date(),
132
142
  },
@@ -139,6 +149,7 @@ export async function seedTestData() {
139
149
  rootCollection: 'http://example.com/entity/1',
140
150
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
141
151
  contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
152
+ meta: { draft: true },
142
153
  createdAt: new Date(),
143
154
  updatedAt: new Date(),
144
155
  },