@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
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
+ import { HTTPStatus, ACError } from "@aeriajs/types";
2
3
  import { useSecurity } from "@aeriajs/security";
3
- import { unsafe } from "@aeriajs/common";
4
+ import { throwIfLeft } from "@aeriajs/common";
4
5
  import {
5
6
  traverseDocument,
6
7
  normalizeProjection,
@@ -13,7 +14,7 @@ export const get = async (payload, context, options) => {
13
14
  const {
14
15
  filters = {},
15
16
  project = []
16
- } = !options?.bypassSecurity ? unsafe(await security.beforeRead(payload)) : payload;
17
+ } = !options?.bypassSecurity ? throwIfLeft(await security.beforeRead(payload)) : payload;
17
18
  if (Object.keys(filters).length === 0) {
18
19
  throw new Error("no filters were passed");
19
20
  }
@@ -22,7 +23,7 @@ export const get = async (payload, context, options) => {
22
23
  memoize: context.description.$id
23
24
  });
24
25
  pipeline.push({
25
- $match: unsafe(await traverseDocument(filters, context.description, {
26
+ $match: throwIfLeft(await traverseDocument(filters, context.description, {
26
27
  autoCast: true,
27
28
  allowOperators: true
28
29
  }))
@@ -40,9 +41,11 @@ export const get = async (payload, context, options) => {
40
41
  }));
41
42
  const result = await context.collection.model.aggregate(pipeline).next();
42
43
  if (!result) {
43
- return null;
44
+ return context.error(HTTPStatus.NotFound, {
45
+ code: ACError.ResourceNotFound
46
+ });
44
47
  }
45
- return fill(unsafe(await traverseDocument(result, context.description, {
48
+ return fill(throwIfLeft(await traverseDocument(result, context.description, {
46
49
  getters: true,
47
50
  fromProperties: true,
48
51
  recurseReferences: true,
@@ -8,7 +8,7 @@ const getAll = async (_payload, context, options = {}) => {
8
8
  const security = (0, security_1.useSecurity)(context);
9
9
  const payload = _payload || {};
10
10
  const { filters = {}, limit = context.config.paginationLimit, sort, project = [], offset = 0, } = !options.bypassSecurity
11
- ? (0, common_1.unsafe)(await security.beforeRead(payload))
11
+ ? (0, common_1.throwIfLeft)(await security.beforeRead(payload))
12
12
  : payload;
13
13
  const { $text, ...filtersRest } = filters;
14
14
  const pipeline = [];
@@ -36,7 +36,7 @@ const getAll = async (_payload, context, options = {}) => {
36
36
  }
37
37
  if (Object.keys(filtersRest).length > 0) {
38
38
  pipeline.push({
39
- $match: (0, common_1.unsafe)(await (0, index_js_1.traverseDocument)(filtersRest, context.description, {
39
+ $match: (0, common_1.throwIfLeft)(await (0, index_js_1.traverseDocument)(filtersRest, context.description, {
40
40
  autoCast: true,
41
41
  allowOperators: true,
42
42
  })),
@@ -69,7 +69,7 @@ const getAll = async (_payload, context, options = {}) => {
69
69
  const result = await context.collection.model.aggregate(pipeline).toArray();
70
70
  const documents = [];
71
71
  for (const document of result) {
72
- documents.push((0, common_1.unsafe)(await (0, index_js_1.traverseDocument)((0, index_js_1.fill)(document, context.description), context.description, {
72
+ documents.push((0, common_1.throwIfLeft)(await (0, index_js_1.traverseDocument)((0, index_js_1.fill)(document, context.description), context.description, {
73
73
  getters: true,
74
74
  fromProperties: true,
75
75
  recurseReferences: true,
@@ -1,6 +1,6 @@
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 {
5
5
  traverseDocument,
6
6
  normalizeProjection,
@@ -17,7 +17,7 @@ export const getAll = async (_payload, context, options = {}) => {
17
17
  sort,
18
18
  project = [],
19
19
  offset = 0
20
- } = !options.bypassSecurity ? unsafe(await security.beforeRead(payload)) : payload;
20
+ } = !options.bypassSecurity ? throwIfLeft(await security.beforeRead(payload)) : payload;
21
21
  const { $text, ...filtersRest } = filters;
22
22
  const pipeline = [];
23
23
  const references = await getReferences(context.description.properties, {
@@ -40,7 +40,7 @@ export const getAll = async (_payload, context, options = {}) => {
40
40
  }
41
41
  if (Object.keys(filtersRest).length > 0) {
42
42
  pipeline.push({
43
- $match: unsafe(await traverseDocument(filtersRest, context.description, {
43
+ $match: throwIfLeft(await traverseDocument(filtersRest, context.description, {
44
44
  autoCast: true,
45
45
  allowOperators: true
46
46
  }))
@@ -73,7 +73,7 @@ export const getAll = async (_payload, context, options = {}) => {
73
73
  const result = await context.collection.model.aggregate(pipeline).toArray();
74
74
  const documents = [];
75
75
  for (const document of result) {
76
- documents.push(unsafe(await traverseDocument(fill(document, context.description), context.description, {
76
+ documents.push(throwIfLeft(await traverseDocument(fill(document, context.description), context.description, {
77
77
  getters: true,
78
78
  fromProperties: true,
79
79
  recurseReferences: true,
@@ -1,5 +1,33 @@
1
- import type { Context, SchemaWithId, InsertPayload } from '@aeriajs/types';
1
+ import type { Context, SchemaWithId, InsertPayload, InsertReturnType } from '@aeriajs/types';
2
+ import { HTTPStatus, ACError, ValidationErrorCode } from '@aeriajs/types';
2
3
  export type InsertOptions = {
3
4
  bypassSecurity?: boolean;
4
5
  };
5
- export declare const insert: <TContext extends Context>(payload: InsertPayload<SchemaWithId<TContext['description']>>, context: TContext, options?: InsertOptions) => Promise<import("@aeriajs/types").Left<import("@aeriajs/types").ACError | import("@aeriajs/types").ValidationError> | import("@aeriajs/types").Right<SchemaWithId<TContext["description"]>>>;
6
+ export declare const insertErrorSchema: {
7
+ readonly type: "object";
8
+ readonly properties: {
9
+ readonly _tag: {
10
+ readonly const: "Error";
11
+ };
12
+ readonly value: {
13
+ readonly type: "object";
14
+ readonly required: readonly ["httpStatus", "code"];
15
+ readonly properties: {
16
+ readonly httpStatus: {
17
+ readonly enum: [HTTPStatus.UnprocessableContent, HTTPStatus.NotFound];
18
+ };
19
+ readonly code: {
20
+ readonly enum: [ACError.InsecureOperator, ACError.OwnershipError, ACError.ResourceNotFound, ACError.TargetImmutable, ValidationErrorCode.EmptyTarget, ValidationErrorCode.InvalidProperties, ValidationErrorCode.MissingProperties];
21
+ };
22
+ readonly message: {
23
+ readonly type: "string";
24
+ };
25
+ readonly details: {
26
+ readonly type: "object";
27
+ readonly variable: true;
28
+ };
29
+ };
30
+ };
31
+ };
32
+ };
33
+ export declare const insert: <TContext extends Context>(payload: InsertPayload<SchemaWithId<TContext['description']>>, context: TContext, options?: InsertOptions) => Promise<InsertReturnType<SchemaWithId<TContext['description']>>>;
@@ -1,13 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.insert = void 0;
3
+ exports.insert = exports.insertErrorSchema = 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");
8
+ exports.insertErrorSchema = (0, common_1.endpointErrorSchema)({
9
+ httpStatus: [
10
+ types_1.HTTPStatus.UnprocessableContent,
11
+ types_1.HTTPStatus.NotFound,
12
+ ],
13
+ code: [
14
+ types_1.ACError.InsecureOperator,
15
+ types_1.ACError.OwnershipError,
16
+ types_1.ACError.ResourceNotFound,
17
+ types_1.ACError.TargetImmutable,
18
+ types_1.ValidationErrorCode.EmptyTarget,
19
+ types_1.ValidationErrorCode.InvalidProperties,
20
+ types_1.ValidationErrorCode.MissingProperties,
21
+ ],
22
+ });
7
23
  const insert = async (payload, context, options) => {
8
24
  const security = (0, security_1.useSecurity)(context);
9
25
  const query = !options?.bypassSecurity
10
- ? (0, common_1.unsafe)(await security.beforeWrite(payload))
26
+ ? (0, common_1.throwIfLeft)(await security.beforeWrite(payload))
11
27
  : payload;
12
28
  const whatEither = await (0, index_js_1.traverseDocument)(query.what, context.description, {
13
29
  recurseDeep: true,
@@ -21,52 +37,69 @@ const insert = async (payload, context, options) => {
21
37
  });
22
38
  if ((0, common_1.isLeft)(whatEither)) {
23
39
  const error = (0, common_1.unwrapEither)(whatEither);
24
- return (0, common_1.left)(error);
40
+ if (typeof error === 'string') {
41
+ return context.error(types_1.HTTPStatus.UnprocessableContent, {
42
+ code: error,
43
+ });
44
+ }
45
+ return context.error(types_1.HTTPStatus.UnprocessableContent, {
46
+ code: error.code,
47
+ details: error.errors,
48
+ });
25
49
  }
26
50
  const what = (0, common_1.unwrapEither)(whatEither);
27
51
  const docId = '_id' in what
28
52
  ? what._id
29
53
  : null;
30
- const readyWhat = (0, index_js_1.prepareInsert)(what, context.description);
54
+ const content = (0, index_js_1.prepareInsert)(what, context.description);
31
55
  const projection = payload.project
32
56
  ? (0, index_js_1.normalizeProjection)(payload.project, context.description)
33
57
  : {};
34
58
  let newId = docId;
35
59
  if (!newId) {
36
60
  const now = new Date();
37
- Object.assign(readyWhat, {
61
+ Object.assign(content, {
38
62
  created_at: now,
39
63
  updated_at: now,
40
64
  });
41
- newId = (await context.collection.model.insertOne(readyWhat)).insertedId;
65
+ newId = (await context.collection.model.insertOne(content)).insertedId;
42
66
  }
43
67
  else {
44
- readyWhat.$set ??= {};
45
- readyWhat.$set.updated_at = new Date();
68
+ content.$set ??= {};
69
+ content.$set.updated_at = new Date();
46
70
  await context.collection.model.updateOne({
47
71
  _id: docId,
48
- }, readyWhat);
72
+ }, content);
49
73
  }
74
+ let doc;
50
75
  if (context.collection.originalFunctions.get) {
51
- const document = await context.collection.originalFunctions.get({
76
+ doc = await context.collection.originalFunctions.get({
52
77
  filters: {
53
78
  _id: newId,
54
79
  },
55
- }, context, {
80
+ }, Object.assign({
81
+ inherited: true,
82
+ }, context), {
56
83
  bypassSecurity: true,
57
84
  });
58
- return (0, common_1.right)(document);
59
85
  }
60
- const document = await context.collection.model.findOne({
61
- _id: newId,
62
- }, {
63
- projection,
64
- });
65
- const result = (0, common_1.unsafe)(await (0, index_js_1.traverseDocument)(document, context.description, {
86
+ else {
87
+ doc = await context.collection.model.findOne({
88
+ _id: newId,
89
+ }, {
90
+ projection,
91
+ });
92
+ }
93
+ if (!doc) {
94
+ return context.error(types_1.HTTPStatus.NotFound, {
95
+ code: types_1.ACError.ResourceNotFound,
96
+ });
97
+ }
98
+ const result = (0, common_1.throwIfLeft)(await (0, index_js_1.traverseDocument)(doc, context.description, {
66
99
  getters: true,
67
100
  fromProperties: true,
68
101
  recurseReferences: true,
69
102
  }));
70
- return (0, common_1.right)(result);
103
+ return result;
71
104
  };
72
105
  exports.insert = insert;
@@ -1,10 +1,26 @@
1
1
  "use strict";
2
+ import { HTTPStatus, ACError, ValidationErrorCode } from "@aeriajs/types";
2
3
  import { useSecurity } from "@aeriajs/security";
3
- import { left, right, isLeft, unwrapEither, unsafe } from "@aeriajs/common";
4
+ import { isLeft, unwrapEither, throwIfLeft, endpointErrorSchema } from "@aeriajs/common";
4
5
  import { traverseDocument, normalizeProjection, prepareInsert } from "../../collection/index.mjs";
6
+ export const insertErrorSchema = endpointErrorSchema({
7
+ httpStatus: [
8
+ HTTPStatus.UnprocessableContent,
9
+ HTTPStatus.NotFound
10
+ ],
11
+ code: [
12
+ ACError.InsecureOperator,
13
+ ACError.OwnershipError,
14
+ ACError.ResourceNotFound,
15
+ ACError.TargetImmutable,
16
+ ValidationErrorCode.EmptyTarget,
17
+ ValidationErrorCode.InvalidProperties,
18
+ ValidationErrorCode.MissingProperties
19
+ ]
20
+ });
5
21
  export const insert = async (payload, context, options) => {
6
22
  const security = useSecurity(context);
7
- const query = !options?.bypassSecurity ? unsafe(await security.beforeWrite(payload)) : payload;
23
+ const query = !options?.bypassSecurity ? throwIfLeft(await security.beforeWrite(payload)) : payload;
8
24
  const whatEither = await traverseDocument(query.what, context.description, {
9
25
  recurseDeep: true,
10
26
  autoCast: true,
@@ -15,46 +31,62 @@ export const insert = async (payload, context, options) => {
15
31
  });
16
32
  if (isLeft(whatEither)) {
17
33
  const error = unwrapEither(whatEither);
18
- return left(error);
34
+ if (typeof error === "string") {
35
+ return context.error(HTTPStatus.UnprocessableContent, {
36
+ code: error
37
+ });
38
+ }
39
+ return context.error(HTTPStatus.UnprocessableContent, {
40
+ code: error.code,
41
+ details: error.errors
42
+ });
19
43
  }
20
44
  const what = unwrapEither(whatEither);
21
45
  const docId = "_id" in what ? what._id : null;
22
- const readyWhat = prepareInsert(what, context.description);
46
+ const content = prepareInsert(what, context.description);
23
47
  const projection = payload.project ? normalizeProjection(payload.project, context.description) : {};
24
48
  let newId = docId;
25
49
  if (!newId) {
26
50
  const now = /* @__PURE__ */ new Date();
27
- Object.assign(readyWhat, {
51
+ Object.assign(content, {
28
52
  created_at: now,
29
53
  updated_at: now
30
54
  });
31
- newId = (await context.collection.model.insertOne(readyWhat)).insertedId;
55
+ newId = (await context.collection.model.insertOne(content)).insertedId;
32
56
  } else {
33
- readyWhat.$set ??= {};
34
- readyWhat.$set.updated_at = /* @__PURE__ */ new Date();
57
+ content.$set ??= {};
58
+ content.$set.updated_at = /* @__PURE__ */ new Date();
35
59
  await context.collection.model.updateOne({
36
60
  _id: docId
37
- }, readyWhat);
61
+ }, content);
38
62
  }
63
+ let doc;
39
64
  if (context.collection.originalFunctions.get) {
40
- const document2 = await context.collection.originalFunctions.get({
65
+ doc = await context.collection.originalFunctions.get({
41
66
  filters: {
42
67
  _id: newId
43
68
  }
44
- }, context, {
69
+ }, Object.assign({
70
+ inherited: true
71
+ }, context), {
45
72
  bypassSecurity: true
46
73
  });
47
- return right(document2);
74
+ } else {
75
+ doc = await context.collection.model.findOne({
76
+ _id: newId
77
+ }, {
78
+ projection
79
+ });
48
80
  }
49
- const document = await context.collection.model.findOne({
50
- _id: newId
51
- }, {
52
- projection
53
- });
54
- const result = unsafe(await traverseDocument(document, context.description, {
81
+ if (!doc) {
82
+ return context.error(HTTPStatus.NotFound, {
83
+ code: ACError.ResourceNotFound
84
+ });
85
+ }
86
+ const result = throwIfLeft(await traverseDocument(doc, context.description, {
55
87
  getters: true,
56
88
  fromProperties: true,
57
89
  recurseReferences: true
58
90
  }));
59
- return right(result);
91
+ return result;
60
92
  };
@@ -1,21 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.remove = void 0;
4
+ const types_1 = require("@aeriajs/types");
4
5
  const common_1 = require("@aeriajs/common");
5
6
  const index_js_1 = require("../../collection/index.js");
6
7
  const remove = async (payload, context) => {
7
8
  if (!payload.filters._id) {
8
- return (0, common_1.left)({
9
- message: 'you must pass an _id as filter',
9
+ return context.error(types_1.HTTPStatus.NotFound, {
10
+ code: types_1.ACError.ResourceNotFound,
10
11
  });
11
12
  }
12
- const filters = (0, common_1.unsafe)(await (0, index_js_1.traverseDocument)(payload.filters, context.description, {
13
+ const filters = (0, common_1.throwIfLeft)(await (0, index_js_1.traverseDocument)(payload.filters, context.description, {
13
14
  autoCast: true,
14
15
  }));
15
16
  const target = await context.collection.model.findOne(filters);
16
17
  if (!target) {
17
- return (0, common_1.left)({
18
- message: 'target not found',
18
+ return context.error(types_1.HTTPStatus.NotFound, {
19
+ code: types_1.ACError.ResourceNotFound,
19
20
  });
20
21
  }
21
22
  await (0, index_js_1.cascadingRemove)(target, context);
@@ -1,19 +1,20 @@
1
1
  "use strict";
2
- import { left, unsafe } from "@aeriajs/common";
2
+ import { HTTPStatus, ACError } from "@aeriajs/types";
3
+ import { throwIfLeft } from "@aeriajs/common";
3
4
  import { traverseDocument, cascadingRemove } from "../../collection/index.mjs";
4
5
  export const remove = async (payload, context) => {
5
6
  if (!payload.filters._id) {
6
- return left({
7
- message: "you must pass an _id as filter"
7
+ return context.error(HTTPStatus.NotFound, {
8
+ code: ACError.ResourceNotFound
8
9
  });
9
10
  }
10
- const filters = unsafe(await traverseDocument(payload.filters, context.description, {
11
+ const filters = throwIfLeft(await traverseDocument(payload.filters, context.description, {
11
12
  autoCast: true
12
13
  }));
13
14
  const target = await context.collection.model.findOne(filters);
14
15
  if (!target) {
15
- return left({
16
- message: "target not found"
16
+ return context.error(HTTPStatus.NotFound, {
17
+ code: ACError.ResourceNotFound
17
18
  });
18
19
  }
19
20
  await cascadingRemove(target, context);
@@ -10,7 +10,7 @@ const removeAll = async (payload, context) => {
10
10
  $in: payload.filters,
11
11
  },
12
12
  };
13
- const filters = (0, common_1.unsafe)(await (0, index_js_1.traverseDocument)(filtersWithId, context.description, {
13
+ const filters = (0, common_1.throwIfLeft)(await (0, index_js_1.traverseDocument)(filtersWithId, context.description, {
14
14
  autoCast: true,
15
15
  }));
16
16
  const it = context.collection.model.find(filters);
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- import { unsafe } from "@aeriajs/common";
2
+ import { throwIfLeft } from "@aeriajs/common";
3
3
  import { traverseDocument, cascadingRemove } from "../../collection/index.mjs";
4
4
  export const removeAll = async (payload, context) => {
5
5
  const filtersWithId = {
@@ -8,7 +8,7 @@ export const removeAll = async (payload, context) => {
8
8
  $in: payload.filters
9
9
  }
10
10
  };
11
- const filters = unsafe(await traverseDocument(filtersWithId, context.description, {
11
+ const filters = throwIfLeft(await traverseDocument(filtersWithId, context.description, {
12
12
  autoCast: true
13
13
  }));
14
14
  const it = context.collection.model.find(filters);
@@ -1,4 +1,9 @@
1
- import type { Context } from '@aeriajs/types';
2
- export declare const upload: <TContext extends Context>(_props: unknown, context: TContext) => Promise<import("@aeriajs/types").Left<import("@aeriajs/types").ValidationError | import("@aeriajs/types").PropertyValidationError> | {
1
+ import { ACError, HTTPStatus, type Context } from '@aeriajs/types';
2
+ export declare const upload: <TContext extends Context>(_props: unknown, context: TContext) => Promise<import("@aeriajs/types").EndpointError<{
3
+ readonly code: ACError.MalformedInput;
4
+ readonly details: import("@aeriajs/types").ValidationError | import("@aeriajs/types").PropertyValidationError;
5
+ } & {
6
+ httpStatus: HTTPStatus.BadRequest;
7
+ }> | {
3
8
  tempId: any;
4
9
  }>;
@@ -24,6 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.upload = void 0;
27
+ const types_1 = require("@aeriajs/types");
27
28
  const common_1 = require("@aeriajs/common");
28
29
  const entrypoint_1 = require("@aeriajs/entrypoint");
29
30
  const validation_1 = require("@aeriajs/validation");
@@ -78,11 +79,17 @@ const upload = async (_props, context) => {
78
79
  extraneous: true,
79
80
  });
80
81
  if ((0, common_1.isLeft)(headersEither)) {
81
- return (0, common_1.left)((0, common_1.unwrapEither)(headersEither));
82
+ return context.error(types_1.HTTPStatus.BadRequest, {
83
+ code: types_1.ACError.MalformedInput,
84
+ details: (0, common_1.unwrapEither)(headersEither),
85
+ });
82
86
  }
83
87
  const metadataEither = validateFileMetadata(context.request.query);
84
88
  if ((0, common_1.isLeft)(metadataEither)) {
85
- return (0, common_1.left)((0, common_1.unwrapEither)(metadataEither));
89
+ return context.error(types_1.HTTPStatus.BadRequest, {
90
+ code: types_1.ACError.MalformedInput,
91
+ details: (0, common_1.unwrapEither)(metadataEither),
92
+ });
86
93
  }
87
94
  const metadata = (0, common_1.unwrapEither)(metadataEither);
88
95
  const path = await streamToFs(metadata, context);
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
- import { isLeft, unwrapEither, left } from "@aeriajs/common";
2
+ import { ACError, HTTPStatus } from "@aeriajs/types";
3
+ import { isLeft, unwrapEither } from "@aeriajs/common";
3
4
  import { getCollection } from "@aeriajs/entrypoint";
4
5
  import { validate, validator } from "@aeriajs/validation";
5
6
  import * as path from "path";
@@ -47,11 +48,17 @@ export const upload = async (_props, context) => {
47
48
  extraneous: true
48
49
  });
49
50
  if (isLeft(headersEither)) {
50
- return left(unwrapEither(headersEither));
51
+ return context.error(HTTPStatus.BadRequest, {
52
+ code: ACError.MalformedInput,
53
+ details: unwrapEither(headersEither)
54
+ });
51
55
  }
52
56
  const metadataEither = validateFileMetadata(context.request.query);
53
57
  if (isLeft(metadataEither)) {
54
- return left(unwrapEither(metadataEither));
58
+ return context.error(HTTPStatus.BadRequest, {
59
+ code: ACError.MalformedInput,
60
+ details: unwrapEither(metadataEither)
61
+ });
55
62
  }
56
63
  const metadata = unwrapEither(metadataEither);
57
64
  const path2 = await streamToFs(metadata, context);
package/dist/use.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const createAeria: () => Promise<import("@aeriajs/types").RouteContext<null> & import("@aeriajs/types").CollectionContext<any, any>>;
1
+ export declare const createAeria: () => Promise<import("@aeriajs/types").RouteContext<null> & import("@aeriajs/types").CollectionContext<import("@aeriajs/types").Description, any>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/core",
3
- "version": "0.0.88",
3
+ "version": "0.0.90",
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.88",
45
- "@aeriajs/common": "^0.0.54",
46
- "@aeriajs/entrypoint": "^0.0.54",
47
- "@aeriajs/http": "^0.0.62",
48
- "@aeriajs/security": "^0.0.88",
49
- "@aeriajs/types": "^0.0.51",
50
- "@aeriajs/validation": "^0.0.57"
44
+ "@aeriajs/builtins": "^0.0.90",
45
+ "@aeriajs/common": "^0.0.56",
46
+ "@aeriajs/entrypoint": "^0.0.56",
47
+ "@aeriajs/http": "^0.0.64",
48
+ "@aeriajs/security": "^0.0.90",
49
+ "@aeriajs/types": "^0.0.53",
50
+ "@aeriajs/validation": "^0.0.59"
51
51
  },
52
52
  "dependencies": {
53
53
  "mongodb": "^6.5.0",
@@ -1,4 +0,0 @@
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;
3
- export declare const fill: <TDocument extends OptionalId<any>>(item: TDocument & Record<string, any>, description: Pick<Description, 'properties' | 'freshItem'>) => Record<string, any> & TDocument;
4
- export declare const prepareInsert: (payload: any, description: Pick<Description, 'properties' | 'form' | 'writable' | 'owned' | 'defaults'>) => Record<string, any>;
@@ -1,89 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.prepareInsert = exports.fill = exports.normalizeProjection = void 0;
4
- const common_1 = require("@aeriajs/common");
5
- const normalizeProjection = (properties, description) => {
6
- const target = Array.from(properties);
7
- if (target.length === 0) {
8
- target.push(...Object.keys(description.properties));
9
- }
10
- const projection = target.reduce((a, key) => {
11
- if (key !== '_id' && description.properties[key].hidden) {
12
- return a;
13
- }
14
- return {
15
- ...a,
16
- [key]: 1,
17
- };
18
- }, {});
19
- return Object.keys(projection).length === 0
20
- ? null
21
- : projection;
22
- };
23
- exports.normalizeProjection = normalizeProjection;
24
- const fill = (item, description) => {
25
- const itemCopy = Object.assign({}, item);
26
- for (const key in itemCopy) {
27
- if (itemCopy[key] === null) {
28
- delete itemCopy[key];
29
- }
30
- }
31
- return Object.assign((0, common_1.freshItem)(description), itemCopy);
32
- };
33
- exports.fill = fill;
34
- const prepareInsert = (payload, description) => {
35
- const rest = Object.assign({}, payload);
36
- const docId = payload._id;
37
- delete rest._id;
38
- delete rest.created_at;
39
- delete rest.updated_at;
40
- const forbidden = (key) => {
41
- return description.properties[key].readOnly
42
- || (description.writable && !description.writable.includes(key));
43
- };
44
- const prepareUpdate = () => Object.entries(rest).reduce((a, [key, value]) => {
45
- // it's a mongodb operator
46
- if (key[0] === '$') {
47
- // it's not possible to use mongodb operators when user specifies
48
- // writable properties nor when it is a creation operation
49
- if (!description.writable && docId) {
50
- a[key] = value;
51
- }
52
- return a;
53
- }
54
- if (forbidden(key)) {
55
- return a;
56
- }
57
- if (value === null || value === undefined) {
58
- a.$unset[key] = 1;
59
- return a;
60
- }
61
- a.$set[key] = value;
62
- return a;
63
- }, {
64
- $set: description.defaults || {},
65
- $unset: {},
66
- });
67
- const prepareCreate = () => Object.entries(rest).reduce((a, [key, value]) => {
68
- if (forbidden(key) || [
69
- undefined,
70
- null,
71
- ].includes(value)) {
72
- return a;
73
- }
74
- return {
75
- ...a,
76
- [key]: value,
77
- };
78
- }, description.defaults || {});
79
- const what = docId
80
- ? prepareUpdate()
81
- : prepareCreate();
82
- Object.keys(what).forEach((k) => {
83
- if (typeof what[k] === 'object' && JSON.stringify(what) === '{}') {
84
- delete what[k];
85
- }
86
- });
87
- return what;
88
- };
89
- exports.prepareInsert = prepareInsert;