@aeriajs/builtins 0.0.87 → 0.0.89

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 (32) hide show
  1. package/dist/collections/file/download.d.ts +6 -5
  2. package/dist/collections/file/download.js +8 -9
  3. package/dist/collections/file/download.mjs +7 -8
  4. package/dist/collections/file/index.d.ts +14 -6
  5. package/dist/collections/file/insert.d.ts +1 -1
  6. package/dist/collections/log/index.d.ts +6 -6
  7. package/dist/collections/user/activate.d.ts +15 -2
  8. package/dist/collections/user/activate.js +21 -13
  9. package/dist/collections/user/activate.mjs +20 -12
  10. package/dist/collections/user/authenticate.d.ts +17 -5
  11. package/dist/collections/user/authenticate.js +26 -18
  12. package/dist/collections/user/authenticate.mjs +23 -15
  13. package/dist/collections/user/createAccount.d.ts +1 -1
  14. package/dist/collections/user/createAccount.js +2 -2
  15. package/dist/collections/user/createAccount.mjs +3 -3
  16. package/dist/collections/user/description.js +0 -7
  17. package/dist/collections/user/description.mjs +0 -7
  18. package/dist/collections/user/getCurrentUser.d.ts +4 -4
  19. package/dist/collections/user/getCurrentUser.js +10 -10
  20. package/dist/collections/user/getCurrentUser.mjs +10 -10
  21. package/dist/collections/user/getInfo.d.ts +18 -5
  22. package/dist/collections/user/getInfo.js +22 -14
  23. package/dist/collections/user/getInfo.mjs +21 -13
  24. package/dist/collections/user/index.d.ts +106 -62
  25. package/dist/collections/user/index.js +3 -10
  26. package/dist/collections/user/index.mjs +3 -10
  27. package/dist/collections/user/insert.d.ts +1 -1
  28. package/dist/functions/describe.d.ts +124 -113
  29. package/dist/functions/describe.js +3 -5
  30. package/dist/functions/describe.mjs +4 -6
  31. package/dist/index.d.ts +126 -74
  32. package/package.json +6 -6
@@ -1,13 +1,14 @@
1
1
  /// <reference types="node" />
2
2
  import type { Context } from '@aeriajs/types';
3
3
  import type { description } from './description.js';
4
+ import { HTTPStatus, ACError } from '@aeriajs/types';
4
5
  import * as fs from 'fs';
5
- export declare enum FileReadError {
6
- DocumentNotFound = "DOCUMENT_NOT_FOUND",
7
- FileNotFound = "FILE_NOT_FOUND"
8
- }
9
6
  export declare const download: (payload: {
10
7
  fileId: string;
11
8
  options: readonly ('picture' | 'download')[];
12
9
  noHeaders?: boolean;
13
- }, context: Context<typeof description>) => Promise<import("@aeriajs/types").Left<FileReadError.DocumentNotFound> | import("@aeriajs/types").Left<FileReadError.FileNotFound> | fs.ReadStream>;
10
+ }, context: Context<typeof description>) => Promise<import("@aeriajs/types").EndpointError<{
11
+ readonly code: ACError.ResourceNotFound;
12
+ } & {
13
+ httpStatus: HTTPStatus.NotFound;
14
+ }> | fs.ReadStream>;
@@ -1,14 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.download = exports.FileReadError = void 0;
3
+ exports.download = void 0;
4
+ const types_1 = require("@aeriajs/types");
4
5
  const core_1 = require("@aeriajs/core");
5
- const common_1 = require("@aeriajs/common");
6
6
  const fs = require("fs");
7
- var FileReadError;
8
- (function (FileReadError) {
9
- FileReadError["DocumentNotFound"] = "DOCUMENT_NOT_FOUND";
10
- FileReadError["FileNotFound"] = "FILE_NOT_FOUND";
11
- })(FileReadError || (exports.FileReadError = FileReadError = {}));
12
7
  const download = async (payload, context) => {
13
8
  const { fileId, options = [] } = payload;
14
9
  const file = await context.collection.model.findOne({
@@ -26,7 +21,9 @@ const download = async (payload, context) => {
26
21
  'content-type': 'application/json',
27
22
  });
28
23
  }
29
- return (0, common_1.left)(FileReadError.DocumentNotFound);
24
+ return context.error(types_1.HTTPStatus.NotFound, {
25
+ code: types_1.ACError.ResourceNotFound,
26
+ });
30
27
  }
31
28
  let stat;
