@aeriajs/core 0.0.277 → 0.0.279

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/__scripts__/postinstall.js +2 -37
  2. package/dist/accessControl.js +14 -18
  3. package/dist/assets.js +25 -31
  4. package/dist/collection/cascadingRemove.js +14 -19
  5. package/dist/collection/define.js +5 -10
  6. package/dist/collection/description.js +2 -7
  7. package/dist/collection/index.js +8 -24
  8. package/dist/collection/makePagination.js +5 -9
  9. package/dist/collection/normalizeProjection.js +1 -5
  10. package/dist/collection/preload.js +13 -18
  11. package/dist/collection/reference.js +19 -26
  12. package/dist/collection/traverseDocument.js +90 -127
  13. package/dist/context.js +16 -53
  14. package/dist/database.js +12 -52
  15. package/dist/endpoints.js +11 -48
  16. package/dist/functions/count.js +11 -15
  17. package/dist/functions/get.js +20 -24
  18. package/dist/functions/getAll.js +8 -12
  19. package/dist/functions/index.js +9 -27
  20. package/dist/functions/insert.js +19 -23
  21. package/dist/functions/remove.js +15 -19
  22. package/dist/functions/removeAll.js +11 -15
  23. package/dist/functions/removeFile.js +7 -11
  24. package/dist/functions/unpaginatedGetAll.js +15 -19
  25. package/dist/functions/upload.js +19 -57
  26. package/dist/index.js +11 -51
  27. package/dist/presets/add.js +1 -4
  28. package/dist/presets/crud.js +1 -4
  29. package/dist/presets/duplicate.js +1 -4
  30. package/dist/presets/index.js +17 -20
  31. package/dist/presets/owned.js +1 -4
  32. package/dist/presets/remove.js +1 -4
  33. package/dist/presets/removeAll.js +1 -4
  34. package/dist/presets/timestamped.js +1 -4
  35. package/dist/presets/view.js +1 -4
  36. package/dist/token.js +7 -15
  37. package/package.json +12 -17
  38. package/dist/__scripts__/postinstall.mjs +0 -50
  39. package/dist/accessControl.mjs +0 -31
  40. package/dist/assets.mjs +0 -67
  41. package/dist/collection/cascadingRemove.mjs +0 -71
  42. package/dist/collection/define.mjs +0 -13
  43. package/dist/collection/description.mjs +0 -8
  44. package/dist/collection/index.mjs +0 -9
  45. package/dist/collection/makePagination.mjs +0 -20
  46. package/dist/collection/normalizeProjection.mjs +0 -17
  47. package/dist/collection/preload.mjs +0 -88
  48. package/dist/collection/reference.mjs +0 -374
  49. package/dist/collection/traverseDocument.mjs +0 -454
  50. package/dist/context.mjs +0 -120
  51. package/dist/database.mjs +0 -49
  52. package/dist/endpoints.mjs +0 -52
  53. package/dist/functions/count.mjs +0 -50
  54. package/dist/functions/get.mjs +0 -89
  55. package/dist/functions/getAll.mjs +0 -14
  56. package/dist/functions/index.mjs +0 -12
  57. package/dist/functions/insert.mjs +0 -102
  58. package/dist/functions/remove.mjs +0 -41
  59. package/dist/functions/removeAll.mjs +0 -40
  60. package/dist/functions/removeFile.mjs +0 -29
  61. package/dist/functions/unpaginatedGetAll.mjs +0 -123
  62. package/dist/functions/upload.mjs +0 -91
  63. package/dist/index.mjs +0 -14
  64. package/dist/presets/add.mjs +0 -12
  65. package/dist/presets/crud.mjs +0 -35
  66. package/dist/presets/duplicate.mjs +0 -11
  67. package/dist/presets/index.mjs +0 -19
  68. package/dist/presets/owned.mjs +0 -9
  69. package/dist/presets/remove.mjs +0 -11
  70. package/dist/presets/removeAll.mjs +0 -11
  71. package/dist/presets/timestamped.mjs +0 -19
  72. package/dist/presets/view.mjs +0 -14
  73. package/dist/token.mjs +0 -31
@@ -1,51 +1,15 @@
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 __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 = (0, common_1.getReferenceProperty)(ctx.property);
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 types_1.Result.result(value);
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 mongodb_1.ObjectId(ctx.root._id),
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 types_1.Result.error(types_1.TraverseError.InvalidDocumentId);
48
+ return Result.error(TraverseError.InvalidDocumentId);
85
49
  }
