@aeriajs/core 0.0.123 → 0.0.124

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.
@@ -1,5 +1,5 @@
1
1
  import type { Description, Property, ValidationError, Context } from '@aeriajs/types';
2
- import { ACError } from '@aeriajs/types';
2
+ import { ACError, TraverseError } from '@aeriajs/types';
3
3
  import { ObjectId } from 'mongodb';
4
4
  export type TraverseOptions = {
5
5
  autoCast?: boolean;
@@ -18,10 +18,6 @@ export type TraverseNormalized = {
18
18
  description: Description;
19
19
  pipe: <T = unknown>(value: unknown, phaseContext: PhaseContext) => T | Promise<T>;
20
20
  };
21
- export declare enum TraverseError {
22
- InvalidDocumentId = "INVALID_DOCUMENT_ID",
23
- InvalidTempfile = "INVALID_TEMPFILE"
24
- }
25
21
  type PhaseContext = {
26
22
  target: any;
27
23
  root: {
@@ -31,6 +27,7 @@ type PhaseContext = {
31
27
  propPath: string;
32
28
  property: Property;
33
29
  options: TraverseOptions & TraverseNormalized;
30
+ isArray?: boolean;
34
31
  };
35
32
  export declare const traverseDocument: <const TWhat extends Record<string, unknown>>(what: TWhat, description: Description, _options: TraverseOptions) => Promise<{
36
33
  readonly _tag: "Result";
@@ -38,7 +35,7 @@ export declare const traverseDocument: <const TWhat extends Record<string, unkno
38
35
  readonly result: any;
39
36
  } | {
40
37
  readonly _tag: "Error";
41
- readonly error: ACError.InsecureOperator | ValidationError;
38
+ readonly error: ACError.InsecureOperator | ValidationError | TraverseError;
42
39
  readonly result: undefined;
43
40
  }>;
44
41
  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.TraverseError = void 0;
26
+ 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");
@@ -31,11 +31,6 @@ 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 TraverseError;
35
- (function (TraverseError) {
36
- TraverseError["InvalidDocumentId"] = "INVALID_DOCUMENT_ID";
37
- TraverseError["InvalidTempfile"] = "INVALID_TEMPFILE";
38
- })(TraverseError || (exports.TraverseError = TraverseError = {}));
39
34
  const getProperty = (propertyName, parentProperty) => {
40
35
  if (propertyName === '_id') {
41
36
  return {
@@ -53,7 +48,7 @@ const getProperty = (propertyName, parentProperty) => {
53
48
  }
54
49
  };
55
50
  const disposeOldFiles = async (ctx, options = {}) => {
56
- if (!options.fromIds && Array.isArray(ctx.target[ctx.propName])) {
51
+ if (!options.preserveIds && Array.isArray(ctx.target[ctx.propName])) {
57
52
  return;
58
53
  }
59
54
  const context = ctx.options.context;
@@ -65,12 +60,12 @@ const disposeOldFiles = async (ctx, options = {}) => {
65
60
  },
66
61
  });
67
62
  if (!doc) {
68
- return types_1.Result.error(TraverseError.InvalidDocumentId);
63
+ return types_1.Result.error(types_1.TraverseError.InvalidDocumentId);
69
64
  }
70
65
  let fileIds = (0, common_1.getValueFromPath)(doc, ctx.propPath);
71
- if (options.fromIds) {
72
- fileIds = fileIds.filter((id) => !options.fromIds.some((fromId) => {
73
- return !id || id.equals(fromId);
66
+ if (options.preserveIds) {
67
+ fileIds = fileIds.filter((id) => !id || !options.preserveIds.some((fromId) => {
68
+ return id.equals(fromId);
74
69
  }));
75
70
  }
76
71
  const fileFilters = {
@@ -187,7 +182,7 @@ const moveFiles = async (value, ctx) => {
187
182
  throw new Error();
188
183
  }
189
184
  if (!value) {
190
- if (ctx.root._id) {
185
+ if (ctx.root._id && !ctx.isArray) {
191
186
  await disposeOldFiles(ctx);
192
187
  }
193
188
  return null;
@@ -196,9 +191,9 @@ const moveFiles = async (value, ctx) => {
196
191
  _id: new mongodb_1.ObjectId(value.tempId),
197
192
  });
198
193
  if (!tempFile) {
199
- return types_1.Result.error(TraverseError.InvalidTempfile);
194
+ return types_1.Result.error(types_1.TraverseError.InvalidTempfile);
200
195
  }
201
- if (ctx.root._id) {
196
+ if (ctx.root._id && !ctx.isArray) {
202
197
  await disposeOldFiles(ctx);
203
198
  }
204
199
  const { _id: fileId, ...newFile } = tempFile;
@@ -227,12 +222,13 @@ const recurseDeep = async (value, ctx) => {
227
222
  const result = await ctx.options.pipe(item, {
228
223
  ...ctx,
229
224
  property: ctx.property.items,
225
+ isArray: true,
230
226
  });
231
227
  items.push(result);
232
228
  }
233
229
  if (ctx.options.moveFiles && '$ref' in ctx.property.items && ctx.property.items.$ref === 'file') {
234
230
  await disposeOldFiles(ctx, {
235
- fromIds: items,
231
+ preserveIds: items,
236
232
  });
237
233
  }
238
234
  return items;
@@ -395,6 +391,7 @@ const traverseDocument = async (what, description, _options) => {
395
391
  if (options.moveFiles) {
396
392
  functions.push(moveFiles);
397
393
  }
394
+ let traverseError;
398
395
  let validationError;
399
396
  const mutateTarget = (fn) => {
400
397
  return async (value, ctx) => {
@@ -409,8 +406,16 @@ const traverseDocument = async (what, description, _options) => {
409
406
  Object.assign(options, {
410
407
  pipe: (0, common_1.pipe)(functions.map(mutateTarget), {
411
408
  returnFirst: (value) => {
412
- if (value?._tag === 'Left') {
413
- validationError = value.value;
409
+ if ((0, common_1.isError)(value)) {
410
+ const error = value.error;
411
+ switch (error) {
412
+ case types_1.TraverseError.InvalidDocumentId:
413
+ case types_1.TraverseError.InvalidTempfile:
414
+ traverseError = error;
415
+ break;
416
+ default:
417
+ validationError = error;
418
+ }
414
419
  return value;
415
420
  }
416
421
  },
@@ -425,6 +430,9 @@ const traverseDocument = async (what, description, _options) => {
425
430
  if (error) {
426
431
  return types_1.Result.error(error);
427
432
  }
433
+ if (traverseError) {
434
+ return types_1.Result.error(traverseError);
435
+ }
428
436
  if (validationError) {
429
437
  return types_1.Result.error((0, validation_1.makeValidationError)({
430
438
  code: types_1.ValidationErrorCode.InvalidProperties,
@@ -1,16 +1,11 @@
1
1
  "use strict";
2
- import { Result, ACError, ValidationErrorCode } from "@aeriajs/types";
3
- import { throwIfError, pipe, isReference, getValueFromPath, isObjectId } from "@aeriajs/common";
2
+ import { Result, ACError, ValidationErrorCode, TraverseError } from "@aeriajs/types";
3
+ import { throwIfError, pipe, isReference, getValueFromPath, isObjectId, isError } 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 TraverseError = /* @__PURE__ */ ((TraverseError2) => {
10
- TraverseError2["InvalidDocumentId"] = "INVALID_DOCUMENT_ID";
11
- TraverseError2["InvalidTempfile"] = "INVALID_TEMPFILE";
12
- return TraverseError2;
13
- })(TraverseError || {});
14
9
  const getProperty = (propertyName, parentProperty) => {
15
10
  if (propertyName === "_id") {
16
11
  return {
@@ -28,7 +23,7 @@ const getProperty = (propertyName, parentProperty) => {
28
23
  }
29
24
  };
30
25
  const disposeOldFiles = async (ctx, options = {}) => {
31
- if (!options.fromIds && Array.isArray(ctx.target[ctx.propName])) {
26
+ if (!options.preserveIds && Array.isArray(ctx.target[ctx.propName])) {
32
27
  return;
33
28
  }
34
29
  const context = ctx.options.context;
@@ -40,12 +35,12 @@ const disposeOldFiles = async (ctx, options = {}) => {
40
35
  }
41
36
  });
42
37
  if (!doc) {
43
- return Result.error("INVALID_DOCUMENT_ID" /* InvalidDocumentId */);
38
+ return Result.error(TraverseError.InvalidDocumentId);
44
39
  }
45
40
  let fileIds = getValueFromPath(doc, ctx.propPath);
46
- if (options.fromIds) {
47
- fileIds = fileIds.filter((id) => !options.fromIds.some((fromId) => {
48
- return !id || id.equals(fromId);
41
+ if (options.preserveIds) {
42
+ fileIds = fileIds.filter((id) => !id || !options.preserveIds.some((fromId) => {
43
+ return id.equals(fromId);
49
44
  }));
50
45
  }
51
46
  const fileFilters = {
@@ -153,7 +148,7 @@ const moveFiles = async (value, ctx) => {
153
148
  throw new Error();
154
149
  }
155
150
  if (!value) {
156
- if (ctx.root._id) {
151
+ if (ctx.root._id && !ctx.isArray) {
157
152
  await disposeOldFiles(ctx);
158
153
  }
159
154
  return null;
@@ -162,9 +157,9 @@ const moveFiles = async (value, ctx) => {
162
157
  _id: new ObjectId(value.tempId)
163
158
  });
164
159
  if (!tempFile) {
165
- return Result.error("INVALID_TEMPFILE" /* InvalidTempfile */);
160
+ return Result.error(TraverseError.InvalidTempfile);
166
161
  }
167
- if (ctx.root._id) {
162
+ if (ctx.root._id && !ctx.isArray) {
168
163
  await disposeOldFiles(ctx);
169
164
  }
170
165
  const { _id: fileId, ...newFile } = tempFile;
@@ -192,13 +187,14 @@ const recurseDeep = async (value, ctx) => {
192
187
  for (const item of value) {
193
188
  const result = await ctx.options.pipe(item, {
194
189
  ...ctx,
195
- property: ctx.property.items
190
+ property: ctx.property.items,
191
+ isArray: true
196
192
  });
197
193
  items.push(result);
198
194
  }
199
195
  if (ctx.options.moveFiles && "$ref" in ctx.property.items && ctx.property.items.$ref === "file") {
200
196
  await disposeOldFiles(ctx, {
201
- fromIds: items
197
+ preserveIds: items
202
198
  });
203
199
  }
204
200
  return items;
@@ -353,6 +349,7 @@ export const traverseDocument = async (what, description, _options) => {
353
349
  if (options.moveFiles) {
354
350
  functions.push(moveFiles);
355
351
  }
352
+ let traverseError;
356
353
  let validationError;
357
354
  const mutateTarget = (fn) => {
358
355
  return async (value, ctx) => {
@@ -367,8 +364,16 @@ export const traverseDocument = async (what, description, _options) => {
367
364
  Object.assign(options, {
368
365
  pipe: pipe(functions.map(mutateTarget), {
369
366
  returnFirst: (value) => {
370
- if (value?._tag === "Left") {
371
- validationError = value.value;
367
+ if (isError(value)) {
368
+ const error2 = value.error;
369
+ switch (error2) {
370
+ case TraverseError.InvalidDocumentId:
371
+ case TraverseError.InvalidTempfile:
372
+ traverseError = error2;
373
+ break;
374
+ default:
375
+ validationError = error2;
376
+ }
372
377
  return value;
373
378
  }
374
379
  }
@@ -383,6 +388,9 @@ export const traverseDocument = async (what, description, _options) => {
383
388
  if (error) {
384
389
  return Result.error(error);
385
390
  }
391
+ if (traverseError) {
392
+ return Result.error(traverseError);
393
+ }
386
394
  if (validationError) {
387
395
  return Result.error(makeValidationError({
388
396
  code: ValidationErrorCode.InvalidProperties,
@@ -1,5 +1,5 @@
1
1
  import type { Context, SchemaWithId, InsertPayload, InsertReturnType } from '@aeriajs/types';
2
- import { HTTPStatus, ACError, ValidationErrorCode } from '@aeriajs/types';
2
+ import { HTTPStatus, ACError, ValidationErrorCode, TraverseError } from '@aeriajs/types';
3
3
  export type InsertOptions = {
4
4
  bypassSecurity?: boolean;
5
5
  };
@@ -20,7 +20,7 @@ export declare const insertErrorSchema: () => {
20
20
  readonly enum: [HTTPStatus.UnprocessableContent, HTTPStatus.NotFound];
21
21
  };
22
22
  readonly code: {
23
- readonly enum: [ACError.InsecureOperator, ACError.OwnershipError, ACError.ResourceNotFound, ACError.TargetImmutable, ValidationErrorCode.EmptyTarget, ValidationErrorCode.InvalidProperties, ValidationErrorCode.MissingProperties];
23
+ readonly enum: [ACError.InsecureOperator, ACError.OwnershipError, ACError.ResourceNotFound, ACError.TargetImmutable, ValidationErrorCode.EmptyTarget, ValidationErrorCode.InvalidProperties, ValidationErrorCode.MissingProperties, TraverseError.InvalidDocumentId, TraverseError.InvalidTempfile];
24
24
  };
25
25
  readonly message: {
26
26
  readonly type: "string";
@@ -18,6 +18,8 @@ const insertErrorSchema = () => (0, common_1.endpointErrorSchema)({
18
18
  types_1.ValidationErrorCode.EmptyTarget,
19
19
  types_1.ValidationErrorCode.InvalidProperties,
20
20
  types_1.ValidationErrorCode.MissingProperties,
21
+ types_1.TraverseError.InvalidDocumentId,
22
+ types_1.TraverseError.InvalidTempfile,
21
23
  ],
22
24
  });
23
25
  exports.insertErrorSchema = insertErrorSchema;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- import { Result, HTTPStatus, ACError, ValidationErrorCode } from "@aeriajs/types";
2
+ import { Result, HTTPStatus, ACError, ValidationErrorCode, TraverseError } from "@aeriajs/types";
3
3
  import { useSecurity } from "@aeriajs/security";
4
4
  import { throwIfError, endpointErrorSchema } from "@aeriajs/common";
5
5
  import { traverseDocument, normalizeProjection, prepareInsert, fill } from "../../collection/index.mjs";
@@ -15,7 +15,9 @@ export const insertErrorSchema = () => endpointErrorSchema({
15
15
  ACError.TargetImmutable,
16
16
  ValidationErrorCode.EmptyTarget,
17
17
  ValidationErrorCode.InvalidProperties,
18
- ValidationErrorCode.MissingProperties
18
+ ValidationErrorCode.MissingProperties,
19
+ TraverseError.InvalidDocumentId,
20
+ TraverseError.InvalidTempfile
19
21
  ]
20
22
  });
21
23
  export const insert = async (payload, context, options) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/core",
3
- "version": "0.0.123",
3
+ "version": "0.0.124",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -41,13 +41,13 @@
41
41
  "mongodb-memory-server": "^9.2.0"
42
42
  },
43
43
  "peerDependencies": {
44
- "@aeriajs/builtins": "^0.0.123",
45
- "@aeriajs/common": "^0.0.77",
46
- "@aeriajs/entrypoint": "^0.0.79",
47
- "@aeriajs/http": "^0.0.88",
48
- "@aeriajs/security": "^0.0.123",
49
- "@aeriajs/types": "^0.0.67",
50
- "@aeriajs/validation": "^0.0.80"
44
+ "@aeriajs/builtins": "^0.0.124",
45
+ "@aeriajs/common": "^0.0.78",
46
+ "@aeriajs/entrypoint": "^0.0.80",
47
+ "@aeriajs/http": "^0.0.89",
48
+ "@aeriajs/security": "^0.0.124",
49
+ "@aeriajs/types": "^0.0.68",
50
+ "@aeriajs/validation": "^0.0.81"
51
51
  },
52
52
  "dependencies": {
53
53
  "mongodb": "^6.5.0",