@zenstackhq/runtime 0.3.10 → 0.3.12
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 → LICENSE} +0 -0
- package/client/index.d.ts +4 -0
- package/client/index.js +7 -0
- package/lib/client.d.ts +3 -0
- package/lib/client.js +34 -0
- package/lib/client.js.map +1 -0
- package/lib/config.d.ts +14 -0
- package/lib/config.js +3 -0
- package/lib/config.js.map +1 -0
- package/lib/constants.d.ts +12 -0
- package/lib/constants.js +16 -0
- package/lib/constants.js.map +1 -0
- package/lib/handler/data/handler.d.ts +19 -0
- package/lib/handler/data/handler.js +306 -0
- package/lib/handler/data/handler.js.map +1 -0
- package/lib/handler/data/nested-write-vistor.d.ts +31 -0
- package/lib/handler/data/nested-write-vistor.js +67 -0
- package/lib/handler/data/nested-write-vistor.js.map +1 -0
- package/lib/handler/data/policy-utils.d.ts +73 -0
- package/lib/handler/data/policy-utils.js +477 -0
- package/lib/handler/data/policy-utils.js.map +1 -0
- package/lib/handler/index.d.ts +1 -0
- package/lib/handler/index.js +9 -0
- package/lib/handler/index.js.map +1 -0
- package/lib/handler/types.d.ts +28 -0
- package/lib/handler/types.js +36 -0
- package/lib/handler/types.js.map +1 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +22 -0
- package/lib/index.js.map +1 -0
- package/lib/request-handler.d.ts +21 -0
- package/lib/request-handler.js +37 -0
- package/lib/request-handler.js.map +1 -0
- package/lib/request.d.ts +16 -0
- package/lib/request.js +171 -0
- package/lib/request.js.map +1 -0
- package/lib/service.d.ts +32 -0
- package/lib/service.js +185 -0
- package/lib/service.js.map +1 -0
- package/lib/types.d.ts +168 -0
- package/lib/types.js +59 -0
- package/lib/types.js.map +1 -0
- package/lib/validation.d.ts +12 -0
- package/lib/validation.js +26 -0
- package/lib/validation.js.map +1 -0
- package/package.json +34 -8
- package/{auth.d.ts → server/auth.d.ts} +0 -0
- package/{auth.js → server/auth.js} +0 -0
- package/server/index.d.ts +16 -0
- package/server/index.js +7 -0
- package/{types.d.ts → types/index.d.ts} +0 -0
- package/{types.js → types/index.js} +0 -0
- package/README.md +0 -5
- package/client.d.ts +0 -1
- package/client.js +0 -3
- package/hooks.d.ts +0 -10
- package/hooks.js +0 -3
- package/index.d.ts +0 -3
- package/index.js +0 -1
- package/server.d.ts +0 -1
- package/server.js +0 -3
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.preprocessWritePayload = exports.injectTransactionId = exports.checkPolicyForIds = exports.preUpdateCheck = exports.readWithCheck = exports.queryIds = exports.ensureArray = exports.or = exports.and = void 0;
|
|
16
|
+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
17
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
18
|
+
const bcryptjs_1 = require("bcryptjs");
|
|
19
|
+
const deepcopy_1 = __importDefault(require("deepcopy"));
|
|
20
|
+
const constants_1 = require("../../constants");
|
|
21
|
+
const types_1 = require("../../types");
|
|
22
|
+
const types_2 = require("../types");
|
|
23
|
+
const nested_write_vistor_1 = require("./nested-write-vistor");
|
|
24
|
+
//#region General helpers
|
|
25
|
+
/**
|
|
26
|
+
* Creates a conjunction of a list of query conditions.
|
|
27
|
+
*/
|
|
28
|
+
function and(...conditions) {
|
|
29
|
+
const filtered = conditions.filter((c) => !!c);
|
|
30
|
+
if (filtered.length === 0) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
else if (filtered.length === 1) {
|
|
34
|
+
return filtered[0];
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return { AND: filtered };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.and = and;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a disjunction of a list of query conditions.
|
|
43
|
+
*/
|
|
44
|
+
function or(...conditions) {
|
|
45
|
+
const filtered = conditions.filter((c) => !!c);
|
|
46
|
+
if (filtered.length === 0) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
else if (filtered.length === 1) {
|
|
50
|
+
return filtered[0];
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return { OR: filtered };
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.or = or;
|
|
57
|
+
/**
|
|
58
|
+
* Wraps a value into array if it's not already one
|
|
59
|
+
*/
|
|
60
|
+
function ensureArray(value) {
|
|
61
|
+
return Array.isArray(value) ? value : [value];
|
|
62
|
+
}
|
|
63
|
+
exports.ensureArray = ensureArray;
|
|
64
|
+
/**
|
|
65
|
+
* Given a where condition, queries db and returns IDs of result entities
|
|
66
|
+
*/
|
|
67
|
+
function queryIds(model, db, where) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
const r = yield db[model].findMany({ select: { id: true }, where });
|
|
70
|
+
return r.map((item) => item.id);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
exports.queryIds = queryIds;
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region Policy enforcement helpers
|
|
76
|
+
/**
|
|
77
|
+
* Read model entities w.r.t the given query args. The result list
|
|
78
|
+
* are guaranteed to fully satisfy 'read' policy rules recursively.
|
|
79
|
+
*
|
|
80
|
+
* For to-many relations involved, items not satisfying policy are
|
|
81
|
+
* silently trimmed. For to-one relation, if relation data fails policy
|
|
82
|
+
* an RequestHandlerError is thrown.
|
|
83
|
+
*
|
|
84
|
+
* @param model the model to query for
|
|
85
|
+
* @param queryArgs the Prisma query args
|
|
86
|
+
* @param service the ZenStack service
|
|
87
|
+
* @param context the query context
|
|
88
|
+
* @param db the db (or transaction)
|
|
89
|
+
* @returns
|
|
90
|
+
*/
|
|
91
|
+
function readWithCheck(model, queryArgs, service, context, db) {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const args = (0, deepcopy_1.default)(queryArgs);
|
|
94
|
+
args.where = and(args.where, yield service.buildQueryGuard(model, 'read', context));
|
|
95
|
+
// recursively inject read guard conditions into the query args
|
|
96
|
+
yield injectNestedReadConditions(model, args, service, context);
|
|
97
|
+
service.verbose(`Reading with validation for ${model}: ${JSON.stringify(args)}`);
|
|
98
|
+
const result = yield db[model].findMany(args);
|
|
99
|
+
yield Promise.all(result.map((item) => postProcessForRead(item, model, args, service, context, db, 'read')));
|
|
100
|
+
return result;
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
exports.readWithCheck = readWithCheck;
|
|
104
|
+
function injectNestedReadConditions(model, args, service, context) {
|
|
105
|
+
var _a, _b, _c, _d;
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
const injectTarget = (_a = args.select) !== null && _a !== void 0 ? _a : args.include;
|
|
108
|
+
if (!injectTarget) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
for (const field of Object.keys(injectTarget)) {
|
|
112
|
+
const fieldInfo = yield service.resolveField(model, field);
|
|
113
|
+
if (!fieldInfo || !fieldInfo.isDataModel) {
|
|
114
|
+
// only care about relation fields
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (fieldInfo.isArray) {
|
|
118
|
+
if (typeof injectTarget[field] !== 'object') {
|
|
119
|
+
injectTarget[field] = {};
|
|
120
|
+
}
|
|
121
|
+
// inject extra condition for to-many relation
|
|
122
|
+
injectTarget[field].where = and(injectTarget.where, yield service.buildQueryGuard(fieldInfo.type, 'read', context));
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
// there's no way of injecting condition for to-one relation, so we
|
|
126
|
+
// make sure 'id' field is selected and check them against query result
|
|
127
|
+
if (((_b = injectTarget[field]) === null || _b === void 0 ? void 0 : _b.select) &&
|
|
128
|
+
((_d = (_c = injectTarget[field]) === null || _c === void 0 ? void 0 : _c.select) === null || _d === void 0 ? void 0 : _d.id) !== true) {
|
|
129
|
+
injectTarget[field].select.id = true;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// recurse
|
|
133
|
+
yield injectNestedReadConditions(fieldInfo.type, injectTarget[field], service, context);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Post processing checks for read model entities.
|
|
139
|
+
* Validates to-one relations (which can't be trimmed
|
|
140
|
+
* at query time) and removes fields that should be
|
|
141
|
+
* omitted.
|
|
142
|
+
*/
|
|
143
|
+
function postProcessForRead(entityData, model, args, service, context, db, operation) {
|
|
144
|
+
var _a, _b;
|
|
145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
+
if (!(entityData === null || entityData === void 0 ? void 0 : entityData.id)) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
for (const field of Object.keys(entityData)) {
|
|
150
|
+
if (yield shouldOmit(service, model, field)) {
|
|
151
|
+
delete entityData[field];
|
|
152
|
+
}
|
|
153
|
+
const fieldValue = entityData[field];
|
|
154
|
+
if (typeof fieldValue === 'bigint') {
|
|
155
|
+
// serialize BigInt with typing info
|
|
156
|
+
entityData[field] = {
|
|
157
|
+
type: 'BigInt',
|
|
158
|
+
data: fieldValue.toString(),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (fieldValue instanceof Date) {
|
|
162
|
+
// serialize Date with typing info
|
|
163
|
+
entityData[field] = {
|
|
164
|
+
type: 'Date',
|
|
165
|
+
data: fieldValue.toISOString(),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
if (typeof fieldValue === 'object') {
|
|
169
|
+
const fieldInfo = yield service.resolveField(model, field);
|
|
170
|
+
if ((fieldInfo === null || fieldInfo === void 0 ? void 0 : fieldInfo.type) === 'Decimal') {
|
|
171
|
+
// serialize Decimal with typing info
|
|
172
|
+
entityData[field] = {
|
|
173
|
+
type: 'Decimal',
|
|
174
|
+
data: fieldValue.toString(),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
else if ((fieldInfo === null || fieldInfo === void 0 ? void 0 : fieldInfo.type) === 'Bytes') {
|
|
178
|
+
entityData[field] = {
|
|
179
|
+
type: 'Bytes',
|
|
180
|
+
data: Array.from(fieldValue),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const injectTarget = (_a = args.select) !== null && _a !== void 0 ? _a : args.include;
|
|
186
|
+
if (!injectTarget) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
// to-one relation data cannot be trimmed by injected guards, we have to
|
|
190
|
+
// post-check them
|
|
191
|
+
for (const field of Object.keys(injectTarget)) {
|
|
192
|
+
const fieldInfo = yield service.resolveField(model, field);
|
|
193
|
+
if (!fieldInfo ||
|
|
194
|
+
!fieldInfo.isDataModel ||
|
|
195
|
+
fieldInfo.isArray ||
|
|
196
|
+
!((_b = entityData === null || entityData === void 0 ? void 0 : entityData[field]) === null || _b === void 0 ? void 0 : _b.id)) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
service.verbose(`Validating read of to-one relation: ${fieldInfo.type}#${entityData[field].id}`);
|
|
200
|
+
yield checkPolicyForIds(fieldInfo.type, [entityData[field].id], operation, service, context, db);
|
|
201
|
+
// recurse
|
|
202
|
+
yield postProcessForRead(entityData[field], fieldInfo.type, injectTarget[field], service, context, db, operation);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Validates that a model entity satisfies 'update' policy rules
|
|
208
|
+
* before conducting an update
|
|
209
|
+
*
|
|
210
|
+
* @param model model under update
|
|
211
|
+
* @param id id of entity under update
|
|
212
|
+
* @param updateArgs Prisma update args
|
|
213
|
+
* @param service the ZenStack service
|
|
214
|
+
* @param context the query context
|
|
215
|
+
* @param transaction the db transaction context
|
|
216
|
+
*/
|
|
217
|
+
function preUpdateCheck(model, id, updateArgs, service, context, transaction) {
|
|
218
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
219
|
+
// check the entity directly under update first
|
|
220
|
+
yield checkPolicyForIds(model, [id], 'update', service, context, transaction);
|
|
221
|
+
// We need to ensure that all nested updates respect policy rules of
|
|
222
|
+
// the corresponding model.
|
|
223
|
+
//
|
|
224
|
+
// Here we use a visitor to collect all necessary
|
|
225
|
+
// checkes. During visiting, for every update we meet agains a relation,
|
|
226
|
+
// we collect its path (starting from the root object). If the relation
|
|
227
|
+
// is a to-many one, it can carry filter condition that we collect as well.
|
|
228
|
+
//
|
|
229
|
+
// After the visiting, we validate that each collected path satisfies
|
|
230
|
+
// corresponding policy rules by making separate queries.
|
|
231
|
+
const visitor = new nested_write_vistor_1.NestedWriteVisitor(service);
|
|
232
|
+
const state = [];
|
|
233
|
+
const checks = [];
|
|
234
|
+
const visitAction = (fieldInfo, action, fieldData, _parentData, state) => __awaiter(this, void 0, void 0, function* () {
|
|
235
|
+
if (!fieldInfo.isDataModel) {
|
|
236
|
+
return state;
|
|
237
|
+
}
|
|
238
|
+
if (![
|
|
239
|
+
'update',
|
|
240
|
+
'updateMany',
|
|
241
|
+
'upsert',
|
|
242
|
+
'delete',
|
|
243
|
+
'deleteMany',
|
|
244
|
+
].includes(action)) {
|
|
245
|
+
// no more nested writes inside, stop recursion
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
// for to-many relation, a filter condition can be attached
|
|
249
|
+
let condition = undefined;
|
|
250
|
+
if (fieldInfo.isArray) {
|
|
251
|
+
switch (action) {
|
|
252
|
+
case 'update':
|
|
253
|
+
case 'updateMany':
|
|
254
|
+
case 'upsert':
|
|
255
|
+
// condition is wrapped in 'where'
|
|
256
|
+
condition = or(...ensureArray(fieldData).map((d) => d.where));
|
|
257
|
+
break;
|
|
258
|
+
case 'delete':
|
|
259
|
+
case 'deleteMany':
|
|
260
|
+
// condition is not wrapped
|
|
261
|
+
condition = or(...ensureArray(fieldData));
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
// build up a new segment of path
|
|
266
|
+
const selectionPath = [
|
|
267
|
+
...state,
|
|
268
|
+
{ field: fieldInfo, where: condition },
|
|
269
|
+
];
|
|
270
|
+
const operation = [
|
|
271
|
+
'update',
|
|
272
|
+
'updateMany',
|
|
273
|
+
'upsert',
|
|
274
|
+
].includes(action)
|
|
275
|
+
? 'update'
|
|
276
|
+
: 'delete';
|
|
277
|
+
// collect an asynchronous check action
|
|
278
|
+
checks.push(checkPolicyForSelectionPath(model, id, selectionPath, operation, transaction, service, context));
|
|
279
|
+
// recurse down with the current path as the new state
|
|
280
|
+
return selectionPath;
|
|
281
|
+
});
|
|
282
|
+
yield visitor.visit(model, updateArgs.data, undefined, state, visitAction);
|
|
283
|
+
yield Promise.all(checks);
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
exports.preUpdateCheck = preUpdateCheck;
|
|
287
|
+
/**
|
|
288
|
+
* Given a list of ids for a model, check if they all match policy rules, and if not,
|
|
289
|
+
* throw a RequestHandlerError.
|
|
290
|
+
*
|
|
291
|
+
* @param model the model
|
|
292
|
+
* @param ids the entity ids
|
|
293
|
+
* @param operation the operation to check for
|
|
294
|
+
* @param service the ZenStack service
|
|
295
|
+
* @param context the query context
|
|
296
|
+
* @param db the db or transaction
|
|
297
|
+
*/
|
|
298
|
+
function checkPolicyForIds(model, ids, operation, service, context, db) {
|
|
299
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
300
|
+
service.verbose(`Checking policy for ${model}#[${ids.join(', ')}] for ${operation}`);
|
|
301
|
+
// build a query condition with policy injected
|
|
302
|
+
const idCondition = ids.length > 1 ? { id: { in: ids } } : { id: ids[0] };
|
|
303
|
+
const query = {
|
|
304
|
+
where: and(idCondition, yield service.buildQueryGuard(model, operation, context)),
|
|
305
|
+
select: { id: true },
|
|
306
|
+
};
|
|
307
|
+
// query with policy injected
|
|
308
|
+
const filteredResult = (yield db[model].findMany(query));
|
|
309
|
+
// see if we get fewer items with policy, if so, reject with an throw
|
|
310
|
+
const filteredIds = filteredResult.map((item) => item.id);
|
|
311
|
+
if (filteredIds.length < ids.length) {
|
|
312
|
+
const gap = ids.filter((id) => !filteredIds.includes(id));
|
|
313
|
+
throw new types_2.RequestHandlerError(types_1.ServerErrorCode.DENIED_BY_POLICY, `denied by policy: entities failed '${operation}' check, ${model}#[${gap.join(', ')}]`);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
exports.checkPolicyForIds = checkPolicyForIds;
|
|
318
|
+
/**
|
|
319
|
+
* Given a selection path, check if the entities at the end of path satisfy
|
|
320
|
+
* policy rules. If not, throw an error.
|
|
321
|
+
*/
|
|
322
|
+
function checkPolicyForSelectionPath(model, id, selectionPath, operation, db, service, context) {
|
|
323
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
324
|
+
const targetField = selectionPath[selectionPath.length - 1].field;
|
|
325
|
+
// build a Prisma query for the path
|
|
326
|
+
const query = buildChainedSelectQuery(id, selectionPath);
|
|
327
|
+
service.verbose(`Query for selection path: model ${model}, path ${JSON.stringify(selectionPath)}, query ${JSON.stringify(query)}`);
|
|
328
|
+
const r = yield db[model].findUnique(query);
|
|
329
|
+
// collect ids at the end of the path
|
|
330
|
+
const ids = collectTerminalEntityIds(selectionPath, r);
|
|
331
|
+
service.verbose(`Collected leaf ids: ${JSON.stringify(ids)}`);
|
|
332
|
+
if (ids.length === 0) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
// check policies for the collected ids
|
|
336
|
+
yield checkPolicyForIds(targetField.type, ids, operation, service, context, db);
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Builds a Prisma query for the given selection path
|
|
341
|
+
*/
|
|
342
|
+
function buildChainedSelectQuery(id, selectionPath) {
|
|
343
|
+
const query = { where: { id }, select: { id: true } };
|
|
344
|
+
let currSelect = query.select;
|
|
345
|
+
for (const path of selectionPath) {
|
|
346
|
+
const nextSelect = { select: { id: true } };
|
|
347
|
+
currSelect[path.field.name] = nextSelect;
|
|
348
|
+
if (path.where) {
|
|
349
|
+
currSelect[path.field.name].where = path.where;
|
|
350
|
+
}
|
|
351
|
+
currSelect = nextSelect.select;
|
|
352
|
+
}
|
|
353
|
+
return query;
|
|
354
|
+
}
|
|
355
|
+
function collectTerminalEntityIds(selectionPath, data) {
|
|
356
|
+
let curr = data;
|
|
357
|
+
for (const path of selectionPath) {
|
|
358
|
+
curr = curr[path.field.name];
|
|
359
|
+
}
|
|
360
|
+
if (!curr) {
|
|
361
|
+
throw new types_2.RequestHandlerError(types_1.ServerErrorCode.UNKNOWN, 'an unexpected error occurred');
|
|
362
|
+
}
|
|
363
|
+
return Array.isArray(curr)
|
|
364
|
+
? curr.map((item) => item.id)
|
|
365
|
+
: [curr.id];
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Injects assignment of zenstack_transaction field for all nested
|
|
369
|
+
* update/create in a Prisma update args recursively.
|
|
370
|
+
*
|
|
371
|
+
* @return a tuple containing all model types that are involved in
|
|
372
|
+
* creation or updating, respectively
|
|
373
|
+
*/
|
|
374
|
+
function injectTransactionId(model, args, operation, transactionId, service) {
|
|
375
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
376
|
+
const updatedModels = new Set();
|
|
377
|
+
const createdModels = new Set();
|
|
378
|
+
if (args.data) {
|
|
379
|
+
args.data[constants_1.TRANSACTION_FIELD_NAME] = `${transactionId}:${operation}`;
|
|
380
|
+
updatedModels.add(model);
|
|
381
|
+
}
|
|
382
|
+
const visitAction = (fieldInfo, action, fieldData) => __awaiter(this, void 0, void 0, function* () {
|
|
383
|
+
if (fieldInfo.isDataModel && fieldData) {
|
|
384
|
+
switch (action) {
|
|
385
|
+
case 'update':
|
|
386
|
+
case 'updateMany':
|
|
387
|
+
ensureArray(fieldData).forEach((item) => {
|
|
388
|
+
if (fieldInfo.isArray && item.data) {
|
|
389
|
+
item.data[constants_1.TRANSACTION_FIELD_NAME] = `${transactionId}:update`;
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
item[constants_1.TRANSACTION_FIELD_NAME] = `${transactionId}:update`;
|
|
393
|
+
}
|
|
394
|
+
updatedModels.add(fieldInfo.type);
|
|
395
|
+
});
|
|
396
|
+
break;
|
|
397
|
+
case 'upsert':
|
|
398
|
+
ensureArray(fieldData).forEach((item) => {
|
|
399
|
+
item.create[constants_1.TRANSACTION_FIELD_NAME] = `${transactionId}:create`;
|
|
400
|
+
createdModels.add(fieldInfo.type);
|
|
401
|
+
item.update[constants_1.TRANSACTION_FIELD_NAME] = `${transactionId}:update`;
|
|
402
|
+
updatedModels.add(fieldInfo.type);
|
|
403
|
+
});
|
|
404
|
+
break;
|
|
405
|
+
case 'create':
|
|
406
|
+
case 'createMany':
|
|
407
|
+
ensureArray(fieldData).forEach((item) => {
|
|
408
|
+
item[constants_1.TRANSACTION_FIELD_NAME] = `${transactionId}:create`;
|
|
409
|
+
createdModels.add(fieldInfo.type);
|
|
410
|
+
});
|
|
411
|
+
break;
|
|
412
|
+
case 'connectOrCreate':
|
|
413
|
+
ensureArray(fieldData).forEach((item) => {
|
|
414
|
+
item.create[constants_1.TRANSACTION_FIELD_NAME] = `${transactionId}:create`;
|
|
415
|
+
createdModels.add(fieldInfo.type);
|
|
416
|
+
});
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return true;
|
|
421
|
+
});
|
|
422
|
+
const visitor = new nested_write_vistor_1.NestedWriteVisitor(service);
|
|
423
|
+
yield visitor.visit(model, args.data, undefined, undefined, visitAction);
|
|
424
|
+
return {
|
|
425
|
+
createdModels: Array.from(createdModels),
|
|
426
|
+
updatedModels: Array.from(updatedModels),
|
|
427
|
+
};
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
exports.injectTransactionId = injectTransactionId;
|
|
431
|
+
/**
|
|
432
|
+
* Preprocesses the given write args to modify field values (in place) based on
|
|
433
|
+
* attributes like @password
|
|
434
|
+
*/
|
|
435
|
+
function preprocessWritePayload(model, args, service) {
|
|
436
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
437
|
+
const visitAction = (fieldInfo, _action, fieldData, parentData) => __awaiter(this, void 0, void 0, function* () {
|
|
438
|
+
var _a, _b, _c;
|
|
439
|
+
// process @password field
|
|
440
|
+
const pwdAttr = (_a = fieldInfo.attributes) === null || _a === void 0 ? void 0 : _a.find((attr) => attr.name === '@password');
|
|
441
|
+
if (pwdAttr && fieldInfo.type === 'String') {
|
|
442
|
+
// hash password value
|
|
443
|
+
let salt = (_b = pwdAttr.args.find((arg) => arg.name === 'salt')) === null || _b === void 0 ? void 0 : _b.value;
|
|
444
|
+
if (!salt) {
|
|
445
|
+
salt = (_c = pwdAttr.args.find((arg) => arg.name === 'saltLength')) === null || _c === void 0 ? void 0 : _c.value;
|
|
446
|
+
}
|
|
447
|
+
if (!salt) {
|
|
448
|
+
salt = constants_1.DEFAULT_PASSWORD_SALT_LENGTH;
|
|
449
|
+
}
|
|
450
|
+
parentData[fieldInfo.name] = (0, bcryptjs_1.hashSync)(fieldData, salt);
|
|
451
|
+
}
|
|
452
|
+
// deserialize Buffer field
|
|
453
|
+
if (fieldInfo.type === 'Bytes' && Array.isArray(fieldData.data)) {
|
|
454
|
+
parentData[fieldInfo.name] = Buffer.from(fieldData.data);
|
|
455
|
+
}
|
|
456
|
+
// deserialize BigInt field
|
|
457
|
+
if (fieldInfo.type === 'BigInt' && typeof fieldData === 'string') {
|
|
458
|
+
parentData[fieldInfo.name] = BigInt(fieldData);
|
|
459
|
+
}
|
|
460
|
+
return true;
|
|
461
|
+
});
|
|
462
|
+
const visitor = new nested_write_vistor_1.NestedWriteVisitor(service);
|
|
463
|
+
yield visitor.visit(model, args.data, undefined, undefined, visitAction);
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
exports.preprocessWritePayload = preprocessWritePayload;
|
|
467
|
+
function shouldOmit(service, model, field) {
|
|
468
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
469
|
+
if ([constants_1.TRANSACTION_FIELD_NAME, constants_1.GUARD_FIELD_NAME].includes(field)) {
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
const fieldInfo = yield service.resolveField(model, field);
|
|
473
|
+
return !!(fieldInfo && fieldInfo.attributes.find((attr) => attr.name === '@omit'));
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
//#endregion
|
|
477
|
+
//# sourceMappingURL=policy-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy-utils.js","sourceRoot":"","sources":["../../../../src/handler/data/policy-utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,sEAAsE;AACtE,uDAAuD;AACvD,uCAAoC;AACpC,wDAAgC;AAChC,+CAIyB;AACzB,uCAOqB;AACrB,oCAAsE;AACtE,+DAA2D;AAE3D,yBAAyB;AAEzB;;GAEG;AACH,SAAgB,GAAG,CAAC,GAAG,UAAqB;IACxC,MAAM,QAAQ,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/C,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAA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,IAAI,wCAAkB,CAAC,OAAO,CAAC,CAAC;QAEhD,MAAM,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,CAAC,CAAC;IAC7E,CAAC;CAAA;AA9CD,wDA8CC;AAED,SAAe,UAAU,CAAC,OAAgB,EAAE,KAAa,EAAE,KAAa;;QACpE,IAAI,CAAC,kCAAsB,EAAE,4BAAgB,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE;YAC5D,OAAO,IAAI,CAAC;SACf;QACD,MAAM,SAAS,GAAG,MAAM,OAAO,CAAC,YAAY,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAC3D,OAAO,CAAC,CAAC,CACL,SAAS,IAAI,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,OAAO,CAAC,CAC1E,CAAC;IACN,CAAC;CAAA;AAED,YAAY"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as DataHandler } from './data/handler';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DataHandler = void 0;
|
|
7
|
+
var handler_1 = require("./data/handler");
|
|
8
|
+
Object.defineProperty(exports, "DataHandler", { enumerable: true, get: function () { return __importDefault(handler_1).default; } });
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/handler/index.ts"],"names":[],"mappings":";;;;;;AAAA,0CAAwD;AAA/C,uHAAA,OAAO,OAAe"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { NextApiRequest, NextApiResponse } from 'next';
|
|
2
|
+
import { ServerErrorCode } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Defines contract for a Next.js API endpoint handler.
|
|
5
|
+
*/
|
|
6
|
+
export interface RequestHandler {
|
|
7
|
+
/**
|
|
8
|
+
* Handles a request for a given route path.
|
|
9
|
+
*
|
|
10
|
+
* @param req The request
|
|
11
|
+
* @param res The response
|
|
12
|
+
* @param path The route path (with /api/zenstack prefix removed)
|
|
13
|
+
*/
|
|
14
|
+
handle(req: NextApiRequest, res: NextApiResponse, path: string[]): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Error thrown during request handling
|
|
18
|
+
*/
|
|
19
|
+
export declare class RequestHandlerError extends Error {
|
|
20
|
+
readonly code: ServerErrorCode;
|
|
21
|
+
constructor(code: ServerErrorCode, message?: string);
|
|
22
|
+
toString(): string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* All write actions supported by Prisma
|
|
26
|
+
*/
|
|
27
|
+
export declare const PrismaWriteActions: readonly ["create", "createMany", "connectOrCreate", "update", "updateMany", "upsert", "delete", "deleteMany", "connect", "none"];
|
|
28
|
+
export type PrismaWriteActionType = typeof PrismaWriteActions[number];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PrismaWriteActions = exports.RequestHandlerError = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
/**
|
|
6
|
+
* Error thrown during request handling
|
|
7
|
+
*/
|
|
8
|
+
class RequestHandlerError extends Error {
|
|
9
|
+
constructor(code, message) {
|
|
10
|
+
message = message
|
|
11
|
+
? `${(0, types_1.getServerErrorMessage)(code)}: ${message}`
|
|
12
|
+
: (0, types_1.getServerErrorMessage)(code);
|
|
13
|
+
super(message);
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
toString() {
|
|
17
|
+
return `Request handler error: ${this.code}, ${this.message}`;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.RequestHandlerError = RequestHandlerError;
|
|
21
|
+
/**
|
|
22
|
+
* All write actions supported by Prisma
|
|
23
|
+
*/
|
|
24
|
+
exports.PrismaWriteActions = [
|
|
25
|
+
'create',
|
|
26
|
+
'createMany',
|
|
27
|
+
'connectOrCreate',
|
|
28
|
+
'update',
|
|
29
|
+
'updateMany',
|
|
30
|
+
'upsert',
|
|
31
|
+
'delete',
|
|
32
|
+
'deleteMany',
|
|
33
|
+
'connect',
|
|
34
|
+
'none',
|
|
35
|
+
];
|
|
36
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/handler/types.ts"],"names":[],"mappings":";;;AACA,oCAAkE;AAoBlE;;GAEG;AACH,MAAa,mBAAoB,SAAQ,KAAK;IAC1C,YAA4B,IAAqB,EAAE,OAAgB;QAC/D,OAAO,GAAG,OAAO;YACb,CAAC,CAAC,GAAG,IAAA,6BAAqB,EAAC,IAAI,CAAC,KAAK,OAAO,EAAE;YAC9C,CAAC,CAAC,IAAA,6BAAqB,EAAC,IAAI,CAAC,CAAC;QAClC,KAAK,CAAC,OAAO,CAAC,CAAC;QAJS,SAAI,GAAJ,IAAI,CAAiB;IAKjD,CAAC;IAED,QAAQ;QACJ,OAAO,0BAA0B,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC;IAClE,CAAC;CACJ;AAXD,kDAWC;AAED;;GAEG;AACU,QAAA,kBAAkB,GAAG;IAC9B,QAAQ;IACR,YAAY;IACZ,iBAAiB;IACjB,QAAQ;IACR,YAAY;IACZ,QAAQ;IACR,QAAQ;IACR,YAAY;IACZ,SAAS;IACT,MAAM;CACA,CAAC"}
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types"), exports);
|
|
18
|
+
__exportStar(require("./config"), exports);
|
|
19
|
+
__exportStar(require("./service"), exports);
|
|
20
|
+
__exportStar(require("./request-handler"), exports);
|
|
21
|
+
__exportStar(require("./validation"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,4CAA0B;AAC1B,oDAAkC;AAClC,+CAA6B"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { NextApiRequest, NextApiResponse } from 'next';
|
|
2
|
+
import { AuthUser, Service } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Options for initializing a Next.js API endpoint request handler. This type is re-exported in @zenstackhq/runtime/server.
|
|
5
|
+
* @see requestHandler
|
|
6
|
+
*/
|
|
7
|
+
export type RequestHandlerOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* Hook method for providing current login user from session.
|
|
10
|
+
*/
|
|
11
|
+
getServerUser: (req: NextApiRequest, res: NextApiResponse) => Promise<AuthUser | undefined>;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Creates a Next.js API endpoint request handler which encapsulates RESTful APIs generated by ZenStack.
|
|
15
|
+
* The created handler should be mounted at /api/zenstack endpoint.
|
|
16
|
+
*
|
|
17
|
+
* @param service ZenStack service which wraps a Prisma db client inside
|
|
18
|
+
* @param options Options for initialization
|
|
19
|
+
* @returns An API endpoint request handler
|
|
20
|
+
*/
|
|
21
|
+
export declare function requestHandler<DbClient>(service: Service<DbClient>, options: RequestHandlerOptions): (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
|