86
- let referenceIds = (0, common_1.getValueFromPath)(doc, ctx.propPath);
50
+ let referenceIds = getValueFromPath(doc, ctx.propPath);
87
51
  if (!referenceIds) {
88
- return types_1.Result.result(value);
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 types_1.Result.result(value);
62
+ return Result.result(value);
99
63
  }
100
64
  }
101
- const refMap = await (0, reference_js_1.getReferences)({
65
+ const refMap = await getReferences({
102
66
  [ctx.propName]: ctx.property,
103
67
  });
104
- await (0, cascadingRemove_js_1.preferredRemove)(referenceIds, refMap[ctx.propName], await (0, context_js_1.createContext)({
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 types_1.Result.result(value);
74
+ return Result.result(value);
111
75
  };
112
76
  const autoCast = (value, ctx) => {
113
77
  switch (typeof value) {
114
78
  case 'boolean': {
115
- return types_1.Result.result(!!value);
79
+ return Result.result(!!value);
116
80
  }
117
81
  case 'string': {
118
- if ((0, common_1.isReference)(ctx.property)) {
119
- return types_1.Result.result(mongodb_1.ObjectId.isValid(value)
120
- ? new mongodb_1.ObjectId(value)
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 types_1.Result.result(!Number.isNaN(timestamp)
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 types_1.Result.result(parseInt(value.toString()));
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 types_1.Result.result(new Date(value));
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 mongodb_1.ObjectId) {
149
- return types_1.Result.result(value);
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 types_1.Result.result(value.map((v) => (0, common_1.throwIfError)(autoCast(v, ctx))));
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] = (0, common_1.throwIfError)(autoCast(v, {
128
+ entries[k] = throwIfError(autoCast(v, {
165
129
  ...ctx,
166
130
  property: subProperty,
167
131
  }));
168
132
  }
169
- return types_1.Result.result(entries);
133
+ return Result.result(entries);
170
134
  }
171
135
  }
172
136
  }
173
137
  }
174
- return types_1.Result.result(value);
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 types_1.Result.result(undefined);
146
+ return Result.result(undefined);
183
147
  }
184
- return types_1.Result.result(await ctx.property.getter(ctx.target, ctx.options.context));
148
+ return Result.result(await ctx.property.getter(ctx.target, ctx.options.context));
185
149
  }
186
- return types_1.Result.result(value);
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 types_1.Result.result(value);
155
+ return Result.result(value);
192
156
  }
193
157
  }
194
158
  if ('$ref' in ctx.property && ctx.property.$ref === 'file') {
195
- return types_1.Result.result(value);
159
+ return Result.result(value);
196
160
  }
197
- const { error } = await (0, validation_1.validatePropertyWithRefs)(value, ctx.property, {
161
+ const { error } = await validatePropertyWithRefs(value, ctx.property, {
198
162
  checkObjectIds: true,
199
163
  context: ctx.options.context,
200
- objectIdConstructor: mongodb_1.ObjectId,
164
+ objectIdConstructor: ObjectId,
201
165
  });
202
166
  if (error) {
203
- return types_1.Result.error({
167
+ return Result.error({
204
168
  [ctx.propName]: error,
205
169
  });
206
170
  }
207
- return types_1.Result.result(value);
171
+ return Result.result(value);
208
172
  };
209
173
  const isValidTempFile = (value) => {
210
174
  if (value && typeof value === 'object') {
211
- if (value instanceof mongodb_1.ObjectId) {
175
+ if (value instanceof ObjectId) {
212
176
  return true;
213
177
  }
214
- return 'tempId' in value && (typeof value.tempId === 'string' || value.tempId instanceof mongodb_1.ObjectId);
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 === types_1.ValidationErrorCode.MissingProperties;
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 types_1.Result.result(value);
188
+ return Result.result(value);
225
189
  }
226
- const tempFileCollection = await (0, entrypoint_1.getCollection)('tempFile');
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 types_1.Result.error(types_1.TraverseError.InvalidTempfile);
195
+ return Result.error(TraverseError.InvalidTempfile);
232
196
  }
233
197
  if (!value) {
234
- return types_1.Result.result(null);
198
+ return Result.result(null);
235
199
  }
236
- if (value instanceof mongodb_1.ObjectId) {
237
- return types_1.Result.result(value);
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 mongodb_1.ObjectId(value.tempId),
207
+ _id: new ObjectId(value.tempId),
244
208
  });
245
209
  if (!tempFile) {
246
- return types_1.Result.error(types_1.TraverseError.InvalidTempfile);
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 types_1.Result.result(file.insertedId);
217
+ return Result.result(file.insertedId);
254
218
  };
255
219
  const recurseDeep = async (value, ctx) => {
256
220
  if (!value) {
257
- return types_1.Result.result(value);
221
+ return Result.result(value);
258
222
  }
259
223
  if ('properties' in ctx.property) {
260
224
  if (ctx.options.validateWholeness) {
261
- const wholenessError = (0, validation_1.validateWholeness)(value, ctx.property);
225
+ const wholenessError = validateWholeness(value, ctx.property);
262
226
  if (wholenessError) {
263
- return types_1.Result.error(wholenessError);
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 types_1.Result.result(value);
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 types_1.Result.result(items);
247
+ return Result.result(items);
284
248
  }
285
- return types_1.Result.result(value);
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] = (0, common_1.throwIfError)(autoCast(value, {
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 types_1.Result.error(types_1.ACError.InsecureOperator);
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 types_1.Result.error(error);
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 types_1.Result.error(error);
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 mongodb_1.ObjectId)) {
380
- const targetDescription = await (0, preload_js_1.preloadDescription)((0, common_1.throwIfError)(await (0, assets_js_1.getCollectionAsset)(propCast.$ref, 'description')));
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 mongodb_1.ObjectId) {
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 mongodb_1.ObjectId(elem));
353
+ documents.push(new ObjectId(elem));
390
354
  continue;
391
355
  }
392
- const { error, result } = await (0, exports.traverseDocument)(elem, targetDescription, ctx.options);
356
+ const { error, result } = await traverseDocument(elem, targetDescription, ctx.options);
393
357
  if (error) {
394
- return types_1.Result.error(error);
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 (0, exports.traverseDocument)(value, targetDescription, ctx.options);
365
+ const { error, result: doc } = await traverseDocument(value, targetDescription, ctx.options);
402
366
  if (error) {
403
- return types_1.Result.error(error);
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 types_1.Result.result(entries);
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 types_1.Result.result(what);
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 types_1.Result.result(whatCopy);
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 = (0, validation_1.validateWholeness)(whatCopy, options.description);
412
+ const wholenessError = validateWholeness(whatCopy, options.description);
449
413
  if (wholenessError) {
450
- return types_1.Result.error(wholenessError);
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 types_1.TraverseError.InvalidDocumentId:
470
- case types_1.TraverseError.InvalidTempfile:
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 types_1.Result.error(error);
441
+ return Result.error(error);
478
442
  }
479
443
  value = ctx.target[ctx.propName] = result;
480
444
  }
481
- return types_1.Result.result(value);
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 types_1.Result.error(error);
454
+ return Result.error(error);
491
455
  }
492
456
  if (traverseError) {
493
- return types_1.Result.error(traverseError);
457
+ return Result.error(traverseError);
494
458
  }
495
459
  if (validationError) {
496
460
  if (isMissingPropertyError(validationError)) {
497
- return types_1.Result.error(validationError);
461
+ return Result.error(validationError);
498
462
  }
499
- return types_1.Result.error((0, validation_1.makeValidationError)({
500
- code: types_1.ValidationErrorCode.InvalidProperties,
463
+ return Result.error(makeValidationError({
464
+ code: ValidationErrorCode.InvalidProperties,
501
465
  details: validationError,
502
466
  }));
503
467
  }
504
- return types_1.Result.result(result);
468
+ return Result.result(result);
505
469
  };
506
- exports.traverseDocument = traverseDocument;
package/dist/context.js CHANGED
@@ -1,44 +1,8 @@
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 __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 (0, exports.createContext)({
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: () => (0, exports.createContext)({
32
+ context: () => createContext({
69
33
  parentContext,
70
34
  collectionName,
71
35
  }),
72
36
  functions: proxiedFunctions,
73
37
  originalFunctions: collection.functions,
74
- model: (0, database_js_1.getDatabaseCollection)(collectionName),
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 Promise.resolve().then(() => __importStar(require('./assets.js')));
87
- const collections = await (0, entrypoint_1.getCollections)();
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 (0, database_js_1.getDatabaseCollection)('log').insertOne({
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 (0, common_1.endpointError)(Object.assign({
100
+ return endpointError(Object.assign({
137
101
  httpStatus,
138
102
  }, error));
139
103
  },
140
104
  limitRate: (params) => {
141
- return (0, security_1.limitRate)(params, context);
105
+ return limitRate(params, context);
142
106
  },
143
107
  };
144
108
  if (isCollectionContext(context, collectionName) && collectionName) {
145
- const description = (0, common_1.throwIfError)(await getCollectionAsset(collectionName, 'description'));
146
- context.description = await (0, preload_js_1.preloadDescription)(description);
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;