@aeriajs/core 0.0.277 → 0.0.278
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/dist/__scripts__/postinstall.js +2 -37
- package/dist/accessControl.js +14 -18
- package/dist/assets.js +25 -31
- package/dist/collection/cascadingRemove.js +14 -19
- package/dist/collection/define.js +5 -10
- package/dist/collection/description.js +2 -7
- package/dist/collection/index.js +8 -24
- package/dist/collection/makePagination.js +5 -9
- package/dist/collection/normalizeProjection.js +1 -5
- package/dist/collection/preload.js +13 -18
- package/dist/collection/reference.js +19 -26
- package/dist/collection/traverseDocument.js +90 -127
- package/dist/context.js +16 -53
- package/dist/database.js +12 -52
- package/dist/endpoints.js +11 -48
- package/dist/functions/count.js +11 -15
- package/dist/functions/get.js +20 -24
- package/dist/functions/getAll.js +8 -12
- package/dist/functions/index.js +9 -27
- package/dist/functions/insert.js +19 -23
- package/dist/functions/remove.js +15 -19
- package/dist/functions/removeAll.js +11 -15
- package/dist/functions/removeFile.js +7 -11
- package/dist/functions/unpaginatedGetAll.js +15 -19
- package/dist/functions/upload.js +19 -57
- package/dist/index.js +11 -51
- package/dist/presets/add.js +1 -4
- package/dist/presets/crud.js +1 -4
- package/dist/presets/duplicate.js +1 -4
- package/dist/presets/index.js +17 -20
- package/dist/presets/owned.js +1 -4
- package/dist/presets/remove.js +1 -4
- package/dist/presets/removeAll.js +1 -4
- package/dist/presets/timestamped.js +1 -4
- package/dist/presets/view.js +1 -4
- package/dist/token.js +7 -15
- package/package.json +12 -17
- package/dist/__scripts__/postinstall.mjs +0 -50
- package/dist/accessControl.mjs +0 -31
- package/dist/assets.mjs +0 -67
- package/dist/collection/cascadingRemove.mjs +0 -71
- package/dist/collection/define.mjs +0 -13
- package/dist/collection/description.mjs +0 -8
- package/dist/collection/index.mjs +0 -9
- package/dist/collection/makePagination.mjs +0 -20
- package/dist/collection/normalizeProjection.mjs +0 -17
- package/dist/collection/preload.mjs +0 -88
- package/dist/collection/reference.mjs +0 -374
- package/dist/collection/traverseDocument.mjs +0 -454
- package/dist/context.mjs +0 -120
- package/dist/database.mjs +0 -49
- package/dist/endpoints.mjs +0 -52
- package/dist/functions/count.mjs +0 -50
- package/dist/functions/get.mjs +0 -89
- package/dist/functions/getAll.mjs +0 -14
- package/dist/functions/index.mjs +0 -12
- package/dist/functions/insert.mjs +0 -102
- package/dist/functions/remove.mjs +0 -41
- package/dist/functions/removeAll.mjs +0 -40
- package/dist/functions/removeFile.mjs +0 -29
- package/dist/functions/unpaginatedGetAll.mjs +0 -123
- package/dist/functions/upload.mjs +0 -91
- package/dist/index.mjs +0 -14
- package/dist/presets/add.mjs +0 -12
- package/dist/presets/crud.mjs +0 -35
- package/dist/presets/duplicate.mjs +0 -11
- package/dist/presets/index.mjs +0 -19
- package/dist/presets/owned.mjs +0 -9
- package/dist/presets/remove.mjs +0 -11
- package/dist/presets/removeAll.mjs +0 -11
- package/dist/presets/timestamped.mjs +0 -19
- package/dist/presets/view.mjs +0 -14
- package/dist/token.mjs +0 -31
|
@@ -1,51 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.traverseDocument = void 0;
|
|
37
|
-
const path = __importStar(require("node:path"));
|
|
38
|
-
const fs = __importStar(require("node:fs/promises"));
|
|
39
|
-
const types_1 = require("@aeriajs/types");
|
|
40
|
-
const common_1 = require("@aeriajs/common");
|
|
41
|
-
const validation_1 = require("@aeriajs/validation");
|
|
42
|
-
const entrypoint_1 = require("@aeriajs/entrypoint");
|
|
43
|
-
const mongodb_1 = require("mongodb");
|
|
44
|
-
const assets_js_1 = require("../assets.js");
|
|
45
|
-
const context_js_1 = require("../context.js");
|
|
46
|
-
const preload_js_1 = require("./preload.js");
|
|
47
|
-
const reference_js_1 = require("./reference.js");
|
|
48
|
-
const cascadingRemove_js_1 = require("./cascadingRemove.js");
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
import * as fs from 'node:fs/promises';
|
|
3
|
+
import { Result, ACError, ValidationErrorCode, TraverseError } from '@aeriajs/types';
|
|
4
|
+
import { throwIfError, isReference, getReferenceProperty, getValueFromPath } from '@aeriajs/common';
|
|
5
|
+
import { makeValidationError, validatePropertyWithRefs, validateWholeness } from '@aeriajs/validation';
|
|
6
|
+
import { getCollection } from '@aeriajs/entrypoint';
|
|
7
|
+
import { ObjectId } from 'mongodb';
|
|
8
|
+
import { getCollectionAsset } from '../assets.js';
|
|
9
|
+
import { createContext } from '../context.js';
|
|
10
|
+
import { preloadDescription } from './preload.js';
|
|
11
|
+
import { getReferences } from './reference.js';
|
|
12
|
+
import { preferredRemove } from './cascadingRemove.js';
|
|
49
13
|
const escapeRegExp = (text) => {
|
|
50
14
|
return text.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
|
|
51
15
|
};
|
|
@@ -67,25 +31,25 @@ const getProperty = (propName, parentProperty) => {
|
|
|
67
31
|
};
|
|
68
32
|
const cleanupReferences = async (value, ctx) => {
|
|
69
33
|
if (ctx.root._id) {
|
|
70
|
-
const refProperty =
|
|
34
|
+
const refProperty = getReferenceProperty(ctx.property);
|
|
71
35
|
if (refProperty && (refProperty.$ref === 'file' || refProperty.inline)) {
|
|
72
36
|
if (ctx.isArray && !Array.isArray(value)) {
|
|
73
|
-
return
|
|
37
|
+
return Result.result(value);
|
|
74
38
|
}
|
|
75
39
|
const context = ctx.options.context;
|
|
76
40
|
const doc = await context.collections[ctx.options.description.$id].model.findOne({
|
|
77
|
-
_id: new
|
|
41
|
+
_id: new ObjectId(ctx.root._id),
|
|
78
42
|
}, {
|
|
79
43
|
projection: {
|
|
80
44
|
[ctx.propPath]: 1,
|
|
81
45
|
},
|
|
82
46
|
});
|
|
83
47
|
if (!doc) {
|
|
84
|
-
return
|
|
48
|
+
return Result.error(TraverseError.InvalidDocumentId);
|
|
85
49
|
}
|
|
86
|
-
let referenceIds =
|
|
50
|
+
let referenceIds = getValueFromPath(doc, ctx.propPath);
|
|
87
51
|
if (!referenceIds) {
|
|
88
|
-
return
|
|
52
|
+
return Result.result(value);
|
|
89
53
|
}
|
|
90
54
|
if (Array.isArray(referenceIds)) {
|
|
91
55
|
if (!Array.isArray(value)) {
|
|
@@ -95,29 +59,29 @@ const cleanupReferences = async (value, ctx) => {
|
|
|
95
59
|
}
|
|
96
60
|
else {
|
|
97
61
|
if (referenceIds.equals(value)) {
|
|
98
|
-
return
|
|
62
|
+
return Result.result(value);
|
|
99
63
|
}
|
|
100
64
|
}
|
|
101
|
-
const refMap = await
|
|
65
|
+
const refMap = await getReferences({
|
|
102
66
|
[ctx.propName]: ctx.property,
|
|
103
67
|
});
|
|
104
|
-
await
|
|
68
|
+
await preferredRemove(referenceIds, refMap[ctx.propName], await createContext({
|
|
105
69
|
parentContext: context,
|
|
106
70
|
collectionName: refProperty.$ref,
|
|
107
71
|
}));
|
|
108
72
|
}
|
|
109
73
|
}
|
|
110
|
-
return
|
|
74
|
+
return Result.result(value);
|
|
111
75
|
};
|
|
112
76
|
const autoCast = (value, ctx) => {
|
|
113
77
|
switch (typeof value) {
|
|
114
78
|
case 'boolean': {
|
|
115
|
-
return
|
|
79
|
+
return Result.result(!!value);
|
|
116
80
|
}
|
|
117
81
|
case 'string': {
|
|
118
|
-
if (
|
|
119
|
-
return
|
|
120
|
-
? new
|
|
82
|
+
if (isReference(ctx.property)) {
|
|
83
|
+
return Result.result(ObjectId.isValid(value)
|
|
84
|
+
? new ObjectId(value)
|
|
121
85
|
: value);
|
|
122
86
|
}
|
|
123
87
|
if ('format' in ctx.property) {
|
|
@@ -125,7 +89,7 @@ const autoCast = (value, ctx) => {
|
|
|
125
89
|
case 'date':
|
|
126
90
|
case 'date-time': {
|
|
127
91
|
const timestamp = Date.parse(value);
|
|
128
|
-
return
|
|
92
|
+
return Result.result(!Number.isNaN(timestamp)
|
|
129
93
|
? new Date(timestamp)
|
|
130
94
|
: null);
|
|
131
95
|
}
|
|
@@ -135,22 +99,22 @@ const autoCast = (value, ctx) => {
|
|
|
135
99
|
}
|
|
136
100
|
case 'number': {
|
|
137
101
|
if ('type' in ctx.property && ctx.property.type === 'integer') {
|
|
138
|
-
return
|
|
102
|
+
return Result.result(parseInt(value.toString()));
|
|
139
103
|
}
|
|
140
104
|
if ('format' in ctx.property) {
|
|
141
105
|
if (ctx.property.format === 'date' || ctx.property.format === 'date-time') {
|
|
142
|
-
return
|
|
106
|
+
return Result.result(new Date(value));
|
|
143
107
|
}
|
|
144
108
|
}
|
|
145
109
|
break;
|
|
146
110
|
}
|
|
147
111
|
case 'object': {
|
|
148
|
-
if (!value || value instanceof
|
|
149
|
-
return
|
|
112
|
+
if (!value || value instanceof ObjectId) {
|
|
113
|
+
return Result.result(value);
|
|
150
114
|
}
|
|
151
115
|
if (!('description' in ctx.options) || !ctx.options.recurseDeep) {
|
|
152
116
|
if (Array.isArray(value)) {
|
|
153
|
-
return
|
|
117
|
+
return Result.result(value.map((v) => throwIfError(autoCast(v, ctx))));
|
|
154
118
|
}
|
|
155
119
|
if (Object.keys(value).length > 0) {
|
|
156
120
|
const entries = {};
|
|
@@ -161,17 +125,17 @@ const autoCast = (value, ctx) => {
|
|
|
161
125
|
if (!subProperty) {
|
|
162
126
|
continue;
|
|
163
127
|
}
|
|
164
|
-
entries[k] =
|
|
128
|
+
entries[k] = throwIfError(autoCast(v, {
|
|
165
129
|
...ctx,
|
|
166
130
|
property: subProperty,
|
|
167
131
|
}));
|
|
168
132
|
}
|
|
169
|
-
return
|
|
133
|
+
return Result.result(entries);
|
|
170
134
|
}
|
|
171
135
|
}
|
|
172
136
|
}
|
|
173
137
|
}
|
|
174
|
-
return
|
|
138
|
+
return Result.result(value);
|
|
175
139
|
};
|
|
176
140
|
const getters = async (value, ctx) => {
|
|
177
141
|
if ('getter' in ctx.property) {
|
|
@@ -179,95 +143,95 @@ const getters = async (value, ctx) => {
|
|
|
179
143
|
throw new Error;
|
|
180
144
|
}
|
|
181
145
|
if (!ctx.property.getter) {
|
|
182
|
-
return
|
|
146
|
+
return Result.result(undefined);
|
|
183
147
|
}
|
|
184
|
-
return
|
|
148
|
+
return Result.result(await ctx.property.getter(ctx.target, ctx.options.context));
|
|
185
149
|
}
|
|
186
|
-
return
|
|
150
|
+
return Result.result(value);
|
|
187
151
|
};
|
|
188
152
|
const validate = async (value, ctx) => {
|
|
189
153
|
if (ctx.options.recurseDeep) {
|
|
190
154
|
if ('properties' in ctx.property) {
|
|
191
|
-
return
|
|
155
|
+
return Result.result(value);
|
|
192
156
|
}
|
|
193
157
|
}
|
|
194
158
|
if ('$ref' in ctx.property && ctx.property.$ref === 'file') {
|
|
195
|
-
return
|
|
159
|
+
return Result.result(value);
|
|
196
160
|
}
|
|
197
|
-
const { error } = await
|
|
161
|
+
const { error } = await validatePropertyWithRefs(value, ctx.property, {
|
|
198
162
|
checkObjectIds: true,
|
|
199
163
|
context: ctx.options.context,
|
|
200
|
-
objectIdConstructor:
|
|
164
|
+
objectIdConstructor: ObjectId,
|
|
201
165
|
});
|
|
202
166
|
if (error) {
|
|
203
|
-
return
|
|
167
|
+
return Result.error({
|
|
204
168
|
[ctx.propName]: error,
|
|
205
169
|
});
|
|
206
170
|
}
|
|
207
|
-
return
|
|
171
|
+
return Result.result(value);
|
|
208
172
|
};
|
|
209
173
|
const isValidTempFile = (value) => {
|
|
210
174
|
if (value && typeof value === 'object') {
|
|
211
|
-
if (value instanceof
|
|
175
|
+
if (value instanceof ObjectId) {
|
|
212
176
|
return true;
|
|
213
177
|
}
|
|
214
|
-
return 'tempId' in value && (typeof value.tempId === 'string' || value.tempId instanceof
|
|
178
|
+
return 'tempId' in value && (typeof value.tempId === 'string' || value.tempId instanceof ObjectId);
|
|
215
179
|
}
|
|
216
180
|
return !!(value === undefined
|
|
217
181
|
|| value === null);
|
|
218
182
|
};
|
|
219
183
|
const isMissingPropertyError = (error) => {
|
|
220
|
-
return 'code' in error && error.code ===
|
|
184
|
+
return 'code' in error && error.code === ValidationErrorCode.MissingProperties;
|
|
221
185
|
};
|
|
222
186
|
const moveFiles = async (value, ctx) => {
|
|
223
187
|
if (!('$ref' in ctx.property) || ctx.property.$ref !== 'file') {
|
|
224
|
-
return
|
|
188
|
+
return Result.result(value);
|
|
225
189
|
}
|
|
226
|
-
const tempFileCollection = await
|
|
190
|
+
const tempFileCollection = await getCollection('tempFile');
|
|
227
191
|
if (!tempFileCollection) {
|
|
228
192
|
throw new Error('The "tempFile" collection is absent, yet it is required to upload files.');
|
|
229
193
|
}
|
|
230
194
|
if (!isValidTempFile(value)) {
|
|
231
|
-
return
|
|
195
|
+
return Result.error(TraverseError.InvalidTempfile);
|
|
232
196
|
}
|
|
233
197
|
if (!value) {
|
|
234
|
-
return
|
|
198
|
+
return Result.result(null);
|
|
235
199
|
}
|
|
236
|
-
if (value instanceof
|
|
237
|
-
return
|
|
200
|
+
if (value instanceof ObjectId) {
|
|
201
|
+
return Result.result(value);
|
|
238
202
|
}
|
|
239
203
|
if (!ctx.options.context) {
|
|
240
204
|
throw new Error();
|
|
241
205
|
}
|
|
242
206
|
const tempFile = await ctx.options.context.collections.tempFile.model.findOne({
|
|
243
|
-
_id: new
|
|
207
|
+
_id: new ObjectId(value.tempId),
|
|
244
208
|
});
|
|
245
209
|
if (!tempFile) {
|
|
246
|
-
return
|
|
210
|
+
return Result.error(TraverseError.InvalidTempfile);
|
|
247
211
|
}
|
|
248
212
|
const { _id: fileId, ...newFile } = tempFile;
|
|
249
213
|
newFile.absolute_path = `${ctx.options.context.config.storage.fs}/${tempFile.absolute_path.split(path.sep).at(-1)}`;
|
|
250
214
|
newFile.owner = ctx.options.context.token.sub;
|
|
251
215
|
await fs.rename(tempFile.absolute_path, newFile.absolute_path);
|
|
252
216
|
const file = await ctx.options.context.collections.file.model.insertOne(newFile);
|
|
253
|
-
return
|
|
217
|
+
return Result.result(file.insertedId);
|
|
254
218
|
};
|
|
255
219
|
const recurseDeep = async (value, ctx) => {
|
|
256
220
|
if (!value) {
|
|
257
|
-
return
|
|
221
|
+
return Result.result(value);
|
|
258
222
|
}
|
|
259
223
|
if ('properties' in ctx.property) {
|
|
260
224
|
if (ctx.options.validateWholeness) {
|
|
261
|
-
const wholenessError =
|
|
225
|
+
const wholenessError = validateWholeness(value, ctx.property);
|
|
262
226
|
if (wholenessError) {
|
|
263
|
-
return
|
|
227
|
+
return Result.error(wholenessError);
|
|
264
228
|
}
|
|
265
229
|
}
|
|
266
230
|
return recurse(value, ctx);
|
|
267
231
|
}
|
|
268
232
|
if ('items' in ctx.property) {
|
|
269
233
|
if (!Array.isArray(value)) {
|
|
270
|
-
return
|
|
234
|
+
return Result.result(value);
|
|
271
235
|
}
|
|
272
236
|
const items = [];
|
|
273
237
|
for (const item of value) {
|
|
@@ -280,9 +244,9 @@ const recurseDeep = async (value, ctx) => {
|
|
|
280
244
|
items.push(result);
|
|
281
245
|
}
|
|
282
246
|
}
|
|
283
|
-
return
|
|
247
|
+
return Result.result(items);
|
|
284
248
|
}
|
|
285
|
-
return
|
|
249
|
+
return Result.result(value);
|
|
286
250
|
};
|
|
287
251
|
const recurse = async (target, ctx) => {
|
|
288
252
|
const entries = {};
|
|
@@ -298,7 +262,7 @@ const recurse = async (target, ctx) => {
|
|
|
298
262
|
if (propName === '_id') {
|
|
299
263
|
if (value) {
|
|
300
264
|
if (ctx.options.autoCast) {
|
|
301
|
-
entries[propName] =
|
|
265
|
+
entries[propName] = throwIfError(autoCast(value, {
|
|
302
266
|
...ctx,
|
|
303
267
|
target,
|
|
304
268
|
propName,
|
|
@@ -323,7 +287,7 @@ const recurse = async (target, ctx) => {
|
|
|
323
287
|
for (const key in value) {
|
|
324
288
|
if (key.startsWith('$')) {
|
|
325
289
|
if (!ctx.options.allowOperators) {
|
|
326
|
-
return
|
|
290
|
+
return Result.error(ACError.InsecureOperator);
|
|
327
291
|
}
|
|
328
292
|
if (key === '$regex' && typeof value[key] === 'string') {
|
|
329
293
|
if (!ctx.options.noRegExpEscaping) {
|
|
@@ -344,7 +308,7 @@ const recurse = async (target, ctx) => {
|
|
|
344
308
|
for (const operation of value) {
|
|
345
309
|
const { error, result } = await recurse(operation, ctx);
|
|
346
310
|
if (error) {
|
|
347
|
-
return
|
|
311
|
+
return Result.error(error);
|
|
348
312
|
}
|
|
349
313
|
operations.push(result);
|
|
350
314
|
}
|
|
@@ -353,7 +317,7 @@ const recurse = async (target, ctx) => {
|
|
|
353
317
|
}
|
|
354
318
|
const { error, result: operator } = await recurse(value, ctx);
|
|
355
319
|
if (error) {
|
|
356
|
-
return
|
|
320
|
+
return Result.error(error);
|
|
357
321
|
}
|
|
358
322
|
entries[propName] = operator;
|
|
359
323
|
continue;
|
|
@@ -376,31 +340,31 @@ const recurse = async (target, ctx) => {
|
|
|
376
340
|
const propCast = 'items' in property
|
|
377
341
|
? property.items
|
|
378
342
|
: property;
|
|
379
|
-
if ('$ref' in propCast && value && !(value instanceof
|
|
380
|
-
const targetDescription = await
|
|
343
|
+
if ('$ref' in propCast && value && !(value instanceof ObjectId)) {
|
|
344
|
+
const targetDescription = await preloadDescription(throwIfError(await getCollectionAsset(propCast.$ref, 'description')));
|
|
381
345
|
if (Array.isArray(value)) {
|
|
382
346
|
const documents = [];
|
|
383
347
|
for (const elem of value) {
|
|
384
|
-
if (elem instanceof
|
|
348
|
+
if (elem instanceof ObjectId) {
|
|
385
349
|
documents.push(elem);
|
|
386
350
|
continue;
|
|
387
351
|
}
|
|
388
352
|
if (typeof elem === 'string') {
|
|
389
|
-
documents.push(new
|
|
353
|
+
documents.push(new ObjectId(elem));
|
|
390
354
|
continue;
|
|
391
355
|
}
|
|
392
|
-
const { error, result } = await
|
|
356
|
+
const { error, result } = await traverseDocument(elem, targetDescription, ctx.options);
|
|
393
357
|
if (error) {
|
|
394
|
-
return
|
|
358
|
+
return Result.error(error);
|
|
395
359
|
}
|
|
396
360
|
documents.push(result);
|
|
397
361
|
}
|
|
398
362
|
entries[propName] = documents;
|
|
399
363
|
continue;
|
|
400
364
|
}
|
|
401
|
-
const { error, result: doc } = await
|
|
365
|
+
const { error, result: doc } = await traverseDocument(value, targetDescription, ctx.options);
|
|
402
366
|
if (error) {
|
|
403
|
-
return
|
|
367
|
+
return Result.error(error);
|
|
404
368
|
}
|
|
405
369
|
entries[propName] = doc;
|
|
406
370
|
continue;
|
|
@@ -420,11 +384,11 @@ const recurse = async (target, ctx) => {
|
|
|
420
384
|
}
|
|
421
385
|
}
|
|
422
386
|
}
|
|
423
|
-
return
|
|
387
|
+
return Result.result(entries);
|
|
424
388
|
};
|
|
425
|
-
const traverseDocument = async (what, description, _options) => {
|
|
389
|
+
export const traverseDocument = async (what, description, _options) => {
|
|
426
390
|
if (!what) {
|
|
427
|
-
return
|
|
391
|
+
return Result.result(what);
|
|
428
392
|
}
|
|
429
393
|
const whatCopy = Object.assign({}, what);
|
|
430
394
|
const options = Object.assign({
|
|
@@ -432,7 +396,7 @@ const traverseDocument = async (what, description, _options) => {
|
|
|
432
396
|
}, _options);
|
|
433
397
|
const functions = [];
|
|
434
398
|
if (!options.validate && Object.keys(whatCopy).length === 0) {
|
|
435
|
-
return
|
|
399
|
+
return Result.result(whatCopy);
|
|
436
400
|
}
|
|
437
401
|
if (options.recurseDeep) {
|
|
438
402
|
functions.push(recurseDeep);
|
|
@@ -445,9 +409,9 @@ const traverseDocument = async (what, description, _options) => {
|
|
|
445
409
|
}
|
|
446
410
|
if (options.validate) {
|
|
447
411
|
if (options.validateWholeness === true) {
|
|
448
|
-
const wholenessError =
|
|
412
|
+
const wholenessError = validateWholeness(whatCopy, options.description);
|
|
449
413
|
if (wholenessError) {
|
|
450
|
-
return
|
|
414
|
+
return Result.error(wholenessError);
|
|
451
415
|
}
|
|
452
416
|
}
|
|
453
417
|
functions.push(validate);
|
|
@@ -466,19 +430,19 @@ const traverseDocument = async (what, description, _options) => {
|
|
|
466
430
|
if (error) {
|
|
467
431
|
const narrowedError = error;
|
|
468
432
|
switch (narrowedError) {
|
|
469
|
-
case
|
|
470
|
-
case
|
|
433
|
+
case TraverseError.InvalidDocumentId:
|
|
434
|
+
case TraverseError.InvalidTempfile:
|
|
471
435
|
traverseError = narrowedError;
|
|
472
436
|
break;
|
|
473
437
|
default: {
|
|
474
438
|
validationError = narrowedError;
|
|
475
439
|
}
|
|
476
440
|
}
|
|
477
|
-
return
|
|
441
|
+
return Result.error(error);
|
|
478
442
|
}
|
|
479
443
|
value = ctx.target[ctx.propName] = result;
|
|
480
444
|
}
|
|
481
|
-
return
|
|
445
|
+
return Result.result(value);
|
|
482
446
|
};
|
|
483
447
|
const { error, result } = await recurse(whatCopy, {
|
|
484
448
|
root: whatCopy,
|
|
@@ -487,20 +451,19 @@ const traverseDocument = async (what, description, _options) => {
|
|
|
487
451
|
options,
|
|
488
452
|
});
|
|
489
453
|
if (error) {
|
|
490
|
-
return
|
|
454
|
+
return Result.error(error);
|
|
491
455
|
}
|
|
492
456
|
if (traverseError) {
|
|
493
|
-
return
|
|
457
|
+
return Result.error(traverseError);
|
|
494
458
|
}
|
|
495
459
|
if (validationError) {
|
|
496
460
|
if (isMissingPropertyError(validationError)) {
|
|
497
|
-
return
|
|
461
|
+
return Result.error(validationError);
|
|
498
462
|
}
|
|
499
|
-
return
|
|
500
|
-
code:
|
|
463
|
+
return Result.error(makeValidationError({
|
|
464
|
+
code: ValidationErrorCode.InvalidProperties,
|
|
501
465
|
details: validationError,
|
|
502
466
|
}));
|
|
503
467
|
}
|
|
504
|
-
return
|
|
468
|
+
return Result.result(result);
|
|
505
469
|
};
|
|
506
|
-
exports.traverseDocument = traverseDocument;
|
package/dist/context.js
CHANGED
|
@@ -1,44 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.createContext = void 0;
|
|
37
|
-
const common_1 = require("@aeriajs/common");
|
|
38
|
-
const entrypoint_1 = require("@aeriajs/entrypoint");
|
|
39
|
-
const security_1 = require("@aeriajs/security");
|
|
40
|
-
const database_js_1 = require("./database.js");
|
|
41
|
-
const preload_js_1 = require("./collection/preload.js");
|
|
1
|
+
import { throwIfError, endpointError } from '@aeriajs/common';
|
|
2
|
+
import { getCollections } from '@aeriajs/entrypoint';
|
|
3
|
+
import { limitRate } from '@aeriajs/security';
|
|
4
|
+
import { getDatabaseCollection } from './database.js';
|
|
5
|
+
import { preloadDescription } from './collection/preload.js';
|
|
42
6
|
const indepthCollection = (collectionName, collections, parentContext) => {
|
|
43
7
|
const candidate = collections[collectionName];
|
|
44
8
|
const collection = typeof candidate === 'function'
|
|
@@ -54,7 +18,7 @@ const indepthCollection = (collectionName, collections, parentContext) => {
|
|
|
54
18
|
return null;
|
|
55
19
|
}
|
|
56
20
|
return async (props, ...args) => {
|
|
57
|
-
const childContext = await
|
|
21
|
+
const childContext = await createContext({
|
|
58
22
|
parentContext,
|
|
59
23
|
collectionName,
|
|
60
24
|
inherited: true,
|
|
@@ -65,26 +29,26 @@ const indepthCollection = (collectionName, collections, parentContext) => {
|
|
|
65
29
|
});
|
|
66
30
|
return {
|
|
67
31
|
...collection,
|
|
68
|
-
context: () =>
|
|
32
|
+
context: () => createContext({
|
|
69
33
|
parentContext,
|
|
70
34
|
collectionName,
|
|
71
35
|
}),
|
|
72
36
|
functions: proxiedFunctions,
|
|
73
37
|
originalFunctions: collection.functions,
|
|
74
|
-
model:
|
|
38
|
+
model: getDatabaseCollection(collectionName),
|
|
75
39
|
};
|
|
76
40
|
};
|
|
77
41
|
const isCollectionContext = (_context, collectionName) => {
|
|
78
42
|
return !!collectionName;
|
|
79
43
|
};
|
|
80
|
-
const createContext = async (_options) => {
|
|
44
|
+
export const createContext = async (_options) => {
|
|
81
45
|
const options = _options || {};
|
|
82
46
|
const { collectionName, parentContext, token = parentContext?.token || {
|
|
83
47
|
authenticated: false,
|
|
84
48
|
sub: null,
|
|
85
49
|
}, } = options;
|
|
86
|
-
const { getCollectionAsset } = await
|
|
87
|
-
const collections = await
|
|
50
|
+
const { getCollectionAsset } = await import('./assets.js');
|
|
51
|
+
const collections = await getCollections();
|
|
88
52
|
let config;
|
|
89
53
|
if (options.config) {
|
|
90
54
|
config = options.config;
|
|
@@ -124,7 +88,7 @@ const createContext = async (_options) => {
|
|
|
124
88
|
},
|
|
125
89
|
}),
|
|
126
90
|
log: async (message, details) => {
|
|
127
|
-
return
|
|
91
|
+
return getDatabaseCollection('log').insertOne({
|
|
128
92
|
message,
|
|
129
93
|
details,
|
|
130
94
|
context: collectionName,
|
|
@@ -133,17 +97,17 @@ const createContext = async (_options) => {
|
|
|
133
97
|
});
|
|
134
98
|
},
|
|
135
99
|
error: (httpStatus, error) => {
|
|
136
|
-
return
|
|
100
|
+
return endpointError(Object.assign({
|
|
137
101
|
httpStatus,
|
|
138
102
|
}, error));
|
|
139
103
|
},
|
|
140
104
|
limitRate: (params) => {
|
|
141
|
-
return
|
|
105
|
+
return limitRate(params, context);
|
|
142
106
|
},
|
|
143
107
|
};
|
|
144
108
|
if (isCollectionContext(context, collectionName) && collectionName) {
|
|
145
|
-
const description =
|
|
146
|
-
context.description = await
|
|
109
|
+
const description = throwIfError(await getCollectionAsset(collectionName, 'description'));
|
|
110
|
+
context.description = await preloadDescription(description);
|
|
147
111
|
context.collectionName = collectionName;
|
|
148
112
|
if (!options.calledFunction && parentContext && 'calledFunction' in parentContext) {
|
|
149
113
|
context.calledFunction = parentContext.calledFunction;
|
|
@@ -155,4 +119,3 @@ const createContext = async (_options) => {
|
|
|
155
119
|
}
|
|
156
120
|
return context;
|
|
157
121
|
};
|
|
158
|
-
exports.createContext = createContext;
|