@aeriajs/core 0.0.174 → 0.0.176

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.
@@ -95,8 +95,9 @@ const recurseSetStage = (reference, path, parentElem, options = {
95
95
  noCond: false,
96
96
  }) => {
97
97
  const refName = path.at(-1);
98
+ const shouldUseArrayIndex = reference.isRecursive && !(reference.isArrayElement && reference.isArray === false);
98
99
  let indexOfArray;
99
- if (reference.isRecursive && !(reference.isArrayElement && reference.isArray === false)) {
100
+ if (shouldUseArrayIndex) {
100
101
  indexOfArray = {
101
102
  $indexOfArray: [
102
103
  `$${getTempName(path)}._id`,
@@ -189,7 +190,7 @@ const recurseSetStage = (reference, path, parentElem, options = {
189
190
  const stages = [];
190
191
  for (const [subRefName, subReference] of Object.entries(reference.deepReferences)) {
191
192
  let newElem;
192
- if (reference.isRecursive) {
193
+ if (shouldUseArrayIndex) {
193
194
  newElem = {
194
195
  $arrayElemAt: [
195
196
  `$${getTempName(path.slice(0, -1))}.${refName}`,
@@ -88,8 +88,9 @@ export const recurseSetStage = (reference, path, parentElem, options = {
88
88
  noCond: false
89
89
  }) => {
90
90
  const refName = path.at(-1);
91
+ const shouldUseArrayIndex = reference.isRecursive && !(reference.isArrayElement && reference.isArray === false);
91
92
  let indexOfArray;
92
- if (reference.isRecursive && !(reference.isArrayElement && reference.isArray === false)) {
93
+ if (shouldUseArrayIndex) {
93
94
  indexOfArray = {
94
95
  $indexOfArray: [
95
96
  `$${getTempName(path)}._id`,
@@ -179,7 +180,7 @@ export const recurseSetStage = (reference, path, parentElem, options = {
179
180
  const stages = [];
180
181
  for (const [subRefName, subReference] of Object.entries(reference.deepReferences)) {
181
182
  let newElem;
182
- if (reference.isRecursive) {
183
+ if (shouldUseArrayIndex) {
183
184
  newElem = {
184
185
  $arrayElemAt: [
185
186
  `$${getTempName(path.slice(0, -1))}.${refName}`,
@@ -27,6 +27,7 @@ exports.traverseDocument = 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");
30
+ const entrypoint_1 = require("@aeriajs/entrypoint");
30
31
  const mongodb_1 = require("mongodb");
31
32
  const assets_js_1 = require("../assets.js");
32
33
  const preload_js_1 = require("./preload.js");
@@ -127,7 +128,7 @@ const autoCast = (value, ctx) => {
127
128
  }
128
129
  }
129
130
  case 'object': {
130
- if (!value || (0, common_1.isObjectId)(value)) {
131
+ if (!value || value instanceof mongodb_1.ObjectId) {
131
132
  return value;
132
133
  }
133
134
  if (!('description' in ctx.options) || !ctx.options.recurseDeep) {
@@ -195,6 +196,10 @@ const moveFiles = async (value, ctx) => {
195
196
  if (!('$ref' in ctx.property) || ctx.property.$ref !== 'file') {
196
197
  return value;
197
198
  }
199
+ const tempFileCollection = await (0, entrypoint_1.getCollection)('tempFile');
200
+ if (!tempFileCollection) {
201
+ throw new Error('The "tempFile" collection is absent, yet it is required to upload files.');
202
+ }
198
203
  if (!isValidTempFile(value)) {
199
204
  return types_1.Result.error(types_1.TraverseError.InvalidTempfile);
200
205
  }
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  import { Result, ACError, ValidationErrorCode, TraverseError } from "@aeriajs/types";
3
- import { throwIfError, pipe, isReference, getValueFromPath, isObjectId, isError } from "@aeriajs/common";
3
+ import { throwIfError, pipe, isReference, getValueFromPath, isError } from "@aeriajs/common";
4
4
  import { makeValidationError, validateProperty, validateWholeness } from "@aeriajs/validation";
5
+ import { getCollection } from "@aeriajs/entrypoint";
5
6
  import { ObjectId } from "mongodb";
6
7
  import { getCollectionAsset } from "../assets.mjs";
7
8
  import { preloadDescription } from "./preload.mjs";
@@ -95,7 +96,7 @@ const autoCast = (value, ctx) => {
95
96
  }
96
97
  }
97
98
  case "object": {
98
- if (!value || isObjectId(value)) {
99
+ if (!value || value instanceof ObjectId) {
99
100
  return value;
100
101
  }
101
102
  if (!("description" in ctx.options) || !ctx.options.recurseDeep) {
@@ -160,6 +161,10 @@ const moveFiles = async (value, ctx) => {
160
161
  if (!("$ref" in ctx.property) || ctx.property.$ref !== "file") {
161
162
  return value;
162
163
  }
164
+ const tempFileCollection = await getCollection("tempFile");
165
+ if (!tempFileCollection) {
166
+ throw new Error('The "tempFile" collection is absent, yet it is required to upload files.');
167
+ }
163
168
  if (!isValidTempFile(value)) {
164
169
  return Result.error(TraverseError.InvalidTempfile);
165
170
  }
@@ -2,5 +2,6 @@ import type { Context, SchemaWithId, GetAllPayload } from '@aeriajs/types';
2
2
  import { Result } from '@aeriajs/types';
3
3
  export type GetAllOptions = {
4
4
  bypassSecurity?: boolean;
5
+ noDefaultLimit?: boolean;
5
6
  };
6
7
  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.defaultPaginationLimit, sort, project, offset = 0, } = payload;
9
+ const { limit, sort, project, offset = 0, } = payload;
10
10
  const filters = payload.filters
11
11
  ? Object.assign({}, payload.filters)
12
12
  : {};
@@ -99,6 +99,9 @@ const getAll = async (payload, context, options = {}) => {
99
99
  if (error) {
100
100
  return types_1.Result.error(error);
101
101
  }
102
+ if (!options.noDefaultLimit) {
103
+ securedPayload.limit ||= context.config.defaultPaginationLimit;
104
+ }
102
105
  return (0, security_1.applyReadMiddlewares)(securedPayload, context, internalGetAll);
103
106
  };
104
107
  exports.getAll = getAll;
@@ -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.defaultPaginationLimit,
13
+ limit,
14
14
  sort,
15
15
  project,
16
16
  offset = 0
@@ -97,5 +97,8 @@ export const getAll = async (payload, context, options = {}) => {
97
97
  if (error) {
98
98
  return Result.error(error);
99
99
  }
100
+ if (!options.noDefaultLimit) {
101
+ securedPayload.limit ||= context.config.defaultPaginationLimit;
102
+ }
100
103
  return applyReadMiddlewares(securedPayload, context, internalGetAll);
101
104
  };
@@ -17,10 +17,10 @@ export declare const insertErrorSchema: () => {
17
17
  readonly required: readonly ["httpStatus", "code"];
18
18
  readonly properties: {
19
19
  readonly httpStatus: {
20
- readonly enum: [HTTPStatus.Forbidden, HTTPStatus.NotFound, HTTPStatus.UnprocessableContent];
20
+ readonly enum: [HTTPStatus.Forbidden, HTTPStatus.NotFound, HTTPStatus.UnprocessableContent, HTTPStatus.BadRequest];
21
21
  };
22
22
  readonly code: {
23
- readonly enum: [ACError.InsecureOperator, ACError.OwnershipError, ACError.ResourceNotFound, ACError.TargetImmutable, ValidationErrorCode.EmptyTarget, ValidationErrorCode.InvalidProperties, ValidationErrorCode.MissingProperties, TraverseError.InvalidDocumentId, TraverseError.InvalidTempfile];
23
+ readonly enum: [ACError.InsecureOperator, ACError.OwnershipError, ACError.ResourceNotFound, ACError.TargetImmutable, ACError.MalformedInput, ValidationErrorCode.EmptyTarget, ValidationErrorCode.InvalidProperties, ValidationErrorCode.MissingProperties, TraverseError.InvalidDocumentId, TraverseError.InvalidTempfile];
24
24
  };
25
25
  readonly message: {
26
26
  readonly type: "string";
@@ -12,12 +12,14 @@ const insertErrorSchema = () => (0, common_1.endpointErrorSchema)({
12
12
  types_1.HTTPStatus.Forbidden,
13
13
  types_1.HTTPStatus.NotFound,
14
14
  types_1.HTTPStatus.UnprocessableContent,
15
+ types_1.HTTPStatus.BadRequest,
15
16
  ],
16
17
  code: [
17
18
  types_1.ACError.InsecureOperator,
18
19
  types_1.ACError.OwnershipError,
19
20
  types_1.ACError.ResourceNotFound,
20
21
  types_1.ACError.TargetImmutable,
22
+ types_1.ACError.MalformedInput,
21
23
  types_1.ValidationErrorCode.EmptyTarget,
22
24
  types_1.ValidationErrorCode.InvalidProperties,
23
25
  types_1.ValidationErrorCode.MissingProperties,
@@ -74,13 +76,16 @@ const internalInsert = async (payload, context) => {
74
76
  ...context,
75
77
  inherited: true,
76
78
  };
77
- const newDocument = (0, common_1.throwIfError)(await (0, get_js_1.get)({
79
+ const { error: getError, result: newDocument } = await (0, get_js_1.get)({
78
80
  filters: {
79
81
  _id: newId,
80
82
  },
81
83
  }, inheritedContext, {
82
84
  bypassSecurity: true,
83
- }));
85
+ });
86
+ if (getError) {
87
+ return types_1.Result.error(getError);
88
+ }
84
89
  return types_1.Result.result(newDocument);
85
90
  };
86
91
  const insert = async (payload, context, options = {}) => {
@@ -2,20 +2,22 @@
2
2
  import { ObjectId } from "mongodb";
3
3
  import { Result, HTTPStatus, ACError, ValidationErrorCode, TraverseError } from "@aeriajs/types";
4
4
  import { useSecurity, applyWriteMiddlewares } from "@aeriajs/security";
5
- import { throwIfError, endpointErrorSchema } from "@aeriajs/common";
5
+ import { endpointErrorSchema } from "@aeriajs/common";
6
6
  import { traverseDocument, prepareCreate, prepareUpdate } from "../collection/index.mjs";
7
7
  import { get } from "./get.mjs";
8
8
  export const insertErrorSchema = () => endpointErrorSchema({
9
9
  httpStatus: [
10
10
  HTTPStatus.Forbidden,
11
11
  HTTPStatus.NotFound,
12
- HTTPStatus.UnprocessableContent
12
+ HTTPStatus.UnprocessableContent,
13
+ HTTPStatus.BadRequest
13
14
  ],
14
15
  code: [
15
16
  ACError.InsecureOperator,
16
17
  ACError.OwnershipError,
17
18
  ACError.ResourceNotFound,
18
19
  ACError.TargetImmutable,
20
+ ACError.MalformedInput,
19
21
  ValidationErrorCode.EmptyTarget,
20
22
  ValidationErrorCode.InvalidProperties,
21
23
  ValidationErrorCode.MissingProperties,
@@ -66,13 +68,16 @@ const internalInsert = async (payload, context) => {
66
68
  ...context,
67
69
  inherited: true
68
70
  };
69
- const newDocument = throwIfError(await get({
71
+ const { error: getError, result: newDocument } = await get({
70
72
  filters: {
71
73
  _id: newId
72
74
  }
73
75
  }, inheritedContext, {
74
76
  bypassSecurity: true
75
- }));
77
+ });
78
+ if (getError) {
79
+ return Result.error(getError);
80
+ }
76
81
  return Result.result(newDocument);
77
82
  };
78
83
  export const insert = async (payload, context, options = {}) => {
@@ -25,7 +25,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.upload = void 0;
27
27
  const types_1 = require("@aeriajs/types");
28
- const entrypoint_1 = require("@aeriajs/entrypoint");
29
28
  const validation_1 = require("@aeriajs/validation");
30
29
  const path = __importStar(require("path"));
31
30
  const fs_1 = require("fs");
@@ -60,10 +59,6 @@ const streamToFs = (metadata, context) => {
60
59
  });
61
60
  };
62
61
  const upload = async (_props, context) => {
63
- const tempFileCollection = await (0, entrypoint_1.getCollection)('tempFile');
64
- if (!tempFileCollection) {
65
- throw new Error('The "tempFile" collection is absent, yet it is required to upload files.');
66
- }
67
62
  const { error: headersError } = (0, validation_1.validate)(context.request.headers, {
68
63
  type: 'object',
69
64
  additionalProperties: true,
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  import { Result, ACError, HTTPStatus } from "@aeriajs/types";
3
- import { getCollection } from "@aeriajs/entrypoint";
4
3
  import { validate, validator } from "@aeriajs/validation";
5
4
  import * as path from "path";
6
5
  import { createWriteStream } from "fs";
@@ -29,10 +28,6 @@ const streamToFs = (metadata, context) => {
29
28
  });
30
29
  };
31
30
  export const upload = async (_props, context) => {
32
- const tempFileCollection = await getCollection("tempFile");
33
- if (!tempFileCollection) {
34
- throw new Error('The "tempFile" collection is absent, yet it is required to upload files.');
35
- }
36
31
  const { error: headersError } = validate(context.request.headers, {
37
32
  type: "object",
38
33
  additionalProperties: true,
package/dist/token.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  import type { SignOptions } from 'jsonwebtoken';
2
- export declare const EXPIRES_IN = 36000;
3
2
  export declare const signToken: ({ iat, exp, ...payload }: Record<string, unknown>, secret?: string | null, options?: SignOptions) => Promise<string>;
4
3
  export declare const decodeToken: <TToken>(token: string, secret?: string) => Promise<TToken>;
package/dist/token.js CHANGED
@@ -3,24 +3,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.decodeToken = exports.signToken = exports.EXPIRES_IN = void 0;
6
+ exports.decodeToken = exports.signToken = void 0;
7
7
  const entrypoint_1 = require("@aeriajs/entrypoint");
8
8
  const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
9
- exports.EXPIRES_IN = 36000;
10
- const getApplicationSecret = async () => {
9
+ const getTokenConfig = async () => {
11
10
  const config = await (0, entrypoint_1.getConfig)();
12
11
  if (!config.secret) {
13
12
  throw new Error('application secret is not set');
14
13
  }
15
- return config.secret;
14
+ return {
15
+ name: config.name,
16
+ secret: config.secret,
17
+ tokenExpiration: config.security.tokenExpiration,
18
+ };
16
19
  };
17
20
  const signToken = async ({ iat, exp, ...payload }, secret, options) => {
18
- return jsonwebtoken_1.default.sign(payload, secret || await getApplicationSecret(), options || {
19
- expiresIn: exports.EXPIRES_IN,
21
+ const tokenConfig = await getTokenConfig();
22
+ if (tokenConfig.name) {
23
+ payload.aud = tokenConfig.name;
24
+ }
25
+ return jsonwebtoken_1.default.sign(payload, secret || tokenConfig.secret, options || {
26
+ expiresIn: tokenConfig.tokenExpiration,
20
27
  });
21
28
  };
22
29
  exports.signToken = signToken;
23
30
  const decodeToken = async (token, secret) => {
24
- return jsonwebtoken_1.default.verify(token, secret || await getApplicationSecret());
31
+ const tokenConfig = await getTokenConfig();
32
+ return jsonwebtoken_1.default.verify(token, secret || tokenConfig.secret);
25
33
  };
26
34
  exports.decodeToken = decodeToken;
package/dist/token.mjs CHANGED
@@ -1,19 +1,27 @@
1
1
  "use strict";
2
2
  import { getConfig } from "@aeriajs/entrypoint";
3
3
  import jwt from "jsonwebtoken";
4
- export const EXPIRES_IN = 36e3;
5
- const getApplicationSecret = async () => {
4
+ const getTokenConfig = async () => {
6
5
  const config = await getConfig();
7
6
  if (!config.secret) {
8
7
  throw new Error("application secret is not set");
9
8
  }
10
- return config.secret;
9
+ return {
10
+ name: config.name,
11
+ secret: config.secret,
12
+ tokenExpiration: config.security.tokenExpiration
13
+ };
11
14
  };
12
15
  export const signToken = async ({ iat, exp, ...payload }, secret, options) => {
13
- return jwt.sign(payload, secret || await getApplicationSecret(), options || {
14
- expiresIn: EXPIRES_IN
16
+ const tokenConfig = await getTokenConfig();
17
+ if (tokenConfig.name) {
18
+ payload.aud = tokenConfig.name;
19
+ }
20
+ return jwt.sign(payload, secret || tokenConfig.secret, options || {
21
+ expiresIn: tokenConfig.tokenExpiration
15
22
  });
16
23
  };
17
24
  export const decodeToken = async (token, secret) => {
18
- return jwt.verify(token, secret || await getApplicationSecret());
25
+ const tokenConfig = await getTokenConfig();
26
+ return jwt.verify(token, secret || tokenConfig.secret);
19
27
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/core",
3
- "version": "0.0.174",
3
+ "version": "0.0.176",
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.174",
46
- "@aeriajs/common": "^0.0.105",
47
- "@aeriajs/entrypoint": "^0.0.107",
48
- "@aeriajs/http": "^0.0.118",
49
- "@aeriajs/security": "^0.0.174",
50
- "@aeriajs/types": "^0.0.88",
51
- "@aeriajs/validation": "^0.0.108"
45
+ "@aeriajs/builtins": "^0.0.176",
46
+ "@aeriajs/common": "^0.0.106",
47
+ "@aeriajs/entrypoint": "^0.0.109",
48
+ "@aeriajs/http": "^0.0.120",
49
+ "@aeriajs/security": "^0.0.176",
50
+ "@aeriajs/types": "^0.0.89",
51
+ "@aeriajs/validation": "^0.0.109"
52
52
  },
53
53
  "dependencies": {
54
54
  "mongodb": "^6.5.0",