@blocklet/sdk 1.8.32 → 1.8.34

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 (53) hide show
  1. package/lib/component/index.d.ts +35 -0
  2. package/lib/component/index.js +83 -80
  3. package/lib/config.d.ts +33 -0
  4. package/lib/config.js +10 -7
  5. package/lib/connect/authenticator.d.ts +5 -0
  6. package/lib/connect/authenticator.js +14 -15
  7. package/lib/connect/handler.d.ts +2 -0
  8. package/lib/connect/handler.js +91 -65
  9. package/lib/connect/shared.d.ts +13 -0
  10. package/lib/connect/shared.js +26 -31
  11. package/lib/database/index.d.ts +13 -0
  12. package/lib/database/index.js +52 -39
  13. package/lib/env.d.ts +12 -0
  14. package/lib/env.js +12 -9
  15. package/lib/error-handler.d.ts +0 -0
  16. package/lib/error-handler.js +5 -5
  17. package/lib/index.d.ts +51 -0
  18. package/lib/index.js +51 -40
  19. package/lib/middlewares/auth.d.ts +3 -0
  20. package/lib/middlewares/auth.js +52 -49
  21. package/lib/middlewares/component.d.ts +7 -0
  22. package/lib/middlewares/component.js +28 -24
  23. package/lib/middlewares/index.d.ts +20 -0
  24. package/lib/middlewares/index.js +16 -8
  25. package/lib/middlewares/user.d.ts +10 -0
  26. package/lib/middlewares/user.js +20 -10
  27. package/lib/security/index.d.ts +9 -0
  28. package/lib/security/index.js +22 -21
  29. package/lib/service/auth.d.ts +9 -0
  30. package/lib/service/auth.js +131 -165
  31. package/lib/service/notification.d.ts +44 -0
  32. package/lib/service/notification.js +149 -129
  33. package/lib/types/notification.d.ts +71 -0
  34. package/lib/types/notification.js +3 -0
  35. package/lib/util/check-blocklet-env.d.ts +2 -0
  36. package/lib/util/check-blocklet-env.js +20 -19
  37. package/lib/util/constants.d.ts +5 -0
  38. package/lib/util/constants.js +6 -2
  39. package/lib/util/send-notification.d.ts +25 -0
  40. package/lib/util/send-notification.js +118 -115
  41. package/lib/validators/index.d.ts +12 -0
  42. package/lib/validators/index.js +13 -7
  43. package/lib/validators/notification.d.ts +59 -0
  44. package/lib/validators/notification.js +127 -97
  45. package/lib/version.d.ts +6 -0
  46. package/lib/version.js +6 -0
  47. package/lib/wallet-authenticator.d.ts +5 -0
  48. package/lib/wallet-authenticator.js +13 -16
  49. package/lib/wallet-handler.d.ts +17 -0
  50. package/lib/wallet-handler.js +121 -88
  51. package/lib/wallet.d.ts +3 -0
  52. package/lib/wallet.js +9 -10
  53. package/package.json +40 -18
@@ -1,175 +1,141 @@
1
- const pickBy = require('lodash/pickBy');
2
- const joinUrl = require('url-join');
3
- const Client = require('@abtnode/client');
4
- const { types } = require('@ocap/mcrypto');
5
- const { WalletType } = require('@ocap/wallet');
6
- const {
7
- NODE_SERVICES,
8
- WELLKNOWN_SERVICE_PATH_PREFIX,
9
- USER_AVATAR_URL_PREFIX,
10
- USER_AVATAR_PATH_PREFIX,
11
- } = require('@abtnode/constant');
12
-
13
- const VERSION = require('../../package.json').version; // version of notification sdk
14
- const checkBlockletEnv = require('../util/check-blocklet-env');
15
-
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ /* eslint-disable @typescript-eslint/indent */
15
+ const pickBy_1 = __importDefault(require("lodash/pickBy"));
16
+ const url_join_1 = __importDefault(require("url-join"));
17
+ const client_1 = __importDefault(require("@abtnode/client"));
18
+ const mcrypto_1 = require("@ocap/mcrypto");
19
+ const wallet_1 = require("@ocap/wallet");
20
+ const constant_1 = __importDefault(require("@abtnode/constant"));
21
+ const check_blocklet_env_1 = __importDefault(require("../util/check-blocklet-env"));
22
+ const version_1 = require("../version");
23
+ const { NODE_SERVICES, WELLKNOWN_SERVICE_PATH_PREFIX, USER_AVATAR_URL_PREFIX, USER_AVATAR_PATH_PREFIX } = constant_1.default;
24
+ const VERSION = version_1.version; // version of notification sdk
16
25
  // Auth 相关的功能 API 都在这里
