@jskit-ai/crud-core 0.2.14 → 0.2.16
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/CHANGELOG.md +21 -0
- package/locales/en.json +1 -11
- package/package.json +19 -19
- package/patterns/{json-api-resource-package → crud-feature-package}/PATTERN.md +69 -37
- package/patterns/{json-api-resource-package → crud-feature-package}/example/packages/books/package.json +5 -4
- package/patterns/crud-feature-package/example/packages/books/src/server/BooksFeature.js +13 -0
- package/patterns/crud-feature-package/example/packages/books/src/shared/bookResource.js +74 -0
- package/patterns/crud-feature-package/example/packages/books/src/shared/index.js +1 -0
- package/src/server/createHooksToCollectChildren.js +56 -34
- package/src/server/crudContracts.js +54 -0
- package/src/server/defineCrudFeature.js +538 -0
- package/src/server/jsonApiModule/actions.js +117 -59
- package/src/server/jsonApiModule/routes.js +35 -11
- package/src/server/jsonApiModule/service.js +227 -32
- package/src/server/jsonApiResourceContract.js +41 -1
- package/src/server/listQueryValidators.js +1 -1
- package/src/server/removedApi.js +87 -0
- package/src/server/types.js +26 -50
- package/test/assistantActionConformance.test.js +20 -21
- package/test/assistantPagination.integration.test.js +32 -42
- package/test/createHooksToCollectChildren.test.js +61 -16
- package/test/crudFeaturePackagePattern.test.js +51 -0
- package/test/crudOnHandles.test.js +451 -0
- package/test/{defineCrudJsonApiFeature.test.js → defineCrudFeature.test.js} +451 -141
- package/test/errorCodes.test.js +11 -52
- package/test/hiddenFields.test.js +63 -0
- package/test/listQueryValidators.test.js +1 -1
- package/test/routeContracts.test.js +1 -1
- package/test/support/crudJsonApiActionOptions.js +3 -3
- package/test/unservedCrudAutomation.test.js +27 -22
- package/patterns/json-api-resource-package/example/packages/books/src/server/BooksFeature.js +0 -13
- package/patterns/json-api-resource-package/example/packages/books/src/shared/bookResource.js +0 -83
- package/patterns/json-api-resource-package/example/packages/books/src/shared/index.js +0 -1
- package/src/server/createCrudServiceFromResource.js +0 -63
- package/src/server/defineCrudJsonApiFeature.js +0 -346
- package/src/server/fieldAccess.js +0 -298
- package/src/server/jsonApiModule/repository.js +0 -355
- package/src/server/lookupPathSupport.js +0 -58
- package/src/server/lookups.js +0 -40
- package/src/server/repositorySupport.js +0 -528
- package/src/server/resourceRuntime/compiledRuntime.js +0 -284
- package/src/server/resourceRuntime/index.js +0 -659
- package/src/server/resourceRuntime/listControls.js +0 -333
- package/src/server/resourceRuntime/lookupHydration.js +0 -626
- package/src/server/resourceRuntime/operationStages.js +0 -140
- package/src/server/resourceRuntime/recordValidation.js +0 -65
- package/src/server/serviceMethods.js +0 -149
- package/test/createCrudServiceFromResource.test.js +0 -323
- package/test/fieldAccess.test.js +0 -86
- package/test/jsonApiRepository.test.js +0 -333
- package/test/jsonApiRepositoryDataCosts.test.js +0 -183
- package/test/jsonApiRepositoryPageLimits.test.js +0 -158
- package/test/jsonApiRepositoryRowLocks.test.js +0 -137
- package/test/jsonApiResourcePackagePattern.test.js +0 -20
- package/test/lookups.test.js +0 -111
- package/test/repositorySupport.test.js +0 -767
- package/test/resourceRuntime.test.js +0 -1014
- package/test/serviceMethods.test.js +0 -139
- /package/patterns/{json-api-resource-package → crud-feature-package}/example/migrations/20260815000000_books.cjs +0 -0
- /package/patterns/{json-api-resource-package → crud-feature-package}/example/packages/books/src/server/types.js +0 -0
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
import { buildCrudRecordChangedEvent, resolveCrudJsonApiEntityIdFromResult } from "../serviceEvents.js";
|
|
15
15
|
import { createCrudAssistantExtension } from "./assistantExtension.js";
|
|
16
16
|
import { buildCrudCustomActionId } from "./customActionId.js";
|
|
17
|
+
import { withRemovedProperties } from "../removedApi.js";
|
|
17
18
|
|
|
18
19
|
const CRUD_OPERATION_NAMES = Object.freeze(["list", "view", "create", "update", "delete"]);
|
|
19
20
|
const CRUD_MUTATION_NAMES = new Set(["create", "update", "delete"]);
|
|
@@ -96,26 +97,34 @@ function normalizeCrudOperationLifecycle(value) {
|
|
|
96
97
|
return Object.freeze(lifecycle);
|
|
97
98
|
}
|
|
98
99
|
|
|
100
|
+
// What a lifecycle hook's argument no longer carries.
|
|
101
|
+
const REMOVED_LIFECYCLE_PROPERTIES = Object.freeze({
|
|
102
|
+
trx: "pass options, which carries the operation's transaction, to each repository call."
|
|
103
|
+
});
|
|
104
|
+
|
|
99
105
|
function buildLifecycleContext(value) {
|
|
100
|
-
const { operation, input, context, resource, repository, service,
|
|
101
|
-
return Object.freeze({
|
|
106
|
+
const { operation, input, context, options, resource, repository, service, result, standard } = value;
|
|
107
|
+
return Object.freeze(withRemovedProperties({
|
|
102
108
|
operation,
|
|
103
109
|
input,
|
|
104
110
|
context,
|
|
111
|
+
options,
|
|
105
112
|
resource,
|
|
106
113
|
repository,
|
|
107
114
|
service,
|
|
108
|
-
trx,
|
|
109
115
|
...(Object.hasOwn(value, "result") ? { result } : {}),
|
|
110
116
|
...(standard ? { standard } : {})
|
|
111
|
-
});
|
|
117
|
+
}, REMOVED_LIFECYCLE_PROPERTIES, "A CRUD lifecycle hook's argument"));
|
|
112
118
|
}
|
|
113
119
|
|
|
114
120
|
function normalizeAdditionalActions(definitions, { namespace, surface, permissionForAction, scopeInputValidator, servedOverHttp }) {
|
|
115
121
|
return Object.freeze(Object.entries(definitions).map(([name, definition]) => {
|
|
116
122
|
const actionName = String(name || "").trim();
|
|
117
123
|
if (!ARCHITECTURE_ID_PATTERN.test(actionName)) {
|
|
118
|
-
throw new TypeError(
|
|
124
|
+
throw new TypeError(
|
|
125
|
+
`CRUD custom action name "${actionName}" is invalid: an action name is lower case, starts with a letter and ` +
|
|
126
|
+
"holds only letters, digits, _, . and - (cancel-day, not cancelDay)."
|
|
127
|
+
);
|
|
119
128
|
}
|
|
120
129
|
if (CRUD_OPERATION_NAMES.includes(actionName)) {
|
|
121
130
|
throw new TypeError(`CRUD custom action name "${actionName}" is reserved.`);
|
|
@@ -155,12 +164,68 @@ function normalizeAdditionalActions(definitions, { namespace, surface, permissio
|
|
|
155
164
|
}));
|
|
156
165
|
}
|
|
157
166
|
|
|
167
|
+
/**
|
|
168
|
+
* What a CRUD feature's actions take from its definition alone, so a feature builds it once and every composition
|
|
169
|
+
* reuses it: each enabled standard operation's id, channels, permission, input and assistant extension, and the
|
|
170
|
+
* feature's custom actions, checked and with the scope's input composed in.
|
|
171
|
+
* @param {object} definition
|
|
172
|
+
* @param {string} definition.namespace
|
|
173
|
+
* @param {import("../types.js").CrudContract} definition.resource
|
|
174
|
+
* @param {string} definition.surface
|
|
175
|
+
* @param {(operation: string) => object} definition.permissionForOperation Also answers for each custom action's name.
|
|
176
|
+
* @param {readonly string[]} definition.operations
|
|
177
|
+
* @param {object | null} definition.scopeInputValidator
|
|
178
|
+
* @param {object | null} definition.listFilterQueryValidator
|
|
179
|
+
* @param {{ create?: object, update?: object }} definition.operationInputs
|
|
180
|
+
* @param {Record<string, object>} definition.actions The feature's custom actions, by name.
|
|
181
|
+
* @param {boolean} definition.servedOverHttp False when the feature is internal or registers no routes.
|
|
182
|
+
*/
|
|
183
|
+
function createCrudJsonApiActionDefinitions({
|
|
184
|
+
namespace,
|
|
185
|
+
resource,
|
|
186
|
+
surface,
|
|
187
|
+
permissionForOperation,
|
|
188
|
+
operations,
|
|
189
|
+
scopeInputValidator,
|
|
190
|
+
listFilterQueryValidator,
|
|
191
|
+
operationInputs,
|
|
192
|
+
actions,
|
|
193
|
+
servedOverHttp
|
|
194
|
+
}) {
|
|
195
|
+
const additional = normalizeAdditionalActions(actions, {
|
|
196
|
+
namespace,
|
|
197
|
+
surface,
|
|
198
|
+
permissionForAction: permissionForOperation,
|
|
199
|
+
scopeInputValidator,
|
|
200
|
+
servedOverHttp
|
|
201
|
+
});
|
|
202
|
+
const enabledOperations = new Set(operations);
|
|
203
|
+
const standard = CRUD_OPERATION_NAMES.filter((operation) => enabledOperations.has(operation)).map((operation) => {
|
|
204
|
+
const permission = permissionForOperation(operation);
|
|
205
|
+
return Object.freeze({
|
|
206
|
+
operation,
|
|
207
|
+
id: `crud.${namespace}.${operation}`,
|
|
208
|
+
channels: resolveCrudActionChannels(permission, servedOverHttp),
|
|
209
|
+
surfaces: [surface],
|
|
210
|
+
permission,
|
|
211
|
+
input: buildActionInput(resource, operation, scopeInputValidator, listFilterQueryValidator, operationInputs),
|
|
212
|
+
extensions: Object.freeze({
|
|
213
|
+
assistant: createCrudAssistantExtension(resource, namespace, operation)
|
|
214
|
+
})
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
return Object.freeze({
|
|
218
|
+
standard: Object.freeze(standard),
|
|
219
|
+
additional
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
158
223
|
/**
|
|
159
224
|
* The standard and custom actions of one CRUD feature, from the options
|
|
160
|
-
*
|
|
225
|
+
* defineCrudFeature resolved.
|
|
161
226
|
* @param {object} dependencies
|
|
162
227
|
* @param {string} dependencies.namespace
|
|
163
|
-
* @param {import("../types.js").
|
|
228
|
+
* @param {import("../types.js").CrudContract} dependencies.resource
|
|
164
229
|
* @param {import("../types.js").CrudRepository} dependencies.repository
|
|
165
230
|
* @param {import("../types.js").CrudService} dependencies.service
|
|
166
231
|
* @param {string} dependencies.surface
|
|
@@ -175,71 +240,62 @@ function normalizeAdditionalActions(definitions, { namespace, surface, permissio
|
|
|
175
240
|
* @param {Record<string, unknown>} dependencies.dependencies The capabilities in the feature's `requires`, by local name.
|
|
176
241
|
* @param {string} dependencies.realtimeAudience
|
|
177
242
|
* @param {boolean} dependencies.servedOverHttp False when the feature is internal or registers no routes.
|
|
243
|
+
* @param {ReturnType<typeof createCrudJsonApiActionDefinitions>} [dependencies.definitions] What
|
|
244
|
+
* createCrudJsonApiActionDefinitions built from the same options, when the caller keeps it; built here otherwise.
|
|
178
245
|
*/
|
|
179
246
|
function createCrudJsonApiActions({
|
|
180
247
|
namespace,
|
|
181
248
|
resource,
|
|
182
249
|
repository,
|
|
183
250
|
service,
|
|
184
|
-
surface,
|
|
185
|
-
permissionForOperation,
|
|
186
|
-
operations,
|
|
187
|
-
scopeInputValidator,
|
|
188
251
|
scopeInputKeys,
|
|
189
|
-
listFilterQueryValidator,
|
|
190
252
|
operationLifecycle,
|
|
191
|
-
|
|
192
|
-
actions,
|
|
193
|
-
dependencies,
|
|
253
|
+
dependencies: actionDependencies,
|
|
194
254
|
realtimeAudience,
|
|
195
|
-
|
|
255
|
+
definitions: builtDefinitions,
|
|
256
|
+
...definitionOptions
|
|
196
257
|
}) {
|
|
258
|
+
const definitions = builtDefinitions ?? createCrudJsonApiActionDefinitions({ namespace, resource, ...definitionOptions });
|
|
197
259
|
const mutationEvent = (operation, entityId) => buildCrudRecordChangedEvent({
|
|
198
260
|
namespace,
|
|
199
261
|
operation,
|
|
200
262
|
entityId,
|
|
201
263
|
audience: realtimeAudience
|
|
202
264
|
});
|
|
203
|
-
const additionalActions = normalizeAdditionalActions(actions, {
|
|
204
|
-
namespace,
|
|
205
|
-
surface,
|
|
206
|
-
permissionForAction: permissionForOperation,
|
|
207
|
-
scopeInputValidator,
|
|
208
|
-
servedOverHttp
|
|
209
|
-
});
|
|
210
265
|
|
|
211
|
-
// HTTP and automation callers keep
|
|
212
|
-
//
|
|
266
|
+
// HTTP and automation callers keep the resource's page cap: a larger limit answers
|
|
267
|
+
// pageLimits().request records, and the response's pagination reports the page size, hasMore
|
|
213
268
|
// and the next cursor, as it always has. Server code asking for more gets an error instead.
|
|
214
|
-
function listDocuments(input,
|
|
269
|
+
function listDocuments(input, options) {
|
|
215
270
|
const query = omitInputKeys(input, scopeInputKeys);
|
|
216
|
-
|
|
217
|
-
|
|
271
|
+
const pageLimit = repository.pageLimits().request;
|
|
272
|
+
if (query.limit != null && Number(query.limit) > pageLimit) {
|
|
273
|
+
query.limit = pageLimit;
|
|
218
274
|
}
|
|
219
|
-
return service.
|
|
275
|
+
return service.list(query, options);
|
|
220
276
|
}
|
|
221
277
|
|
|
222
|
-
function viewDocument(input,
|
|
223
|
-
return service.
|
|
278
|
+
function viewDocument(input, options) {
|
|
279
|
+
return service.view(input.recordId, omitInputKeys(input, [...scopeInputKeys, "recordId"]), options);
|
|
224
280
|
}
|
|
225
281
|
|
|
226
|
-
function createDocument(input,
|
|
227
|
-
return service.
|
|
282
|
+
function createDocument(input, options) {
|
|
283
|
+
return service.create(omitInputKeys(input, scopeInputKeys), options);
|
|
228
284
|
}
|
|
229
285
|
|
|
230
|
-
function updateDocument(input,
|
|
231
|
-
return service.
|
|
286
|
+
function updateDocument(input, options) {
|
|
287
|
+
return service.patch(input.recordId, omitInputKeys(input, [...scopeInputKeys, "recordId"]), options);
|
|
232
288
|
}
|
|
233
289
|
|
|
234
|
-
function deleteDocument(input,
|
|
235
|
-
return service.
|
|
290
|
+
function deleteDocument(input, options) {
|
|
291
|
+
return service.delete(input.recordId, options);
|
|
236
292
|
}
|
|
237
293
|
|
|
238
294
|
/**
|
|
239
295
|
* An operation's result, checked where its events depend on it: a create
|
|
240
296
|
* returns the created record, with its id, as a tagged JSON:API result of a
|
|
241
297
|
* kind the HTTP transport encodes (returnJsonApiDocument, as the service's
|
|
242
|
-
*
|
|
298
|
+
* create and a lifecycle's standard() do, or returnJsonApiData), since
|
|
243
299
|
* its created and record-changed events read the record's id from it.
|
|
244
300
|
* @param {string} operation
|
|
245
301
|
* @param {unknown} result
|
|
@@ -248,7 +304,7 @@ function createCrudJsonApiActions({
|
|
|
248
304
|
if (operation === "create" && !resolveCrudJsonApiEntityIdFromResult({ result })) {
|
|
249
305
|
throw new TypeError(
|
|
250
306
|
`CRUD ${namespace} create must return the created record, with its id, as a tagged JSON:API result ` +
|
|
251
|
-
"(returnJsonApiDocument, as
|
|
307
|
+
"(returnJsonApiDocument, as the CRUD service's create() and a lifecycle's standard() do, or returnJsonApiData)."
|
|
252
308
|
);
|
|
253
309
|
}
|
|
254
310
|
return result;
|
|
@@ -259,20 +315,22 @@ function createCrudJsonApiActions({
|
|
|
259
315
|
* @param {string} operation
|
|
260
316
|
* @param {object} input
|
|
261
317
|
* @param {object} context
|
|
262
|
-
* @param {(input: object, context: object, trx
|
|
318
|
+
* @param {(input: object, options: { context: object, trx?: object }) => Promise<any>} runStandard
|
|
263
319
|
*/
|
|
264
320
|
async function executeOperation(operation, input, context, runStandard) {
|
|
265
321
|
const hooks = operationLifecycle[operation];
|
|
322
|
+
const callOptions = Object.freeze({ context });
|
|
266
323
|
if (!hooks) {
|
|
267
|
-
return requireOperationResult(operation, await runStandard(input,
|
|
324
|
+
return requireOperationResult(operation, await runStandard(input, callOptions));
|
|
268
325
|
}
|
|
269
326
|
|
|
270
|
-
|
|
271
|
-
|
|
327
|
+
// A mutation's hooks and its standard operation share one transaction: the options they receive carry it.
|
|
328
|
+
async function run(options) {
|
|
329
|
+
const base = { operation, input, context, options, resource, repository, service };
|
|
272
330
|
if (hooks.before) {
|
|
273
331
|
await hooks.before(buildLifecycleContext(base));
|
|
274
332
|
}
|
|
275
|
-
const standard = (nextInput = input) => runStandard(nextInput,
|
|
333
|
+
const standard = (nextInput = input) => runStandard(nextInput, options);
|
|
276
334
|
const result = requireOperationResult(operation, hooks.execute
|
|
277
335
|
? await hooks.execute(buildLifecycleContext({ ...base, standard }))
|
|
278
336
|
: await standard());
|
|
@@ -282,17 +340,19 @@ function createCrudJsonApiActions({
|
|
|
282
340
|
return result;
|
|
283
341
|
}
|
|
284
342
|
|
|
285
|
-
const result = CRUD_MUTATION_NAMES.has(operation)
|
|
343
|
+
const result = CRUD_MUTATION_NAMES.has(operation)
|
|
344
|
+
? await repository.withTransaction(callOptions, run)
|
|
345
|
+
: await run(callOptions);
|
|
286
346
|
if (hooks.afterCommit) {
|
|
287
347
|
try {
|
|
288
348
|
await hooks.afterCommit(buildLifecycleContext({
|
|
289
349
|
operation,
|
|
290
350
|
input,
|
|
291
351
|
context,
|
|
352
|
+
options: callOptions,
|
|
292
353
|
resource,
|
|
293
354
|
repository,
|
|
294
355
|
service,
|
|
295
|
-
trx: null,
|
|
296
356
|
result
|
|
297
357
|
}));
|
|
298
358
|
} catch (error) {
|
|
@@ -338,26 +398,23 @@ function createCrudJsonApiActions({
|
|
|
338
398
|
}
|
|
339
399
|
];
|
|
340
400
|
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
const permission = permissionForOperation(definition.operation);
|
|
401
|
+
const standardActions = definitions.standard.map((built) => {
|
|
402
|
+
const definition = standardDefinitions.find(({ operation }) => operation === built.operation);
|
|
344
403
|
return Object.freeze({
|
|
345
|
-
id:
|
|
404
|
+
id: built.id,
|
|
346
405
|
version: 1,
|
|
347
406
|
kind: definition.kind,
|
|
348
|
-
channels:
|
|
349
|
-
surfaces: [
|
|
350
|
-
permission,
|
|
351
|
-
input:
|
|
407
|
+
channels: built.channels,
|
|
408
|
+
surfaces: [...built.surfaces],
|
|
409
|
+
permission: built.permission,
|
|
410
|
+
input: built.input,
|
|
352
411
|
output: null,
|
|
353
|
-
extensions:
|
|
354
|
-
assistant: createCrudAssistantExtension(resource, namespace, definition.operation)
|
|
355
|
-
}),
|
|
412
|
+
extensions: built.extensions,
|
|
356
413
|
events: definition.events,
|
|
357
414
|
execute: definition.execute
|
|
358
415
|
});
|
|
359
416
|
});
|
|
360
|
-
const productActions =
|
|
417
|
+
const productActions = definitions.additional.map((definition) => Object.freeze({
|
|
361
418
|
id: definition.id,
|
|
362
419
|
version: definition.version,
|
|
363
420
|
kind: definition.kind,
|
|
@@ -368,7 +425,7 @@ function createCrudJsonApiActions({
|
|
|
368
425
|
permission: definition.permission,
|
|
369
426
|
extensions: definition.extensions || {},
|
|
370
427
|
events: definition.events,
|
|
371
|
-
execute: (input, context) => definition.execute(Object.freeze({ ...
|
|
428
|
+
execute: (input, context) => definition.execute(Object.freeze({ ...actionDependencies, input, context, resource, repository, service }))
|
|
372
429
|
}));
|
|
373
430
|
return Object.freeze([...standardActions, ...productActions]);
|
|
374
431
|
}
|
|
@@ -382,6 +439,7 @@ function assertCrudOperationName(operation = "") {
|
|
|
382
439
|
|
|
383
440
|
export {
|
|
384
441
|
assertCrudOperationName,
|
|
442
|
+
createCrudJsonApiActionDefinitions,
|
|
385
443
|
createCrudJsonApiActions,
|
|
386
444
|
normalizeCrudOperationLifecycle
|
|
387
445
|
};
|
|
@@ -75,11 +75,11 @@ function normalizeCustomActionRoutes(actions, namespace) {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
78
|
+
* The routes of one CRUD feature, from the options defineCrudFeature resolved: each `{ method, path, contract,
|
|
79
|
+
* handler }`, built from the definition alone, so a feature builds them once and registers them on every router.
|
|
80
80
|
* @param {object} options
|
|
81
81
|
* @param {string} options.namespace
|
|
82
|
-
* @param {import("../types.js").
|
|
82
|
+
* @param {import("../types.js").CrudContract} options.resource
|
|
83
83
|
* @param {string} options.routeBase
|
|
84
84
|
* @param {string} options.relativePath
|
|
85
85
|
* @param {string} options.surface
|
|
@@ -93,7 +93,7 @@ function normalizeCustomActionRoutes(actions, namespace) {
|
|
|
93
93
|
* @param {object | null} options.routeParamsValidator
|
|
94
94
|
* @param {((request: object) => object) | null} options.scopeInput
|
|
95
95
|
*/
|
|
96
|
-
function
|
|
96
|
+
function createCrudJsonApiRoutes({
|
|
97
97
|
namespace,
|
|
98
98
|
resource,
|
|
99
99
|
routeBase,
|
|
@@ -133,6 +133,9 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
133
133
|
const enabledOperations = new Set(operations);
|
|
134
134
|
const customActionRoutes = normalizeCustomActionRoutes(actions, namespace);
|
|
135
135
|
|
|
136
|
+
const routes = [];
|
|
137
|
+
const route = (method, path, contract, handler) => routes.push(Object.freeze({ method, path, contract, handler }));
|
|
138
|
+
|
|
136
139
|
function paramsContract({ record = false } = {}) {
|
|
137
140
|
if (record) {
|
|
138
141
|
return { params: routeContracts.recordRouteParamsValidator };
|
|
@@ -140,7 +143,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
140
143
|
return routeParamsValidator ? { params: routeParamsValidator } : {};
|
|
141
144
|
}
|
|
142
145
|
|
|
143
|
-
if (enabledOperations.has("list"))
|
|
146
|
+
if (enabledOperations.has("list")) route(
|
|
144
147
|
"GET",
|
|
145
148
|
basePath,
|
|
146
149
|
{
|
|
@@ -161,7 +164,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
161
164
|
}
|
|
162
165
|
);
|
|
163
166
|
|
|
164
|
-
if (enabledOperations.has("view"))
|
|
167
|
+
if (enabledOperations.has("view")) route(
|
|
165
168
|
"GET",
|
|
166
169
|
`${basePath}/:recordId`,
|
|
167
170
|
{
|
|
@@ -183,7 +186,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
183
186
|
}
|
|
184
187
|
);
|
|
185
188
|
|
|
186
|
-
if (enabledOperations.has("create"))
|
|
189
|
+
if (enabledOperations.has("create")) route(
|
|
187
190
|
"POST",
|
|
188
191
|
basePath,
|
|
189
192
|
{
|
|
@@ -204,7 +207,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
204
207
|
}
|
|
205
208
|
);
|
|
206
209
|
|
|
207
|
-
if (enabledOperations.has("update"))
|
|
210
|
+
if (enabledOperations.has("update")) route(
|
|
208
211
|
"PATCH",
|
|
209
212
|
`${basePath}/:recordId`,
|
|
210
213
|
{
|
|
@@ -226,7 +229,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
226
229
|
}
|
|
227
230
|
);
|
|
228
231
|
|
|
229
|
-
if (enabledOperations.has("delete"))
|
|
232
|
+
if (enabledOperations.has("delete")) route(
|
|
230
233
|
"DELETE",
|
|
231
234
|
`${basePath}/:recordId`,
|
|
232
235
|
{
|
|
@@ -258,7 +261,7 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
258
261
|
}
|
|
259
262
|
: {})
|
|
260
263
|
};
|
|
261
|
-
|
|
264
|
+
route(
|
|
262
265
|
actionRoute.method,
|
|
263
266
|
`${basePath}${actionRoute.path}`,
|
|
264
267
|
{
|
|
@@ -284,6 +287,27 @@ function registerCrudJsonApiRoutes(router, {
|
|
|
284
287
|
}
|
|
285
288
|
);
|
|
286
289
|
}
|
|
290
|
+
return Object.freeze(routes);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Registers routes createCrudJsonApiRoutes built, each with its own copy of its contract.
|
|
295
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeRouter} router
|
|
296
|
+
* @param {ReadonlyArray<{ method: string, path: string, contract: object, handler: Function }>} routes
|
|
297
|
+
*/
|
|
298
|
+
function registerCrudRoutes(router, routes) {
|
|
299
|
+
for (const { method, path, contract, handler } of routes) {
|
|
300
|
+
router.register(method, path, { ...contract }, handler);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Registers the routes of one CRUD feature, from the options defineCrudFeature resolved.
|
|
306
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeRouter} router
|
|
307
|
+
* @param {Parameters<typeof createCrudJsonApiRoutes>[0]} options
|
|
308
|
+
*/
|
|
309
|
+
function registerCrudJsonApiRoutes(router, options) {
|
|
310
|
+
registerCrudRoutes(router, createCrudJsonApiRoutes(options));
|
|
287
311
|
}
|
|
288
312
|
|
|
289
|
-
export { registerCrudJsonApiRoutes };
|
|
313
|
+
export { createCrudJsonApiRoutes, registerCrudJsonApiRoutes, registerCrudRoutes };
|