@nocobase/sdk 0.9.4-alpha.2 → 0.10.0-alpha.3
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 +21 -9
- package/es/APIClient.js +92 -50
- package/lib/APIClient.d.ts +21 -9
- package/lib/APIClient.js +92 -50
- package/package.json +2 -2
- package/src/APIClient.ts +91 -45
- package/src/__tests__/api-client.test.ts +19 -11
package/es/APIClient.d.ts
CHANGED
|
@@ -19,30 +19,42 @@ export interface IResource {
|
|
|
19
19
|
}
|
|
20
20
|
export declare class Auth {
|
|
21
21
|
protected api: APIClient;
|
|
22
|
-
protected
|
|
23
|
-
|
|
22
|
+
protected KEYS: {
|
|
23
|
+
locale: string;
|
|
24
|
+
role: string;
|
|
25
|
+
token: string;
|
|
26
|
+
authenticator: string;
|
|
27
|
+
};
|
|
24
28
|
protected options: {
|
|
25
|
-
token: any;
|
|
26
29
|
locale: any;
|
|
27
30
|
role: any;
|
|
31
|
+
authenticator: any;
|
|
32
|
+
token: any;
|
|
28
33
|
};
|
|
29
34
|
constructor(api: APIClient);
|
|
30
35
|
initKeys(): void;
|
|
31
36
|
get locale(): string;
|
|
32
|
-
get role(): string;
|
|
33
|
-
get token(): string;
|
|
34
37
|
set locale(value: string);
|
|
38
|
+
get role(): string;
|
|
35
39
|
set role(value: string);
|
|
40
|
+
get token(): string;
|
|
36
41
|
set token(value: string);
|
|
37
|
-
|
|
42
|
+
get authenticator(): string;
|
|
43
|
+
set authenticator(value: string);
|
|
44
|
+
getOption(key: string): string;
|
|
45
|
+
setOption(key: string, value?: string): void;
|
|
38
46
|
getLocale(): string;
|
|
39
47
|
setLocale(locale: string): void;
|
|
40
|
-
getToken(): string;
|
|
41
|
-
setToken(token: string): void;
|
|
42
48
|
getRole(): string;
|
|
43
49
|
setRole(role: string): void;
|
|
50
|
+
getToken(): string;
|
|
51
|
+
setToken(token: string): void;
|
|
52
|
+
getAuthenticator(): string;
|
|
53
|
+
setAuthenticator(authenticator: string): void;
|
|
54
|
+
middleware(config: AxiosRequestConfig): AxiosRequestConfig<any>;
|
|
44
55
|
signIn(values: any, authenticator?: string): Promise<AxiosResponse<any>>;
|
|
45
|
-
|
|
56
|
+
signUp(values: any, authenticator?: string): Promise<AxiosResponse<any>>;
|
|
57
|
+
signOut(): Promise<AxiosResponse<any, any>>;
|
|
46
58
|
}
|
|
47
59
|
export declare abstract class Storage {
|
|
48
60
|
abstract clear(): void;
|
package/es/APIClient.js
CHANGED
|
@@ -14,18 +14,20 @@ import qs from 'qs';
|
|
|
14
14
|
export class Auth {
|
|
15
15
|
constructor(api) {
|
|
16
16
|
this.api = void 0;
|
|
17
|
-
this.
|
|
18
|
-
|
|
17
|
+
this.KEYS = {
|
|
18
|
+
locale: 'NOCOBASE_LOCALE',
|
|
19
|
+
role: 'NOCOBASE_ROLE',
|
|
20
|
+
token: 'NOCOBASE_TOKEN',
|
|
21
|
+
authenticator: 'NOCOBASE_AUTH'
|
|
22
|
+
};
|
|
19
23
|
this.options = {
|
|
20
|
-
token: null,
|
|
21
24
|
locale: null,
|
|
22
|
-
role: null
|
|
25
|
+
role: null,
|
|
26
|
+
authenticator: null,
|
|
27
|
+
token: null
|
|
23
28
|
};
|
|
24
29
|
this.api = api;
|
|
25
30
|
this.initKeys();
|
|
26
|
-
this.locale = this.getLocale();
|
|
27
|
-
this.role = this.getRole();
|
|
28
|
-
this.token = this.getToken();
|
|
29
31
|
this.api.axios.interceptors.request.use(this.middleware.bind(this));
|
|
30
32
|
}
|
|
31
33
|
initKeys() {
|
|
@@ -37,90 +39,127 @@ export class Auth {
|
|
|
37
39
|
return;
|
|
38
40
|
}
|
|
39
41
|
const appName = match[1];
|
|
40
|
-
this.
|
|
41
|
-
this.
|
|
42
|
+
this.KEYS['role'] = `${appName.toUpperCase()}_` + this.KEYS['role'];
|
|
43
|
+
this.KEYS['locale'] = `${appName.toUpperCase()}_` + this.KEYS['locale'];
|
|
42
44
|
}
|
|
43
45
|
get locale() {
|
|
44
46
|
return this.getLocale();
|
|
45
47
|
}
|
|
46
|
-
get role() {
|
|
47
|
-
return this.getRole();
|
|
48
|
-
}
|
|
49
|
-
get token() {
|
|
50
|
-
return this.getToken();
|
|
51
|
-
}
|
|
52
48
|
set locale(value) {
|
|
53
49
|
this.setLocale(value);
|
|
54
50
|
}
|
|
51
|
+
get role() {
|
|
52
|
+
return this.getRole();
|
|
53
|
+
}
|
|
55
54
|
set role(value) {
|
|
56
55
|
this.setRole(value);
|
|
57
56
|
}
|
|
57
|
+
get token() {
|
|
58
|
+
return this.getToken();
|
|
59
|
+
}
|
|
58
60
|
set token(value) {
|
|
59
61
|
this.setToken(value);
|
|
60
62
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
get authenticator() {
|
|
64
|
+
return this.getAuthenticator();
|
|
65
|
+
}
|
|
66
|
+
set authenticator(value) {
|
|
67
|
+
this.setAuthenticator(value);
|
|
68
|
+
}
|
|
69
|
+
getOption(key) {
|
|
70
|
+
if (!this.KEYS[key]) {
|
|
71
|
+
return;
|
|
67
72
|
}
|
|
68
|
-
|
|
69
|
-
|
|
73
|
+
return this.api.storage.getItem(this.KEYS[key]);
|
|
74
|
+
}
|
|
75
|
+
setOption(key, value) {
|
|
76
|
+
if (!this.KEYS[key]) {
|
|
77
|
+
return;
|
|
70
78
|
}
|
|
71
|
-
|
|
79
|
+
this.options[key] = value;
|
|
80
|
+
return this.api.storage.setItem(this.KEYS[key], value || '');
|
|
72
81
|
}
|
|
73
82
|
getLocale() {
|
|
74
|
-
return this.
|
|
83
|
+
return this.getOption('locale');
|
|
75
84
|
}
|
|
76
85
|
setLocale(locale) {
|
|
77
|
-
this.
|
|
78
|
-
|
|
86
|
+
this.setOption('locale', locale);
|
|
87
|
+
}
|
|
88
|
+
getRole() {
|
|
89
|
+
return this.getOption('role');
|
|
90
|
+
}
|
|
91
|
+
setRole(role) {
|
|
92
|
+
this.setOption('role', role);
|
|
79
93
|
}
|
|
80
94
|
getToken() {
|
|
81
|
-
return this.
|
|
95
|
+
return this.getOption('token');
|
|
82
96
|
}
|
|
83
97
|
setToken(token) {
|
|
84
|
-
this.
|
|
85
|
-
this.api.storage.setItem('NOCOBASE_TOKEN', token || '');
|
|
86
|
-
if (!token) {
|
|
87
|
-
this.setRole(null);
|
|
88
|
-
// this.setLocale(null);
|
|
89
|
-
}
|
|
98
|
+
this.setOption('token', token);
|
|
90
99
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return this.api.storage.getItem(this.NOCOBASE_ROLE_KEY);
|
|
100
|
+
getAuthenticator() {
|
|
101
|
+
return this.getOption('authenticator');
|
|
94
102
|
}
|
|
95
|
-
|
|
96
|
-
this.
|
|
97
|
-
this.api.storage.setItem(this.NOCOBASE_ROLE_KEY, role || '');
|
|
103
|
+
setAuthenticator(authenticator) {
|
|
104
|
+
this.setOption('authenticator', authenticator);
|
|
98
105
|
}
|
|
99
|
-
|
|
106
|
+
middleware(config) {
|
|
107
|
+
if (this.locale) {
|
|
108
|
+
config.headers['X-Locale'] = this.locale;
|
|
109
|
+
}
|
|
110
|
+
if (this.role) {
|
|
111
|
+
config.headers['X-Role'] = this.role;
|
|
112
|
+
}
|
|
113
|
+
if (this.authenticator && !config.headers['X-Authenticator']) {
|
|
114
|
+
config.headers['X-Authenticator'] = this.authenticator;
|
|
115
|
+
}
|
|
116
|
+
if (this.token) {
|
|
117
|
+
config.headers['Authorization'] = `Bearer ${this.token}`;
|
|
118
|
+
}
|
|
119
|
+
return config;
|
|
120
|
+
}
|
|
121
|
+
signIn(values, authenticator) {
|
|
100
122
|
var _this = this;
|
|
101
123
|
return _asyncToGenerator(function* () {
|
|
102
124
|
var _response$data;
|
|
103
125
|
const response = yield _this.api.request({
|
|
104
126
|
method: 'post',
|
|
105
|
-
url: '
|
|
127
|
+
url: 'auth:signIn',
|
|
106
128
|
data: values,
|
|
107
|
-
|
|
108
|
-
authenticator
|
|
129
|
+
headers: {
|
|
130
|
+
'X-Authenticator': authenticator
|
|
109
131
|
}
|
|
110
132
|
});
|
|
111
133
|
const data = response === null || response === void 0 ? void 0 : (_response$data = response.data) === null || _response$data === void 0 ? void 0 : _response$data.data;
|
|
112
134
|
_this.setToken(data === null || data === void 0 ? void 0 : data.token);
|
|
135
|
+
_this.setAuthenticator(authenticator);
|
|
113
136
|
return response;
|
|
114
137
|
})();
|
|
115
138
|
}
|
|
116
|
-
|
|
139
|
+
signUp(values, authenticator) {
|
|
117
140
|
var _this2 = this;
|
|
118
141
|
return _asyncToGenerator(function* () {
|
|
119
|
-
yield _this2.api.request({
|
|
142
|
+
return yield _this2.api.request({
|
|
143
|
+
method: 'post',
|
|
144
|
+
url: 'auth:signUp',
|
|
145
|
+
data: values,
|
|
146
|
+
headers: {
|
|
147
|
+
'X-Authenticator': authenticator
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
})();
|
|
151
|
+
}
|
|
152
|
+
signOut() {
|
|
153
|
+
var _this3 = this;
|
|
154
|
+
return _asyncToGenerator(function* () {
|
|
155
|
+
const response = yield _this3.api.request({
|
|
120
156
|
method: 'post',
|
|
121
|
-
url: '
|
|
157
|
+
url: 'auth:signOut'
|
|
122
158
|
});
|
|
123
|
-
|
|
159
|
+
_this3.setToken(null);
|
|
160
|
+
_this3.setRole(null);
|
|
161
|
+
_this3.setAuthenticator(null);
|
|
162
|
+
return response;
|
|
124
163
|
})();
|
|
125
164
|
}
|
|
126
165
|
}
|
|
@@ -200,7 +239,7 @@ export class APIClient {
|
|
|
200
239
|
return this.axios.request(config);
|
|
201
240
|
}
|
|
202
241
|
resource(name, of) {
|
|
203
|
-
var
|
|
242
|
+
var _this4 = this;
|
|
204
243
|
const target = {};
|
|
205
244
|
const handler = {
|
|
206
245
|
get: (_, actionName) => {
|
|
@@ -225,13 +264,16 @@ export class APIClient {
|
|
|
225
264
|
if (typeof filter === 'string') {
|
|
226
265
|
config['params']['filter'] = filter;
|
|
227
266
|
} else {
|
|
267
|
+
if (filter['*']) {
|
|
268
|
+
delete filter['*'];
|
|
269
|
+
}
|
|
228
270
|
config['params']['filter'] = JSON.stringify(filter);
|
|
229
271
|
}
|
|
230
272
|
}
|
|
231
273
|
if (config.method !== 'get') {
|
|
232
274
|
config['data'] = values || {};
|
|
233
275
|
}
|
|
234
|
-
return yield
|
|
276
|
+
return yield _this4.request(_objectSpread(_objectSpread({}, config), opts));
|
|
235
277
|
});
|
|
236
278
|
return function (_x, _x2) {
|
|
237
279
|
return _ref2.apply(this, arguments);
|
package/lib/APIClient.d.ts
CHANGED
|
@@ -19,30 +19,42 @@ export interface IResource {
|
|
|
19
19
|
}
|
|
20
20
|
export declare class Auth {
|
|
21
21
|
protected api: APIClient;
|
|
22
|
-
protected
|
|
23
|
-
|
|
22
|
+
protected KEYS: {
|
|
23
|
+
locale: string;
|
|
24
|
+
role: string;
|
|
25
|
+
token: string;
|
|
26
|
+
authenticator: string;
|
|
27
|
+
};
|
|
24
28
|
protected options: {
|
|
25
|
-
token: any;
|
|
26
29
|
locale: any;
|
|
27
30
|
role: any;
|
|
31
|
+
authenticator: any;
|
|
32
|
+
token: any;
|
|
28
33
|
};
|
|
29
34
|
constructor(api: APIClient);
|
|
30
35
|
initKeys(): void;
|
|
31
36
|
get locale(): string;
|
|
32
|
-
get role(): string;
|
|
33
|
-
get token(): string;
|
|
34
37
|
set locale(value: string);
|
|
38
|
+
get role(): string;
|
|
35
39
|
set role(value: string);
|
|
40
|
+
get token(): string;
|
|
36
41
|
set token(value: string);
|
|
37
|
-
|
|
42
|
+
get authenticator(): string;
|
|
43
|
+
set authenticator(value: string);
|
|
44
|
+
getOption(key: string): string;
|
|
45
|
+
setOption(key: string, value?: string): void;
|
|
38
46
|
getLocale(): string;
|
|
39
47
|
setLocale(locale: string): void;
|
|
40
|
-
getToken(): string;
|
|
41
|
-
setToken(token: string): void;
|
|
42
48
|
getRole(): string;
|
|
43
49
|
setRole(role: string): void;
|
|
50
|
+
getToken(): string;
|
|
51
|
+
setToken(token: string): void;
|
|
52
|
+
getAuthenticator(): string;
|
|
53
|
+
setAuthenticator(authenticator: string): void;
|
|
54
|
+
middleware(config: AxiosRequestConfig): AxiosRequestConfig<any>;
|
|
44
55
|
signIn(values: any, authenticator?: string): Promise<AxiosResponse<any>>;
|
|
45
|
-
|
|
56
|
+
signUp(values: any, authenticator?: string): Promise<AxiosResponse<any>>;
|
|
57
|
+
signOut(): Promise<AxiosResponse<any, any>>;
|
|
46
58
|
}
|
|
47
59
|
export declare abstract class Storage {
|
|
48
60
|
abstract clear(): void;
|
package/lib/APIClient.js
CHANGED
|
@@ -21,18 +21,20 @@ function _asyncToGenerator(fn) { return function () { var self = this, args = ar
|
|
|
21
21
|
class Auth {
|
|
22
22
|
constructor(api) {
|
|
23
23
|
this.api = void 0;
|
|
24
|
-
this.
|
|
25
|
-
|
|
24
|
+
this.KEYS = {
|
|
25
|
+
locale: 'NOCOBASE_LOCALE',
|
|
26
|
+
role: 'NOCOBASE_ROLE',
|
|
27
|
+
token: 'NOCOBASE_TOKEN',
|
|
28
|
+
authenticator: 'NOCOBASE_AUTH'
|
|
29
|
+
};
|
|
26
30
|
this.options = {
|
|
27
|
-
token: null,
|
|
28
31
|
locale: null,
|
|
29
|
-
role: null
|
|
32
|
+
role: null,
|
|
33
|
+
authenticator: null,
|
|
34
|
+
token: null
|
|
30
35
|
};
|
|
31
36
|
this.api = api;
|
|
32
37
|
this.initKeys();
|
|
33
|
-
this.locale = this.getLocale();
|
|
34
|
-
this.role = this.getRole();
|
|
35
|
-
this.token = this.getToken();
|
|
36
38
|
this.api.axios.interceptors.request.use(this.middleware.bind(this));
|
|
37
39
|
}
|
|
38
40
|
initKeys() {
|
|
@@ -44,90 +46,127 @@ class Auth {
|
|
|
44
46
|
return;
|
|
45
47
|
}
|
|
46
48
|
const appName = match[1];
|
|
47
|
-
this.
|
|
48
|
-
this.
|
|
49
|
+
this.KEYS['role'] = `${appName.toUpperCase()}_` + this.KEYS['role'];
|
|
50
|
+
this.KEYS['locale'] = `${appName.toUpperCase()}_` + this.KEYS['locale'];
|
|
49
51
|
}
|
|
50
52
|
get locale() {
|
|
51
53
|
return this.getLocale();
|
|
52
54
|
}
|
|
53
|
-
get role() {
|
|
54
|
-
return this.getRole();
|
|
55
|
-
}
|
|
56
|
-
get token() {
|
|
57
|
-
return this.getToken();
|
|
58
|
-
}
|
|
59
55
|
set locale(value) {
|
|
60
56
|
this.setLocale(value);
|
|
61
57
|
}
|
|
58
|
+
get role() {
|
|
59
|
+
return this.getRole();
|
|
60
|
+
}
|
|
62
61
|
set role(value) {
|
|
63
62
|
this.setRole(value);
|
|
64
63
|
}
|
|
64
|
+
get token() {
|
|
65
|
+
return this.getToken();
|
|
66
|
+
}
|
|
65
67
|
set token(value) {
|
|
66
68
|
this.setToken(value);
|
|
67
69
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
70
|
+
get authenticator() {
|
|
71
|
+
return this.getAuthenticator();
|
|
72
|
+
}
|
|
73
|
+
set authenticator(value) {
|
|
74
|
+
this.setAuthenticator(value);
|
|
75
|
+
}
|
|
76
|
+
getOption(key) {
|
|
77
|
+
if (!this.KEYS[key]) {
|
|
78
|
+
return;
|
|
74
79
|
}
|
|
75
|
-
|
|
76
|
-
|
|
80
|
+
return this.api.storage.getItem(this.KEYS[key]);
|
|
81
|
+
}
|
|
82
|
+
setOption(key, value) {
|
|
83
|
+
if (!this.KEYS[key]) {
|
|
84
|
+
return;
|
|
77
85
|
}
|
|
78
|
-
|
|
86
|
+
this.options[key] = value;
|
|
87
|
+
return this.api.storage.setItem(this.KEYS[key], value || '');
|
|
79
88
|
}
|
|
80
89
|
getLocale() {
|
|
81
|
-
return this.
|
|
90
|
+
return this.getOption('locale');
|
|
82
91
|
}
|
|
83
92
|
setLocale(locale) {
|
|
84
|
-
this.
|
|
85
|
-
|
|
93
|
+
this.setOption('locale', locale);
|
|
94
|
+
}
|
|
95
|
+
getRole() {
|
|
96
|
+
return this.getOption('role');
|
|
97
|
+
}
|
|
98
|
+
setRole(role) {
|
|
99
|
+
this.setOption('role', role);
|
|
86
100
|
}
|
|
87
101
|
getToken() {
|
|
88
|
-
return this.
|
|
102
|
+
return this.getOption('token');
|
|
89
103
|
}
|
|
90
104
|
setToken(token) {
|
|
91
|
-
this.
|
|
92
|
-
this.api.storage.setItem('NOCOBASE_TOKEN', token || '');
|
|
93
|
-
if (!token) {
|
|
94
|
-
this.setRole(null);
|
|
95
|
-
// this.setLocale(null);
|
|
96
|
-
}
|
|
105
|
+
this.setOption('token', token);
|
|
97
106
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return this.api.storage.getItem(this.NOCOBASE_ROLE_KEY);
|
|
107
|
+
getAuthenticator() {
|
|
108
|
+
return this.getOption('authenticator');
|
|
101
109
|
}
|
|
102
|
-
|
|
103
|
-
this.
|
|
104
|
-
|
|
110
|
+
setAuthenticator(authenticator) {
|
|
111
|
+
this.setOption('authenticator', authenticator);
|
|
112
|
+
}
|
|
113
|
+
middleware(config) {
|
|
114
|
+
if (this.locale) {
|
|
115
|
+
config.headers['X-Locale'] = this.locale;
|
|
116
|
+
}
|
|
117
|
+
if (this.role) {
|
|
118
|
+
config.headers['X-Role'] = this.role;
|
|
119
|
+
}
|
|
120
|
+
if (this.authenticator && !config.headers['X-Authenticator']) {
|
|
121
|
+
config.headers['X-Authenticator'] = this.authenticator;
|
|
122
|
+
}
|
|
123
|
+
if (this.token) {
|
|
124
|
+
config.headers['Authorization'] = `Bearer ${this.token}`;
|
|
125
|
+
}
|
|
126
|
+
return config;
|
|
105
127
|
}
|
|
106
|
-
signIn(values, authenticator
|
|
128
|
+
signIn(values, authenticator) {
|
|
107
129
|
var _this = this;
|
|
108
130
|
return _asyncToGenerator(function* () {
|
|
109
131
|
var _response$data;
|
|
110
132
|
const response = yield _this.api.request({
|
|
111
133
|
method: 'post',
|
|
112
|
-
url: '
|
|
134
|
+
url: 'auth:signIn',
|
|
113
135
|
data: values,
|
|
114
|
-
|
|
115
|
-
authenticator
|
|
136
|
+
headers: {
|
|
137
|
+
'X-Authenticator': authenticator
|
|
116
138
|
}
|
|
117
139
|
});
|
|
118
140
|
const data = response === null || response === void 0 ? void 0 : (_response$data = response.data) === null || _response$data === void 0 ? void 0 : _response$data.data;
|
|
119
141
|
_this.setToken(data === null || data === void 0 ? void 0 : data.token);
|
|
142
|
+
_this.setAuthenticator(authenticator);
|
|
120
143
|
return response;
|
|
121
144
|
})();
|
|
122
145
|
}
|
|
123
|
-
|
|
146
|
+
signUp(values, authenticator) {
|
|
124
147
|
var _this2 = this;
|
|
125
148
|
return _asyncToGenerator(function* () {
|
|
126
|
-
yield _this2.api.request({
|
|
149
|
+
return yield _this2.api.request({
|
|
127
150
|
method: 'post',
|
|
128
|
-
url: '
|
|
151
|
+
url: 'auth:signUp',
|
|
152
|
+
data: values,
|
|
153
|
+
headers: {
|
|
154
|
+
'X-Authenticator': authenticator
|
|
155
|
+
}
|
|
129
156
|
});
|
|
130
|
-
|
|
157
|
+
})();
|
|
158
|
+
}
|
|
159
|
+
signOut() {
|
|
160
|
+
var _this3 = this;
|
|
161
|
+
return _asyncToGenerator(function* () {
|
|
162
|
+
const response = yield _this3.api.request({
|
|
163
|
+
method: 'post',
|
|
164
|
+
url: 'auth:signOut'
|
|
165
|
+
});
|
|
166
|
+
_this3.setToken(null);
|
|
167
|
+
_this3.setRole(null);
|
|
168
|
+
_this3.setAuthenticator(null);
|
|
169
|
+
return response;
|
|
131
170
|
})();
|
|
132
171
|
}
|
|
133
172
|
}
|
|
@@ -210,7 +249,7 @@ class APIClient {
|
|
|
210
249
|
return this.axios.request(config);
|
|
211
250
|
}
|
|
212
251
|
resource(name, of) {
|
|
213
|
-
var
|
|
252
|
+
var _this4 = this;
|
|
214
253
|
const target = {};
|
|
215
254
|
const handler = {
|
|
216
255
|
get: (_, actionName) => {
|
|
@@ -235,13 +274,16 @@ class APIClient {
|
|
|
235
274
|
if (typeof filter === 'string') {
|
|
236
275
|
config['params']['filter'] = filter;
|
|
237
276
|
} else {
|
|
277
|
+
if (filter['*']) {
|
|
278
|
+
delete filter['*'];
|
|
279
|
+
}
|
|
238
280
|
config['params']['filter'] = JSON.stringify(filter);
|
|
239
281
|
}
|
|
240
282
|
}
|
|
241
283
|
if (config.method !== 'get') {
|
|
242
284
|
config['data'] = values || {};
|
|
243
285
|
}
|
|
244
|
-
return yield
|
|
286
|
+
return yield _this4.request(_objectSpread(_objectSpread({}, config), opts));
|
|
245
287
|
});
|
|
246
288
|
return function (_x, _x2) {
|
|
247
289
|
return _ref2.apply(this, arguments);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0-alpha.3",
|
|
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": "1f0b27fc9ab2398cd41c308a6b01a986e025cd20"
|
|
16
16
|
}
|
package/src/APIClient.ts
CHANGED
|
@@ -25,22 +25,23 @@ export interface IResource {
|
|
|
25
25
|
export class Auth {
|
|
26
26
|
protected api: APIClient;
|
|
27
27
|
|
|
28
|
-
protected
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
protected KEYS = {
|
|
29
|
+
locale: 'NOCOBASE_LOCALE',
|
|
30
|
+
role: 'NOCOBASE_ROLE',
|
|
31
|
+
token: 'NOCOBASE_TOKEN',
|
|
32
|
+
authenticator: 'NOCOBASE_AUTH',
|
|
33
|
+
};
|
|
31
34
|
|
|
32
35
|
protected options = {
|
|
33
|
-
token: null,
|
|
34
36
|
locale: null,
|
|
35
37
|
role: null,
|
|
38
|
+
authenticator: null,
|
|
39
|
+
token: null,
|
|
36
40
|
};
|
|
37
41
|
|
|
38
42
|
constructor(api: APIClient) {
|
|
39
43
|
this.api = api;
|
|
40
44
|
this.initKeys();
|
|
41
|
-
this.locale = this.getLocale();
|
|
42
|
-
this.role = this.getRole();
|
|
43
|
-
this.token = this.getToken();
|
|
44
45
|
this.api.axios.interceptors.request.use(this.middleware.bind(this));
|
|
45
46
|
}
|
|
46
47
|
|
|
@@ -53,98 +54,140 @@ export class Auth {
|
|
|
53
54
|
return;
|
|
54
55
|
}
|
|
55
56
|
const appName = match[1];
|
|
56
|
-
this.
|
|
57
|
-
this.
|
|
57
|
+
this.KEYS['role'] = `${appName.toUpperCase()}_` + this.KEYS['role'];
|
|
58
|
+
this.KEYS['locale'] = `${appName.toUpperCase()}_` + this.KEYS['locale'];
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
get locale() {
|
|
61
62
|
return this.getLocale();
|
|
62
63
|
}
|
|
63
64
|
|
|
65
|
+
set locale(value: string) {
|
|
66
|
+
this.setLocale(value);
|
|
67
|
+
}
|
|
68
|
+
|
|
64
69
|
get role() {
|
|
65
70
|
return this.getRole();
|
|
66
71
|
}
|
|
67
72
|
|
|
73
|
+
set role(value: string) {
|
|
74
|
+
this.setRole(value);
|
|
75
|
+
}
|
|
76
|
+
|
|
68
77
|
get token() {
|
|
69
78
|
return this.getToken();
|
|
70
79
|
}
|
|
71
80
|
|
|
72
|
-
set
|
|
73
|
-
this.
|
|
81
|
+
set token(value: string) {
|
|
82
|
+
this.setToken(value);
|
|
74
83
|
}
|
|
75
84
|
|
|
76
|
-
|
|
77
|
-
this.
|
|
85
|
+
get authenticator() {
|
|
86
|
+
return this.getAuthenticator();
|
|
78
87
|
}
|
|
79
88
|
|
|
80
|
-
set
|
|
81
|
-
this.
|
|
89
|
+
set authenticator(value: string) {
|
|
90
|
+
this.setAuthenticator(value);
|
|
82
91
|
}
|
|
83
92
|
|
|
84
|
-
|
|
85
|
-
if (this.
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
if (this.role) {
|
|
89
|
-
config.headers['X-Role'] = this.role;
|
|
93
|
+
getOption(key: string) {
|
|
94
|
+
if (!this.KEYS[key]) {
|
|
95
|
+
return;
|
|
90
96
|
}
|
|
91
|
-
|
|
92
|
-
|
|
97
|
+
return this.api.storage.getItem(this.KEYS[key]);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
setOption(key: string, value?: string) {
|
|
101
|
+
if (!this.KEYS[key]) {
|
|
102
|
+
return;
|
|
93
103
|
}
|
|
94
|
-
|
|
104
|
+
this.options[key] = value;
|
|
105
|
+
return this.api.storage.setItem(this.KEYS[key], value || '');
|
|
95
106
|
}
|
|
96
107
|
|
|
97
108
|
getLocale() {
|
|
98
|
-
return this.
|
|
109
|
+
return this.getOption('locale');
|
|
99
110
|
}
|
|
100
111
|
|
|
101
112
|
setLocale(locale: string) {
|
|
102
|
-
this.
|
|
103
|
-
|
|
113
|
+
this.setOption('locale', locale);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
getRole() {
|
|
117
|
+
return this.getOption('role');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
setRole(role: string) {
|
|
121
|
+
this.setOption('role', role);
|
|
104
122
|
}
|
|
105
123
|
|
|
106
124
|
getToken() {
|
|
107
|
-
return this.
|
|
125
|
+
return this.getOption('token');
|
|
108
126
|
}
|
|
109
127
|
|
|
110
128
|
setToken(token: string) {
|
|
111
|
-
this.
|
|
112
|
-
this.api.storage.setItem('NOCOBASE_TOKEN', token || '');
|
|
113
|
-
if (!token) {
|
|
114
|
-
this.setRole(null);
|
|
115
|
-
// this.setLocale(null);
|
|
116
|
-
}
|
|
129
|
+
this.setOption('token', token);
|
|
117
130
|
}
|
|
118
131
|
|
|
119
|
-
|
|
120
|
-
return this.
|
|
132
|
+
getAuthenticator() {
|
|
133
|
+
return this.getOption('authenticator');
|
|
121
134
|
}
|
|
122
135
|
|
|
123
|
-
|
|
124
|
-
this.
|
|
125
|
-
this.api.storage.setItem(this.NOCOBASE_ROLE_KEY, role || '');
|
|
136
|
+
setAuthenticator(authenticator: string) {
|
|
137
|
+
this.setOption('authenticator', authenticator);
|
|
126
138
|
}
|
|
127
139
|
|
|
128
|
-
|
|
140
|
+
middleware(config: AxiosRequestConfig) {
|
|
141
|
+
if (this.locale) {
|
|
142
|
+
config.headers['X-Locale'] = this.locale;
|
|
143
|
+
}
|
|
144
|
+
if (this.role) {
|
|
145
|
+
config.headers['X-Role'] = this.role;
|
|
146
|
+
}
|
|
147
|
+
if (this.authenticator && !config.headers['X-Authenticator']) {
|
|
148
|
+
config.headers['X-Authenticator'] = this.authenticator;
|
|
149
|
+
}
|
|
150
|
+
if (this.token) {
|
|
151
|
+
config.headers['Authorization'] = `Bearer ${this.token}`;
|
|
152
|
+
}
|
|
153
|
+
return config;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async signIn(values: any, authenticator?: string): Promise<AxiosResponse<any>> {
|
|
129
157
|
const response = await this.api.request({
|
|
130
158
|
method: 'post',
|
|
131
|
-
url: '
|
|
159
|
+
url: 'auth:signIn',
|
|
132
160
|
data: values,
|
|
133
|
-
|
|
134
|
-
authenticator,
|
|
161
|
+
headers: {
|
|
162
|
+
'X-Authenticator': authenticator,
|
|
135
163
|
},
|
|
136
164
|
});
|
|
137
165
|
const data = response?.data?.data;
|
|
138
166
|
this.setToken(data?.token);
|
|
167
|
+
this.setAuthenticator(authenticator);
|
|
139
168
|
return response;
|
|
140
169
|
}
|
|
141
170
|
|
|
171
|
+
async signUp(values: any, authenticator?: string): Promise<AxiosResponse<any>> {
|
|
172
|
+
return await this.api.request({
|
|
173
|
+
method: 'post',
|
|
174
|
+
url: 'auth:signUp',
|
|
175
|
+
data: values,
|
|
176
|
+
headers: {
|
|
177
|
+
'X-Authenticator': authenticator,
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
142
182
|
async signOut() {
|
|
143
|
-
await this.api.request({
|
|
183
|
+
const response = await this.api.request({
|
|
144
184
|
method: 'post',
|
|
145
|
-
url: '
|
|
185
|
+
url: 'auth:signOut',
|
|
146
186
|
});
|
|
147
187
|
this.setToken(null);
|
|
188
|
+
this.setRole(null);
|
|
189
|
+
this.setAuthenticator(null);
|
|
190
|
+
return response;
|
|
148
191
|
}
|
|
149
192
|
}
|
|
150
193
|
|
|
@@ -254,6 +297,9 @@ export class APIClient {
|
|
|
254
297
|
if (typeof filter === 'string') {
|
|
255
298
|
config['params']['filter'] = filter;
|
|
256
299
|
} else {
|
|
300
|
+
if (filter['*']) {
|
|
301
|
+
delete filter['*'];
|
|
302
|
+
}
|
|
257
303
|
config['params']['filter'] = JSON.stringify(filter);
|
|
258
304
|
}
|
|
259
305
|
}
|
|
@@ -24,13 +24,16 @@ describe('api-client', () => {
|
|
|
24
24
|
baseURL: 'https://localhost:8000/api',
|
|
25
25
|
});
|
|
26
26
|
const mock = new MockAdapter(api.axios);
|
|
27
|
-
mock.onPost('
|
|
27
|
+
mock.onPost('auth:signIn').reply(200, {
|
|
28
28
|
data: { id: 1, name: 'John Smith', token: '123' },
|
|
29
29
|
});
|
|
30
|
-
const response = await api.auth.signIn({});
|
|
30
|
+
const response = await api.auth.signIn({}, 'basic');
|
|
31
31
|
expect(response.status).toBe(200);
|
|
32
|
-
expect(api.auth.
|
|
33
|
-
|
|
32
|
+
expect(api.auth.getToken()).toBe('123');
|
|
33
|
+
const token = localStorage.getItem('NOCOBASE_TOKEN');
|
|
34
|
+
expect(token).toBe('123');
|
|
35
|
+
const auth = localStorage.getItem('NOCOBASE_AUTH');
|
|
36
|
+
expect(auth).toBe('basic');
|
|
34
37
|
});
|
|
35
38
|
|
|
36
39
|
test('resource action', async () => {
|
|
@@ -72,14 +75,15 @@ describe('api-client', () => {
|
|
|
72
75
|
});
|
|
73
76
|
|
|
74
77
|
const mock = new MockAdapter(api.axios);
|
|
75
|
-
mock.onPost('
|
|
78
|
+
mock.onPost('auth:signIn').reply(200, {
|
|
76
79
|
data: { id: 1, name: 'John Smith', token: '123' },
|
|
77
80
|
});
|
|
78
81
|
|
|
79
82
|
const response = await api.auth.signIn({});
|
|
80
83
|
expect(response.status).toBe(200);
|
|
81
|
-
expect(api.auth.
|
|
82
|
-
|
|
84
|
+
expect(api.auth.getToken()).toBe('123');
|
|
85
|
+
const token = items.get('NOCOBASE_TOKEN');
|
|
86
|
+
expect(token).toBe('123');
|
|
83
87
|
});
|
|
84
88
|
|
|
85
89
|
test('custom auth', async () => {
|
|
@@ -87,10 +91,11 @@ describe('api-client', () => {
|
|
|
87
91
|
async signIn(values: any): Promise<AxiosResponse<any, any>> {
|
|
88
92
|
const response = await this.api.request({
|
|
89
93
|
method: 'post',
|
|
90
|
-
url: '
|
|
94
|
+
url: 'auth:test',
|
|
91
95
|
data: values,
|
|
92
96
|
});
|
|
93
97
|
const data = response?.data?.data;
|
|
98
|
+
this.setAuthenticator('test');
|
|
94
99
|
this.setToken(data?.token);
|
|
95
100
|
return response;
|
|
96
101
|
}
|
|
@@ -104,13 +109,16 @@ describe('api-client', () => {
|
|
|
104
109
|
expect(api.auth).toBeInstanceOf(TestAuth);
|
|
105
110
|
|
|
106
111
|
const mock = new MockAdapter(api.axios);
|
|
107
|
-
mock.onPost('
|
|
112
|
+
mock.onPost('auth:test').reply(200, {
|
|
108
113
|
data: { id: 1, name: 'John Smith', token: '123' },
|
|
109
114
|
});
|
|
110
115
|
|
|
111
116
|
const response = await api.auth.signIn({});
|
|
112
117
|
expect(response.status).toBe(200);
|
|
113
|
-
expect(api.auth.
|
|
114
|
-
|
|
118
|
+
expect(api.auth.getToken()).toBe('123');
|
|
119
|
+
const token = localStorage.getItem('NOCOBASE_TOKEN');
|
|
120
|
+
expect(token).toBe('123');
|
|
121
|
+
const auth = localStorage.getItem('NOCOBASE_AUTH');
|
|
122
|
+
expect(auth).toBe('test');
|
|
115
123
|
});
|
|
116
124
|
});
|