@aeriajs/core 0.0.88 → 0.0.90

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 (47) hide show
  1. package/dist/assets.js +3 -7
  2. package/dist/assets.mjs +5 -9
  3. package/dist/collection/define.d.ts +1 -1
  4. package/dist/collection/fill.d.ts +2 -0
  5. package/dist/collection/fill.js +14 -0
  6. package/dist/collection/fill.mjs +11 -0
  7. package/dist/collection/index.d.ts +3 -1
  8. package/dist/collection/index.js +3 -1
  9. package/dist/collection/index.mjs +3 -1
  10. package/dist/collection/normalizeProjection.d.ts +2 -0
  11. package/dist/collection/normalizeProjection.js +22 -0
  12. package/dist/collection/normalizeProjection.mjs +17 -0
  13. package/dist/collection/pagination.js +1 -1
  14. package/dist/collection/pagination.mjs +1 -1
  15. package/dist/collection/prepareInsert.d.ts +2 -0
  16. package/dist/collection/prepareInsert.js +46 -0
  17. package/dist/collection/prepareInsert.mjs +41 -0
  18. package/dist/collection/reference.js +3 -3
  19. package/dist/collection/reference.mjs +4 -4
  20. package/dist/collection/traverseDocument.d.ts +5 -4
  21. package/dist/collection/traverseDocument.js +16 -15
  22. package/dist/collection/traverseDocument.mjs +15 -14
  23. package/dist/context.d.ts +1 -1
  24. package/dist/context.js +2 -2
  25. package/dist/context.mjs +3 -3
  26. package/dist/functions/builtin/count.js +2 -2
  27. package/dist/functions/builtin/count.mjs +3 -3
  28. package/dist/functions/builtin/get.d.ts +2 -2
  29. package/dist/functions/builtin/get.js +7 -4
  30. package/dist/functions/builtin/get.mjs +8 -5
  31. package/dist/functions/builtin/getAll.js +3 -3
  32. package/dist/functions/builtin/getAll.mjs +4 -4
  33. package/dist/functions/builtin/insert.d.ts +30 -2
  34. package/dist/functions/builtin/insert.js +52 -19
  35. package/dist/functions/builtin/insert.mjs +51 -19
  36. package/dist/functions/builtin/remove.js +6 -5
  37. package/dist/functions/builtin/remove.mjs +7 -6
  38. package/dist/functions/builtin/removeAll.js +1 -1
  39. package/dist/functions/builtin/removeAll.mjs +2 -2
  40. package/dist/functions/builtin/upload.d.ts +7 -2
  41. package/dist/functions/builtin/upload.js +9 -2
  42. package/dist/functions/builtin/upload.mjs +10 -3
  43. package/dist/use.d.ts +1 -1
  44. package/package.json +8 -8
  45. package/dist/collection/utils.d.ts +0 -4
  46. package/dist/collection/utils.js +0 -89
  47. package/dist/collection/utils.mjs +0 -76
