@blocklet/sdk 1.8.10 → 1.8.13
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.
- package/README.md +1 -361
- package/lib/env.js +1 -0
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -2,364 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
Blocklet SDK for blocklet developer
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
```shell
|
|
8
|
-
yarn add @blocklet/sdk
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
or
|
|
12
|
-
|
|
13
|
-
```shell
|
|
14
|
-
npm install @blocklet/sdk
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## Auth SDK
|
|
18
|
-
|
|
19
|
-
### Usage
|
|
20
|
-
|
|
21
|
-
```javascript
|
|
22
|
-
const Auth = require('@blocklet/sdk/service/auth');
|
|
23
|
-
|
|
24
|
-
const client = new Auth();
|
|
25
|
-
|
|
26
|
-
const userDid = 'xxxxxxxx';
|
|
27
|
-
|
|
28
|
-
const { user } = await client.getUser(userDid);
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### API
|
|
32
|
-
|
|
33
|
-
#### client.getUser(did)
|
|
34
|
-
|
|
35
|
-
Get user by user did
|
|
36
|
-
|
|
37
|
-
- _@param_ **did** `string`
|
|
38
|
-
- _@return_ `{ code, user }`
|
|
39
|
-
|
|
40
|
-
#### client.getOwner()
|
|
41
|
-
|
|
42
|
-
Get owner of the app
|
|
43
|
-
|
|
44
|
-
- _@param_ **did** `string`
|
|
45
|
-
- _@return_ `{ code, user }`
|
|
46
|
-
|
|
47
|
-
#### client.getUsers()
|
|
48
|
-
|
|
49
|
-
Get all users of the app
|
|
50
|
-
|
|
51
|
-
- _@param_ **paging** `Object`
|
|
52
|
-
- **paging.pageSize** ``
|
|
53
|
-
- **paging.page** ``
|
|
54
|
-
- _@param_ **query** `Object`
|
|
55
|
-
- **query.role** `String` Match users by role name
|
|
56
|
-
- `$none`: Match users which does not have a role
|
|
57
|
-
- **query.approved** `Boolean` Match users by approved
|
|
58
|
-
- **query.search** `String` Match users by did or fullName
|
|
59
|
-
- _@param_ **sort** `Object`
|
|
60
|
-
- **sort.createdAt** `Number`
|
|
61
|
-
- **sort.updatedAt** `Number`
|
|
62
|
-
- **sort.lastLoginAt** `Number`
|
|
63
|
-
- > `-1`: The latest time is at first. `1`: The latest time is at last.
|
|
64
|
-
- _@return_ `{ code, users, paging }`
|
|
65
|
-
|
|
66
|
-
```
|
|
67
|
-
Paging {
|
|
68
|
-
total: number of users
|
|
69
|
-
pageSize: number of users per page
|
|
70
|
-
pageCount: number of page
|
|
71
|
-
page: current page number
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
#### client.getPermissionsByRole(role)
|
|
76
|
-
|
|
77
|
-
Get all permissions of a role
|
|
78
|
-
|
|
79
|
-
- _@param_ **role** `string`
|
|
80
|
-
- _@return_ `{ code, permissions }`
|
|
81
|
-
|
|
82
|
-
#### client.getRoles()
|
|
83
|
-
|
|
84
|
-
Get all roles of the app
|
|
85
|
-
|
|
86
|
-
- _@return_ `{ code, roles }`
|
|
87
|
-
|
|
88
|
-
#### client.createRole({ name, title, description })
|
|
89
|
-
|
|
90
|
-
- _@param_ **name** `string` the key of the role, should be unique
|
|
91
|
-
- _@param_ **title** `string`
|
|
92
|
-
- _@param_ **description** `string`
|
|
93
|
-
- _@return_ `{ code, role }`
|
|
94
|
-
|
|
95
|
-
#### client.updateRole(name, { title, description })
|
|
96
|
-
|
|
97
|
-
- _@param_ **name** `string` the key of the role
|
|
98
|
-
- _@param_ **title** `string`
|
|
99
|
-
- _@param_ **description** `string`
|
|
100
|
-
- _@return_ `{ code, role }`
|
|
101
|
-
|
|
102
|
-
#### client.deleteRole(name, { title, description })
|
|
103
|
-
|
|
104
|
-
- _@param_ **name** `string` the key of the role
|
|
105
|
-
- _@return_ `{ code }`
|
|
106
|
-
|
|
107
|
-
#### client.grantPermissionForRole(role, permission)
|
|
108
|
-
|
|
109
|
-
- _@param_ **role** `string` the name of the role
|
|
110
|
-
- _@param_ **permission** `string` the name of the permission
|
|
111
|
-
- _@return_ `{ code }`
|
|
112
|
-
|
|
113
|
-
#### client.revokePermissionFromRole(role, permission)
|
|
114
|
-
|
|
115
|
-
- _@param_ **role** `string` the name of the role
|
|
116
|
-
- _@param_ **permission** `string` the name of the permission
|
|
117
|
-
- _@return_ `{ code }`
|
|
118
|
-
|
|
119
|
-
#### client.updatePermissionsForRole(role, permissions)
|
|
120
|
-
|
|
121
|
-
Full update permissions of a role
|
|
122
|
-
|
|
123
|
-
- _@param_ **role** `string` the name of the role
|
|
124
|
-
- _@param_ **permissions** `array<string>` name of the permissions
|
|
125
|
-
- _@return_ `{ code, role }`
|
|
126
|
-
|
|
127
|
-
#### client.hasPermission(role, permission)
|
|
128
|
-
|
|
129
|
-
- _@param_ **role** `string` the name of the role
|
|
130
|
-
- _@param_ **permission** `string` the name of the permission
|
|
131
|
-
- _@return_ `{ code, result }`
|
|
132
|
-
- **result** `boolean`
|
|
133
|
-
|
|
134
|
-
#### client.getPermissions()
|
|
135
|
-
|
|
136
|
-
Get all permissions of the app
|
|
137
|
-
|
|
138
|
-
- _@return_ `{ code, permissions }`
|
|
139
|
-
|
|
140
|
-
#### client.createPermission({ name, title, description })
|
|
141
|
-
|
|
142
|
-
- _@param_ **name** `Permission` the key of the permission, should be unique
|
|
143
|
-
- format: `<action>_<resource>`. e.g. `query_article`, `mutate_user`
|
|
144
|
-
- _@param_ **description** `string`
|
|
145
|
-
- _@return_ `{ code, role }`
|
|
146
|
-
|
|
147
|
-
#### client.updatePermission(name, { title, description })
|
|
148
|
-
|
|
149
|
-
- _@param_ **name** `string` the key of the role
|
|
150
|
-
- _@param_ **title** `string`
|
|
151
|
-
- _@param_ **description** `string`
|
|
152
|
-
- _@return_ `{ code }`
|
|
153
|
-
|
|
154
|
-
#### client.deletePermission(name, { title, description })
|
|
155
|
-
|
|
156
|
-
- _@param_ **name** `string` the key of the permission
|
|
157
|
-
- _@return_ `{ code }`
|
|
158
|
-
|
|
159
|
-
## Notification SDK
|
|
160
|
-
|
|
161
|
-
### Usage
|
|
162
|
-
|
|
163
|
-
```javascript
|
|
164
|
-
const Notification = require('@blocklet/sdk/service/notification');
|
|
165
|
-
|
|
166
|
-
const userDid = 'xxxxxxxx';
|
|
167
|
-
|
|
168
|
-
const notification = {
|
|
169
|
-
title: 'xxx',
|
|
170
|
-
body: 'xxx',
|
|
171
|
-
attachments: [
|
|
172
|
-
{
|
|
173
|
-
type: 'asset',
|
|
174
|
-
data: {
|
|
175
|
-
did: 'xxx',
|
|
176
|
-
chainHost: 'https://chainhost',
|
|
177
|
-
},
|
|
178
|
-
},
|
|
179
|
-
],
|
|
180
|
-
actions: [
|
|
181
|
-
{
|
|
182
|
-
name: 'xxx',
|
|
183
|
-
title: 'Go To Website',
|
|
184
|
-
link: 'https://arcblock.io',
|
|
185
|
-
},
|
|
186
|
-
],
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
const content = { message: 'this is a message' };
|
|
190
|
-
const actions = [];
|
|
191
|
-
|
|
192
|
-
await Notification.sendToUser(userDid, notification);
|
|
193
|
-
|
|
194
|
-
await Notification.sendToUser(userDid, [notification, anotherNotification]);
|
|
195
|
-
await Notification.sendToUser([userDid, anotherUserDid], notification);
|
|
196
|
-
await Notification.sendToUser([userDid, anotherUserDid], [notification, anotherNotification]);
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
### API
|
|
200
|
-
|
|
201
|
-
#### notification.sendToUser(receiver, notification)
|
|
202
|
-
|
|
203
|
-
Send notification to an account
|
|
204
|
-
|
|
205
|
-
- **receiver** `string | array<string>` required
|
|
206
|
-
- **notification** `object | array<object>` required
|
|
207
|
-
- **notification.title** `string`
|
|
208
|
-
- **notification.body** `string`
|
|
209
|
-
- **notification.attachments** `array<object>`
|
|
210
|
-
- **attachment.type** `enum` 'asset', 'vc', 'token' required
|
|
211
|
-
- **attachment.data** `object`
|
|
212
|
-
- _type: text_
|
|
213
|
-
- **type** `string`
|
|
214
|
-
- **message** `string`
|
|
215
|
-
- _type: asset_
|
|
216
|
-
- **did** `string`
|
|
217
|
-
- **chainHost** `string` uri
|
|
218
|
-
- _type: vc_
|
|
219
|
-
- **credential** `object`
|
|
220
|
-
- **tag** `string`
|
|
221
|
-
- _type: token_
|
|
222
|
-
- **address** `string` did
|
|
223
|
-
- **amount** `string`
|
|
224
|
-
- **symbol** `string`
|
|
225
|
-
- **senderDid** `string`
|
|
226
|
-
- **chainHost** `string`
|
|
227
|
-
- **decimal** `integer`
|
|
228
|
-
- **notification.actions** `array<object>`
|
|
229
|
-
- **name** `string` required
|
|
230
|
-
- **title** `string`
|
|
231
|
-
- **color** `string`
|
|
232
|
-
- **bgColor** `string`
|
|
233
|
-
- **link** `string` uri
|
|
234
|
-
|
|
235
|
-
## WalletAuthenticator SDK
|
|
236
|
-
|
|
237
|
-
### Usage
|
|
238
|
-
|
|
239
|
-
```javascript
|
|
240
|
-
const { WalletAuthenticator } = require('@blocklet/sdk');
|
|
241
|
-
|
|
242
|
-
const authenticator = new WalletAuthenticator();
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
## WalletHandler SDK
|
|
246
|
-
|
|
247
|
-
### Usage
|
|
248
|
-
|
|
249
|
-
```javascript
|
|
250
|
-
const AuthStorage = require('@arcblock/did-auth-storage-nedb');
|
|
251
|
-
const { WalletAuthenticator, WalletHandlers } = require('@blocklet/sdk');
|
|
252
|
-
|
|
253
|
-
const authenticator = new WalletAuthenticator();
|
|
254
|
-
|
|
255
|
-
const handlers = new WalletHandlers({
|
|
256
|
-
authenticator,
|
|
257
|
-
tokenStorage: new AuthStorage({
|
|
258
|
-
dbPath: path.join(process.env.BLOCKLET_DATA_DIR, 'auth.db'),
|
|
259
|
-
onload: (err) => {
|
|
260
|
-
if (err) {
|
|
261
|
-
// eslint-disable-next-line no-console
|
|
262
|
-
console.error(`Failed to load database from ${path.join(process.env.BLOCKLET_DATA_DIR, 'auth.db')}`, err);
|
|
263
|
-
}
|
|
264
|
-
},
|
|
265
|
-
}),
|
|
266
|
-
});
|
|
267
|
-
```
|
|
268
|
-
|
|
269
|
-
## Database SDK
|
|
270
|
-
|
|
271
|
-
A database library for develop blocklet, it's a wrapper of [nedb](https://www.github.com/Arcblock/nedb).
|
|
272
|
-
Supply a simpler way to use nedb. Just use `new Database([dbName])`, or you can pass a object option as second parameter to create a database as origin nedb way `new Database([dbName], [options])`
|
|
273
|
-
|
|
274
|
-
Supply full-promise support.
|
|
275
|
-
|
|
276
|
-
### Usage
|
|
277
|
-
|
|
278
|
-
```javascript
|
|
279
|
-
const { Database } = require('@blocklet/sdk');
|
|
280
|
-
|
|
281
|
-
(async () => {
|
|
282
|
-
const db1 = new Database('db1');
|
|
283
|
-
const data1 = await db1.find().skip(1).limit(10);
|
|
284
|
-
|
|
285
|
-
class MyDatabase extends Database {
|
|
286
|
-
constructor(name) {
|
|
287
|
-
super(name);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
async extraFn() {
|
|
291
|
-
return 'extra';
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
const db2 = new MyDatabase('db2');
|
|
295
|
-
const data2 = await db2.find().paginate(1, 10);
|
|
296
|
-
const data2Extra = await db2.extraFn();
|
|
297
|
-
})();
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
## Log SDK
|
|
301
|
-
|
|
302
|
-
For Blocklet to write business logs, provides a consistent write format.
|
|
303
|
-
|
|
304
|
-
### Usage
|
|
305
|
-
|
|
306
|
-
## getWallet
|
|
307
|
-
|
|
308
|
-
### Usage
|
|
309
|
-
|
|
310
|
-
```javascript
|
|
311
|
-
const { getWallet } = require('@blocklet/sdk');
|
|
312
|
-
|
|
313
|
-
// wallet is an instance of @ocap/wallet const { wallet } = env;
|
|
314
|
-
const wallet = getWallet();
|
|
315
|
-
const { address, secretKey, publicKey } = wallet;
|
|
316
|
-
```
|
|
317
|
-
|
|
318
|
-
## env
|
|
319
|
-
|
|
320
|
-
### Usage
|
|
321
|
-
|
|
322
|
-
```javascript
|
|
323
|
-
const { env } = require('@blocklet/sdk');
|
|
324
|
-
|
|
325
|
-
const { appId, appName, appDescription, appUrl, isComponent, dataDir, cacheDir } = env;
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
## middlewares
|
|
329
|
-
|
|
330
|
-
### Usage
|
|
331
|
-
|
|
332
|
-
```javascript
|
|
333
|
-
const express = require('express');
|
|
334
|
-
const { middlewares } = require('@blocklet/sdk');
|
|
335
|
-
|
|
336
|
-
const app = express();
|
|
337
|
-
|
|
338
|
-
app.get('/', middlewares.user(), (req, res) => {
|
|
339
|
-
const { did, fullName, role } = req.user;
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
app.get('/auth1', middlewares.auth(), (req, res) => {
|
|
343
|
-
// will return 401 if user is not connected
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
app.get('/auth2', middlewares.auth({ roles: ['admin', 'owner'] }), (req, res) => {
|
|
347
|
-
// will return 401 if user is not connected
|
|
348
|
-
// will return 403 if user role is neither owner nor admin
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
app.get('/auth2', middlewares.auth({ permissions: ['mutate_data', 'query_data'] }), (req, res) => {
|
|
352
|
-
// will return 401 if user is not connected
|
|
353
|
-
// will return 403 if neither 'mutate_data' nor 'query data' in user permissions
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
app.get(
|
|
357
|
-
'/auth3',
|
|
358
|
-
middlewares.auth({ roles: ['admin', 'owner'], permissions: ['mutate_data', 'query_data'] }),
|
|
359
|
-
(req, res) => {
|
|
360
|
-
// will return 401 if user is not connected
|
|
361
|
-
// will return 403 if user role is neither owner nor admin
|
|
362
|
-
// will return 403 if neither 'mutate_data' nor 'query data' in user permissions
|
|
363
|
-
}
|
|
364
|
-
);
|
|
365
|
-
```
|
|
5
|
+
See [https://developer.blocklet.io/docs/](https://developer.blocklet.io/docs/)
|
package/lib/env.js
CHANGED
|
@@ -38,6 +38,7 @@ module.exports = Object.freeze({
|
|
|
38
38
|
isComponent: process.env.BLOCKLET_DID !== process.env.BLOCKLET_REAL_DID,
|
|
39
39
|
dataDir: process.env.BLOCKLET_DATA_DIR,
|
|
40
40
|
cacheDir: process.env.BLOCKLET_CACHE_DIR,
|
|
41
|
+
mode: process.env.BLOCKLET_MODE,
|
|
41
42
|
getWebEndpoint,
|
|
42
43
|
getChildWebEndpoint,
|
|
43
44
|
getParentWebEndpoint,
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.8.
|
|
6
|
+
"version": "1.8.13",
|
|
7
7
|
"description": "graphql client to read/write data on abt node",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -19,17 +19,17 @@
|
|
|
19
19
|
"author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@abtnode/client": "1.8.
|
|
23
|
-
"@abtnode/constant": "1.8.
|
|
24
|
-
"@arcblock/did-auth": "1.17.
|
|
25
|
-
"@arcblock/jwt": "1.17.
|
|
26
|
-
"@arcblock/ws": "1.17.
|
|
27
|
-
"@blocklet/meta": "1.8.
|
|
28
|
-
"@did-connect/authenticator": "^2.1.
|
|
29
|
-
"@did-connect/handler": "^2.1.
|
|
22
|
+
"@abtnode/client": "1.8.13",
|
|
23
|
+
"@abtnode/constant": "1.8.13",
|
|
24
|
+
"@arcblock/did-auth": "1.17.15",
|
|
25
|
+
"@arcblock/jwt": "1.17.15",
|
|
26
|
+
"@arcblock/ws": "1.17.15",
|
|
27
|
+
"@blocklet/meta": "1.8.13",
|
|
28
|
+
"@did-connect/authenticator": "^2.1.11",
|
|
29
|
+
"@did-connect/handler": "^2.1.11",
|
|
30
30
|
"@nedb/core": "^1.3.4",
|
|
31
|
-
"@ocap/mcrypto": "1.17.
|
|
32
|
-
"@ocap/wallet": "1.17.
|
|
31
|
+
"@ocap/mcrypto": "1.17.15",
|
|
32
|
+
"@ocap/wallet": "1.17.15",
|
|
33
33
|
"axios": "^0.27.2",
|
|
34
34
|
"fs-extra": "^10.1.0",
|
|
35
35
|
"joi": "^17.6.0",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"detect-port": "^1.3.0",
|
|
42
42
|
"jest": "^27.5.1"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "028f33d8a3a4f999456bfe8e7bd2e1a53b664b47"
|
|
45
45
|
}
|