32
29
  try {
@@ -36,7 +33,9 @@ const download = async (payload, context) => {
36
33
  context.response.writeHead(404, {
37
34
  'content-type': 'application/json',
38
35
  });
39
- return (0, common_1.left)(FileReadError.FileNotFound);
36
+ return context.error(types_1.HTTPStatus.NotFound, {
37
+ code: types_1.ACError.ResourceNotFound,
38
+ });
40
39
  }
41
40
  const range = context.request.headers.range;
42
41
  if (typeof range === 'string') {
@@ -1,12 +1,7 @@
1
1
  "use strict";
2
+ import { HTTPStatus, ACError } from "@aeriajs/types";
2
3
  import { ObjectId } from "@aeriajs/core";
3
- import { left } from "@aeriajs/common";
4
4
  import * as fs from "fs";
5
- export var FileReadError = /* @__PURE__ */ ((FileReadError2) => {
6
- FileReadError2["DocumentNotFound"] = "DOCUMENT_NOT_FOUND";
7
- FileReadError2["FileNotFound"] = "FILE_NOT_FOUND";
8
- return FileReadError2;
9
- })(FileReadError || {});
10
5
  export const download = async (payload, context) => {
11
6
  const { fileId, options = [] } = payload;
12
7
  const file = await context.collection.model.findOne({
@@ -24,7 +19,9 @@ export const download = async (payload, context) => {
24
19
  "content-type": "application/json"
25
20
  });
26
21
  }
27
- return left("DOCUMENT_NOT_FOUND" /* DocumentNotFound */);
22
+ return context.error(HTTPStatus.NotFound, {
23
+ code: ACError.ResourceNotFound
24
+ });
28
25
  }
29
26
  let stat;
30
27
  try {
@@ -33,7 +30,9 @@ export const download = async (payload, context) => {
33
30
  context.response.writeHead(404, {
34
31
  "content-type": "application/json"
35
32
  });
36
- return left("FILE_NOT_FOUND" /* FileNotFound */);
33
+ return context.error(HTTPStatus.NotFound, {
34
+ code: ACError.ResourceNotFound
35
+ });
37
36
  }
38
37
  const range = context.request.headers.range;
39
38
  if (typeof range === "string") {
@@ -142,7 +142,7 @@ export declare const file: Omit<import("@aeriajs/types").Collection<{
142
142
  };
143
143
  };
144
144
  functions: {
145
- readonly get: (payload: import("@aeriajs/types").GetPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetOptions | undefined) => Promise<import("@aeriajs/types").SchemaWithId<any> | null>;
145
+ readonly get: (payload: import("@aeriajs/types").GetPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetOptions | undefined) => Promise<any>;
146
146
  readonly insert: (payload: {
147
147
  what: {
148
148
  content: string;
@@ -245,7 +245,7 @@ export declare const file: Omit<import("@aeriajs/types").Collection<{
245
245
  readonly ask: true;
246
246
  };
247
247
  };
248
- }>) => Promise<import("@aeriajs/types").Left<import("@aeriajs/types").ACError | import("@aeriajs/types").ValidationError> | import("@aeriajs/types").Right<import("@aeriajs/types").SchemaWithId<{
248
+ }>) => Promise<import("@aeriajs/types").InsertReturnType<import("@aeriajs/types").SchemaWithId<{
249
249
  readonly $id: "file";
250
250
  readonly owned: "always";
251
251
  readonly presets: readonly ["owned"];
@@ -348,7 +348,11 @@ export declare const file: Omit<import("@aeriajs/types").Collection<{
348
348
  readonly ask: true;
349
349
  };
350
350
  };
351
- }>) => Promise<import("@aeriajs/types").Left<import("./download.js").FileReadError.DocumentNotFound> | import("@aeriajs/types").Left<import("./download.js").FileReadError.FileNotFound> | import("fs").ReadStream>;
351
+ }>) => Promise<import("@aeriajs/types").EndpointError<{
352
+ readonly code: import("@aeriajs/types").ACError.ResourceNotFound;
353
+ } & {
354
+ httpStatus: import("@aeriajs/types").HTTPStatus.NotFound;
355
+ }> | import("fs").ReadStream>;
352
356
  readonly remove: (payload: import("@aeriajs/types").RemovePayload<import("@aeriajs/types").SchemaWithId<{
353
357
  readonly $id: "file";
354
358
  readonly owned: "always";
@@ -608,7 +612,7 @@ export declare const file: Omit<import("@aeriajs/types").Collection<{
608
612
  };
609
613
  };
610
614
  functions: {
611
- readonly get: (payload: import("@aeriajs/types").GetPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetOptions | undefined) => Promise<import("@aeriajs/types").SchemaWithId<any> | null>;
615
+ readonly get: (payload: import("@aeriajs/types").GetPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetOptions | undefined) => Promise<any>;
612
616
  readonly insert: (payload: {
613
617
  what: {
614
618
  content: string;
@@ -711,7 +715,7 @@ export declare const file: Omit<import("@aeriajs/types").Collection<{
711
715
  readonly ask: true;
712
716
  };
713
717
  };
714
- }>) => Promise<import("@aeriajs/types").Left<import("@aeriajs/types").ACError | import("@aeriajs/types").ValidationError> | import("@aeriajs/types").Right<import("@aeriajs/types").SchemaWithId<{
718
+ }>) => Promise<import("@aeriajs/types").InsertReturnType<import("@aeriajs/types").SchemaWithId<{
715
719
  readonly $id: "file";
716
720
  readonly owned: "always";
717
721
  readonly presets: readonly ["owned"];
@@ -814,7 +818,11 @@ export declare const file: Omit<import("@aeriajs/types").Collection<{
814
818
  readonly ask: true;
815
819
  };
816
820
  };
817
- }>) => Promise<import("@aeriajs/types").Left<import("./download.js").FileReadError.DocumentNotFound> | import("@aeriajs/types").Left<import("./download.js").FileReadError.FileNotFound> | import("fs").ReadStream>;
821
+ }>) => Promise<import("@aeriajs/types").EndpointError<{
822
+ readonly code: import("@aeriajs/types").ACError.ResourceNotFound;
823
+ } & {
824
+ httpStatus: import("@aeriajs/types").HTTPStatus.NotFound;
825
+ }> | import("fs").ReadStream>;
818
826
  readonly remove: (payload: import("@aeriajs/types").RemovePayload<import("@aeriajs/types").SchemaWithId<{
819
827
  readonly $id: "file";
820
828
  readonly owned: "always";
@@ -4,7 +4,7 @@ export declare const insert: (payload: {
4
4
  what: {
5
5
  content: string;
6
6
  } & Pick<PackReferences<SchemaWithId<typeof description>>, '_id' | 'name' | 'owner' | 'absolute_path'>;
7
- }, context: Context<typeof description>) => Promise<import("@aeriajs/types").Left<import("@aeriajs/types").ACError | import("@aeriajs/types").ValidationError> | import("@aeriajs/types").Right<SchemaWithId<{
7
+ }, context: Context<typeof description>) => Promise<import("@aeriajs/types").InsertReturnType<SchemaWithId<{
8
8
  readonly $id: "file";
9
9
  readonly owned: "always";
10
10
  readonly presets: readonly ["owned"];
@@ -27,9 +27,9 @@ export declare const log: Omit<import("@aeriajs/types").Collection<{
27
27
  readonly filters: readonly ["context", "message", "owner"];
28
28
  };
29
29
  functions: {
30
- readonly get: (payload: import("@aeriajs/types").GetPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetOptions | undefined) => Promise<import("@aeriajs/types").SchemaWithId<any> | null>;
31
- readonly getAll: (_payload: import("@aeriajs/types").GetAllPayload<import("@aeriajs/types").SchemaWithId<any>> | undefined, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetAllOptions | undefined) => Promise<import("@aeriajs/types").SchemaWithId<any>[]>;
32
- readonly insert: (payload: import("@aeriajs/types").InsertPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").InsertOptions | undefined) => Promise<import("@aeriajs/types").Left<import("@aeriajs/types").ACError | import("@aeriajs/types").ValidationError> | import("@aeriajs/types").Right<import("@aeriajs/types").SchemaWithId<any>>>;
30
+ readonly get: (payload: import("@aeriajs/types").GetPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetOptions | undefined) => Promise<any>;
31
+ readonly getAll: (_payload: import("@aeriajs/types").GetAllPayload<import("@aeriajs/types").SchemaWithId<import("@aeriajs/types").Description>> | undefined, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetAllOptions | undefined) => Promise<import("@aeriajs/types").SchemaWithId<any>[]>;
32
+ readonly insert: (payload: import("@aeriajs/types").InsertPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").InsertOptions | undefined) => Promise<import("@aeriajs/types").InsertReturnType<import("@aeriajs/types").SchemaWithId<any>>>;
33
33
  };
34
34
  exposedFunctions: {
35
35
  readonly get: true;
@@ -92,9 +92,9 @@ export declare const log: Omit<import("@aeriajs/types").Collection<{
92
92
  readonly filters: readonly ["context", "message", "owner"];
93
93
  };
94
94
  functions: {
95
- readonly get: (payload: import("@aeriajs/types").GetPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetOptions | undefined) => Promise<import("@aeriajs/types").SchemaWithId<any> | null>;
96
- readonly getAll: (_payload: import("@aeriajs/types").GetAllPayload<import("@aeriajs/types").SchemaWithId<any>> | undefined, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetAllOptions | undefined) => Promise<import("@aeriajs/types").SchemaWithId<any>[]>;
97
- readonly insert: (payload: import("@aeriajs/types").InsertPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").InsertOptions | undefined) => Promise<import("@aeriajs/types").Left<import("@aeriajs/types").ACError | import("@aeriajs/types").ValidationError> | import("@aeriajs/types").Right<import("@aeriajs/types").SchemaWithId<any>>>;
95
+ readonly get: (payload: import("@aeriajs/types").GetPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetOptions | undefined) => Promise<any>;
96
+ readonly getAll: (_payload: import("@aeriajs/types").GetAllPayload<import("@aeriajs/types").SchemaWithId<import("@aeriajs/types").Description>> | undefined, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").GetAllOptions | undefined) => Promise<import("@aeriajs/types").SchemaWithId<any>[]>;
97
+ readonly insert: (payload: import("@aeriajs/types").InsertPayload<import("@aeriajs/types").SchemaWithId<any>>, context: import("@aeriajs/types").StrictContext<any>, options?: import("@aeriajs/core").InsertOptions | undefined) => Promise<import("@aeriajs/types").InsertReturnType<import("@aeriajs/types").SchemaWithId<any>>>;
98
98
  };
99
99
  contracts: {
100
100
  readonly get?: import("@aeriajs/types").Contract | undefined;
@@ -1,10 +1,23 @@
1
1
  import type { Context } from '@aeriajs/types';
2
2
  import type { description } from './description.js';
3
- export declare enum ActivationErrors {
3
+ import { HTTPStatus } from '@aeriajs/types';
4
+ export declare enum ActivationError {
4
5
  UserNotFound = "USER_NOT_FOUND",
5
6
  AlreadyActiveUser = "ALREADY_ACTIVE_USER",
6
7
  InvalidLink = "INVALID_LINK"
7
8
  }
8
9
  export declare const activate: (payload: {
9
10
  password: string;
10
- }, context: Context<typeof description>) => Promise<import("@aeriajs/types").GenericResponse | import("@aeriajs/types").Left<ActivationErrors.InvalidLink> | import("@aeriajs/types").Left<ActivationErrors.UserNotFound> | import("@aeriajs/types").Left<ActivationErrors.AlreadyActiveUser> | import("@aeriajs/types").Right<true>>;
11
+ }, context: Context<typeof description>) => Promise<import("@aeriajs/types").GenericResponse | import("@aeriajs/types").EndpointError<{
12
+ readonly code: ActivationError.InvalidLink;
13
+ } & {
14
+ httpStatus: HTTPStatus.NotFound;
15
+ }> | import("@aeriajs/types").EndpointError<{
16
+ readonly code: ActivationError.UserNotFound;
17
+ } & {
18
+ httpStatus: HTTPStatus.NotFound;
19
+ }> | import("@aeriajs/types").EndpointError<{
20
+ readonly code: ActivationError.AlreadyActiveUser;
21
+ } & {
22
+ httpStatus: HTTPStatus.Forbidden;
23
+ }> | undefined>;
@@ -1,19 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.activate = exports.ActivationErrors = void 0;
3
+ exports.activate = exports.ActivationError = void 0;
4
4
  const core_1 = require("@aeriajs/core");
5
- const common_1 = require("@aeriajs/common");
5
+ const types_1 = require("@aeriajs/types");
6
6
  const bcrypt = require("bcrypt");
7
- var ActivationErrors;
8
- (function (ActivationErrors) {
9
- ActivationErrors["UserNotFound"] = "USER_NOT_FOUND";
10
- ActivationErrors["AlreadyActiveUser"] = "ALREADY_ACTIVE_USER";
11
- ActivationErrors["InvalidLink"] = "INVALID_LINK";
12
- })(ActivationErrors || (exports.ActivationErrors = ActivationErrors = {}));
7
+ var ActivationError;
8
+ (function (ActivationError) {
9
+ ActivationError["UserNotFound"] = "USER_NOT_FOUND";
10
+ ActivationError["AlreadyActiveUser"] = "ALREADY_ACTIVE_USER";
11
+ ActivationError["InvalidLink"] = "INVALID_LINK";
12
+ })(ActivationError || (exports.ActivationError = ActivationError = {}));
13
13
  const activate = async (payload, context) => {
14
14
  const { u: userId, t: token, } = context.request.query;
15
15
  if (!userId || !token) {
16
- return (0, common_1.left)(ActivationErrors.InvalidLink);
16
+ return context.error(types_1.HTTPStatus.NotFound, {
17
+ code: ActivationError.InvalidLink,
18
+ });
17
19
  }
18
20
  const user = await context.collection.model.findOne({
19
21
  _id: new core_1.ObjectId(userId),
@@ -23,14 +25,20 @@ const activate = async (payload, context) => {
23
25
  },
24
26
  });
25
27
  if (!user) {
26
- return (0, common_1.left)(ActivationErrors.UserNotFound);
28
+ return context.error(types_1.HTTPStatus.NotFound, {
29
+ code: ActivationError.UserNotFound,
30
+ });
27
31
  }
28
32
  if (user.active) {
29
- return (0, common_1.left)(ActivationErrors.AlreadyActiveUser);
33
+ return context.error(types_1.HTTPStatus.Forbidden, {
34
+ code: ActivationError.AlreadyActiveUser,
35
+ });
30
36
  }
31
37
  const equal = await bcrypt.compare(user._id.toString(), token);
32
38
  if (!equal) {
33
- return (0, common_1.left)(ActivationErrors.InvalidLink);
39
+ return context.error(types_1.HTTPStatus.NotFound, {
40
+ code: ActivationError.InvalidLink,
41
+ });
34
42
  }
35
43
  if (!user.password) {
36
44
  if (!payload.password) {
@@ -46,7 +54,7 @@ const activate = async (payload, context) => {
46
54
  password: await bcrypt.hash(payload.password, 10),
47
55
  },
48
56
  });
49
- return (0, common_1.right)(true);
57
+ return;
50
58
  }
51
59
  await context.collection.model.updateOne({
52
60
  _id: user._id,
@@ -1,20 +1,22 @@
1
1
  "use strict";
2
2
  import { ObjectId } from "@aeriajs/core";
3
- import { left, right } from "@aeriajs/common";
3
+ import { HTTPStatus } from "@aeriajs/types";
4
4
  import * as bcrypt from "bcrypt";
5
- export var ActivationErrors = /* @__PURE__ */ ((ActivationErrors2) => {
6
- ActivationErrors2["UserNotFound"] = "USER_NOT_FOUND";
7
- ActivationErrors2["AlreadyActiveUser"] = "ALREADY_ACTIVE_USER";
8
- ActivationErrors2["InvalidLink"] = "INVALID_LINK";
9
- return ActivationErrors2;
10
- })(ActivationErrors || {});
5
+ export var ActivationError = /* @__PURE__ */ ((ActivationError2) => {
6
+ ActivationError2["UserNotFound"] = "USER_NOT_FOUND";
7
+ ActivationError2["AlreadyActiveUser"] = "ALREADY_ACTIVE_USER";
8
+ ActivationError2["InvalidLink"] = "INVALID_LINK";
9
+ return ActivationError2;
10
+ })(ActivationError || {});
11
11
  export const activate = async (payload, context) => {
12
12
  const {
13
13
  u: userId,
14
14
  t: token
15
15
  } = context.request.query;
16
16
  if (!userId || !token) {
17
- return left("INVALID_LINK" /* InvalidLink */);
17
+ return context.error(HTTPStatus.NotFound, {
18
+ code: "INVALID_LINK" /* InvalidLink */
19
+ });
18
20
  }
19
21
  const user = await context.collection.model.findOne({
20
22
  _id: new ObjectId(userId)
@@ -24,14 +26,20 @@ export const activate = async (payload, context) => {
24
26
  }
25
27
  });
26
28
  if (!user) {
27
- return left("USER_NOT_FOUND" /* UserNotFound */);
29
+ return context.error(HTTPStatus.NotFound, {
30
+ code: "USER_NOT_FOUND" /* UserNotFound */
31
+ });
28
32
  }
29
33
  if (user.active) {
30
- return left("ALREADY_ACTIVE_USER" /* AlreadyActiveUser */);
34
+ return context.error(HTTPStatus.Forbidden, {
35
+ code: "ALREADY_ACTIVE_USER" /* AlreadyActiveUser */
36
+ });
31
37
  }
32
38
  const equal = await bcrypt.compare(user._id.toString(), token);
33
39
  if (!equal) {
34
- return left("INVALID_LINK" /* InvalidLink */);
40
+ return context.error(HTTPStatus.NotFound, {
41
+ code: "INVALID_LINK" /* InvalidLink */
42
+ });
35
43
  }
36
44
  if (!user.password) {
37
45
  if (!payload.password) {
@@ -50,7 +58,7 @@ export const activate = async (payload, context) => {
50
58
  }
51
59
  }
52
60
  );
53
- return right(true);
61
+ return;
54
62
  }
55
63
  await context.collection.model.updateOne(
56
64
  {
@@ -1,6 +1,7 @@
1
1
  import type { Context, SchemaWithId, TokenRecipient } from '@aeriajs/types';
2
2
  import type { description } from './description.js';
3
- import { type ObjectId } from '@aeriajs/core';
3
+ import type { ObjectId } from '@aeriajs/core';
4
+ import { HTTPStatus, ACError } from '@aeriajs/types';
4
5
  type Props = {
5
6
  email: string;
6
7
  password: string;
@@ -14,8 +15,7 @@ type Return = {
14
15
  };
15
16
  token: TokenRecipient;
16
17
  };
17
- export declare enum AuthenticationErrors {
18
- Unauthenticated = "UNAUTHENTICATED",
18
+ export declare enum AuthenticationError {
19
19
  InvalidCredentials = "INVALID_CREDENTIALS",
20
20
  InactiveUser = "INACTIVE_USER"
21
21
  }
@@ -32,7 +32,11 @@ export declare const getDefaultUser: () => Promise<{
32
32
  content: string;
33
33
  };
34
34
  }>;
35
- export declare const authenticate: (props: Props, context: Context<typeof description>) => Promise<import("@aeriajs/types").Left<AuthenticationErrors.Unauthenticated> | import("@aeriajs/types").Right<Return | {
35
+ export declare const authenticate: (props: Props, context: Context<typeof description>) => Promise<Return | import("@aeriajs/types").EndpointError<{
36
+ readonly code: ACError.AuthorizationError;
37
+ } & {
38
+ httpStatus: HTTPStatus.Unauthorized;
39
+ }> | {
36
40
  user: {
37
41
  _id: null;
38
42
  name: string;
@@ -44,5 +48,13 @@ export declare const authenticate: (props: Props, context: Context<typeof descri
44
48
  type: string;
45
49
  content: string;
46
50
  };
47
- }> | import("@aeriajs/types").Left<AuthenticationErrors.InvalidCredentials> | import("@aeriajs/types").Left<AuthenticationErrors.InactiveUser>>;
51
+ } | import("@aeriajs/types").EndpointError<{
52
+ readonly code: AuthenticationError.InvalidCredentials;
53
+ } & {
54
+ httpStatus: HTTPStatus.Unauthorized;
55
+ }> | import("@aeriajs/types").EndpointError<{
56
+ readonly code: AuthenticationError.InactiveUser;
57
+ } & {
58
+ httpStatus: HTTPStatus.Unauthorized;
59
+ }>>;
48
60
  export {};
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.authenticate = exports.getDefaultUser = exports.AuthenticationErrors = void 0;
3
+ exports.authenticate = exports.getDefaultUser = exports.AuthenticationError = void 0;
4
+ const types_1 = require("@aeriajs/types");
5
+ const common_1 = require("@aeriajs/common");
4
6
  const bcrypt_1 = require("bcrypt");
5
7
  const core_1 = require("@aeriajs/core");
6
- const common_1 = require("@aeriajs/common");
7
- var AuthenticationErrors;
8
- (function (AuthenticationErrors) {
9
- AuthenticationErrors["Unauthenticated"] = "UNAUTHENTICATED";
10
- AuthenticationErrors["InvalidCredentials"] = "INVALID_CREDENTIALS";
11
- AuthenticationErrors["InactiveUser"] = "INACTIVE_USER";
12
- })(AuthenticationErrors || (exports.AuthenticationErrors = AuthenticationErrors = {}));
8
+ var AuthenticationError;
9
+ (function (AuthenticationError) {
10
+ AuthenticationError["InvalidCredentials"] = "INVALID_CREDENTIALS";
11
+ AuthenticationError["InactiveUser"] = "INACTIVE_USER";
12
+ })(AuthenticationError || (exports.AuthenticationError = AuthenticationError = {}));
13
13
  const getUser = async (userId, context) => {
14
14
  const leanUser = await context.collection.functions.get({
15
15
  filters: {
@@ -17,7 +17,7 @@ const getUser = async (userId, context) => {
17
17
  },
18
18
  populate: ['picture_file'],
19
19
  });
20
- if (!leanUser) {
20
+ if ((0, common_1.isError)(leanUser)) {
21
21
  throw new Error();
22
22
  }
23
23
  const tokenContent = {
@@ -81,21 +81,25 @@ const authenticate = async (props, context) => {
81
81
  if ('revalidate' in props) {
82
82
  const { token } = props;
83
83
  if (!token && !context.token.authenticated) {
84
- return (0, common_1.left)(AuthenticationErrors.Unauthenticated);
84
+ return context.error(types_1.HTTPStatus.Unauthorized, {
85
+ code: types_1.ACError.AuthorizationError,
86
+ });
85
87
  }
86
88
  const decodedToken = token
87
89
  ? await (0, core_1.decodeToken)(token.content)
88
90
  : context.token;
89
- return (0, common_1.right)(decodedToken.sub
90
- ? await getUser(decodedToken.sub, context)
91
- : await (0, exports.getDefaultUser)());
91
+ return decodedToken.sub
92
+ ? getUser(decodedToken.sub, context)
93
+ : (0, exports.getDefaultUser)();
92
94
  }
93
95
  if (typeof props.email !== 'string') {
94
- return (0, common_1.left)(AuthenticationErrors.InvalidCredentials);
96
+ return context.error(types_1.HTTPStatus.Unauthorized, {
97
+ code: AuthenticationError.InvalidCredentials,
98
+ });
95
99
  }
96
100
  if (context.config.defaultUser) {
97
101
  if (props.email === context.config.defaultUser.username && props.password === context.config.defaultUser.password) {
98
- return (0, common_1.right)(await (0, exports.getDefaultUser)());
102
+ return (0, exports.getDefaultUser)();
99
103
  }
100
104
  }
101
105
  const user = await context.collection.model.findOne({
@@ -108,11 +112,15 @@ const authenticate = async (props, context) => {
108
112
  },
109
113
  });
110
114
  if (!user || !user.password || !await (0, bcrypt_1.compare)(props.password, user.password)) {
111
- return (0, common_1.left)(AuthenticationErrors.InvalidCredentials);
115
+ return context.error(types_1.HTTPStatus.Unauthorized, {
116
+ code: AuthenticationError.InvalidCredentials,
117
+ });
112
118
  }
113
119
  if (!user.active) {
114
- return (0, common_1.left)(AuthenticationErrors.InactiveUser);
120
+ return context.error(types_1.HTTPStatus.Unauthorized, {
121
+ code: AuthenticationError.InactiveUser,
122
+ });
115
123
  }
116
- return (0, common_1.right)(await getUser(user._id, context));
124
+ return getUser(user._id, context);
117
125
  };
118
126
  exports.authenticate = authenticate;
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
+ import { HTTPStatus, ACError } from "@aeriajs/types";
3
+ import { isError } from "@aeriajs/common";
2
4
  import { compare as bcryptCompare } from "bcrypt";
3
5
  import { signToken, decodeToken } from "@aeriajs/core";
4
- import { left, right } from "@aeriajs/common";
5
- export var AuthenticationErrors = /* @__PURE__ */ ((AuthenticationErrors2) => {
6
- AuthenticationErrors2["Unauthenticated"] = "UNAUTHENTICATED";
7
- AuthenticationErrors2["InvalidCredentials"] = "INVALID_CREDENTIALS";
8
- AuthenticationErrors2["InactiveUser"] = "INACTIVE_USER";
9
- return AuthenticationErrors2;
10
- })(AuthenticationErrors || {});
6
+ export var AuthenticationError = /* @__PURE__ */ ((AuthenticationError2) => {
7
+ AuthenticationError2["InvalidCredentials"] = "INVALID_CREDENTIALS";
8
+ AuthenticationError2["InactiveUser"] = "INACTIVE_USER";
9
+ return AuthenticationError2;
10
+ })(AuthenticationError || {});
11
11
  const getUser = async (userId, context) => {
12
12
  const leanUser = await context.collection.functions.get({
13
13
  filters: {
@@ -15,7 +15,7 @@ const getUser = async (userId, context) => {
15
15
  },
16
16
  populate: ["picture_file"]
17
17
  });
18
- if (!leanUser) {
18
+ if (isError(leanUser)) {
19
19
  throw new Error();
20
20
  }
21
21
  const tokenContent = {
@@ -77,17 +77,21 @@ export const authenticate = async (props, context) => {
77
77
  if ("revalidate" in props) {
78
78
  const { token } = props;
79
79
  if (!token && !context.token.authenticated) {
80
- return left("UNAUTHENTICATED" /* Unauthenticated */);
80
+ return context.error(HTTPStatus.Unauthorized, {
81
+ code: ACError.AuthorizationError
82
+ });
81
83
  }
82
84
  const decodedToken = token ? await decodeToken(token.content) : context.token;
83
- return right(decodedToken.sub ? await getUser(decodedToken.sub, context) : await getDefaultUser());
85
+ return decodedToken.sub ? getUser(decodedToken.sub, context) : getDefaultUser();
84
86
  }
85
87
  if (typeof props.email !== "string") {
86
- return left("INVALID_CREDENTIALS" /* InvalidCredentials */);
88
+ return context.error(HTTPStatus.Unauthorized, {
89
+ code: "INVALID_CREDENTIALS" /* InvalidCredentials */
90
+ });
87
91
  }
88
92
  if (context.config.defaultUser) {
89
93
  if (props.email === context.config.defaultUser.username && props.password === context.config.defaultUser.password) {
90
- return right(await getDefaultUser());
94
+ return getDefaultUser();
91
95
  }
92
96
  }
93
97
  const user = await context.collection.model.findOne(
@@ -103,10 +107,14 @@ export const authenticate = async (props, context) => {
103
107
  }
104
108
  );
105
109
  if (!user || !user.password || !await bcryptCompare(props.password, user.password)) {
106
- return left("INVALID_CREDENTIALS" /* InvalidCredentials */);
110
+ return context.error(HTTPStatus.Unauthorized, {
111
+ code: "INVALID_CREDENTIALS" /* InvalidCredentials */
112
+ });
107
113
  }
108
114
  if (!user.active) {
109
- return left("INACTIVE_USER" /* InactiveUser */);
115
+ return context.error(HTTPStatus.Unauthorized, {
116
+ code: "INACTIVE_USER" /* InactiveUser */
117
+ });
110
118
  }
111
- return right(await getUser(user._id, context));
119
+ return getUser(user._id, context);
112
120
  };
@@ -1,3 +1,3 @@
1
1
  import type { Context, Schema } from '@aeriajs/types';
2
2
  import type { description } from './description.js';
3
- export declare const createAccount: (payload: Omit<Schema<typeof description>, 'roles'>, context: Context<typeof description>) => Promise<import("@aeriajs/types").Left<import("@aeriajs/types").ValidationError | import("@aeriajs/types").PropertyValidationError> | import("@aeriajs/types").Right<any>>;
3
+ export declare const createAccount: (payload: Omit<Schema<typeof description>, 'roles'>, context: Context<typeof description>) => Promise<any>;
@@ -38,7 +38,7 @@ const createAccount = async (payload, context) => {
38
38
  ],
39
39
  });
40
40
  if ((0, common_1.isLeft)(validationEither)) {
41
- return (0, common_1.left)((0, common_1.unwrapEither)(validationEither));
41
+ return validationEither;
42
42
  }
43
43
  if (context.config.security.signupDefaults) {
44
44
  Object.assign(user, context.config.security.signupDefaults);
@@ -58,6 +58,6 @@ const createAccount = async (payload, context) => {
58
58
  if (!newUser) {
59
59
  throw new Error();
60
60
  }
61
- return (0, common_1.right)(newUser);
61
+ return newUser;
62
62
  };
63
63
  exports.createAccount = createAccount;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- import { isLeft, unwrapEither, left, right } from "@aeriajs/common";
2
+ import { isLeft } from "@aeriajs/common";
3
3
  import { validate } from "@aeriajs/validation";
4
4
  import * as bcrypt from "bcrypt";
5
5
  export const createAccount = async (payload, context) => {
@@ -36,7 +36,7 @@ export const createAccount = async (payload, context) => {
36
36
  ]
37
37
  });
38
38
  if (isLeft(validationEither)) {
39
- return left(unwrapEither(validationEither));
39
+ return validationEither;
40
40
  }
41
41
  if (context.config.security.signupDefaults) {
42
42
  Object.assign(user, context.config.security.signupDefaults);
@@ -56,5 +56,5 @@ export const createAccount = async (payload, context) => {
56
56
  if (!newUser) {
57
57
  throw new Error();
58
58
  }
59
- return right(newUser);
59
+ return newUser;
60
60
  };
@@ -79,13 +79,6 @@ exports.description = (0, core_1.defineDescription)({
79
79
  type: 'boolean',
80
80
  readOnly: true,
81
81
  },
82
- // resources_usage: {
83
- // type: 'object',
84
- // additionalProperties: {
85
- // $ref: 'resourceUsage',
86
- // inline: true,
87
- // },
88
- // },
89
82
  updated_at: {
90
83
  type: 'string',
91
84
  format: 'date-time',
@@ -73,13 +73,6 @@ export const description = defineDescription({
73
73
  type: "boolean",
74
74
  readOnly: true
75
75
  },
76
- // resources_usage: {
77
- // type: 'object',
78
- // additionalProperties: {
79
- // $ref: 'resourceUsage',
80
- // inline: true,
81
- // },
82
- // },
83
76
  updated_at: {
84
77
  type: "string",
85
78
  format: "date-time"