@aeriajs/builtins 0.0.112 → 0.0.114

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.
@@ -451,15 +451,15 @@ export declare const file: Omit<import("@aeriajs/types").Collection<{
451
451
  readonly ask: true;
452
452
  };
453
453
  };
454
- }>) => Promise<{
455
- readonly _tag: "Result";
456
- readonly error: undefined;
457
- readonly result: any;
458
- } | import("@aeriajs/types").Result.Error<{
454
+ }>) => Promise<import("@aeriajs/types").Result.Error<{
459
455
  readonly code: import("@aeriajs/types").ACError.ResourceNotFound;
460
456
  } & {
461
457
  httpStatus: import("@aeriajs/types").HTTPStatus.NotFound;
462
- }>>;
458
+ }> | {
459
+ readonly _tag: "Result";
460
+ readonly error: undefined;
461
+ readonly result: import("mongodb").WithId<Omit<import("@aeriajs/types").PackReferences<import("@aeriajs/types").SchemaWithId<import("@aeriajs/types").Description<any>>>, "_id">> | null;
462
+ }>;
463
463
  readonly removeAll: (payload: import("@aeriajs/types").RemoveAllPayload, context: import("@aeriajs/types").Context<{
464
464
  readonly $id: "file";
465
465
  readonly owned: "always";
@@ -509,15 +509,15 @@ export declare const file: Omit<import("@aeriajs/types").Collection<{
509
509
  readonly ask: true;
510
510
  };
511
511
  };
512
- }>) => Promise<{
513
- readonly _tag: "Result";
514
- readonly error: undefined;
515
- readonly result: any;
516
- } | import("@aeriajs/types").Result.Error<{
512
+ }>) => Promise<import("@aeriajs/types").Result.Error<{
517
513
  readonly code: import("@aeriajs/types").ACError.ResourceNotFound;
518
514
  } & {
519
515
  httpStatus: import("@aeriajs/types").HTTPStatus.NotFound;
520
- }>>;
516
+ }> | {
517
+ readonly _tag: "Result";
518
+ readonly error: undefined;
519
+ readonly result: import("mongodb").WithId<Omit<import("@aeriajs/types").PackReferences<import("@aeriajs/types").SchemaWithId<import("@aeriajs/types").Description<any>>>, "_id">> | null;
520
+ }>;
521
521
  };
522
522
  exposedFunctions: {
523
523
  readonly get: "unauthenticated";
@@ -937,15 +937,15 @@ export declare const file: Omit<import("@aeriajs/types").Collection<{
937
937
  readonly ask: true;
938
938
  };
939
939
  };
940
- }>) => Promise<{
941
- readonly _tag: "Result";
942
- readonly error: undefined;
943
- readonly result: any;
944
- } | import("@aeriajs/types").Result.Error<{
940
+ }>) => Promise<import("@aeriajs/types").Result.Error<{
945
941
  readonly code: import("@aeriajs/types").ACError.ResourceNotFound;
946
942
  } & {
947
943
  httpStatus: import("@aeriajs/types").HTTPStatus.NotFound;
948
- }>>;
944
+ }> | {
945
+ readonly _tag: "Result";
946
+ readonly error: undefined;
947
+ readonly result: import("mongodb").WithId<Omit<import("@aeriajs/types").PackReferences<import("@aeriajs/types").SchemaWithId<import("@aeriajs/types").Description<any>>>, "_id">> | null;
948
+ }>;
949
949
  readonly removeAll: (payload: import("@aeriajs/types").RemoveAllPayload, context: import("@aeriajs/types").Context<{
950
950
  readonly $id: "file";
951
951
  readonly owned: "always";
@@ -995,15 +995,15 @@ export declare const file: Omit<import("@aeriajs/types").Collection<{
995
995
  readonly ask: true;
996
996
  };
997
997
  };
998
- }>) => Promise<{
999
- readonly _tag: "Result";
1000
- readonly error: undefined;
1001
- readonly result: any;
1002
- } | import("@aeriajs/types").Result.Error<{
998
+ }>) => Promise<import("@aeriajs/types").Result.Error<{
1003
999
  readonly code: import("@aeriajs/types").ACError.ResourceNotFound;
1004
1000
  } & {
1005
1001
  httpStatus: import("@aeriajs/types").HTTPStatus.NotFound;
1006
- }>>;
1002
+ }> | {
1003
+ readonly _tag: "Result";
1004
+ readonly error: undefined;
1005
+ readonly result: import("mongodb").WithId<Omit<import("@aeriajs/types").PackReferences<import("@aeriajs/types").SchemaWithId<import("@aeriajs/types").Description<any>>>, "_id">> | null;
1006
+ }>;
1007
1007
  };