package/dist/assets.js CHANGED
@@ -60,13 +60,9 @@ const getFunction = async (collectionName, functionName, token, options = {
60
60
  const securityPolicy = collection.security?.functions?.[functionName];
61
61
  if (securityPolicy) {
62
62
  if (securityPolicy.rateLimiting) {
63
- const rateLimitingEither = await (0, security_1.limitRate)(securityPolicy.rateLimiting, context);
64
- if ((0, common_1.isLeft)(rateLimitingEither)) {
65
- const error = (0, common_1.unwrapEither)(rateLimitingEither);
66
- return context.error(types_1.HTTPStatus.TooManyRequests, {
67
- code: error,
68
- message: 'rate limit was achieved for this resource',
69
- });
63
+ const rate = await (0, security_1.limitRate)(securityPolicy.rateLimiting, context);
64
+ if ((0, common_1.isError)(rate)) {
65
+ return rate;
70
66
  }
71
67
  }
72
68
  }
package/dist/assets.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
- import { ACError, HTTPStatus } from "@aeriajs/types";
3
- import { left, right, isLeft, unwrapEither } from "@aeriajs/common";
2
+ import { ACError } from "@aeriajs/types";
3
+ import { isError, left, right, isLeft, unwrapEither } from "@aeriajs/common";
4
4
  import { limitRate } from "@aeriajs/security";
5
5
  import { getCollection } from "@aeriajs/entrypoint";
6
6
  import { isFunctionExposed, FunctionExposedStatus } from "./accessControl.mjs";
@@ -58,13 +58,9 @@ export const getFunction = async (collectionName, functionName, token, options =
58
58
  const securityPolicy = collection.security?.functions?.[functionName];
59
59
  if (securityPolicy) {
60
60
  if (securityPolicy.rateLimiting) {
61
- const rateLimitingEither = await limitRate(securityPolicy.rateLimiting, context);
62
- if (isLeft(rateLimitingEither)) {
63
- const error = unwrapEither(rateLimitingEither);
64
- return context.error(HTTPStatus.TooManyRequests, {
65
- code: error,
66
- message: "rate limit was achieved for this resource"
67
- });
61
+ const rate = await limitRate(securityPolicy.rateLimiting, context);
62
+ if (isError(rate)) {
63
+ return rate;
68
64
  }
69
65
  }
70
66
  }
@@ -20,7 +20,7 @@ export declare const extendCollection: <const TLeftCollection extends Collection
20
20
  description?: Partial<Description> | undefined;
21
21
  item?: Partial<any> | undefined;
22
22
  security?: Partial<CollectionSecurityPolicy<any> | undefined>;
23
- functions?: Partial<Record<string, (payload: any, context: import("@aeriajs/types").Context, ...args: any[]) => any> | undefined>;
23
+ functions?: Partial<Record<string, (payload: any, context: import("@aeriajs/types").Context<any>, ...args: any[]) => any> | undefined>;
24
24
  contracts?: Partial<Record<string, Contract> | undefined>;
25
25
  exposedFunctions?: Partial<Record<string, AccessCondition> | undefined>;
26
26
  }>(left: TLeftCollection, right: TRightCollection) => ExtendCollection<TLeftCollection, TRightCollection>;
@@ -0,0 +1,2 @@
1
+ import type { Description } from '@aeriajs/types';
2
+ export declare const fill: <TDocument extends OptionalId<any>>(doc: TDocument & Record<string, any>, description: Description) => Record<string, any> & TDocument;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fill = void 0;
4
+ const common_1 = require("@aeriajs/common");
5
+ const fill = (doc, description) => {
6
+ const docCopy = Object.assign({}, doc);
7
+ for (const key in docCopy) {
8
+ if (docCopy[key] === null) {
9
+ delete docCopy[key];
10
+ }
11
+ }
12
+ return Object.assign((0, common_1.freshItem)(description), docCopy);
13
+ };
14
+ exports.fill = fill;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ import { freshItem } from "@aeriajs/common";
3
+ export const fill = (doc, description) => {
4
+ const docCopy = Object.assign({}, doc);
5
+ for (const key in docCopy) {
6
+ if (docCopy[key] === null) {
7
+ delete docCopy[key];
8
+ }
9
+ }
10
+ return Object.assign(freshItem(description), docCopy);
11
+ };
@@ -1,8 +1,10 @@
1
1
  export * from './cascadingRemove.js';
2
2
  export * from './define.js';
3
3
  export * from './description.js';
4
+ export * from './fill.js';
5
+ export * from './normalizeProjection.js';
4
6
  export * from './pagination.js';
5
7
  export * from './preload.js';
8
+ export * from './prepareInsert.js';
6
9
  export * from './reference.js';
7
10
  export * from './traverseDocument.js';
8
- export * from './utils.js';
@@ -17,8 +17,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./cascadingRemove.js"), exports);
18
18
  __exportStar(require("./define.js"), exports);
19
19
  __exportStar(require("./description.js"), exports);
20
+ __exportStar(require("./fill.js"), exports);
21
+ __exportStar(require("./normalizeProjection.js"), exports);
20
22
  __exportStar(require("./pagination.js"), exports);
21
23
  __exportStar(require("./preload.js"), exports);
24
+ __exportStar(require("./prepareInsert.js"), exports);
22
25
  __exportStar(require("./reference.js"), exports);
23
26
  __exportStar(require("./traverseDocument.js"), exports);
24
- __exportStar(require("./utils.js"), exports);
@@ -2,8 +2,10 @@
2
2
  export * from "./cascadingRemove.mjs";
3
3
  export * from "./define.mjs";
4
4
  export * from "./description.mjs";
5
+ export * from "./fill.mjs";
6
+ export * from "./normalizeProjection.mjs";
5
7
  export * from "./pagination.mjs";
6
8
  export * from "./preload.mjs";
9
+ export * from "./prepareInsert.mjs";
7
10
  export * from "./reference.mjs";
8
11
  export * from "./traverseDocument.mjs";
9
- export * from "./utils.mjs";
@@ -0,0 +1,2 @@
1
+ import type { Description } from '@aeriajs/types';
2
+ export declare const normalizeProjection: <TDescription extends Pick<Description, "properties">, TProjectedProperties extends (keyof TDescription["properties"])[]>(properties: TProjectedProperties, description: TDescription) => {} | null;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizeProjection = void 0;
4
+ const normalizeProjection = (properties, description) => {
5
+ const target = Array.from(properties);
6
+ if (target.length === 0) {
7
+ target.push(...Object.keys(description.properties));
8
+ }
9
+ const projection = target.reduce((a, key) => {
10
+ if (key !== '_id' && description.properties[key].hidden) {
11
+ return a;
12
+ }
13
+ return {
14
+ ...a,
15
+ [key]: 1,
16
+ };
17
+ }, {});
18
+ return Object.keys(projection).length === 0
19
+ ? null
20
+ : projection;
21
+ };
22
+ exports.normalizeProjection = normalizeProjection;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ export const normalizeProjection = (properties, description) => {
3
+ const target = Array.from(properties);
4
+ if (target.length === 0) {
5
+ target.push(...Object.keys(description.properties));
6
+ }
7
+ const projection = target.reduce((a, key) => {
8
+ if (key !== "_id" && description.properties[key].hidden) {
9
+ return a;
10
+ }
11
+ return {
12
+ ...a,
13
+ [key]: 1
14
+ };
15
+ }, {});
16
+ return Object.keys(projection).length === 0 ? null : projection;
17
+ };
@@ -6,7 +6,7 @@ const makePagination = async (payload, documents, context) => {
6
6
  ? payload.limit
7
7
  : context.config.paginationLimit;
8
8
  const offset = payload.offset || 0;
9
- const recordsTotal = context.collection.functions.count
9
+ const recordsTotal = context.collection.originalFunctions.count
10
10
  ? await context.collection.functions.count({
11
11
  filters: payload.filters,
12
12
  })
@@ -2,7 +2,7 @@
2
2
  export const makePagination = async (payload, documents, context) => {
3
3
  const limit = payload.limit ? payload.limit : context.config.paginationLimit;
4
4
  const offset = payload.offset || 0;
5
- const recordsTotal = context.collection.functions.count ? await context.collection.functions.count({
5
+ const recordsTotal = context.collection.originalFunctions.count ? await context.collection.functions.count({
6
6
  filters: payload.filters
7
7
  }) : await context.collection.model.countDocuments(payload.filters);
8
8
  return {
@@ -0,0 +1,2 @@
1
+ import type { Description } from '@aeriajs/types';
2
+ export declare const prepareInsert: <TPayload extends Record<string, any>>(payload: TPayload, description: Description) => Record<string, any>;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepareInsert = void 0;
4
+ const prepareCreate = (doc, description) => {
5
+ const result = Object.assign({}, description.defaults || {});
6
+ for (const propName in doc) {
7
+ const value = doc[propName];
8
+ if (value === null || value === undefined) {
9
+ continue;
10
+ }
11
+ result[propName] = value;
12
+ }
13
+ return result;
14
+ };
15
+ const prepareUpdate = (doc) => {
16
+ const result = {
17
+ $set: {},
18
+ $unset: {},
19
+ };
20
+ for (const propName in doc) {
21
+ const value = doc[propName];
22
+ if (value === null || value === undefined) {
23
+ result.$unset[propName] = value;
24
+ continue;
25
+ }
26
+ result.$set[propName] = value;
27
+ }
28
+ return result;
29
+ };
30
+ const prepareInsert = (payload, description) => {
31
+ const doc = Object.assign({}, payload);
32
+ const docId = payload._id;
33
+ delete doc._id;
34
+ delete doc.created_at;
35
+ delete doc.updated_at;
36
+ const what = docId
37
+ ? prepareUpdate(doc)
38
+ : prepareCreate(doc, description);
39
+ Object.keys(what).forEach((k) => {
40
+ if (typeof what[k] === 'object' && Object.keys(what[k]).length === 0) {
41
+ delete what[k];
42
+ }
43
+ });
44
+ return what;
45
+ };
46
+ exports.prepareInsert = prepareInsert;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ const prepareCreate = (doc, description) => {
3
+ const result = Object.assign({}, description.defaults || {});
4
+ for (const propName in doc) {
5
+ const value = doc[propName];
6
+ if (value === null || value === void 0) {
7
+ continue;
8
+ }
9
+ result[propName] = value;
10
+ }
11
+ return result;
12
+ };
13
+ const prepareUpdate = (doc) => {
14
+ const result = {
15
+ $set: {},
16
+ $unset: {}
17
+ };
18
+ for (const propName in doc) {
19
+ const value = doc[propName];
20
+ if (value === null || value === void 0) {
21
+ result.$unset[propName] = value;
22
+ continue;
23
+ }
24
+ result.$set[propName] = value;
25
+ }
26
+ return result;
27
+ };
28
+ export const prepareInsert = (payload, description) => {
29
+ const doc = Object.assign({}, payload);
30
+ const docId = payload._id;
31
+ delete doc._id;
32
+ delete doc.created_at;
33
+ delete doc.updated_at;
34
+ const what = docId ? prepareUpdate(doc) : prepareCreate(doc, description);
35
+ Object.keys(what).forEach((k) => {
36
+ if (typeof what[k] === "object" && Object.keys(what[k]).length === 0) {
37
+ delete what[k];
38
+ }
39
+ });
40
+ return what;
41
+ };
@@ -101,7 +101,7 @@ const getReferences = async (properties, options) => {
101
101
  }
102
102
  }
103
103
  else {
104
- const description = (0, common_1.unsafe)(await (0, assets_js_1.getCollectionAsset)(refProperty.$ref, 'description'));
104
+ const description = (0, common_1.throwIfLeft)(await (0, assets_js_1.getCollectionAsset)(refProperty.$ref, 'description'));
105
105
  const deepReferences = await (0, exports.getReferences)(description.properties, {
106
106
  depth: depth + 1,
107
107
  memoize: `${memoize}.${propName}`,
@@ -159,7 +159,7 @@ const buildLookupStages = async (reference, propName, options) => {
159
159
  else {
160
160
  const subPipeline = [];
161
161
  if (reference.deepReferences) {
162
- const subProperties = (0, common_1.unsafe)(await (0, assets_js_1.getCollectionAsset)(reference.referencedCollection, 'description')).properties;
162
+ const subProperties = (0, common_1.throwIfLeft)(await (0, assets_js_1.getCollectionAsset)(reference.referencedCollection, 'description')).properties;
163
163
  subPipeline.push(...await (0, exports.buildLookupPipeline)(reference.deepReferences, {
164
164
  project: reference.populatedProperties,
165
165
  properties: subProperties,
@@ -227,7 +227,7 @@ const buildLookupStages = async (reference, propName, options) => {
227
227
  continue;
228
228
  }
229
229
  if (refMap.referencedCollection) {
230
- const description = (0, common_1.unsafe)(await (0, assets_js_1.getCollectionAsset)(refMap.referencedCollection, 'description'));
230
+ const description = (0, common_1.throwIfLeft)(await (0, assets_js_1.getCollectionAsset)(refMap.referencedCollection, 'description'));
231
231
  const { stages: result } = await buildLookupStages(refMap, refName, {
232
232
  depth: depth + 1,
233
233
  parent: withParent(propName),
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- import { unsafe, getReferenceProperty } from "@aeriajs/common";
2
+ import { throwIfLeft, getReferenceProperty } from "@aeriajs/common";
3
3
  import { getCollectionAsset } from "../assets.mjs";
4
4
  import { prepareCollectionName } from "../database.mjs";
5
5
  const referenceMemo = {};
@@ -90,7 +90,7 @@ export const getReferences = async (properties, options) => {
90
90
  }
91
91
  }
92
92
  } else {
93
- const description = unsafe(await getCollectionAsset(refProperty.$ref, "description"));
93
+ const description = throwIfLeft(await getCollectionAsset(refProperty.$ref, "description"));
94
94
  const deepReferences = await getReferences(description.properties, {
95
95
  depth: depth + 1,
96
96
  memoize: `${memoize}.${propName}`
@@ -147,7 +147,7 @@ const buildLookupStages = async (reference, propName, options) => {
147
147
  } else {
148
148
  const subPipeline = [];
149
149
  if (reference.deepReferences) {
150
- const subProperties = unsafe(await getCollectionAsset(reference.referencedCollection, "description")).properties;
150
+ const subProperties = throwIfLeft(await getCollectionAsset(reference.referencedCollection, "description")).properties;
151
151
  subPipeline.push(...await buildLookupPipeline(reference.deepReferences, {
152
152
  project: reference.populatedProperties,
153
153
  properties: subProperties
@@ -210,7 +210,7 @@ const buildLookupStages = async (reference, propName, options) => {
210
210
  continue;
211
211
  }
212
212
  if (refMap.referencedCollection) {
213
- const description = unsafe(await getCollectionAsset(refMap.referencedCollection, "description"));
213
+ const description = throwIfLeft(await getCollectionAsset(refMap.referencedCollection, "description"));
214
214
  const { stages: result2 } = await buildLookupStages(refMap, refName, {
215
215
  depth: depth + 1,
216
216
  parent: withParent(propName),
@@ -1,10 +1,11 @@
1
- import type { Description, Property, ACError, ValidationError, Context } from '@aeriajs/types';
1
+ import type { Description, Property, ValidationError, Context } from '@aeriajs/types';
2
+ import { ACError } from '@aeriajs/types';
2
3
  import { ObjectId } from 'mongodb';
3
4
  export type TraverseOptions = {
4
5
  autoCast?: boolean;
5
6
  getters?: boolean;
6
7
  validate?: boolean;
7
- validateRequired?: string[];
8
+ validateRequired?: Description['required'];
8
9
  moveFiles?: boolean;
9
10
  fromProperties?: boolean;
10
11
  allowOperators?: boolean;
@@ -16,7 +17,7 @@ export type TraverseNormalized = {
16
17
  description: Description;
17
18
  pipe: <T = unknown>(value: unknown, phaseContext: PhaseContext) => T | Promise<T>;
18
19
  };
19
- export declare enum TraverseErrors {
20
+ export declare enum TraverseError {
20
21
  InvalidDocumentId = "INVALID_DOCUMENT_ID",
21
22
  InvalidTempfile = "INVALID_TEMPFILE"
22
23
  }
@@ -30,5 +31,5 @@ type PhaseContext = {
30
31
  property: Property;
31
32
  options: TraverseOptions & TraverseNormalized;
32
33
  };
33
- export declare const traverseDocument: <const TWhat extends Record<string, any>>(what: TWhat, description: Description, _options: TraverseOptions) => Promise<import("@aeriajs/types").Right<any> | import("@aeriajs/types").Left<ACError | ValidationError>>;
34
+ export declare const traverseDocument: <const TWhat extends Record<string, unknown>>(what: TWhat, description: Description, _options: TraverseOptions) => Promise<import("@aeriajs/types").Right<any> | import("@aeriajs/types").Left<ACError.InsecureOperator | ValidationError>>;
34
35
  export {};
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.traverseDocument = exports.TraverseErrors = void 0;
26
+ exports.traverseDocument = exports.TraverseError = void 0;
27
27
  const types_1 = require("@aeriajs/types");
28
28
  const common_1 = require("@aeriajs/common");
29
29
  const validation_1 = require("@aeriajs/validation");
@@ -31,11 +31,11 @@ const mongodb_1 = require("mongodb");
31
31
  const assets_js_1 = require("../assets.js");
32
32
  const preload_js_1 = require("./preload.js");
33
33
  const fs = __importStar(require("fs/promises"));
34
- var TraverseErrors;
35
- (function (TraverseErrors) {
36
- TraverseErrors["InvalidDocumentId"] = "INVALID_DOCUMENT_ID";
37
- TraverseErrors["InvalidTempfile"] = "INVALID_TEMPFILE";
38
- })(TraverseErrors || (exports.TraverseErrors = TraverseErrors = {}));
34
+ var TraverseError;
35
+ (function (TraverseError) {
36
+ TraverseError["InvalidDocumentId"] = "INVALID_DOCUMENT_ID";
37
+ TraverseError["InvalidTempfile"] = "INVALID_TEMPFILE";
38
+ })(TraverseError || (exports.TraverseError = TraverseError = {}));
39
39
  const getProperty = (propertyName, parentProperty) => {
40
40
  if (propertyName === '_id') {
41
41
  return {
@@ -65,7 +65,7 @@ const disposeOldFiles = async (ctx, options = {}) => {
65
65
  },
66
66
  });
67
67
  if (!doc) {
68
- return (0, common_1.left)(TraverseErrors.InvalidDocumentId);
68
+ return (0, common_1.left)(TraverseError.InvalidDocumentId);
69
69
  }
70
70
  let fileIds = (0, common_1.getValueFromPath)(doc, ctx.propPath);
71
71
  if (options.fromIds) {
@@ -196,7 +196,7 @@ const moveFiles = async (value, ctx) => {
196
196
  _id: new mongodb_1.ObjectId(value.tempId),
197
197
  });
198
198
  if (!tempFile) {
199
- return (0, common_1.left)(TraverseErrors.InvalidTempfile);
199
+ return (0, common_1.left)(TraverseError.InvalidTempfile);
200
200
  }
201
201
  if (ctx.root._id) {
202
202
  await disposeOldFiles(ctx);
@@ -268,12 +268,13 @@ const recurse = async (target, ctx) => {
268
268
  // if first propName is preceded by '$' we assume
269
269
  // it contains MongoDB query operators
270
270
  if (Object.keys(value)[0]?.startsWith('$')) {
271
- if (ctx.options.allowOperators) {
272
- entries.push([
273
- propName,
274
- value,
275
- ]);
271
+ if (!ctx.options.allowOperators) {
272
+ return (0, common_1.left)(types_1.ACError.InsecureOperator);
276
273
  }
274
+ entries.push([
275
+ propName,
276
+ value,
277
+ ]);
277
278
  continue;
278
279
  }
279
280
  if (Array.isArray(value)) {
@@ -306,7 +307,7 @@ const recurse = async (target, ctx) => {
306
307
  ? property.items
307
308
  : property;
308
309
  if ('$ref' in propCast && value && !(value instanceof mongodb_1.ObjectId)) {
309
- const targetDescription = await (0, preload_js_1.preloadDescription)((0, common_1.unsafe)(await (0, assets_js_1.getCollectionAsset)(propCast.$ref, 'description')));
310
+ const targetDescription = await (0, preload_js_1.preloadDescription)((0, common_1.throwIfLeft)(await (0, assets_js_1.getCollectionAsset)(propCast.$ref, 'description')));
310
311
  if (Array.isArray(value)) {
311
312
  const documents = [];
312
313
  for (const elem of value) {
@@ -412,7 +413,7 @@ const traverseDocument = async (what, description, _options) => {
412
413
  }
413
414
  if (validationError) {
414
415
  return (0, common_1.left)((0, validation_1.makeValidationError)({
415
- code: types_1.ValidationErrorCodes.InvalidProperties,
416
+ code: types_1.ValidationErrorCode.InvalidProperties,
416
417
  errors: validationError,
417
418
  }));
418
419
  }
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
- import { ValidationErrorCodes } from "@aeriajs/types";
3
- import { left, right, isLeft, unwrapEither, unsafe, pipe, isReference, getValueFromPath, isObjectId } from "@aeriajs/common";
2
+ import { ACError, ValidationErrorCode } from "@aeriajs/types";
3
+ import { left, right, isLeft, unwrapEither, throwIfLeft, pipe, isReference, getValueFromPath, isObjectId } from "@aeriajs/common";
4
4
  import { makeValidationError, validateProperty, validateWholeness } from "@aeriajs/validation";
5
5
  import { ObjectId } from "mongodb";
6
6
  import { getCollectionAsset } from "../assets.mjs";
7
7
  import { preloadDescription } from "./preload.mjs";
8
8
  import * as fs from "fs/promises";
9
- export var TraverseErrors = /* @__PURE__ */ ((TraverseErrors2) => {
10
- TraverseErrors2["InvalidDocumentId"] = "INVALID_DOCUMENT_ID";
11
- TraverseErrors2["InvalidTempfile"] = "INVALID_TEMPFILE";
12
- return TraverseErrors2;
13
- })(TraverseErrors || {});
9
+ export var TraverseError = /* @__PURE__ */ ((TraverseError2) => {
10
+ TraverseError2["InvalidDocumentId"] = "INVALID_DOCUMENT_ID";
11
+ TraverseError2["InvalidTempfile"] = "INVALID_TEMPFILE";
12
+ return TraverseError2;
13
+ })(TraverseError || {});
14
14
  const getProperty = (propertyName, parentProperty) => {
15
15
  if (propertyName === "_id") {
16
16
  return {
@@ -230,12 +230,13 @@ const recurse = async (target, ctx) => {
230
230
  }
231
231
  if (!property && value && (value.constructor === Object || value.constructor === Array)) {
232
232
  if (Object.keys(value)[0]?.startsWith("$")) {
233
- if (ctx.options.allowOperators) {
234
- entries.push([
235
- propName,
236
- value
237
- ]);
233
+ if (!ctx.options.allowOperators) {
234
+ return left(ACError.InsecureOperator);
238
235
  }
236
+ entries.push([
237
+ propName,
238
+ value
239
+ ]);
239
240
  continue;
240
241
  }
241
242
  if (Array.isArray(value)) {
@@ -266,7 +267,7 @@ const recurse = async (target, ctx) => {
266
267
  if (ctx.options.recurseReferences) {
267
268
  const propCast = "items" in property ? property.items : property;
268
269
  if ("$ref" in propCast && value && !(value instanceof ObjectId)) {
269
- const targetDescription = await preloadDescription(unsafe(await getCollectionAsset(propCast.$ref, "description")));
270
+ const targetDescription = await preloadDescription(throwIfLeft(await getCollectionAsset(propCast.$ref, "description")));
270
271
  if (Array.isArray(value)) {
271
272
  const documents = [];
272
273
  for (const elem of value) {
@@ -370,7 +371,7 @@ export const traverseDocument = async (what, description, _options) => {
370
371
  }
371
372
  if (validationError) {
372
373
  return left(makeValidationError({
373
- code: ValidationErrorCodes.InvalidProperties,
374
+ code: ValidationErrorCode.InvalidProperties,
374
375
  errors: validationError
375
376
  }));
376
377
  }
package/dist/context.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  import type { ContextOptions } from '@aeriajs/types';
2
- export declare const createContext: (options?: ContextOptions) => Promise<import("@aeriajs/types").RouteContext<null> & import("@aeriajs/types").CollectionContext<any, any>>;
2
+ export declare const createContext: (options?: ContextOptions) => Promise<import("@aeriajs/types").RouteContext<null> & import("@aeriajs/types").CollectionContext<import("@aeriajs/types").Description, any>>;
package/dist/context.js CHANGED
@@ -79,13 +79,13 @@ const createContext = async (options = {}) => {
79
79
  context.error = (httpStatus, endpointError) => {
80
80
  return (0, common_1.error)(Object.assign({
81
81
  httpStatus,
82
- }, endpointError), context);
82
+ }, endpointError));
83
83
  };
84
84
  context.limitRate = (params) => {
85
85
  return (0, security_1.limitRate)(params, context);
86
86
  };
87
87
  if (collectionName) {
88
- const description = (0, common_1.unsafe)(await getCollectionAsset(collectionName, 'description'));
88
+ const description = (0, common_1.throwIfLeft)(await getCollectionAsset(collectionName, 'description'));
89
89
  context.description = await (0, preload_js_1.preloadDescription)(description);
90
90
  context.collectionName = collectionName;
91
91
  context.collection = indepthCollection(collectionName, collections, context);
package/dist/context.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- import { unsafe, error } from "@aeriajs/common";
2
+ import { throwIfLeft, error } from "@aeriajs/common";
3
3
  import { getCollections } from "@aeriajs/entrypoint";
4
4
  import { limitRate } from "@aeriajs/security";
5
5
  import { getDatabaseCollection } from "./database.mjs";
@@ -54,13 +54,13 @@ export const createContext = async (options = {}) => {
54
54
  context.error = (httpStatus, endpointError) => {
55
55
  return error(Object.assign({
56
56
  httpStatus
57
- }, endpointError), context);
57
+ }, endpointError));
58
58
  };
59
59
  context.limitRate = (params) => {
60
60
  return limitRate(params, context);
61
61
  };
62
62
  if (collectionName) {
63
- const description = unsafe(await getCollectionAsset(collectionName, "description"));
63
+ const description = throwIfLeft(await getCollectionAsset(collectionName, "description"));
64
64
  context.description = await preloadDescription(description);
65
65
  context.collectionName = collectionName;
66
66
  context.collection = indepthCollection(collectionName, collections, context);
@@ -6,9 +6,9 @@ const common_1 = require("@aeriajs/common");
6
6
  const index_js_1 = require("../../collection/index.js");
7
7
  const count = async (payload, context) => {
8
8
  const security = (0, security_1.useSecurity)(context);
9
- const { filters } = (0, common_1.unsafe)(await security.beforeRead(payload));
9
+ const { filters } = (0, common_1.throwIfLeft)(await security.beforeRead(payload));
10
10
  const { $text, ...filtersRest } = filters;
11
- const traversedFilters = (0, common_1.unsafe)(await (0, index_js_1.traverseDocument)(filtersRest, context.description, {
11
+ const traversedFilters = (0, common_1.throwIfLeft)(await (0, index_js_1.traverseDocument)(filtersRest, context.description, {
12
12
  autoCast: true,
13
13
  allowOperators: true,
14
14
  }));
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  import { useSecurity } from "@aeriajs/security";
3
- import { unsafe } from "@aeriajs/common";
3
+ import { throwIfLeft } from "@aeriajs/common";
4
4
  import { traverseDocument } from "../../collection/index.mjs";
5
5
  export const count = async (payload, context) => {
6
6
  const security = useSecurity(context);
7
- const { filters } = unsafe(await security.beforeRead(payload));
7
+ const { filters } = throwIfLeft(await security.beforeRead(payload));
8
8
  const { $text, ...filtersRest } = filters;
9
- const traversedFilters = unsafe(await traverseDocument(filtersRest, context.description, {
9
+ const traversedFilters = throwIfLeft(await traverseDocument(filtersRest, context.description, {
10
10
  autoCast: true,
11
11
  allowOperators: true
12
12
  }));
@@ -1,5 +1,5 @@
1
- import type { Context, SchemaWithId, GetPayload } from '@aeriajs/types';
1
+ import type { Context, SchemaWithId, GetPayload, GetReturnType } from '@aeriajs/types';
2
2
  export type GetOptions = {
3
3
  bypassSecurity?: boolean;
4
4
  };
5
- export declare const get: <TContext extends Context>(payload: GetPayload<SchemaWithId<TContext['description']>>, context: TContext extends Context<any> ? TContext : never, options?: GetOptions) => Promise<SchemaWithId<TContext['description']> | null>;
5
+ export declare const get: <TContext extends Context>(payload: GetPayload<SchemaWithId<TContext['description']>>, context: TContext extends Context<any> ? TContext : never, options?: GetOptions) => Promise<GetReturnType<TContext['description']>>;
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.get = void 0;
4
+ const types_1 = require("@aeriajs/types");
4
5
  const security_1 = require("@aeriajs/security");
5
6
  const common_1 = require("@aeriajs/common");
6
7
  const index_js_1 = require("../../collection/index.js");
7
8
  const get = async (payload, context, options) => {
8
9
  const security = (0, security_1.useSecurity)(context);
9
10
  const { filters = {}, project = [], } = !options?.bypassSecurity
10
- ? (0, common_1.unsafe)(await security.beforeRead(payload))
11
+ ? (0, common_1.throwIfLeft)(await security.beforeRead(payload))
11
12
  : payload;
12
13
  if (Object.keys(filters).length === 0) {
13
14
  throw new Error('no filters were passed');
@@ -17,7 +18,7 @@ const get = async (payload, context, options) => {
17
18
  memoize: context.description.$id,
18
19
  });
19
20
  pipeline.push({
20
- $match: (0, common_1.unsafe)(await (0, index_js_1.traverseDocument)(filters, context.description, {
21
+ $match: (0, common_1.throwIfLeft)(await (0, index_js_1.traverseDocument)(filters, context.description, {
21
22
  autoCast: true,
22
23
  allowOperators: true,
23
24
  })),
@@ -35,9 +36,11 @@ const get = async (payload, context, options) => {
35
36
  }));
36
37
  const result = await context.collection.model.aggregate(pipeline).next();
37
38
  if (!result) {
38
- return null;
39
+ return context.error(types_1.HTTPStatus.NotFound, {
40
+ code: types_1.ACError.ResourceNotFound,
41
+ });
39
42
  }
40
- return (0, index_js_1.fill)((0, common_1.unsafe)(await (0, index_js_1.traverseDocument)(result, context.description, {
43
+ return (0, index_js_1.fill)((0, common_1.throwIfLeft)(await (0, index_js_1.traverseDocument)(result, context.description, {
41
44
  getters: true,
42
45
  fromProperties: true,
43
46
  recurseReferences: true,