@nocobase/sdk 0.9.1-alpha.2 → 0.9.2-alpha.2
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/es/APIClient.d.ts +3 -0
- package/es/APIClient.js +34 -76
- package/lib/APIClient.d.ts +3 -0
- package/lib/APIClient.js +34 -87
- package/lib/index.js +0 -2
- package/package.json +2 -2
- package/src/APIClient.ts +22 -4
package/es/APIClient.d.ts
CHANGED
|
@@ -19,12 +19,15 @@ export interface IResource {
|
|
|
19
19
|
}
|
|
20
20
|
export declare class Auth {
|
|
21
21
|
protected api: APIClient;
|
|
22
|
+
protected NOCOBASE_LOCALE_KEY: string;
|
|
23
|
+
protected NOCOBASE_ROLE_KEY: string;
|
|
22
24
|
protected options: {
|
|
23
25
|
token: any;
|
|
24
26
|
locale: any;
|
|
25
27
|
role: any;
|
|
26
28
|
};
|
|
27
29
|
constructor(api: APIClient);
|
|
30
|
+
initKeys(): void;
|
|
28
31
|
get locale(): string;
|
|
29
32
|
get role(): string;
|
|
30
33
|
get token(): string;
|
package/es/APIClient.js
CHANGED
|
@@ -1,114 +1,105 @@
|
|
|
1
1
|
const _excluded = ["authClass", "storageClass"],
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
_excluded2 = ["values", "filter"];
|
|
4
3
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
5
|
-
|
|
6
4
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
7
|
-
|
|
8
|
-
function
|
|
9
|
-
|
|
5
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
7
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
10
8
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
11
|
-
|
|
12
9
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
13
|
-
|
|
14
10
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
15
|
-
|
|
16
11
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
17
|
-
|
|
18
12
|
import axios from 'axios';
|
|
19
13
|
import qs from 'qs';
|
|
20
14
|
export class Auth {
|
|
21
15
|
constructor(api) {
|
|
22
16
|
this.api = void 0;
|
|
17
|
+
this.NOCOBASE_LOCALE_KEY = 'NOCOBASE_LOCALE';
|
|
18
|
+
this.NOCOBASE_ROLE_KEY = 'NOCOBASE_ROLE';
|
|
23
19
|
this.options = {
|
|
24
20
|
token: null,
|
|
25
21
|
locale: null,
|
|
26
22
|
role: null
|
|
27
23
|
};
|
|
28
24
|
this.api = api;
|
|
25
|
+
this.initKeys();
|
|
29
26
|
this.locale = this.getLocale();
|
|
30
27
|
this.role = this.getRole();
|
|
31
28
|
this.token = this.getToken();
|
|
32
29
|
this.api.axios.interceptors.request.use(this.middleware.bind(this));
|
|
33
30
|
}
|
|
34
|
-
|
|
31
|
+
initKeys() {
|
|
32
|
+
if (!window) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const match = window.location.pathname.match(/^\/apps\/([^/]*)\//);
|
|
36
|
+
if (!match) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const appName = match[1];
|
|
40
|
+
this.NOCOBASE_LOCALE_KEY = `${appName.toUpperCase()}_NOCOBASE_LOCALE`;
|
|
41
|
+
this.NOCOBASE_ROLE_KEY = `${appName.toUpperCase()}_NOCOBASE_ROLE`;
|
|
42
|
+
}
|
|
35
43
|
get locale() {
|
|
36
44
|
return this.getLocale();
|
|
37
45
|
}
|
|
38
|
-
|
|
39
46
|
get role() {
|
|
40
47
|
return this.getRole();
|
|
41
48
|
}
|
|
42
|
-
|
|
43
49
|
get token() {
|
|
44
50
|
return this.getToken();
|
|
45
51
|
}
|
|
46
|
-
|
|
47
52
|
set locale(value) {
|
|
48
53
|
this.setLocale(value);
|
|
49
54
|
}
|
|
50
|
-
|
|
51
55
|
set role(value) {
|
|
52
56
|
this.setRole(value);
|
|
53
57
|
}
|
|
54
|
-
|
|
55
58
|
set token(value) {
|
|
56
59
|
this.setToken(value);
|
|
57
60
|
}
|
|
58
|
-
|
|
59
61
|
middleware(config) {
|
|
60
62
|
if (this.locale) {
|
|
61
63
|
config.headers['X-Locale'] = this.locale;
|
|
62
64
|
}
|
|
63
|
-
|
|
64
65
|
if (this.role) {
|
|
65
66
|
config.headers['X-Role'] = this.role;
|
|
66
67
|
}
|
|
67
|
-
|
|
68
68
|
if (this.token) {
|
|
69
69
|
config.headers['Authorization'] = `Bearer ${this.token}`;
|
|
70
70
|
}
|
|
71
|
-
|
|
72
71
|
return config;
|
|
73
72
|
}
|
|
74
|
-
|
|
75
73
|
getLocale() {
|
|
76
|
-
return this.api.storage.getItem(
|
|
74
|
+
return this.api.storage.getItem(this.NOCOBASE_LOCALE_KEY);
|
|
77
75
|
}
|
|
78
|
-
|
|
79
76
|
setLocale(locale) {
|
|
80
77
|
this.options.locale = locale;
|
|
81
|
-
this.api.storage.setItem(
|
|
78
|
+
this.api.storage.setItem(this.NOCOBASE_LOCALE_KEY, locale || '');
|
|
82
79
|
}
|
|
83
|
-
|
|
84
80
|
getToken() {
|
|
85
81
|
return this.api.storage.getItem('NOCOBASE_TOKEN');
|
|
86
82
|
}
|
|
87
|
-
|
|
88
83
|
setToken(token) {
|
|
89
84
|
this.options.token = token;
|
|
90
85
|
this.api.storage.setItem('NOCOBASE_TOKEN', token || '');
|
|
91
|
-
|
|
92
86
|
if (!token) {
|
|
93
|
-
this.setRole(null);
|
|
87
|
+
this.setRole(null);
|
|
88
|
+
// this.setLocale(null);
|
|
94
89
|
}
|
|
95
90
|
}
|
|
96
91
|
|
|
97
92
|
getRole() {
|
|
98
|
-
return this.api.storage.getItem(
|
|
93
|
+
return this.api.storage.getItem(this.NOCOBASE_ROLE_KEY);
|
|
99
94
|
}
|
|
100
|
-
|
|
101
95
|
setRole(role) {
|
|
102
96
|
this.options.role = role;
|
|
103
|
-
this.api.storage.setItem(
|
|
97
|
+
this.api.storage.setItem(this.NOCOBASE_ROLE_KEY, role || '');
|
|
104
98
|
}
|
|
105
|
-
|
|
106
99
|
signIn(values, authenticator = 'password') {
|
|
107
100
|
var _this = this;
|
|
108
|
-
|
|
109
101
|
return _asyncToGenerator(function* () {
|
|
110
102
|
var _response$data;
|
|
111
|
-
|
|
112
103
|
const response = yield _this.api.request({
|
|
113
104
|
method: 'post',
|
|
114
105
|
url: 'users:signin',
|
|
@@ -118,26 +109,20 @@ export class Auth {
|
|
|
118
109
|
}
|
|
119
110
|
});
|
|
120
111
|
const data = response === null || response === void 0 ? void 0 : (_response$data = response.data) === null || _response$data === void 0 ? void 0 : _response$data.data;
|
|
121
|
-
|
|
122
112
|
_this.setToken(data === null || data === void 0 ? void 0 : data.token);
|
|
123
|
-
|
|
124
113
|
return response;
|
|
125
114
|
})();
|
|
126
115
|
}
|
|
127
|
-
|
|
128
116
|
signOut() {
|
|
129
117
|
var _this2 = this;
|
|
130
|
-
|
|
131
118
|
return _asyncToGenerator(function* () {
|
|
132
119
|
yield _this2.api.request({
|
|
133
120
|
method: 'post',
|
|
134
121
|
url: 'users:signout'
|
|
135
122
|
});
|
|
136
|
-
|
|
137
123
|
_this2.setToken(null);
|
|
138
124
|
})();
|
|
139
125
|
}
|
|
140
|
-
|
|
141
126
|
}
|
|
142
127
|
export class Storage {}
|
|
143
128
|
export class MemoryStorage extends Storage {
|
|
@@ -145,57 +130,45 @@ export class MemoryStorage extends Storage {
|
|
|
145
130
|
super(...args);
|
|
146
131
|
this.items = new Map();
|
|
147
132
|
}
|
|
148
|
-
|
|
149
133
|
clear() {
|
|
150
134
|
this.items.clear();
|
|
151
135
|
}
|
|
152
|
-
|
|
153
136
|
getItem(key) {
|
|
154
137
|
return this.items.get(key);
|
|
155
138
|
}
|
|
156
|
-
|
|
157
139
|
setItem(key, value) {
|
|
158
140
|
return this.items.set(key, value);
|
|
159
141
|
}
|
|
160
|
-
|
|
161
142
|
removeItem(key) {
|
|
162
143
|
return this.items.delete(key);
|
|
163
144
|
}
|
|
164
|
-
|
|
165
145
|
}
|
|
166
146
|
export class APIClient {
|
|
167
147
|
constructor(instance) {
|
|
168
148
|
this.axios = void 0;
|
|
169
149
|
this.auth = void 0;
|
|
170
150
|
this.storage = void 0;
|
|
171
|
-
|
|
172
151
|
if (typeof instance === 'function') {
|
|
173
152
|
this.axios = instance;
|
|
174
153
|
} else {
|
|
175
154
|
const _ref = instance || {},
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
155
|
+
authClass = _ref.authClass,
|
|
156
|
+
storageClass = _ref.storageClass,
|
|
157
|
+
others = _objectWithoutProperties(_ref, _excluded);
|
|
180
158
|
this.axios = axios.create(others);
|
|
181
159
|
this.initStorage(storageClass);
|
|
182
|
-
|
|
183
160
|
if (authClass) {
|
|
184
161
|
this.auth = new authClass(this);
|
|
185
162
|
}
|
|
186
163
|
}
|
|
187
|
-
|
|
188
164
|
if (!this.storage) {
|
|
189
165
|
this.initStorage();
|
|
190
166
|
}
|
|
191
|
-
|
|
192
167
|
if (!this.auth) {
|
|
193
168
|
this.auth = new Auth(this);
|
|
194
169
|
}
|
|
195
|
-
|
|
196
170
|
this.interceptors();
|
|
197
171
|
}
|
|
198
|
-
|
|
199
172
|
initStorage(storage) {
|
|
200
173
|
if (storage) {
|
|
201
174
|
this.storage = new storage(this);
|
|
@@ -205,7 +178,6 @@ export class APIClient {
|
|
|
205
178
|
this.storage = new MemoryStorage();
|
|
206
179
|
}
|
|
207
180
|
}
|
|
208
|
-
|
|
209
181
|
interceptors() {
|
|
210
182
|
this.axios.interceptors.request.use(config => {
|
|
211
183
|
config.paramsSerializer = params => {
|
|
@@ -214,27 +186,21 @@ export class APIClient {
|
|
|
214
186
|
arrayFormat: 'brackets'
|
|
215
187
|
});
|
|
216
188
|
};
|
|
217
|
-
|
|
218
189
|
return config;
|
|
219
190
|
});
|
|
220
191
|
}
|
|
221
|
-
|
|
222
192
|
request(config) {
|
|
223
193
|
const resource = config.resource,
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
194
|
+
resourceOf = config.resourceOf,
|
|
195
|
+
action = config.action,
|
|
196
|
+
params = config.params;
|
|
228
197
|
if (resource) {
|
|
229
198
|
return this.resource(resource, resourceOf)[action](params);
|
|
230
199
|
}
|
|
231
|
-
|
|
232
200
|
return this.axios.request(config);
|
|
233
201
|
}
|
|
234
|
-
|
|
235
202
|
resource(name, of) {
|
|
236
203
|
var _this3 = this;
|
|
237
|
-
|
|
238
204
|
const target = {};
|
|
239
205
|
const handler = {
|
|
240
206
|
get: (_, actionName) => {
|
|
@@ -243,22 +209,18 @@ export class APIClient {
|
|
|
243
209
|
const config = {
|
|
244
210
|
url
|
|
245
211
|
};
|
|
246
|
-
|
|
247
212
|
if (['get', 'list'].includes(actionName)) {
|
|
248
213
|
config['method'] = 'get';
|
|
249
214
|
} else {
|
|
250
215
|
config['method'] = 'post';
|
|
251
216
|
}
|
|
252
|
-
|
|
253
217
|
return /*#__PURE__*/function () {
|
|
254
218
|
var _ref2 = _asyncToGenerator(function* (params, opts) {
|
|
255
219
|
const _ref3 = params || {},
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
220
|
+
values = _ref3.values,
|
|
221
|
+
filter = _ref3.filter,
|
|
222
|
+
others = _objectWithoutProperties(_ref3, _excluded2);
|
|
260
223
|
config['params'] = others;
|
|
261
|
-
|
|
262
224
|
if (filter) {
|
|
263
225
|
if (typeof filter === 'string') {
|
|
264
226
|
config['params']['filter'] = filter;
|
|
@@ -266,14 +228,11 @@ export class APIClient {
|
|
|
266
228
|
config['params']['filter'] = JSON.stringify(filter);
|
|
267
229
|
}
|
|
268
230
|
}
|
|
269
|
-
|
|
270
231
|
if (config.method !== 'get') {
|
|
271
232
|
config['data'] = values || {};
|
|
272
233
|
}
|
|
273
|
-
|
|
274
234
|
return yield _this3.request(_objectSpread(_objectSpread({}, config), opts));
|
|
275
235
|
});
|
|
276
|
-
|
|
277
236
|
return function (_x, _x2) {
|
|
278
237
|
return _ref2.apply(this, arguments);
|
|
279
238
|
};
|
|
@@ -282,5 +241,4 @@ export class APIClient {
|
|
|
282
241
|
};
|
|
283
242
|
return new Proxy(target, handler);
|
|
284
243
|
}
|
|
285
|
-
|
|
286
244
|
}
|
package/lib/APIClient.d.ts
CHANGED
|
@@ -19,12 +19,15 @@ export interface IResource {
|
|
|
19
19
|
}
|
|
20
20
|
export declare class Auth {
|
|
21
21
|
protected api: APIClient;
|
|
22
|
+
protected NOCOBASE_LOCALE_KEY: string;
|
|
23
|
+
protected NOCOBASE_ROLE_KEY: string;
|
|
22
24
|
protected options: {
|
|
23
25
|
token: any;
|
|
24
26
|
locale: any;
|
|
25
27
|
role: any;
|
|
26
28
|
};
|
|
27
29
|
constructor(api: APIClient);
|
|
30
|
+
initKeys(): void;
|
|
28
31
|
get locale(): string;
|
|
29
32
|
get role(): string;
|
|
30
33
|
get token(): string;
|
package/lib/APIClient.js
CHANGED
|
@@ -4,122 +4,109 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.Storage = exports.MemoryStorage = exports.Auth = exports.APIClient = void 0;
|
|
7
|
-
|
|
8
7
|
var _axios = _interopRequireDefault(require("axios"));
|
|
9
|
-
|
|
10
8
|
var _qs = _interopRequireDefault(require("qs"));
|
|
11
|
-
|
|
12
9
|
const _excluded = ["authClass", "storageClass"],
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
_excluded2 = ["values", "filter"];
|
|
15
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
|
-
|
|
17
12
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
18
|
-
|
|
19
13
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
20
|
-
|
|
21
|
-
function
|
|
22
|
-
|
|
14
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
16
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
23
17
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
24
|
-
|
|
25
18
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
26
|
-
|
|
27
19
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
28
|
-
|
|
29
20
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
30
|
-
|
|
31
21
|
class Auth {
|
|
32
22
|
constructor(api) {
|
|
33
23
|
this.api = void 0;
|
|
24
|
+
this.NOCOBASE_LOCALE_KEY = 'NOCOBASE_LOCALE';
|
|
25
|
+
this.NOCOBASE_ROLE_KEY = 'NOCOBASE_ROLE';
|
|
34
26
|
this.options = {
|
|
35
27
|
token: null,
|
|
36
28
|
locale: null,
|
|
37
29
|
role: null
|
|
38
30
|
};
|
|
39
31
|
this.api = api;
|
|
32
|
+
this.initKeys();
|
|
40
33
|
this.locale = this.getLocale();
|
|
41
34
|
this.role = this.getRole();
|
|
42
35
|
this.token = this.getToken();
|
|
43
36
|
this.api.axios.interceptors.request.use(this.middleware.bind(this));
|
|
44
37
|
}
|
|
45
|
-
|
|
38
|
+
initKeys() {
|
|
39
|
+
if (!window) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const match = window.location.pathname.match(/^\/apps\/([^/]*)\//);
|
|
43
|
+
if (!match) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const appName = match[1];
|
|
47
|
+
this.NOCOBASE_LOCALE_KEY = `${appName.toUpperCase()}_NOCOBASE_LOCALE`;
|
|
48
|
+
this.NOCOBASE_ROLE_KEY = `${appName.toUpperCase()}_NOCOBASE_ROLE`;
|
|
49
|
+
}
|
|
46
50
|
get locale() {
|
|
47
51
|
return this.getLocale();
|
|
48
52
|
}
|
|
49
|
-
|
|
50
53
|
get role() {
|
|
51
54
|
return this.getRole();
|
|
52
55
|
}
|
|
53
|
-
|
|
54
56
|
get token() {
|
|
55
57
|
return this.getToken();
|
|
56
58
|
}
|
|
57
|
-
|
|
58
59
|
set locale(value) {
|
|
59
60
|
this.setLocale(value);
|
|
60
61
|
}
|
|
61
|
-
|
|
62
62
|
set role(value) {
|
|
63
63
|
this.setRole(value);
|
|
64
64
|
}
|
|
65
|
-
|
|
66
65
|
set token(value) {
|
|
67
66
|
this.setToken(value);
|
|
68
67
|
}
|
|
69
|
-
|
|
70
68
|
middleware(config) {
|
|
71
69
|
if (this.locale) {
|
|
72
70
|
config.headers['X-Locale'] = this.locale;
|
|
73
71
|
}
|
|
74
|
-
|
|
75
72
|
if (this.role) {
|
|
76
73
|
config.headers['X-Role'] = this.role;
|
|
77
74
|
}
|
|
78
|
-
|
|
79
75
|
if (this.token) {
|
|
80
76
|
config.headers['Authorization'] = `Bearer ${this.token}`;
|
|
81
77
|
}
|
|
82
|
-
|
|
83
78
|
return config;
|
|
84
79
|
}
|
|
85
|
-
|
|
86
80
|
getLocale() {
|
|
87
|
-
return this.api.storage.getItem(
|
|
81
|
+
return this.api.storage.getItem(this.NOCOBASE_LOCALE_KEY);
|
|
88
82
|
}
|
|
89
|
-
|
|
90
83
|
setLocale(locale) {
|
|
91
84
|
this.options.locale = locale;
|
|
92
|
-
this.api.storage.setItem(
|
|
85
|
+
this.api.storage.setItem(this.NOCOBASE_LOCALE_KEY, locale || '');
|
|
93
86
|
}
|
|
94
|
-
|
|
95
87
|
getToken() {
|
|
96
88
|
return this.api.storage.getItem('NOCOBASE_TOKEN');
|
|
97
89
|
}
|
|
98
|
-
|
|
99
90
|
setToken(token) {
|
|
100
91
|
this.options.token = token;
|
|
101
92
|
this.api.storage.setItem('NOCOBASE_TOKEN', token || '');
|
|
102
|
-
|
|
103
93
|
if (!token) {
|
|
104
|
-
this.setRole(null);
|
|
94
|
+
this.setRole(null);
|
|
95
|
+
// this.setLocale(null);
|
|
105
96
|
}
|
|
106
97
|
}
|
|
107
98
|
|
|
108
99
|
getRole() {
|
|
109
|
-
return this.api.storage.getItem(
|
|
100
|
+
return this.api.storage.getItem(this.NOCOBASE_ROLE_KEY);
|
|
110
101
|
}
|
|
111
|
-
|
|
112
102
|
setRole(role) {
|
|
113
103
|
this.options.role = role;
|
|
114
|
-
this.api.storage.setItem(
|
|
104
|
+
this.api.storage.setItem(this.NOCOBASE_ROLE_KEY, role || '');
|
|
115
105
|
}
|
|
116
|
-
|
|
117
106
|
signIn(values, authenticator = 'password') {
|
|
118
107
|
var _this = this;
|
|
119
|
-
|
|
120
108
|
return _asyncToGenerator(function* () {
|
|
121
109
|
var _response$data;
|
|
122
|
-
|
|
123
110
|
const response = yield _this.api.request({
|
|
124
111
|
method: 'post',
|
|
125
112
|
url: 'users:signin',
|
|
@@ -129,93 +116,69 @@ class Auth {
|
|
|
129
116
|
}
|
|
130
117
|
});
|
|
131
118
|
const data = response === null || response === void 0 ? void 0 : (_response$data = response.data) === null || _response$data === void 0 ? void 0 : _response$data.data;
|
|
132
|
-
|
|
133
119
|
_this.setToken(data === null || data === void 0 ? void 0 : data.token);
|
|
134
|
-
|
|
135
120
|
return response;
|
|
136
121
|
})();
|
|
137
122
|
}
|
|
138
|
-
|
|
139
123
|
signOut() {
|
|
140
124
|
var _this2 = this;
|
|
141
|
-
|
|
142
125
|
return _asyncToGenerator(function* () {
|
|
143
126
|
yield _this2.api.request({
|
|
144
127
|
method: 'post',
|
|
145
128
|
url: 'users:signout'
|
|
146
129
|
});
|
|
147
|
-
|
|
148
130
|
_this2.setToken(null);
|
|
149
131
|
})();
|
|
150
132
|
}
|
|
151
|
-
|
|
152
133
|
}
|
|
153
|
-
|
|
154
134
|
exports.Auth = Auth;
|
|
155
|
-
|
|
156
135
|
class Storage {}
|
|
157
|
-
|
|
158
136
|
exports.Storage = Storage;
|
|
159
|
-
|
|
160
137
|
class MemoryStorage extends Storage {
|
|
161
138
|
constructor(...args) {
|
|
162
139
|
super(...args);
|
|
163
140
|
this.items = new Map();
|
|
164
141
|
}
|
|
165
|
-
|
|
166
142
|
clear() {
|
|
167
143
|
this.items.clear();
|
|
168
144
|
}
|
|
169
|
-
|
|
170
145
|
getItem(key) {
|
|
171
146
|
return this.items.get(key);
|
|
172
147
|
}
|
|
173
|
-
|
|
174
148
|
setItem(key, value) {
|
|
175
149
|
return this.items.set(key, value);
|
|
176
150
|
}
|
|
177
|
-
|
|
178
151
|
removeItem(key) {
|
|
179
152
|
return this.items.delete(key);
|
|
180
153
|
}
|
|
181
|
-
|
|
182
154
|
}
|
|
183
|
-
|
|
184
155
|
exports.MemoryStorage = MemoryStorage;
|
|
185
|
-
|
|
186
156
|
class APIClient {
|
|
187
157
|
constructor(instance) {
|
|
188
158
|
this.axios = void 0;
|
|
189
159
|
this.auth = void 0;
|
|
190
160
|
this.storage = void 0;
|
|
191
|
-
|
|
192
161
|
if (typeof instance === 'function') {
|
|
193
162
|
this.axios = instance;
|
|
194
163
|
} else {
|
|
195
164
|
const _ref = instance || {},
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
165
|
+
authClass = _ref.authClass,
|
|
166
|
+
storageClass = _ref.storageClass,
|
|
167
|
+
others = _objectWithoutProperties(_ref, _excluded);
|
|
200
168
|
this.axios = _axios.default.create(others);
|
|
201
169
|
this.initStorage(storageClass);
|
|
202
|
-
|
|
203
170
|
if (authClass) {
|
|
204
171
|
this.auth = new authClass(this);
|
|
205
172
|
}
|
|
206
173
|
}
|
|
207
|
-
|
|
208
174
|
if (!this.storage) {
|
|
209
175
|
this.initStorage();
|
|
210
176
|
}
|
|
211
|
-
|
|
212
177
|
if (!this.auth) {
|
|
213
178
|
this.auth = new Auth(this);
|
|
214
179
|
}
|
|
215
|
-
|
|
216
180
|
this.interceptors();
|
|
217
181
|
}
|
|
218
|
-
|
|
219
182
|
initStorage(storage) {
|
|
220
183
|
if (storage) {
|
|
221
184
|
this.storage = new storage(this);
|
|
@@ -225,7 +188,6 @@ class APIClient {
|
|
|
225
188
|
this.storage = new MemoryStorage();
|
|
226
189
|
}
|
|
227
190
|
}
|
|
228
|
-
|
|
229
191
|
interceptors() {
|
|
230
192
|
this.axios.interceptors.request.use(config => {
|
|
231
193
|
config.paramsSerializer = params => {
|
|
@@ -234,27 +196,21 @@ class APIClient {
|
|
|
234
196
|
arrayFormat: 'brackets'
|
|
235
197
|
});
|
|
236
198
|
};
|
|
237
|
-
|
|
238
199
|
return config;
|
|
239
200
|
});
|
|
240
201
|
}
|
|
241
|
-
|
|
242
202
|
request(config) {
|
|
243
203
|
const resource = config.resource,
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
204
|
+
resourceOf = config.resourceOf,
|
|
205
|
+
action = config.action,
|
|
206
|
+
params = config.params;
|
|
248
207
|
if (resource) {
|
|
249
208
|
return this.resource(resource, resourceOf)[action](params);
|
|
250
209
|
}
|
|
251
|
-
|
|
252
210
|
return this.axios.request(config);
|
|
253
211
|
}
|
|
254
|
-
|
|
255
212
|
resource(name, of) {
|
|
256
213
|
var _this3 = this;
|
|
257
|
-
|
|
258
214
|
const target = {};
|
|
259
215
|
const handler = {
|
|
260
216
|
get: (_, actionName) => {
|
|
@@ -263,22 +219,18 @@ class APIClient {
|
|
|
263
219
|
const config = {
|
|
264
220
|
url
|
|
265
221
|
};
|
|
266
|
-
|
|
267
222
|
if (['get', 'list'].includes(actionName)) {
|
|
268
223
|
config['method'] = 'get';
|
|
269
224
|
} else {
|
|
270
225
|
config['method'] = 'post';
|
|
271
226
|
}
|
|
272
|
-
|
|
273
227
|
return /*#__PURE__*/function () {
|
|
274
228
|
var _ref2 = _asyncToGenerator(function* (params, opts) {
|
|
275
229
|
const _ref3 = params || {},
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
230
|
+
values = _ref3.values,
|
|
231
|
+
filter = _ref3.filter,
|
|
232
|
+
others = _objectWithoutProperties(_ref3, _excluded2);
|
|
280
233
|
config['params'] = others;
|
|
281
|
-
|
|
282
234
|
if (filter) {
|
|
283
235
|
if (typeof filter === 'string') {
|
|
284
236
|
config['params']['filter'] = filter;
|
|
@@ -286,14 +238,11 @@ class APIClient {
|
|
|
286
238
|
config['params']['filter'] = JSON.stringify(filter);
|
|
287
239
|
}
|
|
288
240
|
}
|
|
289
|
-
|
|
290
241
|
if (config.method !== 'get') {
|
|
291
242
|
config['data'] = values || {};
|
|
292
243
|
}
|
|
293
|
-
|
|
294
244
|
return yield _this3.request(_objectSpread(_objectSpread({}, config), opts));
|
|
295
245
|
});
|
|
296
|
-
|
|
297
246
|
return function (_x, _x2) {
|
|
298
247
|
return _ref2.apply(this, arguments);
|
|
299
248
|
};
|
|
@@ -302,7 +251,5 @@ class APIClient {
|
|
|
302
251
|
};
|
|
303
252
|
return new Proxy(target, handler);
|
|
304
253
|
}
|
|
305
|
-
|
|
306
254
|
}
|
|
307
|
-
|
|
308
255
|
exports.APIClient = APIClient;
|
package/lib/index.js
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
7
6
|
var _APIClient = require("./APIClient");
|
|
8
|
-
|
|
9
7
|
Object.keys(_APIClient).forEach(function (key) {
|
|
10
8
|
if (key === "default" || key === "__esModule") return;
|
|
11
9
|
if (key in exports && exports[key] === _APIClient[key]) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/sdk",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.2-alpha.2",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "lib",
|
|
6
6
|
"module": "es/index.js",
|
|
@@ -12,5 +12,5 @@
|
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"axios-mock-adapter": "^1.20.0"
|
|
14
14
|
},
|
|
15
|
-
"gitHead": "
|
|
15
|
+
"gitHead": "1a0de6908b2b1854bdf434fcafa8909cb65bb8a3"
|
|
16
16
|
}
|
package/src/APIClient.ts
CHANGED
|
@@ -25,6 +25,10 @@ export interface IResource {
|
|
|
25
25
|
export class Auth {
|
|
26
26
|
protected api: APIClient;
|
|
27
27
|
|
|
28
|
+
protected NOCOBASE_LOCALE_KEY = 'NOCOBASE_LOCALE';
|
|
29
|
+
|
|
30
|
+
protected NOCOBASE_ROLE_KEY = 'NOCOBASE_ROLE';
|
|
31
|
+
|
|
28
32
|
protected options = {
|
|
29
33
|
token: null,
|
|
30
34
|
locale: null,
|
|
@@ -33,12 +37,26 @@ export class Auth {
|
|
|
33
37
|
|
|
34
38
|
constructor(api: APIClient) {
|
|
35
39
|
this.api = api;
|
|
40
|
+
this.initKeys();
|
|
36
41
|
this.locale = this.getLocale();
|
|
37
42
|
this.role = this.getRole();
|
|
38
43
|
this.token = this.getToken();
|
|
39
44
|
this.api.axios.interceptors.request.use(this.middleware.bind(this));
|
|
40
45
|
}
|
|
41
46
|
|
|
47
|
+
initKeys() {
|
|
48
|
+
if (!window) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const match = window.location.pathname.match(/^\/apps\/([^/]*)\//);
|
|
52
|
+
if (!match) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const appName = match[1];
|
|
56
|
+
this.NOCOBASE_LOCALE_KEY = `${appName.toUpperCase()}_NOCOBASE_LOCALE`;
|
|
57
|
+
this.NOCOBASE_ROLE_KEY = `${appName.toUpperCase()}_NOCOBASE_ROLE`;
|
|
58
|
+
}
|
|
59
|
+
|
|
42
60
|
get locale() {
|
|
43
61
|
return this.getLocale();
|
|
44
62
|
}
|
|
@@ -77,12 +95,12 @@ export class Auth {
|
|
|
77
95
|
}
|
|
78
96
|
|
|
79
97
|
getLocale() {
|
|
80
|
-
return this.api.storage.getItem(
|
|
98
|
+
return this.api.storage.getItem(this.NOCOBASE_LOCALE_KEY);
|
|
81
99
|
}
|
|
82
100
|
|
|
83
101
|
setLocale(locale: string) {
|
|
84
102
|
this.options.locale = locale;
|
|
85
|
-
this.api.storage.setItem(
|
|
103
|
+
this.api.storage.setItem(this.NOCOBASE_LOCALE_KEY, locale || '');
|
|
86
104
|
}
|
|
87
105
|
|
|
88
106
|
getToken() {
|
|
@@ -99,12 +117,12 @@ export class Auth {
|
|
|
99
117
|
}
|
|
100
118
|
|
|
101
119
|
getRole() {
|
|
102
|
-
return this.api.storage.getItem(
|
|
120
|
+
return this.api.storage.getItem(this.NOCOBASE_ROLE_KEY);
|
|
103
121
|
}
|
|
104
122
|
|
|
105
123
|
setRole(role: string) {
|
|
106
124
|
this.options.role = role;
|
|
107
|
-
this.api.storage.setItem(
|
|
125
|
+
this.api.storage.setItem(this.NOCOBASE_ROLE_KEY, role || '');
|
|
108
126
|
}
|
|
109
127
|
|
|
110
128
|
async signIn(values, authenticator: string = 'password'): Promise<AxiosResponse<any>> {
|