1008
1008
  contracts: {
1009
1009
  readonly get?: import("@aeriajs/types").Contract | undefined;
@@ -1,11 +1,11 @@
1
1
  import type { Context, SchemaWithId, RemovePayload } from '@aeriajs/types';
2
2
  import type { description } from './description.js';
3
- export declare const remove: (payload: RemovePayload<SchemaWithId<typeof description>>, context: Context<typeof description>) => Promise<{
4
- readonly _tag: "Result";
5
- readonly error: undefined;
6
- readonly result: any;
7
- } | import("@aeriajs/types").Result.Error<{
3
+ export declare const remove: (payload: RemovePayload<SchemaWithId<typeof description>>, context: Context<typeof description>) => Promise<import("@aeriajs/types").Result.Error<{
8
4
  readonly code: import("@aeriajs/types").ACError.ResourceNotFound;
9
5
  } & {
10
6
  httpStatus: import("@aeriajs/types").HTTPStatus.NotFound;
11
- }>>;
7
+ }> | {
8
+ readonly _tag: "Result";
9
+ readonly error: undefined;
10
+ readonly result: import("mongodb").WithId<Omit<import("@aeriajs/types").PackReferences<SchemaWithId<import("@aeriajs/types").Description<any>>>, "_id">> | null;
11
+ }>;
@@ -1,11 +1,11 @@
1
- import type { Context, RemoveAllPayload } from '@aeriajs/types';
1
+ import type { Context, SchemaWithId, PackReferences, RemoveAllPayload } from '@aeriajs/types';
2
2
  import type { description } from './description.js';
3
- export declare const removeAll: (payload: RemoveAllPayload, context: Context<typeof description>) => Promise<{
4
- readonly _tag: "Result";
5
- readonly error: undefined;
6
- readonly result: any;
7
- } | import("@aeriajs/types").Result.Error<{
3
+ export declare const removeAll: (payload: RemoveAllPayload, context: Context<typeof description>) => Promise<import("@aeriajs/types").Result.Error<{
8
4
  readonly code: import("@aeriajs/types").ACError.ResourceNotFound;
9
5
  } & {
10
6
  httpStatus: import("@aeriajs/types").HTTPStatus.NotFound;
11
- }>>;
7
+ }> | {
8
+ readonly _tag: "Result";
9
+ readonly error: undefined;
10
+ readonly result: import("mongodb").WithId<Omit<PackReferences<SchemaWithId<import("@aeriajs/types").Description<any>>>, "_id">> | null;
11
+ }>;
@@ -1,6 +1,6 @@
1
1
  import type { Context } from '@aeriajs/types';
2
2
  import type { description } from './description.js';
3
- import { HTTPStatus } from '@aeriajs/types';
3
+ import { Result, ACError, HTTPStatus } from '@aeriajs/types';
4
4
  export declare enum ActivationError {
5
5
  UserNotFound = "USER_NOT_FOUND",
6
6
  AlreadyActiveUser = "ALREADY_ACTIVE_USER",
@@ -8,16 +8,36 @@ export declare enum ActivationError {
8
8
  }
9
9
  export declare const activate: (payload: {
10
10
  password: string;
11
- }, context: Context<typeof description>) => Promise<import("@aeriajs/types").GenericResponse | import("@aeriajs/types").Result.Error<{
11
+ }, context: Context<typeof description> & {
12
+ request: {
13
+ query: {
14
+ u?: string;
15
+ t?: string;
16
+ };
17
+ payload: {
18
+ password?: string;
19
+ };
20
+ };
21
+ }) => Promise<import("@aeriajs/types").GenericResponse | Result.Error<{
12
22
  readonly code: ActivationError.InvalidLink;
13
23
  } & {
14
24
  httpStatus: HTTPStatus.NotFound;
15
- }> | import("@aeriajs/types").Result.Error<{
25
+ }> | Result.Error<{
16
26
  readonly code: ActivationError.UserNotFound;
17
27
  } & {
18
28
  httpStatus: HTTPStatus.NotFound;
19
- }> | import("@aeriajs/types").Result.Error<{
29
+ }> | Result.Error<{
20
30
  readonly code: ActivationError.AlreadyActiveUser;
21
31
  } & {
22
32
  httpStatus: HTTPStatus.Forbidden;
23
- }> | undefined>;
33
+ }> | Result.Error<{
34
+ readonly code: ACError.MalformedInput;
35
+ } & {
36
+ httpStatus: HTTPStatus.UnprocessableContent;
37
+ }> | {
38
+ readonly _tag: "Result";
39
+ readonly error: undefined;
40
+ readonly result: {
41
+ readonly userId: any;
42
+ };
43
+ } | undefined>;
@@ -4,6 +4,7 @@ exports.activate = exports.ActivationError = void 0;
4
4
  const core_1 = require("@aeriajs/core");
