@yuntower/yuntower-account-node-sdk 0.0.12 → 0.0.14

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/dist/index.d.ts CHANGED
@@ -57,5 +57,13 @@ declare class YunTowerAccountSDK {
57
57
  msg: string;
58
58
  data: any;
59
59
  }>;
60
+ /**
61
+ * 获取用户第三方账号信息
62
+ * @param {string} access_token 用户访问凭证
63
+ * @param {string} appid 应用ID
64
+ * @param {string} appsecret 应用密钥
65
+ * @returns
66
+ */
67
+ getThirdPartyAccount(access_token: string, appid?: string, appsecret?: string): Promise<any>;
60
68
  }
61
69
  export default YunTowerAccountSDK;
package/dist/index.js CHANGED
@@ -121,5 +121,21 @@ class YunTowerAccountSDK {
121
121
  });
122
122
  return res;
123
123
  }
124
+ /**
125
+ * 获取用户第三方账号信息
126
+ * @param {string} access_token 用户访问凭证
127
+ * @param {string} appid 应用ID
128
+ * @param {string} appsecret 应用密钥
129
+ * @returns
130
+ */
131
+ async getThirdPartyAccount(access_token, appid = this.config.appid, appsecret = this.config.appsecret) {
132
+ const res = await this.fetch(`${this.config.api}/user/thirdparty`, "POST", {
133
+ appid,
134
+ appsecret
135
+ }, {
136
+ Authorization: `Bearer ${access_token}`
137
+ });
138
+ return res;
139
+ }
124
140
  }
125
141
  export default YunTowerAccountSDK;
