@aeriajs/core 0.0.161 → 0.0.163

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.
@@ -32,7 +32,7 @@ import type {} from '@aeriajs/types'
32
32
  declare global {
33
33
  type UnpackCollections<TCollections> = {
34
34
  [P in keyof TCollections]: TCollections[P] extends infer Candidate
35
- ? Candidate extends (...args: any[]) => infer Coll
35
+ ? Candidate extends (...args: unknown[]) => infer Coll
36
36
  ? Coll
37
37
  : Candidate
38
38
  : never
@@ -8,7 +8,7 @@ import type {} from '@aeriajs/types'
8
8
  declare global {
9
9
  type UnpackCollections<TCollections> = {
10
10
  [P in keyof TCollections]: TCollections[P] extends infer Candidate
11
- ? Candidate extends (...args: any[]) => infer Coll
11
+ ? Candidate extends (...args: unknown[]) => infer Coll
12
12
  ? Coll
13
13
  : Candidate
14
14
  : never
@@ -3,10 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.makePagination = void 0;
4
4
  const common_1 = require("@aeriajs/common");
5
5
  const makePagination = async (payload, documents, context) => {
6
- const limit = payload.limit
7
- ? payload.limit
8
- : context.config.paginationLimit;
9
- const offset = payload.offset || 0;
6
+ const { offset = 0, limit = context.config.defaultPaginationLimit, } = payload;
10
7
  const recordsTotal = typeof context.collection.functions.count === 'function'
11
8
  ? (0, common_1.throwIfError)(await context.collection.functions.count({
12
9
  filters: payload.filters,
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  import { throwIfError } from "@aeriajs/common";
3
3
  export const makePagination = async (payload, documents, context) => {
4
- const limit = payload.limit ? payload.limit : context.config.paginationLimit;
5
- const offset = payload.offset || 0;
4
+ const {
5
+ offset = 0,
6
+ limit = context.config.defaultPaginationLimit
7
+ } = payload;
6
8
  const recordsTotal = typeof context.collection.functions.count === "function" ? throwIfError(await context.collection.functions.count({
7
9
  filters: payload.filters
8
10
  })) : await context.collection.model.countDocuments(payload.filters);
@@ -8,6 +8,7 @@ export type TraverseOptionsBase = {
8
8
  fromProperties?: boolean;
9
9
  allowOperators?: boolean;
10
10
  skipUndefined?: boolean;
11
+ preserveHidden?: boolean;
11
12
  recurseDeep?: boolean;
12
13
  recurseReferences?: boolean;
13
14
  };
@@ -38,7 +39,7 @@ type PhaseContext = {
38
39
  export declare const traverseDocument: <const TWhat extends Record<string, unknown> | null>(what: TWhat, description: Description, _options: TraverseOptions) => Promise<{
39
40
  readonly _tag: "Result";
40
41
  readonly error: undefined;
41
- readonly result: any;
42
+ readonly result: TWhat;
42
43
  } | {
43
44
  readonly _tag: "Error";
44
45
  readonly error: ACError.InsecureOperator | ValidationError | TraverseError;
@@ -317,7 +317,7 @@ const recurse = async (target, ctx) => {
317
317
  ]);
318
318
  }
319
319
  if (property) {
320
- if (property.hidden) {
320
+ if (!ctx.options.preserveHidden && property.hidden) {
321
321
  continue;
322
322
  }
323
323
  if ('getters' in ctx.options && ctx.options.getters && 'getter' in property) {
@@ -391,7 +391,7 @@ const traverseDocument = async (what, description, _options) => {
391
391
  return types_1.Result.result(what);
392
392
  }
393
393
  if (!options.validate && Object.keys(what).length === 0) {
394
- return types_1.Result.result({});
394
+ return types_1.Result.result(what);
395
395
  }
396
396
  if (options.recurseDeep) {
397
397
  functions.push(recurseDeep);
@@ -279,7 +279,7 @@ const recurse = async (target, ctx) => {
279
279
  ]);
280
280
  }
281
281
  if (property) {
282
- if (property.hidden) {
282
+ if (!ctx.options.preserveHidden && property.hidden) {
283
283
  continue;
284
284
  }
285
285
  if ("getters" in ctx.options && ctx.options.getters && "getter" in property) {
@@ -349,7 +349,7 @@ export const traverseDocument = async (what, description, _options) => {
349
349
  return Result.result(what);
350
350
  }
351
351
  if (!options.validate && Object.keys(what).length === 0) {
352
- return Result.result({});
352
+ return Result.result(what);
353
353
  }
354
354
  if (options.recurseDeep) {
355
355
  functions.push(recurseDeep);
@@ -1,4 +1,5 @@
1
1
  import type { Context, SchemaWithId, CountPayload } from '@aeriajs/types';
2
+ import { Result } from '@aeriajs/types';
2
3
  export type CountOptions = {
3
4
  bypassSecurity?: boolean;
4
5
  };
@@ -6,8 +7,4 @@ export declare const count: <TContext extends Context>(payload: CountPayload<Sch
6
7
  readonly _tag: "Result";
7
8
  readonly error: undefined;
8
9
  readonly result: any;
9
- } | {
10
- readonly _tag: "Error";
11
- readonly error: import("@aeriajs/types").ACError.InvalidLimit | import("@aeriajs/types").ACError.OwnershipError;
12
- readonly result: undefined;
13
- }>;
10
+ } | Result.Error<any>>;
@@ -46,6 +46,6 @@ const count = async (payload, context, options = {}) => {
46
46
  if (error) {
47
47
  return types_1.Result.error(error);
48
48
  }
49
- return internalCount(securedPayload, context);
49
+ return (0, security_1.applyReadMiddlewares)(securedPayload, context, internalCount);
50
50
  };
51
51
  exports.count = count;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- import { useSecurity } from "@aeriajs/security";
2
+ import { useSecurity, applyReadMiddlewares } from "@aeriajs/security";
3
3
  import { Result } from "@aeriajs/types";
4
4
  import { throwIfError } from "@aeriajs/common";
5
5
  import { traverseDocument } from "../collection/index.mjs";
@@ -40,5 +40,5 @@ export const count = async (payload, context, options = {}) => {
40
40
  if (error) {
41
41
  return Result.error(error);
42
42
  }
43
- return internalCount(securedPayload, context);
43
+ return applyReadMiddlewares(securedPayload, context, internalCount);
44
44
  };
@@ -65,6 +65,6 @@ const get = async (payload, context, options = {}) => {
65
65
  code: error,
66
66
  });
67
67
  }
68
- return internalGet(securedPayload, context);
68
+ return (0, security_1.applyReadMiddlewares)(securedPayload, context, internalGet);
69
69
  };
70
70
  exports.get = get;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- import { useSecurity } from "@aeriajs/security";
2
+ import { useSecurity, applyReadMiddlewares } from "@aeriajs/security";
3
3
  import { Result, HTTPStatus, ACError } from "@aeriajs/types";
4
4
  import { throwIfError } from "@aeriajs/common";
5
5
  import {
@@ -70,5 +70,5 @@ export const get = async (payload, context, options = {}) => {
70
70
  code: error
71
71
  });
72
72
  }
73
- return internalGet(securedPayload, context);
73
+ return applyReadMiddlewares(securedPayload, context, internalGet);
74
74
  };
@@ -1,13 +1,6 @@
1
1
  import type { Context, SchemaWithId, GetAllPayload } from '@aeriajs/types';
2
+ import { Result } from '@aeriajs/types';
2
3
  export type GetAllOptions = {
3
4
  bypassSecurity?: boolean;
4
5
  };
5
- export declare const getAll: <TContext extends Context>(payload: GetAllPayload<SchemaWithId<TContext["description"]>> | undefined, context: TContext, options?: GetAllOptions) => Promise<{
6
- readonly _tag: "Error";
7
- readonly error: import("@aeriajs/types").ACError.InvalidLimit | import("@aeriajs/types").ACError.OwnershipError;
8
- readonly result: undefined;
9
- } | {
10
- readonly _tag: "Result";
11
- readonly error: undefined;
12
- readonly result: SchemaWithId<TContext["description"]>[];
13
- }>;
6
+ export declare const getAll: <TContext extends Context>(payload: GetAllPayload<SchemaWithId<TContext["description"]>> | undefined, context: TContext, options?: GetAllOptions) => Promise<Result.Either<any, any>>;
@@ -6,7 +6,7 @@ const types_1 = require("@aeriajs/types");
6
6
  const common_1 = require("@aeriajs/common");
7
7
  const index_js_1 = require("../collection/index.js");
8
8
  const internalGetAll = async (payload, context) => {
9
- const { limit = context.config.paginationLimit, sort, project, offset = 0, } = payload;
9
+ const { limit = context.config.defaultPaginationLimit, sort, project, offset = 0, } = payload;
10
10
  const filters = payload.filters
11
11
  ? Object.assign({}, payload.filters)
12
12
  : {};
@@ -99,6 +99,6 @@ const getAll = async (payload, context, options = {}) => {
99
99
  if (error) {
100
100
  return types_1.Result.error(error);
101
101
  }
102
- return internalGetAll(securedPayload, context);
102
+ return (0, security_1.applyReadMiddlewares)(securedPayload, context, internalGetAll);
103
103
  };
104
104
  exports.getAll = getAll;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- import { useSecurity } from "@aeriajs/security";
2
+ import { useSecurity, applyReadMiddlewares } from "@aeriajs/security";
3
3
  import { Result } from "@aeriajs/types";
4
4
  import { throwIfError } from "@aeriajs/common";
5
5
  import {
@@ -10,7 +10,7 @@ import {
10
10
  } from "../collection/index.mjs";
11
11
  const internalGetAll = async (payload, context) => {
12
12
  const {
13
- limit = context.config.paginationLimit,
13
+ limit = context.config.defaultPaginationLimit,
14
14
  sort,
15
15
  project,
16
16
  offset = 0
@@ -97,5 +97,5 @@ export const getAll = async (payload, context, options = {}) => {
97
97
  if (error) {
98
98
  return Result.error(error);
99
99
  }
100
- return internalGetAll(securedPayload, context);
100
+ return applyReadMiddlewares(securedPayload, context, internalGetAll);
101
101
  };
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.insert = exports.insertErrorSchema = void 0;
4
+ const mongodb_1 = require("mongodb");
4
5
  const types_1 = require("@aeriajs/types");
5
6
  const security_1 = require("@aeriajs/security");
6
7
  const common_1 = require("@aeriajs/common");
@@ -34,6 +35,7 @@ const internalInsert = async (payload, context) => {
34
35
  : context.description.required,
35
36
  moveFiles: true,
36
37
  skipUndefined: true,
38
+ preserveHidden: true,
37
39
  context,
38
40
  });
39
41
  if (error) {
@@ -47,7 +49,7 @@ const internalInsert = async (payload, context) => {
47
49
  details: error.errors,
48
50
  });
49
51
  }
50
- const docId = '_id' in what
52
+ const docId = '_id' in what && what._id instanceof mongodb_1.ObjectId
51
53
  ? what._id
52
54
  : null;
53
55
  const content = (0, index_js_1.prepareInsert)(what, context.description);
@@ -67,7 +69,7 @@ const internalInsert = async (payload, context) => {
67
69
  content.$set ??= {};
68
70
  content.$set.updated_at = new Date();
69
71
  await context.collection.model.updateOne({
70
- _id: docId,
72
+ _id: newId,
71
73
  }, content);
72
74
  }
73
75
  if (context.collection.originalFunctions?.get) {
@@ -110,6 +112,6 @@ const insert = async (payload, context, options = {}) => {
110
112
  code: error,
111
113
  });
112
114
  }
113
- return internalInsert(securedPayload, context);
115
+ return (0, security_1.applyWriteMiddlewares)(securedPayload, context, internalInsert);
114
116
  };
115
117
  exports.insert = insert;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
+ import { ObjectId } from "mongodb";
2
3
  import { Result, HTTPStatus, ACError, ValidationErrorCode, TraverseError } from "@aeriajs/types";
3
- import { useSecurity } from "@aeriajs/security";
4
+ import { useSecurity, applyWriteMiddlewares } from "@aeriajs/security";
4
5
  import { throwIfError, endpointErrorSchema } from "@aeriajs/common";
5
6
  import { traverseDocument, normalizeProjection, prepareInsert } from "../collection/index.mjs";
6
7
  export const insertErrorSchema = () => endpointErrorSchema({
@@ -29,6 +30,7 @@ const internalInsert = async (payload, context) => {
29
30
  validateRequired: "_id" in payload.what && payload.what._id ? [] : context.description.required,
30
31
  moveFiles: true,
31
32
  skipUndefined: true,
33
+ preserveHidden: true,
32
34
  context
33
35
  });
34
36
  if (error) {
@@ -42,7 +44,7 @@ const internalInsert = async (payload, context) => {
42
44
  details: error.errors
43
45
  });
44
46
  }
45
- const docId = "_id" in what ? what._id : null;
47
+ const docId = "_id" in what && what._id instanceof ObjectId ? what._id : null;
46
48
  const content = prepareInsert(what, context.description);
47
49
  const projection = payload.project ? normalizeProjection(payload.project, context.description) : {};
48
50
  let newId = docId;
@@ -57,7 +59,7 @@ const internalInsert = async (payload, context) => {
57
59
  content.$set ??= {};
58
60
  content.$set.updated_at = /* @__PURE__ */ new Date();
59
61
  await context.collection.model.updateOne({
60
- _id: docId
62
+ _id: newId
61
63
  }, content);
62
64
  }
63
65
  if (context.collection.originalFunctions?.get) {
@@ -100,5 +102,5 @@ export const insert = async (payload, context, options = {}) => {
100
102
  code: error
101
103
  });
102
104
  }
103
- return internalInsert(securedPayload, context);
105
+ return applyWriteMiddlewares(securedPayload, context, internalInsert);
104
106
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/core",
3
- "version": "0.0.161",
3
+ "version": "0.0.163",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "aeriaMain": "tests/fixtures/aeriaMain.js",
@@ -42,13 +42,13 @@
42
42
  "mongodb-memory-server": "^9.2.0"
43
43
  },
44
44
  "peerDependencies": {
45
- "@aeriajs/builtins": "^0.0.161",
46
- "@aeriajs/common": "^0.0.98",
47
- "@aeriajs/entrypoint": "^0.0.100",
48
- "@aeriajs/http": "^0.0.111",
49
- "@aeriajs/security": "^0.0.161",
50
- "@aeriajs/types": "^0.0.84",
51
- "@aeriajs/validation": "^0.0.101"
45
+ "@aeriajs/builtins": "^0.0.163",
46
+ "@aeriajs/common": "^0.0.99",
47
+ "@aeriajs/entrypoint": "^0.0.101",
48
+ "@aeriajs/http": "^0.0.112",
49
+ "@aeriajs/security": "^0.0.163",
50
+ "@aeriajs/types": "^0.0.85",
51
+ "@aeriajs/validation": "^0.0.102"
52
52
  },
53
53
  "dependencies": {
54
54
  "mongodb": "^6.5.0",