17
26
  // core/state/lib/api/team.js L42
18
-
19
27
  // 所有可配置调用的函数在这里,如果需要额外增加,则需要在这里新增对应的函数
20
28
  // core/gql/lib/config.js L25
21
-
22
29
  const isNotNullOrUndefined = (x) => ![null, undefined].includes(x);
23
-
24
- const type =
25
- process.env.BLOCKLET_WALLET_TYPE !== 'eth'
26
- ? WalletType({ role: types.RoleType.ROLE_APPLICATION, pk: types.KeyType.ED25519, hash: types.HashType.SHA3 })
30
+ const type = process.env.BLOCKLET_WALLET_TYPE !== 'eth'
31
+ ? (0, wallet_1.WalletType)({ role: mcrypto_1.types.RoleType.ROLE_APPLICATION, pk: mcrypto_1.types.KeyType.ED25519, hash: mcrypto_1.types.HashType.SHA3 })
27
32
  : 'eth';
28
-
29
33
  const fixAvatar = (user) => {
30
- const avatar = user?.avatar;
31
- if (avatar && avatar.startsWith(USER_AVATAR_URL_PREFIX)) {
32
- user.avatar = joinUrl(
33
- WELLKNOWN_SERVICE_PATH_PREFIX,
34
- USER_AVATAR_PATH_PREFIX,
35
- avatar.replace(USER_AVATAR_URL_PREFIX, '')
36
- );
37
- }
38
- return user;
34
+ const avatar = user === null || user === void 0 ? void 0 : user.avatar;
35
+ if (avatar && avatar.startsWith(USER_AVATAR_URL_PREFIX)) {
36
+ user.avatar = (0, url_join_1.default)(WELLKNOWN_SERVICE_PATH_PREFIX, USER_AVATAR_PATH_PREFIX, avatar.replace(USER_AVATAR_URL_PREFIX, ''));
37
+ }
38
+ return user;
39
39
  };