5
5
  const types_1 = require("@aeriajs/types");
6
6
  const bcrypt = require("bcrypt");
7
+ const getActivationLink_js_1 = require("./getActivationLink.js");
7
8
  var ActivationError;
8
9
  (function (ActivationError) {
9
10
  ActivationError["UserNotFound"] = "USER_NOT_FOUND";
@@ -34,7 +35,8 @@ const activate = async (payload, context) => {
34
35
  code: ActivationError.AlreadyActiveUser,
35
36
  });
36
37
  }
37
- const equal = await bcrypt.compare(user._id.toString(), token);
38
+ const activationToken = await (0, getActivationLink_js_1.getActivationToken)(userId.toString(), context);
39
+ const equal = await bcrypt.compare(activationToken, token);
38
40
  if (!equal) {
39
41
  return context.error(types_1.HTTPStatus.NotFound, {
40
42
  code: ActivationError.InvalidLink,
@@ -42,8 +44,13 @@ const activate = async (payload, context) => {
42
44
  }
43
45
  if (!user.password) {
44
46
  if (!payload.password) {
45
- return context.response.writeHead(302, {
46
- location: `/user/activation?step=password&u=${userId}&t=${token}`,
47
+ if (context.request.method === 'GET') {
48
+ return context.response.writeHead(302, {
49
+ location: `/user/activation?step=password&u=${userId}&t=${token}`,
50
+ }).end();
51
+ }
52
+ return context.error(types_1.HTTPStatus.UnprocessableContent, {
53
+ code: types_1.ACError.MalformedInput,
47
54
  });
48
55
  }
49
56
  await context.collection.model.updateOne({
@@ -63,8 +70,13 @@ const activate = async (payload, context) => {
63
70
  active: true,
64
71
  },
65
72
  });
66
- return context.response.writeHead(302, {
67
- location: '/user/activation',
73
+ if (context.request.method === 'GET') {
74
+ return context.response.writeHead(302, {
75
+ location: '/user/activation',
76
+ }).end();
77
+ }
78
+ return types_1.Result.result({
79
+ userId: user._id,
68
80
  });
69
81
  };
70
82
  exports.activate = activate;
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  import { ObjectId } from "@aeriajs/core";
3
- import { HTTPStatus } from "@aeriajs/types";
3
+ import { Result, ACError, HTTPStatus } from "@aeriajs/types";
4
4
  import * as bcrypt from "bcrypt";
5
+ import { getActivationToken } from "./getActivationLink.mjs";
5
6
  export var ActivationError = /* @__PURE__ */ ((ActivationError2) => {
6
7
  ActivationError2["UserNotFound"] = "USER_NOT_FOUND";
7
8
  ActivationError2["AlreadyActiveUser"] = "ALREADY_ACTIVE_USER";
@@ -35,7 +36,8 @@ export const activate = async (payload, context) => {
35
36
  code: "ALREADY_ACTIVE_USER" /* AlreadyActiveUser */
36
37
  });
37
38
  }
38
- const equal = await bcrypt.compare(user._id.toString(), token);
39
+ const activationToken = await getActivationToken(userId.toString(), context);
40
+ const equal = await bcrypt.compare(activationToken, token);
39
41
  if (!equal) {
40
42
  return context.error(HTTPStatus.NotFound, {
41
43
  code: "INVALID_LINK" /* InvalidLink */
@@ -43,8 +45,13 @@ export const activate = async (payload, context) => {
43
45
  }
44
46
  if (!user.password) {
45
47
  if (!payload.password) {
46
- return context.response.writeHead(302, {
47
- location: `/user/activation?step=password&u=${userId}&t=${token}`
48
+ if (context.request.method === "GET") {
49
+ return context.response.writeHead(302, {
50
+ location: `/user/activation?step=password&u=${userId}&t=${token}`
51
+ }).end();
52
+ }
53
+ return context.error(HTTPStatus.UnprocessableContent, {
54
+ code: ACError.MalformedInput
48
55
  });
49
56
  }
50
57
  await context.collection.model.updateOne(
@@ -70,7 +77,12 @@ export const activate = async (payload, context) => {
70
77
  }
71
78
  }
72
79
  );
73
- return context.response.writeHead(302, {
74
- location: "/user/activation"
80
+ if (context.request.method === "GET") {
81
+ return context.response.writeHead(302, {
82
+ location: "/user/activation"
83
+ }).end();
84
+ }
85
+ return Result.result({
86
+ userId: user._id
75
87
  });
76
88
  };
@@ -86,6 +86,11 @@ export declare const description: {
86
86
  readonly fetchItem: true;
87
87
  readonly translate: true;
88
88
  };
89
+ readonly copyActivationLink: {
90
+ readonly label: "copy_activation_link";
91
+ readonly icon: "link";
92
+ readonly translate: true;
93
+ };
89
94
  };
90
95
  readonly icon: "users";
91
96
  readonly filters: readonly ["name", "roles", "email", "phone_number"];
@@ -111,6 +111,11 @@ exports.description = (0, core_1.defineDescription)({
111
111
  fetchItem: true,
112
112
  translate: true,
113
113
  },
114
+ 'copyActivationLink': {
115
+ label: 'copy_activation_link',
116
+ icon: 'link',
117
+ translate: true,
118
+ },
114
119
  },
115
120
  icon: 'users',
116
121
  filters: [
@@ -104,6 +104,11 @@ export const description = defineDescription({
104
104
  icon: "key",
105
105
  fetchItem: true,
106
106
  translate: true
107
+ },
108
+ "copyActivationLink": {
109
+ label: "copy_activation_link",
110
+ icon: "link",
111
+ translate: true
107
112
  }
108
113
  },
109
114
  icon: "users",
@@ -1,6 +1,22 @@
1
+ import type { Context } from '@aeriajs/types';
1
2
  import type { ObjectId } from '@aeriajs/core';
2
- export declare const getActivationLink: (userId: ObjectId) => Promise<{
3
+ import { Result, HTTPStatus } from '@aeriajs/types';
4
+ import { ActivationError } from './activate.js';
5
+ export declare const getActivationToken: (strId: string, context: Context) => Promise<string>;
6
+ export declare const getActivationLink: (payload: {
7
+ userId: ObjectId | string;
8
+ }, context: Context) => Promise<{
9
+ readonly _tag: "Error";
10
+ readonly error: import("@aeriajs/types").StrictEndpointError<import("@aeriajs/types").ACError.ResourceNotFound | import("@aeriajs/types").ACError.MalformedInput, unknown, HTTPStatus.BadRequest | HTTPStatus.NotFound>;
11
+ readonly result: undefined;
12
+ } | Result.Error<{
13
+ readonly code: ActivationError.AlreadyActiveUser;
14
+ } & {
15
+ httpStatus: HTTPStatus.Forbidden;
16
+ }> | {
3
17
  readonly _tag: "Result";
4
18
  readonly error: undefined;
5
- readonly result: string;
19
+ readonly result: {
20
+ readonly url: string;
21
+ };
6
22
  }>;
@@ -1,13 +1,39 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getActivationLink = void 0;
4
- const entrypoint_1 = require("@aeriajs/entrypoint");
3
+ exports.getActivationLink = exports.getActivationToken = void 0;
5
4
  const types_1 = require("@aeriajs/types");
6
5
  const bcrypt = require("bcrypt");
7
- const getActivationLink = async (userId) => {
8
- const config = await (0, entrypoint_1.getConfig)();
9
- const activationToken = await bcrypt.hash(userId.toString(), 10);
10
- const link = `${config.publicUrl}/user/activate?u=${userId.toString()}&t=${activationToken}`;
11
- return types_1.Result.result(link);
6
+ const activate_js_1 = require("./activate.js");
7
+ const getActivationToken = async (strId, context) => {
8
+ if (context.calledFunction === 'getActivationToken') {
9
+ throw new Error('cannot be called externally');
10
+ }
11
+ if (!context.config.secret) {
12
+ throw new Error('config.secret is not set');
13
+ }
14
+ return `${context.config.secret}:${strId}`;
15
+ };
16
+ exports.getActivationToken = getActivationToken;
17
+ const getActivationLink = async (payload, context) => {
18
+ const { error, result: user } = await context.collections.user.functions.get({
19
+ filters: {
20
+ _id: payload.userId,
21
+ },
22
+ project: ['active'],
23
+ });
24
+ if (error) {
25
+ return types_1.Result.error(error);
26
+ }
27
+ if (user.active) {
28
+ return context.error(types_1.HTTPStatus.Forbidden, {
29
+ code: activate_js_1.ActivationError.AlreadyActiveUser,
30
+ });
31
+ }
32
+ const activationToken = await (0, exports.getActivationToken)(payload.userId.toString(), context);
33
+ const encryptedActivationToken = await bcrypt.hash(activationToken, 10);
34
+ const url = `${context.config.publicUrl}/user/activate?u=${payload.userId.toString()}&t=${encryptedActivationToken}`;
35
+ return types_1.Result.result({
36
+ url,
37
+ });
12
38
  };
13
39
  exports.getActivationLink = getActivationLink;
@@ -1,10 +1,35 @@
1
1
  "use strict";
2
- import { getConfig } from "@aeriajs/entrypoint";
3
- import { Result } from "@aeriajs/types";
2
+ import { Result, HTTPStatus } from "@aeriajs/types";
4
3
  import * as bcrypt from "bcrypt";
5
- export const getActivationLink = async (userId) => {
6
- const config = await getConfig();
7
- const activationToken = await bcrypt.hash(userId.toString(), 10);
8
- const link = `${config.publicUrl}/user/activate?u=${userId.toString()}&t=${activationToken}`;
9
- return Result.result(link);
4
+ import { ActivationError } from "./activate.mjs";
5
+ export const getActivationToken = async (strId, context) => {
6
+ if (context.calledFunction === "getActivationToken") {
7
+ throw new Error("cannot be called externally");
8
+ }
9
+ if (!context.config.secret) {
10
+ throw new Error("config.secret is not set");
11
+ }
12
+ return `${context.config.secret}:${strId}`;
13
+ };
14
+ export const getActivationLink = async (payload, context) => {
15
+ const { error, result: user } = await context.collections.user.functions.get({
16
+ filters: {
17
+ _id: payload.userId
18
+ },
19
+ project: ["active"]
20
+ });
21
+ if (error) {
22
+ return Result.error(error);
23
+ }
24
+ if (user.active) {
25
+ return context.error(HTTPStatus.Forbidden, {
26
+ code: ActivationError.AlreadyActiveUser
27
+ });
28
+ }
29
+ const activationToken = await getActivationToken(payload.userId.toString(), context);
30
+ const encryptedActivationToken = await bcrypt.hash(activationToken, 10);
31
+ const url = `${context.config.publicUrl}/user/activate?u=${payload.userId.toString()}&t=${encryptedActivationToken}`;
32
+ return Result.result({
33
+ url
34
+ });
10
35
  };
@@ -92,6 +92,11 @@ export declare const getCurrentUser: (_payload: undefined, context: Context<type
92
92
  readonly fetchItem: true;
93
93
  readonly translate: true;
94
94
  };
95
+ readonly copyActivationLink: {
96
+ readonly label: "copy_activation_link";
97
+ readonly icon: "link";
98
+ readonly translate: true;
99
+ };
95
100
  };
96
101
  readonly icon: "users";
97
102
  readonly filters: readonly ["name", "roles", "email", "phone_number"];
@@ -4,6 +4,7 @@ exports.getInfo = exports.ActivationError = void 0;
4
4
  const bcrypt = require("bcrypt");
5
5
  const types_1 = require("@aeriajs/types");
6
6
  const core_1 = require("@aeriajs/core");
7
+ const getActivationLink_js_1 = require("./getActivationLink.js");
7
8
  var ActivationError;
8
9
  (function (ActivationError) {
9
10
  ActivationError["UserNotFound"] = "USER_NOT_FOUND";
@@ -30,7 +31,8 @@ const getInfo = async (payload, context) => {
30
31
  code: ActivationError.AlreadyActiveUser,
31
32
  });
32
33
  }
33
- const equal = await bcrypt.compare(user._id.toString(), token);
34
+ const activationToken = await (0, getActivationLink_js_1.getActivationToken)(user._id.toString(), context);
35
+ const equal = await bcrypt.compare(activationToken, token);
34
36
  if (!equal) {
35
37
  return context.error(types_1.HTTPStatus.NotFound, {
36
38
  code: ActivationError.InvalidLink,
@@ -2,6 +2,7 @@
2
2
  import * as bcrypt from "bcrypt";
3
3
  import { Result, HTTPStatus } from "@aeriajs/types";
4
4
  import { ObjectId } from "@aeriajs/core";
5
+ import { getActivationToken } from "./getActivationLink.mjs";
5
6
  export var ActivationError = /* @__PURE__ */ ((ActivationError2) => {
6
7
  ActivationError2["UserNotFound"] = "USER_NOT_FOUND";
7
8
  ActivationError2["AlreadyActiveUser"] = "ALREADY_ACTIVE_USER";
@@ -31,7 +32,8 @@ export const getInfo = async (payload, context) => {
31
32
  code: "ALREADY_ACTIVE_USER" /* AlreadyActiveUser */
32
33
  });
33
34
  }
34
- const equal = await bcrypt.compare(user._id.toString(), token);
35
+ const activationToken = await getActivationToken(user._id.toString(), context);
36
+ const equal = await bcrypt.compare(activationToken, token);
35
37
  if (!equal) {
36
38
  return context.error(HTTPStatus.NotFound, {
37
39
  code: "INVALID_LINK" /* InvalidLink */