package/package.json CHANGED
@@ -1,11 +1,23 @@
1
1
  {
2
2
  "name": "@yuntower/yuntower-account-node-sdk",
3
3
  "description": "YunTower Account Node SDK",
4
- "version": "0.0.12",
4
+ "version": "0.0.14",
5
5
  "private": false,
6
6
  "author": "yuntower",
7
7
  "license": "Apache-2.0",
8
- "main": "index.js",
8
+ "main": "dist/index.js",
9
+ "module": "dist/index.js",
10
+ "types": "dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "require": "./dist/index.js",
14
+ "import": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
9
21
  "dependencies": {
10
22
  "undici-types": "^6.19.8"
11
23
  },
package/index.js DELETED
@@ -1,226 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
24
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- Object.defineProperty(exports, "__esModule", { value: true });
50
- var YunTowerAccountSDK = /** @class */ (function () {
51
- function YunTowerAccountSDK(_a) {
52
- var appid = _a.appid, appsecret = _a.appsecret, _b = _a.state, state = _b === void 0 ? null : _b, _c = _a.scope, scope = _c === void 0 ? 'user_profile' : _c;
53
- if (!appid || !appsecret || !scope) {
54
- console.error('[YunTowerAccountSDK] 参数缺失');
55
- }
56
- if (!['user_profile'].includes(scope)) {
57
- console.error('[YunTowerAccountSDK] scope参数错误,目前只支持[user_profile]');
58
- }
59
- this.config = {
60
- auth: 'https://account.yuntower.com',
61
- api: 'https://v1.api.account.yuntower.cn',
62
- // auth:'http://localhost:3000',
63
- // api:'http://127.0.0.1:8787',
64
- origin_white_list: ['account.yuntower.cn', 'account.yuntower.com', 'localhost:3000'],
65
- appid: appid,
66
- appsecret: appsecret,
67
- scope: scope,
68
- state: state,
69
- };
70
- }
71
- /**
72
- * fetch
73
- * @param {string} url - 请求URL
74
- * @param {string} method - 请求方法 (GET | POST)
75
- * @param {Object} [data] - POST请求时的数据
76
- * @param {Object} [headers] - 自定义请求头
77
- * @returns {Promise<any>} - 解析后的响应体
78
- */
79
- YunTowerAccountSDK.prototype.fetch = function (url_1, method_1) {
80
- return __awaiter(this, arguments, void 0, function (url, method, data, headers) {
81
- var defaultHeaders, options, response, responseBody, error_1;
82
- if (data === void 0) { data = {}; }
83
- if (headers === void 0) { headers = {}; }
84
- return __generator(this, function (_a) {
85
- switch (_a.label) {
86
- case 0:
87
- _a.trys.push([0, 3, , 4]);
88
- defaultHeaders = __assign({ 'Content-Type': 'application/json' }, headers);
89
- options = {
90
- method: method,
91
- headers: defaultHeaders,
92
- };
93
- // 如果是POST请求,需要设置body
94
- if (method.toUpperCase() === 'POST') {
95
- options.body = JSON.stringify(data);
96
- }
97
- return [4 /*yield*/, fetch(url, options)];
98
- case 1:
99
- response = _a.sent();
100
- // 检查响应状态
101
- if (!response.ok) {
102
- throw new Error("HTTP error! Status: ".concat(response.status));
103
- }
104
- return [4 /*yield*/, response.json()];
105
- case 2:
106
- responseBody = _a.sent();
107
- console.log('[YunTowerAccountSDK]: ', responseBody);
108
- return [2 /*return*/, responseBody];
109
- case 3:
110
- error_1 = _a.sent();
111
- console.error('Fetch error:', error_1);
112
- throw error_1;
113
- case 4: return [2 /*return*/];
114
- }
115
- });
116
- });
117
- };
118
- /**
119
- * 获取用户访问凭证
120
- * @param {string} token 临时Token
121
- * @param {string} tuid 临时UID
122
- * @param {string} appid 应用ID
123
- * @param {string} appsecret 应用密钥
124
- * @returns
125
- */
126
- YunTowerAccountSDK.prototype.getUserToken = function (token_1, tuid_1) {
127
- return __awaiter(this, arguments, void 0, function (token, tuid, appid, appsecret) {
128
- var res;
129
- if (appid === void 0) { appid = this.config.appid; }
130
- if (appsecret === void 0) { appsecret = this.config.appsecret; }
131
- return __generator(this, function (_a) {
132
- switch (_a.label) {
133
- case 0: return [4 /*yield*/, this.fetch("".concat(this.config.api, "/user/token/get"), "POST", {
134
- token: token,
135
- tuid: tuid,
136
- appid: appid,
137
- appsecret: appsecret
138
- })];
139
- case 1:
140
- res = _a.sent();
141
- return [2 /*return*/, res];
142
- }
143
- });
144
- });
145
- };
146
- /**
147
- * 获取用户数据
148
- * @param {string} access_token 用户访问凭证
149
- * @param {string} appid 应用ID
150
- * @param {string} appsecret 应用密钥
151
- * @returns
152
- */
153
- YunTowerAccountSDK.prototype.getUserInfo = function (access_token_1) {
154
- return __awaiter(this, arguments, void 0, function (access_token, appid, appsecret) {
155
- var res;
156
- if (appid === void 0) { appid = this.config.appid; }
157
- if (appsecret === void 0) { appsecret = this.config.appsecret; }
158
- return __generator(this, function (_a) {
159
- switch (_a.label) {
160
- case 0: return [4 /*yield*/, this.fetch("".concat(this.config.api, "/user/data"), "POST", {
161
- appid: appid,
162
- appsecret: appsecret
163
- }, {
164
- Authorization: "Bearer ".concat(access_token)
165
- })];
166
- case 1:
167
- res = _a.sent();
168
- return [2 /*return*/, res];
169
- }
170
- });
171
- });
172
- };
173
- /**
174
- * 刷新用户访问凭证
175
- * @param {string} refresh_token 用户刷新凭证
176
- * @param {string} appid 应用ID
177
- * @param {string} appsecret 应用密钥
178
- * @returns
179
- */
180
- YunTowerAccountSDK.prototype.refreshUserToken = function (refresh_token_1) {
181
- return __awaiter(this, arguments, void 0, function (refresh_token, appid, appsecret) {
182
- var res;
183
- if (appid === void 0) { appid = this.config.appid; }
184
- if (appsecret === void 0) { appsecret = this.config.appsecret; }
185
- return __generator(this, function (_a) {
186
- switch (_a.label) {
187
- case 0: return [4 /*yield*/, this.fetch("".concat(this.config.api, "/user/token/refresh"), "POST", {
188
- appid: appid,
189
- appsecret: appsecret
190
- }, {
191
- Authorization: "Bearer ".concat(refresh_token)
192
- })];
193
- case 1:
194
- res = _a.sent();
195
- return [2 /*return*/, res];
196
- }
197
- });
198
- });
199
- };
200
- /**
201
- * 退出登录状态
202
- * @param {string} access_token 用户访问凭证
203
- */
204
- YunTowerAccountSDK.prototype.logout = function (access_token_1) {
205
- return __awaiter(this, arguments, void 0, function (access_token, appid, appsecret) {
206
- var res;
207
- if (appid === void 0) { appid = this.config.appid; }
208
- if (appsecret === void 0) { appsecret = this.config.appsecret; }
209
- return __generator(this, function (_a) {
210
- switch (_a.label) {
211
- case 0: return [4 /*yield*/, this.fetch("".concat(this.config.api, "/user/logout"), "POST", {
212
- appid: appid,
213
- appsecret: appsecret
214
- }, {
215
- Authorization: "Bearer ".concat(access_token)
216
- })];
217
- case 1:
218
- res = _a.sent();
219
- return [2 /*return*/, res];
220
- }
221
- });
222
- });
223
- };
224
- return YunTowerAccountSDK;
225
- }());
226
- exports.default = YunTowerAccountSDK;
package/src/index.ts DELETED
@@ -1,163 +0,0 @@
1
- class YunTowerAccountSDK {
2
- config: {
3
- auth: string,
4
- api: string,
5
- origin_white_list: string[],
6
- appid: string,
7
- appsecret: string,
8
- scope: string,
9
- state: any
10
- };
11
-
12
- constructor({
13
- appid,
14
- appsecret,
15
- state = null,
16
- scope = 'user_profile',
17
- }: {
18
- appid: string;
19
- appsecret: string;
20
- scope: string;
21
- state?: any;
22
- }) {
23
- if (!appid || !appsecret || !scope) {
24
- console.error('[YunTowerAccountSDK] 参数缺失');
25
- }
26
-
27
- if (!['user_profile'].includes(scope)) {
28
- console.error('[YunTowerAccountSDK] scope参数错误,目前只支持[user_profile]');
29
- }
30
-
31
- this.config = {
32
- auth: 'https://account.yuntower.com',
33
- api: 'https://v1.api.account.yuntower.cn',
34
- // auth:'http://localhost:3000',
35
- // api:'http://127.0.0.1:8787',
36
- origin_white_list: ['account.yuntower.cn', 'account.yuntower.com', 'localhost:3000'],
37
- appid,
38
- appsecret,
39
- scope,
40
- state,
41
- };
42
- }
43
-
44
- /**
45
- * fetch
46
- * @param {string} url - 请求URL
47
- * @param {string} method - 请求方法 (GET | POST)
48
- * @param {Object} [data] - POST请求时的数据
49
- * @param {Object} [headers] - 自定义请求头
50
- * @returns {Promise<any>} - 解析后的响应体
51
- */
52
- async fetch(url: string, method: 'GET' | 'POST', data: object = {}, headers: object = {}): Promise<any> {
53
- try {
54
- // 设置默认请求头
55
- const defaultHeaders = {
56
- 'Content-Type': 'application/json',
57
- ...headers,
58
- };
59
-
60
- // 构建fetch选项
61
- const options: RequestInit = {
62
- method,
63
- headers: defaultHeaders,
64
- };
65
-
66
- // 如果是POST请求,需要设置body
67
- if (method.toUpperCase() === 'POST') {
68
- options.body = JSON.stringify(data);
69
- }
70
-
71
- // 发起请求
72
- const response = await fetch(url, options);
73
-
74
- // 检查响应状态
75
- if (!response.ok) {
76
- throw new Error(`HTTP error! Status: ${response.status}`);
77
- }
78
-
79
- // 解析响应体
80
- const responseBody = await response.json();
81
- console.log('[YunTowerAccountSDK]: ', responseBody)
82
- return responseBody;
83
- } catch (error) {
84
- console.error('Fetch error:', error);
85
- throw error;
86
- }
87
- }
88
-
89
- /**
90
- * 获取用户访问凭证
91
- * @param {string} token 临时Token
92
- * @param {string} tuid 临时UID
93
- * @param {string} appid 应用ID
94
- * @param {string} appsecret 应用密钥
95
- * @returns
96
- */
97
- async getUserToken(token: string, tuid: string, appid: string = this.config.appid, appsecret: string = this.config.appsecret): Promise<any> {
98
- const res = await this.fetch(`${this.config.api}/user/token/get`, "POST", {
99
- token,
100
- tuid,
101
- appid,
102
- appsecret
103
- });
104
-
105
- return res;
106
- }
107
-
108
- /**
109
- * 获取用户数据
110
- * @param {string} access_token 用户访问凭证
111
- * @param {string} appid 应用ID
112
- * @param {string} appsecret 应用密钥
113
- * @returns
114
- */
115
- async getUserInfo(access_token: string, appid: string = this.config.appid, appsecret: string = this.config.appsecret): Promise<any> {
116
- const res = await this.fetch(`${this.config.api}/user/data`, "POST", {
117
- appid,
118
- appsecret
119
- }, {
120
- Authorization: `Bearer ${access_token}`
121
- });
122
-
123
- return res;
124
- }
125
-
126
- /**
127
- * 刷新用户访问凭证
128
- * @param {string} refresh_token 用户刷新凭证
129
- * @param {string} appid 应用ID
130
- * @param {string} appsecret 应用密钥
131
- * @returns
132
- */
133
- async refreshUserToken(refresh_token: string, appid: string = this.config.appid, appsecret: string = this.config.appsecret): Promise<any> {
134
- const res = await this.fetch(`${this.config.api}/user/token/refresh`, "POST", {
135
- appid,
136
- appsecret
137
- }, {
138
- Authorization: `Bearer ${refresh_token}`
139
- });
140
-
141
- return res;
142
- }
143
-
144
- /**
145
- * 退出登录状态
146
- * @param {string} access_token 用户访问凭证
147
- */
148
- async logout(access_token: string, appid: string = this.config.appid, appsecret: string = this.config.appsecret): Promise<{
149
- code: number,
150
- msg: string,
151
- data: any
152
- }> {
153
- const res = await this.fetch(`${this.config.api}/user/logout`, "POST", {
154
- appid,
155
- appsecret
156
- }, {
157
- Authorization: `Bearer ${access_token}`
158
- });
159
- return res;
160
- }
161
- }
162
-
163
- export default YunTowerAccountSDK;
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ESNext",
4
- "module": "ESNext",
5
- "outDir": "dist",
6
- "rootDir": "src",
7
- "declaration": true,
8
- "emitDeclarationOnly": false,
9
- "esModuleInterop": true,
10
- "moduleResolution": "Bundler",
11
- "strict": true,
12
- "skipLibCheck": true,
13
- "types": []
14
- },
15
- "include": ["src"],
16
- "exclude": ["node_modules", "dist"]
17
- }