40
-
41
- class AuthService extends Client {
42
- constructor(httpEndpoint) {
43
- checkBlockletEnv();
44
-
45
- super(httpEndpoint || `http://127.0.0.1:${process.env.ABT_NODE_PORT}/api/gql`.trim(), `BlockletSDK/${VERSION}`);
46
-
47
- this.setAuthAccessKey({
48
- accessKeyId: process.env.BLOCKLET_APP_ID,
49
- accessKeySecret: process.env.BLOCKLET_APP_SK,
50
- type,
51
- });
52
-
53
- this._blockletDid = process.env.BLOCKLET_DID;
54
-
55
- const apiList = [
56
- // user
57
- 'getUsers',
58
- 'getUser',
59
- 'getOwner',
60
- // 'removeUser',
61
- 'updateUserRole', // deprecated
62
- // 'updateUserApproval',
63
-
64
- // invitation
65
- // 'getInvitations',
66
- // 'createMemberInvitation',
67
- // 'deleteInvitation',
68
-
69
- // rbac
70
- 'getRoles',
71
- 'createRole',
72
- 'updateRole',
73
- 'deleteRole',
74
- 'grantPermissionForRole', // grant
75
- 'revokePermissionFromRole', // revoke
76
- 'updatePermissionsForRole', // grant many
77
- 'hasPermission',
78
-
79
- // user passport
80
- 'issuePassportToUser',
81
- 'revokeUserPassport',
82
- 'enableUserPassport',
83
- // 'getPassportIssuances',
84
- // 'createPassportIssuance',
85
- // 'deletePassportIssuance',
86
-
87
- // permission
88
- 'getPermissions',
89
- 'createPermission',
90
- 'updatePermission',
91
- 'deletePermission',
92
- 'getPermissionsByRole',
93
-
94
- // disabled in current
95
- // 'configTrustedPassports',
96
- ];
97
-
98
- const teamDid = this._blockletDid;
99
-
100
- const apiFallback =
101
- (fn) =>
102
- (params = {}, ...args) =>
103
- fn({ input: { ...params, teamDid }, ...args });
104
-
105
- const apiConvertDid = (fn) => (did) => fn({ input: { user: { did }, teamDid } });
106
-
107
- const apiFnMap = {
108
- getUser: (fn) => async (did) => {
109
- const res = await fn({ input: { user: { did }, teamDid } });
110
- fixAvatar(res.user);
111
- return res;
112
- },
113
-
114
- getUsers: (fn) => async (args) => {
115
- const res = await fn({ input: { teamDid, ...args } });
116
- (res.users || []).forEach(fixAvatar);
117
- return res;
118
- },
119
-
120
- removeUser: apiConvertDid,
121
- updateUserRole: (fn) => (did, role) => fn({ input: { user: { did, role }, teamDid } }),
122
- updateUserApproval: (fn) => (did, approved) => fn({ input: { user: { did, approved }, teamDid } }),
123
- getPermissionsByRole: (fn) => (name) => fn({ input: { role: { name }, teamDid } }),
124
- createRole:
125
- (fn) =>
126
- ({ name, title, description }) =>
127
- fn({ input: { ...pickBy({ name, title, description }, isNotNullOrUndefined), teamDid } }),
128
- updateRole:
129
- (fn) =>
130
- (name, { title, description }) =>
131
- fn({ input: { role: pickBy({ name, title, description }, isNotNullOrUndefined), teamDid } }), // todo
132
- deleteRole: (fn) => (name) => fn({ input: { name, teamDid } }),
133
- grantPermissionForRole: (fn) => (roleName, permissionName) =>
134
- fn({ input: { teamDid, roleName, grantName: permissionName } }),
135
- revokePermissionFromRole: (fn) => (roleName, permissionName) =>
136
- fn({ input: { teamDid, roleName, grantName: permissionName } }),
137
- updatePermissionsForRole: (fn) => (roleName, permissionNames) =>
138
- fn({ input: { teamDid, roleName, grantNames: permissionNames } }),
139
- createPermission:
140
- (fn) =>
141
- ({ name, description }) =>
142
- fn({ input: { ...pickBy({ name, description }, isNotNullOrUndefined), teamDid } }),
143
- updatePermission:
144
- (fn) =>
145
- (name, { description }) =>
146
- fn({ input: { permission: pickBy({ name, description }, isNotNullOrUndefined), teamDid } }), // todo
147
- deletePermission: (fn) => (name) => fn({ input: { name, teamDid } }),
148
- hasPermission: (fn) => (role, permission) => fn({ input: { teamDid, role, permission } }),
149
- };
150
-
151
- apiList.forEach((api) => {
152
- const fn = this[api];
153
- this[api] = apiFnMap[api] ? apiFnMap[api](fn) : apiFallback(fn);
154
- });
155
-
156
- // eslint-disable-next-line no-constructor-return
157
- return new Proxy(this, {
158
- get(target, propKey) {
159
- if (!apiList.includes(propKey)) {
160
- return undefined;
161
- }
162
- return target[propKey];
163
- },
164
- });
165
- }
166
-
167
- _getAuthHeaders() {
168
- const headers = super._getAuthHeaders();
169
- headers['x-access-blocklet'] = this._blockletDid;
170
- headers['x-access-service'] = NODE_SERVICES.AUTH_SERVICE;
171
- return headers;
172
- }
40
+ // FIXME: types from @abtnode/types
41
+ class AuthService extends client_1.default {
42
+ constructor(httpEndpoint) {
43
+ (0, check_blocklet_env_1.default)();
44
+ // @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 2.
45
+ super(httpEndpoint || `http://127.0.0.1:${process.env.ABT_NODE_PORT}/api/gql`.trim(), `BlockletSDK/${VERSION}`);
46
+ this.setAuthAccessKey({
47
+ accessKeyId: process.env.BLOCKLET_APP_ID,
48
+ accessKeySecret: process.env.BLOCKLET_APP_SK,
49
+ type,
50
+ });
51
+ this._blockletDid = process.env.BLOCKLET_DID;
52
+ const apiList = [
53
+ // user
54
+ 'getUsers',
55
+ 'getUser',
56
+ 'getOwner',
57
+ // 'removeUser',
58
+ 'updateUserRole',
59
+ // 'updateUserApproval',
60
+ // invitation
61
+ // 'getInvitations',
62
+ // 'createMemberInvitation',
63
+ // 'deleteInvitation',
64
+ // rbac
65
+ 'getRoles',
66
+ 'createRole',
67
+ 'updateRole',
68
+ 'deleteRole',
69
+ 'grantPermissionForRole',
70
+ 'revokePermissionFromRole',
71
+ 'updatePermissionsForRole',
72
+ 'hasPermission',
73
+ // user passport
74
+ 'issuePassportToUser',
75
+ 'revokeUserPassport',
76
+ 'enableUserPassport',
77
+ // 'getPassportIssuances',
78
+ // 'createPassportIssuance',
79
+ // 'deletePassportIssuance',
80
+ // permission
81
+ 'getPermissions',
82
+ 'createPermission',
83
+ 'updatePermission',
84
+ 'deletePermission',
85
+ 'getPermissionsByRole',
86
+ // disabled in current
87
+ // 'configTrustedPassports',
88
+ ];
89
+ const teamDid = this._blockletDid;
90
+ const apiFallback = (fn) => (params = {}, ...args) => fn(Object.assign({ input: Object.assign(Object.assign({}, params), { teamDid }) }, args));
91
+ const apiConvertDid = (fn) => (did) => fn({ input: { user: { did }, teamDid } });
92
+ const apiFnMap = {
93
+ getUser: (fn) => (did) => __awaiter(this, void 0, void 0, function* () {
94
+ const res = yield fn({ input: { user: { did }, teamDid } });
95
+ fixAvatar(res.user);
96
+ return res;
97
+ }),
98
+ getUsers: (fn) => (args) => __awaiter(this, void 0, void 0, function* () {
99
+ const res = yield fn({ input: Object.assign({ teamDid }, args) });
100
+ (res.users || []).forEach(fixAvatar);
101
+ return res;
102
+ }),
103
+ removeUser: apiConvertDid,
104
+ updateUserRole: (fn) => (did, role) => fn({ input: { user: { did, role }, teamDid } }),
105
+ updateUserApproval: (fn) => (did, approved) => fn({ input: { user: { did, approved }, teamDid } }),
106
+ getPermissionsByRole: (fn) => (name) => fn({ input: { role: { name }, teamDid } }),
107
+ createRole: (fn) => ({ name, title, description }) => fn({ input: Object.assign(Object.assign({}, (0, pickBy_1.default)({ name, title, description }, isNotNullOrUndefined)), { teamDid }) }),
108
+ updateRole: (fn) => (name, { title, description }) => fn({ input: { role: (0, pickBy_1.default)({ name, title, description }, isNotNullOrUndefined), teamDid } }),
109
+ deleteRole: (fn) => (name) => fn({ input: { name, teamDid } }),
110
+ grantPermissionForRole: (fn) => (roleName, permissionName) => fn({ input: { teamDid, roleName, grantName: permissionName } }),
111
+ revokePermissionFromRole: (fn) => (roleName, permissionName) => fn({ input: { teamDid, roleName, grantName: permissionName } }),
112
+ updatePermissionsForRole: (fn) => (roleName, permissionNames) => fn({ input: { teamDid, roleName, grantNames: permissionNames } }),
113
+ createPermission: (fn) => ({ name, description }) => fn({ input: Object.assign(Object.assign({}, (0, pickBy_1.default)({ name, description }, isNotNullOrUndefined)), { teamDid }) }),
114
+ updatePermission: (fn) => (name, { description }) => fn({ input: { permission: (0, pickBy_1.default)({ name, description }, isNotNullOrUndefined), teamDid } }),
115
+ deletePermission: (fn) => (name) => fn({ input: { name, teamDid } }),
116
+ hasPermission: (fn) => (role, permission) => fn({ input: { teamDid, role, permission } }),
117
+ };
118
+ apiList.forEach((api) => {
119
+ const fn = this[api];
120
+ this[api] = apiFnMap[api] ? apiFnMap[api](fn) : apiFallback(fn);
121
+ });
122
+ // eslint-disable-next-line no-constructor-return
123
+ return new Proxy(this, {
124
+ get(target, propKey) {
125
+ // @ts-expect-error TS(2345) FIXME: Argument of type 'string | symbol' is not assignab... Remove this comment to see the full error message
126
+ if (!apiList.includes(propKey)) {
127
+ return undefined;
128
+ }
129
+ return target[propKey];
130
+ },
131
+ });
132
+ }
133
+ _getAuthHeaders() {
134
+ // @ts-expect-error TS(2339) FIXME: Property '_getAuthHeaders' does not exist on type ... Remove this comment to see the full error message
135
+ const headers = super._getAuthHeaders();
136
+ headers['x-access-blocklet'] = this._blockletDid;
137
+ headers['x-access-service'] = NODE_SERVICES.AUTH_SERVICE;
138
+ return headers;
139
+ }
173
140
  }
174
-
175
141
  module.exports = AuthService;
@@ -0,0 +1,44 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from 'events';
3
+ import { TNotificationInput, TSendOptions } from '../types/notification';
4
+ declare type $TSFixMe = any;
5
+ /**
6
+ *
7
+ * @param {Notification} notification
8
+ * @param {{
9
+ * keepForOfflineUser: Boolean
10
+ * }} options
11
+ * @returns
12
+ */
13
+ declare const doSendToUser: (receiver: string | string[], notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
14
+ /**
15
+ *
16
+ * @param {Notification} notification
17
+ * @param {{
18
+ * channel: String
19
+ * event: String
20
+ * socketId: String
21
+ * socketDid: String
22
+ * }} options
23
+ * @returns
24
+ */
25
+ declare const broadcast: (notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
26
+ export declare const on: (event: string, cb?: $TSFixMe) => EventEmitter;
27
+ export declare const off: any;
28
+ export declare const _message: {
29
+ on: (event: string, cb: $TSFixMe) => EventEmitter;
30
+ off: any;
31
+ };
32
+ export { doSendToUser as sendToUser };
33
+ export { broadcast };
34
+ declare const _default: {
35
+ sendToUser: (receiver: string | string[], notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
36
+ broadcast: (notification: TNotificationInput, options?: TSendOptions) => Promise<any>;
37
+ on: (event: string, cb?: any) => EventEmitter;
38
+ off: any;
39
+ _message: {
40
+ on: (event: string, cb: any) => EventEmitter;
41
+ off: any;
42
+ };
43
+ };
44
+ export default _default;