@jskit-ai/crud-core 0.2.14 → 0.2.15
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 +16 -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 +509 -0
- package/src/server/jsonApiModule/actions.js +44 -30
- package/src/server/jsonApiModule/routes.js +2 -2
- package/src/server/jsonApiModule/service.js +213 -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 +50 -0
- package/test/crudOnHandles.test.js +454 -0
- package/test/{defineCrudJsonApiFeature.test.js → defineCrudFeature.test.js} +444 -141
- package/test/errorCodes.test.js +11 -52
- package/test/hiddenFields.test.js +62 -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
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { normalizeObject } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Adds each virtual field's projection to a read query.
|
|
5
|
-
* @param {object} dbQuery
|
|
6
|
-
* @param {import("../types.js").CompiledCrudRepositoryRuntime} runtime
|
|
7
|
-
* @param {{ knex: import("@jskit-ai/database-runtime/server/types").RuntimeKnex, tableName: string }} projection
|
|
8
|
-
*/
|
|
9
|
-
function applyCrudRepositoryVirtualProjections(dbQuery, runtime, { knex, tableName }) {
|
|
10
|
-
for (const virtualField of runtime.virtualFields) {
|
|
11
|
-
virtualField.applyProjection(dbQuery, {
|
|
12
|
-
knex,
|
|
13
|
-
tableName,
|
|
14
|
-
alias: virtualField.alias,
|
|
15
|
-
fieldKey: virtualField.key
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return dbQuery;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Runs an operation's applyQuery stage, which the compiled runtime holds as a function or null.
|
|
24
|
-
* @param {object} dbQuery
|
|
25
|
-
* @param {Function | null | undefined} stage
|
|
26
|
-
* @param {object} stageContext
|
|
27
|
-
* @param {{ context: string, stageKey: string }} labels
|
|
28
|
-
*/
|
|
29
|
-
function applyConfiguredQueryStage(dbQuery, stage, stageContext, { context, stageKey }) {
|
|
30
|
-
if (!stage) {
|
|
31
|
-
return {
|
|
32
|
-
queryBuilder: dbQuery
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const directResult = stage(dbQuery, stageContext);
|
|
37
|
-
if (directResult === undefined || directResult === dbQuery) {
|
|
38
|
-
return {
|
|
39
|
-
queryBuilder: dbQuery
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (isCrudRepositoryQueryBuilder(directResult)) {
|
|
44
|
-
return {
|
|
45
|
-
queryBuilder: directResult
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// readable-check: allow method-probe - data: an application's query stage may return a promise
|
|
50
|
-
if (directResult && typeof directResult.then === "function") {
|
|
51
|
-
return Promise.resolve(directResult).then((awaitedResult) => {
|
|
52
|
-
if (awaitedResult === undefined || awaitedResult === dbQuery) {
|
|
53
|
-
return {
|
|
54
|
-
queryBuilder: dbQuery
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (isCrudRepositoryQueryBuilder(awaitedResult)) {
|
|
59
|
-
throw new TypeError(
|
|
60
|
-
`${context} ${stageKey} cannot return a query builder asynchronously. Mutate the provided builder and return undefined instead.`
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
throw new TypeError(
|
|
65
|
-
`${context} ${stageKey} must return undefined, the provided query builder, or another query builder synchronously.`
|
|
66
|
-
);
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return Promise.reject(new TypeError(
|
|
71
|
-
`${context} ${stageKey} must return undefined, the provided query builder, or another query builder synchronously.`
|
|
72
|
-
));
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function isCrudRepositoryQueryBuilder(value) {
|
|
76
|
-
if (!value || (typeof value !== "object" && typeof value !== "function") || Array.isArray(value)) {
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return [
|
|
81
|
-
"where",
|
|
82
|
-
"whereIn",
|
|
83
|
-
"select",
|
|
84
|
-
"insert",
|
|
85
|
-
"update",
|
|
86
|
-
"delete",
|
|
87
|
-
"del",
|
|
88
|
-
"first",
|
|
89
|
-
"modify"
|
|
90
|
-
// readable-check: allow method-probe - platform object: a query stage's result is a query builder when it has one of its methods
|
|
91
|
-
].some((key) => typeof value[key] === "function");
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Runs an operation's payload stage, which the compiled runtime holds as a function or null.
|
|
96
|
-
* @param {object} payload
|
|
97
|
-
* @param {Function | null | undefined} stage
|
|
98
|
-
* @param {object} stageContext
|
|
99
|
-
* @param {{ context: string, stageKey: string }} labels
|
|
100
|
-
*/
|
|
101
|
-
async function applyConfiguredObjectStage(payload, stage, stageContext, { context, stageKey }) {
|
|
102
|
-
const normalizedPayload = normalizeObject(payload);
|
|
103
|
-
if (!stage) {
|
|
104
|
-
return normalizedPayload;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const nextPayload = await stage(normalizedPayload, stageContext);
|
|
108
|
-
if (nextPayload === undefined) {
|
|
109
|
-
return normalizedPayload;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (!nextPayload || typeof nextPayload !== "object" || Array.isArray(nextPayload)) {
|
|
113
|
-
throw new TypeError(`${context} ${stageKey} must return an object when it returns a value.`);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return nextPayload;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* What every stage of one repository call receives besides its own values: the
|
|
121
|
-
* runtime, the repository options, the call's options and a state object the
|
|
122
|
-
* call's stages share.
|
|
123
|
-
* @param {import("../types.js").CompiledCrudRepositoryRuntime} runtime
|
|
124
|
-
* @param {object} callOptions
|
|
125
|
-
*/
|
|
126
|
-
function buildCrudRepositoryActionContextBase(runtime, callOptions) {
|
|
127
|
-
return {
|
|
128
|
-
runtime,
|
|
129
|
-
repositoryOptions: runtime.repositoryOptions,
|
|
130
|
-
callOptions,
|
|
131
|
-
state: {}
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export {
|
|
136
|
-
applyCrudRepositoryVirtualProjections,
|
|
137
|
-
applyConfiguredQueryStage,
|
|
138
|
-
applyConfiguredObjectStage,
|
|
139
|
-
buildCrudRepositoryActionContextBase
|
|
140
|
-
};
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { createValidationError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
2
|
-
import { normalizeObject } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
|
-
import { codedFieldErrors, errorFieldErrors, validateSchemaPayload } from "@jskit-ai/kernel/shared/validators";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A create or patch payload validated by the resource's body schema for it, when it has one.
|
|
7
|
-
* @param {import("../types.js").CompiledCrudRepositoryRuntime} runtime
|
|
8
|
-
* @param {object} payload
|
|
9
|
-
* @param {"create" | "patch"} operationKey
|
|
10
|
-
*/
|
|
11
|
-
async function normalizeRepositoryInputPayload(runtime, payload, operationKey) {
|
|
12
|
-
const input = runtime.input[operationKey];
|
|
13
|
-
const normalizedPayload = normalizeObject(payload);
|
|
14
|
-
|
|
15
|
-
if (!input) {
|
|
16
|
-
return normalizedPayload;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
try {
|
|
20
|
-
const nextPayload = validateSchemaPayload(input, normalizedPayload, {
|
|
21
|
-
phase: "input",
|
|
22
|
-
context: `${runtime.context} operations.${operationKey}.body`
|
|
23
|
-
});
|
|
24
|
-
return normalizeObject(nextPayload);
|
|
25
|
-
} catch (error) {
|
|
26
|
-
const fieldErrors = codedFieldErrors(error);
|
|
27
|
-
if (fieldErrors) {
|
|
28
|
-
throw createValidationError(fieldErrors);
|
|
29
|
-
}
|
|
30
|
-
throw error;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* A stored record validated by the resource's view output schema.
|
|
36
|
-
* @param {import("../types.js").CompiledCrudRepositoryRuntime} runtime
|
|
37
|
-
* @param {object} record
|
|
38
|
-
* @param {{ operation: string }} labels The operation its error names.
|
|
39
|
-
*/
|
|
40
|
-
async function normalizeRepositoryOutputRecord(runtime, record, { operation }) {
|
|
41
|
-
try {
|
|
42
|
-
return validateSchemaPayload(runtime.output, record, {
|
|
43
|
-
phase: "output",
|
|
44
|
-
context: `${runtime.context} operations.view.output`
|
|
45
|
-
});
|
|
46
|
-
} catch (error) {
|
|
47
|
-
const fieldErrors = errorFieldErrors(error)?.fieldErrors;
|
|
48
|
-
if (fieldErrors) {
|
|
49
|
-
const detailMessage = Object.entries(fieldErrors)
|
|
50
|
-
.map(([field, message]) => `${field}: ${message}`)
|
|
51
|
-
.join(", ");
|
|
52
|
-
throw new TypeError(
|
|
53
|
-
`${runtime.context} ${operation} output validation failed: ${detailMessage}.`
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
throw new TypeError(
|
|
57
|
-
`${runtime.context} ${operation} output validation failed: ${String(error?.message || "Invalid value.")}.`
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export {
|
|
63
|
-
normalizeRepositoryInputPayload,
|
|
64
|
-
normalizeRepositoryOutputRecord
|
|
65
|
-
};
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
2
|
-
import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
|
-
import { normalizeObjectInput } from "@jskit-ai/kernel/shared/validators";
|
|
4
|
-
import { requireCrudNamespace } from "@jskit-ai/resource-crud-core/shared/crudNamespaceSupport";
|
|
5
|
-
import { createCrudFieldAccessRuntime } from "./fieldAccess.js";
|
|
6
|
-
|
|
7
|
-
function createCrudServiceRuntime(resource = {}, { context = "crudService" } = {}) {
|
|
8
|
-
const namespace = requireCrudNamespace(resource.namespace, { context: `${context} resource.namespace` });
|
|
9
|
-
const fieldAccessRuntime = createCrudFieldAccessRuntime(resource, { context });
|
|
10
|
-
|
|
11
|
-
return Object.freeze({
|
|
12
|
-
context,
|
|
13
|
-
namespace,
|
|
14
|
-
resource,
|
|
15
|
-
fieldAccessRuntime
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function splitCrudListRepositoryCall(query = {}, options = {}) {
|
|
20
|
-
const normalizedQuery = normalizeObjectInput(query);
|
|
21
|
-
const repositoryQuery = {
|
|
22
|
-
...normalizedQuery
|
|
23
|
-
};
|
|
24
|
-
delete repositoryQuery.include;
|
|
25
|
-
|
|
26
|
-
const normalizedInclude = Object.hasOwn(normalizedQuery, "include")
|
|
27
|
-
? normalizeText(normalizedQuery.include)
|
|
28
|
-
: undefined;
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
query: repositoryQuery,
|
|
32
|
-
options: normalizedInclude === undefined
|
|
33
|
-
? options
|
|
34
|
-
: {
|
|
35
|
-
...options,
|
|
36
|
-
include: options.include === undefined ? normalizedInclude : options.include
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @param {import("./types.js").CrudServiceRuntime} runtime
|
|
43
|
-
* @param {import("./types.js").CrudRecordRepository} repository
|
|
44
|
-
*/
|
|
45
|
-
async function crudServiceListRecords(runtime, repository, fieldAccess = {}, query = {}, options = {}) {
|
|
46
|
-
const repositoryCall = splitCrudListRepositoryCall(query, options);
|
|
47
|
-
const result = await repository.list(repositoryCall.query, repositoryCall.options);
|
|
48
|
-
return runtime.fieldAccessRuntime.filterReadableListResult(result, fieldAccess, {
|
|
49
|
-
action: "list",
|
|
50
|
-
query,
|
|
51
|
-
options,
|
|
52
|
-
context: options.context
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @param {import("./types.js").CrudServiceRuntime} runtime
|
|
58
|
-
* @param {import("./types.js").CrudRecordRepository} repository
|
|
59
|
-
*/
|
|
60
|
-
async function crudServiceGetRecord(runtime, repository, fieldAccess = {}, recordId, options = {}) {
|
|
61
|
-
const record = await repository.findById(recordId, options);
|
|
62
|
-
if (!record) {
|
|
63
|
-
throw new AppError(404, "Record not found.", { code: "crud.record.notFound" });
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return runtime.fieldAccessRuntime.filterReadableRecord(record, fieldAccess, {
|
|
67
|
-
action: "view",
|
|
68
|
-
recordId,
|
|
69
|
-
options,
|
|
70
|
-
context: options.context
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* @param {import("./types.js").CrudServiceRuntime} runtime
|
|
76
|
-
* @param {import("./types.js").CrudRecordRepository} repository
|
|
77
|
-
*/
|
|
78
|
-
async function crudServiceCreateRecord(runtime, repository, fieldAccess = {}, payload = {}, options = {}) {
|
|
79
|
-
const writablePayload = await runtime.fieldAccessRuntime.enforceWritablePayload(payload, fieldAccess, {
|
|
80
|
-
action: "create",
|
|
81
|
-
payload,
|
|
82
|
-
options,
|
|
83
|
-
context: options.context
|
|
84
|
-
});
|
|
85
|
-
const record = await repository.create(writablePayload, options);
|
|
86
|
-
if (!record) {
|
|
87
|
-
throw new Error(`${runtime.namespace}Service could not load the created record.`);
|
|
88
|
-
}
|
|
89
|
-
return runtime.fieldAccessRuntime.filterReadableRecord(record, fieldAccess, {
|
|
90
|
-
action: "create",
|
|
91
|
-
options,
|
|
92
|
-
context: options.context
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* @param {import("./types.js").CrudServiceRuntime} runtime
|
|
98
|
-
* @param {import("./types.js").CrudRecordRepository} repository
|
|
99
|
-
*/
|
|
100
|
-
async function crudServiceUpdateRecord(runtime, repository, fieldAccess = {}, recordId, payload = {}, options = {}) {
|
|
101
|
-
const existingRecord = await repository.findById(recordId, options);
|
|
102
|
-
if (!existingRecord) {
|
|
103
|
-
throw new AppError(404, "Record not found.", { code: "crud.record.notFound" });
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const writablePayload = await runtime.fieldAccessRuntime.enforceWritablePayload(payload, fieldAccess, {
|
|
107
|
-
action: "update",
|
|
108
|
-
recordId,
|
|
109
|
-
payload,
|
|
110
|
-
options,
|
|
111
|
-
context: options.context,
|
|
112
|
-
existingRecord
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
const record = await repository.updateById(recordId, writablePayload, {
|
|
116
|
-
...options,
|
|
117
|
-
existingRecord
|
|
118
|
-
});
|
|
119
|
-
if (!record) {
|
|
120
|
-
throw new AppError(404, "Record not found.", { code: "crud.record.notFound" });
|
|
121
|
-
}
|
|
122
|
-
return runtime.fieldAccessRuntime.filterReadableRecord(record, fieldAccess, {
|
|
123
|
-
action: "update",
|
|
124
|
-
recordId,
|
|
125
|
-
options,
|
|
126
|
-
context: options.context
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* @param {import("./types.js").CrudServiceRuntime} runtime
|
|
132
|
-
* @param {import("./types.js").CrudRecordRepository} repository
|
|
133
|
-
*/
|
|
134
|
-
async function crudServiceDeleteRecord(runtime, repository, fieldAccess = {}, recordId, options = {}) {
|
|
135
|
-
const deleted = await repository.deleteById(recordId, options);
|
|
136
|
-
if (!deleted) {
|
|
137
|
-
throw new AppError(404, "Record not found.", { code: "crud.record.notFound" });
|
|
138
|
-
}
|
|
139
|
-
return deleted;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export {
|
|
143
|
-
createCrudServiceRuntime,
|
|
144
|
-
crudServiceListRecords,
|
|
145
|
-
crudServiceGetRecord,
|
|
146
|
-
crudServiceCreateRecord,
|
|
147
|
-
crudServiceUpdateRecord,
|
|
148
|
-
crudServiceDeleteRecord
|
|
149
|
-
};
|
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { createSchema } from "json-rest-schema";
|
|
4
|
-
import { createCrudServiceFromResource } from "../src/server/createCrudServiceFromResource.js";
|
|
5
|
-
|
|
6
|
-
function createOperationSchemaDefinition(structure = {}, mode = "replace") {
|
|
7
|
-
return {
|
|
8
|
-
schema: createSchema(structure),
|
|
9
|
-
mode
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function createResourceWithOutputSchema(overrides = {}) {
|
|
14
|
-
return {
|
|
15
|
-
namespace: "contacts",
|
|
16
|
-
operations: {
|
|
17
|
-
view: {
|
|
18
|
-
output: createOperationSchemaDefinition({
|
|
19
|
-
id: { type: "integer", required: true },
|
|
20
|
-
name: { type: "string", required: true },
|
|
21
|
-
optionalSecret: { type: "string", required: false },
|
|
22
|
-
nullableSecret: {
|
|
23
|
-
type: "string",
|
|
24
|
-
required: true,
|
|
25
|
-
nullable: true
|
|
26
|
-
},
|
|
27
|
-
defaultedSecret: {
|
|
28
|
-
type: "string",
|
|
29
|
-
required: true,
|
|
30
|
-
default: ""
|
|
31
|
-
}
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
...overrides
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function createRepositoryDouble(overrides = {}) {
|
|
40
|
-
return {
|
|
41
|
-
async list(query) {
|
|
42
|
-
return { items: [query], nextCursor: null };
|
|
43
|
-
},
|
|
44
|
-
async findById(recordId) {
|
|
45
|
-
return recordId === 1 ? { id: 1 } : null;
|
|
46
|
-
},
|
|
47
|
-
async create(payload) {
|
|
48
|
-
return { id: 2, ...payload };
|
|
49
|
-
},
|
|
50
|
-
async updateById(recordId, payload) {
|
|
51
|
-
if (recordId !== 1) {
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
return { id: 1, ...payload };
|
|
55
|
-
},
|
|
56
|
-
async deleteById(recordId) {
|
|
57
|
-
if (recordId !== 1) {
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
return { id: 1, deleted: true };
|
|
61
|
-
},
|
|
62
|
-
...overrides
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
test("createCrudServiceFromResource delegates service methods and applies 404 semantics", async () => {
|
|
67
|
-
const { createBaseService } = createCrudServiceFromResource({
|
|
68
|
-
namespace: "contacts"
|
|
69
|
-
});
|
|
70
|
-
const service = createBaseService({
|
|
71
|
-
repository: createRepositoryDouble()
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
const list = await service.listRecords({ limit: 2 }, {});
|
|
75
|
-
assert.deepEqual(list, {
|
|
76
|
-
items: [{ limit: 2 }],
|
|
77
|
-
nextCursor: null
|
|
78
|
-
});
|
|
79
|
-
assert.deepEqual(await service.getRecord(1, {}), { id: 1 });
|
|
80
|
-
await assert.rejects(
|
|
81
|
-
() => service.getRecord(9, {}),
|
|
82
|
-
(error) => error?.status === 404 && error?.message === "Record not found."
|
|
83
|
-
);
|
|
84
|
-
assert.deepEqual(await service.createRecord({ name: "A" }, {}), { id: 2, name: "A" });
|
|
85
|
-
assert.deepEqual(await service.updateRecord(1, { name: "B" }, {}), { id: 1, name: "B" });
|
|
86
|
-
await assert.rejects(
|
|
87
|
-
() => service.updateRecord(9, { name: "B" }, {}),
|
|
88
|
-
(error) => error?.status === 404 && error?.message === "Record not found."
|
|
89
|
-
);
|
|
90
|
-
assert.deepEqual(await service.deleteRecord(1, {}), { id: 1, deleted: true });
|
|
91
|
-
await assert.rejects(
|
|
92
|
-
() => service.deleteRecord(9, {}),
|
|
93
|
-
(error) => error?.status === 404 && error?.message === "Record not found."
|
|
94
|
-
);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test("createCrudServiceFromResource forwards list include as repository call option", async () => {
|
|
98
|
-
const listCalls = [];
|
|
99
|
-
const { createBaseService } = createCrudServiceFromResource({
|
|
100
|
-
namespace: "contacts"
|
|
101
|
-
});
|
|
102
|
-
const service = createBaseService({
|
|
103
|
-
repository: createRepositoryDouble({
|
|
104
|
-
async list(query = {}, options = {}) {
|
|
105
|
-
listCalls.push({
|
|
106
|
-
query,
|
|
107
|
-
options
|
|
108
|
-
});
|
|
109
|
-
return {
|
|
110
|
-
items: [],
|
|
111
|
-
nextCursor: null
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
})
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
await service.listRecords({
|
|
118
|
-
q: "tony",
|
|
119
|
-
include: "primaryVetId"
|
|
120
|
-
}, {
|
|
121
|
-
visibilityContext: {
|
|
122
|
-
visibility: "workspace"
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
assert.deepEqual(listCalls[0], {
|
|
127
|
-
query: {
|
|
128
|
-
q: "tony"
|
|
129
|
-
},
|
|
130
|
-
options: {
|
|
131
|
-
visibilityContext: {
|
|
132
|
-
visibility: "workspace"
|
|
133
|
-
},
|
|
134
|
-
include: "primaryVetId"
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
test("createCrudServiceFromResource passes existing records to repository update options", async () => {
|
|
140
|
-
const updateCalls = [];
|
|
141
|
-
const { createBaseService } = createCrudServiceFromResource({
|
|
142
|
-
namespace: "contacts"
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
const service = createBaseService({
|
|
146
|
-
repository: createRepositoryDouble({
|
|
147
|
-
async findById(recordId) {
|
|
148
|
-
return recordId === 1
|
|
149
|
-
? { id: 1, name: "Existing" }
|
|
150
|
-
: null;
|
|
151
|
-
},
|
|
152
|
-
async updateById(recordId, payload, options = {}) {
|
|
153
|
-
updateCalls.push({ recordId, payload, options });
|
|
154
|
-
return { id: 1, ...payload };
|
|
155
|
-
}
|
|
156
|
-
})
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
const record = await service.updateRecord(1, { name: "B" }, {});
|
|
160
|
-
|
|
161
|
-
assert.deepEqual(updateCalls, [
|
|
162
|
-
{
|
|
163
|
-
recordId: 1,
|
|
164
|
-
payload: { name: "B" },
|
|
165
|
-
options: {
|
|
166
|
-
existingRecord: { id: 1, name: "Existing" }
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
]);
|
|
170
|
-
assert.deepEqual(record, { id: 1, name: "B" });
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
test("createCrudServiceFromResource validates required inputs", async () => {
|
|
174
|
-
assert.throws(
|
|
175
|
-
() => createCrudServiceFromResource({}),
|
|
176
|
-
/resource\.namespace/
|
|
177
|
-
);
|
|
178
|
-
|
|
179
|
-
const { createBaseService } = createCrudServiceFromResource({
|
|
180
|
-
namespace: "contacts"
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
assert.throws(
|
|
184
|
-
() => createBaseService({}),
|
|
185
|
-
/requires repository/
|
|
186
|
-
);
|
|
187
|
-
|
|
188
|
-
const service = createBaseService({
|
|
189
|
-
repository: createRepositoryDouble({
|
|
190
|
-
async create() {
|
|
191
|
-
return null;
|
|
192
|
-
}
|
|
193
|
-
})
|
|
194
|
-
});
|
|
195
|
-
await assert.rejects(
|
|
196
|
-
() => service.createRecord({}, {}),
|
|
197
|
-
/contactsService could not load the created record/
|
|
198
|
-
);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
test("createCrudServiceFromResource readable field hooks require view output schema", async () => {
|
|
202
|
-
const { createBaseService } = createCrudServiceFromResource({
|
|
203
|
-
namespace: "contacts"
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
const service = createBaseService({
|
|
207
|
-
repository: createRepositoryDouble(),
|
|
208
|
-
fieldAccess: {
|
|
209
|
-
readable: () => ["id"]
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
await assert.rejects(
|
|
214
|
-
() => service.getRecord(1, {}),
|
|
215
|
-
/requires resource\.operations\.view\.output for fieldAccess\.readable/
|
|
216
|
-
);
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
test("createCrudServiceFromResource enforces writable field access hooks", async () => {
|
|
220
|
-
const createCalls = [];
|
|
221
|
-
const { createBaseService } = createCrudServiceFromResource(createResourceWithOutputSchema());
|
|
222
|
-
|
|
223
|
-
const service = createBaseService({
|
|
224
|
-
repository: createRepositoryDouble({
|
|
225
|
-
async create(payload) {
|
|
226
|
-
createCalls.push(payload);
|
|
227
|
-
return { id: 2, ...payload };
|
|
228
|
-
}
|
|
229
|
-
}),
|
|
230
|
-
fieldAccess: {
|
|
231
|
-
writable: () => ["name"]
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
await assert.rejects(
|
|
236
|
-
() => service.createRecord({ name: "A", optionalSecret: "hidden" }, {}),
|
|
237
|
-
(error) => error?.status === 403 && /Write access denied for fields: optionalSecret/.test(error?.message || "")
|
|
238
|
-
);
|
|
239
|
-
assert.equal(createCalls.length, 0);
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
test("createCrudServiceFromResource supports writable field strip mode", async () => {
|
|
243
|
-
const createCalls = [];
|
|
244
|
-
const { createBaseService } = createCrudServiceFromResource(createResourceWithOutputSchema());
|
|
245
|
-
|
|
246
|
-
const service = createBaseService({
|
|
247
|
-
repository: createRepositoryDouble({
|
|
248
|
-
async create(payload) {
|
|
249
|
-
createCalls.push(payload);
|
|
250
|
-
return { id: 2, ...payload, nullableSecret: "x", defaultedSecret: "y" };
|
|
251
|
-
}
|
|
252
|
-
}),
|
|
253
|
-
fieldAccess: {
|
|
254
|
-
writable: () => ["name"],
|
|
255
|
-
writeMode: "strip"
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
await service.createRecord({ name: "A", optionalSecret: "hidden" }, {});
|
|
260
|
-
assert.deepEqual(createCalls, [{ name: "A" }]);
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
test("createCrudServiceFromResource applies readable field access hooks with drop/null/default redaction", async () => {
|
|
264
|
-
const { createBaseService } = createCrudServiceFromResource(createResourceWithOutputSchema());
|
|
265
|
-
|
|
266
|
-
const service = createBaseService({
|
|
267
|
-
repository: createRepositoryDouble({
|
|
268
|
-
async findById() {
|
|
269
|
-
return {
|
|
270
|
-
id: 1,
|
|
271
|
-
name: "A",
|
|
272
|
-
optionalSecret: "drop-me",
|
|
273
|
-
nullableSecret: "redact-to-null",
|
|
274
|
-
defaultedSecret: "redact-to-default"
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
}),
|
|
278
|
-
fieldAccess: {
|
|
279
|
-
readable: () => ["id", "name"]
|
|
280
|
-
}
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
const record = await service.getRecord(1, {});
|
|
284
|
-
assert.deepEqual(record, {
|
|
285
|
-
id: 1,
|
|
286
|
-
name: "A",
|
|
287
|
-
nullableSecret: null,
|
|
288
|
-
defaultedSecret: ""
|
|
289
|
-
});
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
test("createCrudServiceFromResource readable filtering fails fast for required non-nullable fields without defaults", async () => {
|
|
293
|
-
const { createBaseService } = createCrudServiceFromResource({
|
|
294
|
-
namespace: "contacts",
|
|
295
|
-
operations: {
|
|
296
|
-
view: {
|
|
297
|
-
output: {
|
|
298
|
-
schema: createSchema({
|
|
299
|
-
id: { type: "integer", required: true },
|
|
300
|
-
strictSecret: { type: "string", required: true }
|
|
301
|
-
}),
|
|
302
|
-
mode: "replace"
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
const service = createBaseService({
|
|
309
|
-
repository: createRepositoryDouble({
|
|
310
|
-
async findById() {
|
|
311
|
-
return { id: 1, strictSecret: "value" };
|
|
312
|
-
}
|
|
313
|
-
}),
|
|
314
|
-
fieldAccess: {
|
|
315
|
-
readable: () => ["id"]
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
await assert.rejects(
|
|
320
|
-
() => service.getRecord(1, {}),
|
|
321
|
-
/cannot redact required non-nullable field "strictSecret" without a default value/
|
|
322
|
-
);
|
|
323
|
-
});
|