@abtnode/util 1.16.11-beta-069c3537 → 1.16.11-next-a232f5fb
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/lib/format-context.js +0 -4
- package/lib/format-error.js +28 -0
- package/lib/user.js +32 -0
- package/package.json +5 -5
package/lib/format-context.js
CHANGED
|
@@ -3,10 +3,6 @@ const get = require('lodash/get');
|
|
|
3
3
|
|
|
4
4
|
// Format context: https://github.com/graphql/express-graphql
|
|
5
5
|
module.exports = (ctx = {}) => {
|
|
6
|
-
// 不要更改入参的值,base case
|
|
7
|
-
// if (ctx && ctx.user) {
|
|
8
|
-
// delete ctx.user.avatar;
|
|
9
|
-
// }
|
|
10
6
|
const contextUser = omit(ctx?.user || {}, 'avatar');
|
|
11
7
|
|
|
12
8
|
const safeGet = (key, _default = '') =>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const formatError = (err) => {
|
|
2
|
+
const { details, errors, response } = err;
|
|
3
|
+
|
|
4
|
+
// graphql error
|
|
5
|
+
if (Array.isArray(errors)) {
|
|
6
|
+
return errors.map((x) => x.message).join('\n');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// joi validate error
|
|
10
|
+
if (Array.isArray(details)) {
|
|
11
|
+
const formatted = details.map((e) => {
|
|
12
|
+
const errorMessage = e.message.replace(/["]/g, "'");
|
|
13
|
+
const errorPath = e.path.join('.');
|
|
14
|
+
return `${errorPath}: ${errorMessage}`;
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
return `Validate failed: ${formatted.join(';')}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// axios error
|
|
21
|
+
if (response) {
|
|
22
|
+
return `Request failed: ${response.status} ${response.statusText}: ${JSON.stringify(response.data)}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return err.message;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
module.exports = formatError;
|
package/lib/user.js
CHANGED
|
@@ -5,6 +5,7 @@ const cloneDeep = require('lodash/cloneDeep');
|
|
|
5
5
|
const { USER_AVATAR_DIR, USER_AVATAR_URL_PREFIX } = require('@abtnode/constant');
|
|
6
6
|
|
|
7
7
|
const md5 = require('./md5');
|
|
8
|
+
const axios = require('./axios');
|
|
8
9
|
|
|
9
10
|
const getAvatarFile = (dataDir, fileName) =>
|
|
10
11
|
path.join(dataDir, USER_AVATAR_DIR, fileName.substring(0, 2), fileName.substring(2));
|
|
@@ -90,9 +91,40 @@ function updateConnectedAccount(connectedAccounts = [], connectedAccount = {}) {
|
|
|
90
91
|
return updated;
|
|
91
92
|
}
|
|
92
93
|
|
|
94
|
+
const getAvatarByUrl = async (url) => {
|
|
95
|
+
try {
|
|
96
|
+
const { data } = await axios.get(url, {
|
|
97
|
+
responseType: 'arraybuffer',
|
|
98
|
+
});
|
|
99
|
+
const base64Content = Buffer.from(data, 'binary').toString('base64');
|
|
100
|
+
|
|
101
|
+
return `data:image/png;base64,${base64Content}`;
|
|
102
|
+
} catch (error) {
|
|
103
|
+
console.error(`Fetch avatar failed: ${url}`, error);
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const getEmailHash = (email) => md5(email.trim().toLowerCase());
|
|
109
|
+
|
|
110
|
+
const getAvatarByEmail = async (email) => {
|
|
111
|
+
try {
|
|
112
|
+
const emailHash = getEmailHash(email);
|
|
113
|
+
const gravatarUrl = `https://www.gravatar.com/avatar/${emailHash}`;
|
|
114
|
+
const avatarBase64 = await getAvatarByUrl(gravatarUrl);
|
|
115
|
+
return avatarBase64;
|
|
116
|
+
} catch (error) {
|
|
117
|
+
console.error(`Fetch gravatar failed: ${email}`, error);
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
93
122
|
module.exports = {
|
|
94
123
|
extractUserAvatar,
|
|
95
124
|
parseUserAvatar,
|
|
96
125
|
getAvatarFile,
|
|
97
126
|
updateConnectedAccount,
|
|
127
|
+
getAvatarByUrl,
|
|
128
|
+
getAvatarByEmail,
|
|
129
|
+
getEmailHash,
|
|
98
130
|
};
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.11-
|
|
6
|
+
"version": "1.16.11-next-a232f5fb",
|
|
7
7
|
"description": "ArcBlock's JavaScript utility",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
|
|
19
19
|
"license": "Apache-2.0",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@abtnode/constant": "1.16.11-
|
|
22
|
-
"@abtnode/logger": "1.16.11-
|
|
23
|
-
"@blocklet/constant": "1.16.11-
|
|
21
|
+
"@abtnode/constant": "1.16.11-next-a232f5fb",
|
|
22
|
+
"@abtnode/logger": "1.16.11-next-a232f5fb",
|
|
23
|
+
"@blocklet/constant": "1.16.11-next-a232f5fb",
|
|
24
24
|
"@ocap/client": "1.18.80",
|
|
25
25
|
"@ocap/mcrypto": "1.18.80",
|
|
26
26
|
"@ocap/util": "1.18.80",
|
|
@@ -71,5 +71,5 @@
|
|
|
71
71
|
"jest": "^27.5.1",
|
|
72
72
|
"unzipper": "^0.10.11"
|
|
73
73
|
},
|
|
74
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "1af264e9b6648aa191fbf23c67717262ac717697"
|
|
75
75
|
}
|