@nocobase/sdk 2.0.0-alpha.8 → 2.1.0-alpha.1

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.
@@ -7,6 +7,8 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
  import { AxiosInstance, AxiosRequestConfig, AxiosResponse, RawAxiosRequestHeaders } from 'axios';
10
+ import { Auth } from './Auth';
11
+ import { BaseStorage, LocalStorage, MemoryStorage } from './Storage';
10
12
  export interface ActionParams {
11
13
  filterByTk?: any;
12
14
  [key: string]: any;
@@ -21,116 +23,25 @@ type ResourceAction = (params?: ActionParams, opts?: any) => Promise<any>;
21
23
  export type IResource = {
22
24
  [key: string]: ResourceAction;
23
25
  };
24
- export declare class Auth {
25
- protected api: APIClient;
26
- get storagePrefix(): string;
27
- get KEYS(): {
28
- locale: string;
29
- role: string;
30
- token: string;
31
- authenticator: string;
32
- theme: string;
33
- };
34
- protected options: {
35
- locale: any;
36
- role: any;
37
- authenticator: any;
38
- token: any;
39
- };
40
- constructor(api: APIClient);
41
- get locale(): string;
42
- set locale(value: string);
43
- get role(): string;
44
- set role(value: string);
45
- get token(): string;
46
- set token(value: string);
47
- get authenticator(): string;
48
- set authenticator(value: string);
49
- /**
50
- * @internal
51
- */
52
- getOption(key: string): string;
53
- /**
54
- * @internal
55
- */
56
- setOption(key: string, value?: string): void;
57
- /**
58
- * @internal
59
- * use {@link Auth#locale} instead
60
- */
61
- getLocale(): string;
62
- /**
63
- * @internal
64
- * use {@link Auth#locale} instead
65
- */
66
- setLocale(locale: string): void;
67
- /**
68
- * @internal
69
- * use {@link Auth#role} instead
70
- */
71
- getRole(): string;
72
- /**
73
- * @internal
74
- * use {@link Auth#role} instead
75
- */
76
- setRole(role: string): void;
77
- /**
78
- * @internal
79
- * use {@link Auth#token} instead
80
- */
81
- getToken(): string;
82
- /**
83
- * @internal
84
- * use {@link Auth#token} instead
85
- */
86
- setToken(token: string): void;
87
- /**
88
- * @internal
89
- * use {@link Auth#authenticator} instead
90
- */
91
- getAuthenticator(): string;
92
- /**
93
- * @internal
94
- * use {@link Auth#authenticator} instead
95
- */
96
- setAuthenticator(authenticator: string): void;
97
- middleware(config: AxiosRequestConfig): AxiosRequestConfig<any>;
98
- signIn(values: any, authenticator?: string): Promise<AxiosResponse<any>>;
99
- signUp(values: any, authenticator?: string): Promise<AxiosResponse<any>>;
100
- signOut(): Promise<AxiosResponse<any, any>>;
101
- lostPassword(values: any): Promise<AxiosResponse<any>>;
102
- resetPassword(values: any): Promise<AxiosResponse<any>>;
103
- checkResetToken(values: any): Promise<AxiosResponse<any>>;
104
- }
105
- export declare abstract class Storage {
106
- abstract clear(): void;
107
- abstract getItem(key: string): string | null;
108
- abstract removeItem(key: string): void;
109
- abstract setItem(key: string, value: string): void;
110
- }
111
- export declare class MemoryStorage extends Storage {
112
- items: Map<any, any>;
113
- clear(): void;
114
- getItem(key: string): any;
115
- setItem(key: string, value: string): Map<any, any>;
116
- removeItem(key: string): boolean;
117
- }
118
26
  interface ExtendedOptions {
119
27
  authClass?: any;
120
28
  storageType?: 'localStorage' | 'sessionStorage' | 'memory';
121
29
  storageClass?: any;
122
30
  storagePrefix?: string;
31
+ appName?: string;
123
32
  }
124
33
  export type APIClientOptions = AxiosInstance | (AxiosRequestConfig & ExtendedOptions);
125
34
  export declare class APIClient {
126
35
  options?: APIClientOptions;
127
36
  axios: AxiosInstance;
128
37
  auth: Auth;
129
- storage: Storage;
38
+ storage: BaseStorage;
130
39
  storagePrefix: string;
40
+ baseStoragePrefix: string;
131
41
  toErrMessages(error: any): any;
132
42
  getHeaders(): {};
133
43
  constructor(options?: APIClientOptions);
44
+ createStorage(storageType: 'localStorage' | 'sessionStorage' | 'memory'): MemoryStorage | LocalStorage;
134
45
  private initStorage;
135
46
  interceptors(): void;
136
47
  request<T = any, R = AxiosResponse<T>, D = any>(config: (AxiosRequestConfig<D> | ResourceActionOptions) & {
package/lib/APIClient.js CHANGED
@@ -37,263 +37,20 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
37
37
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
38
  var APIClient_exports = {};
39
39
  __export(APIClient_exports, {
40
- APIClient: () => APIClient,
41
- Auth: () => Auth,
42
- MemoryStorage: () => MemoryStorage,
43
- Storage: () => Storage
40
+ APIClient: () => APIClient
44
41
  });
45
42
  module.exports = __toCommonJS(APIClient_exports);
46
43
  var import_axios = __toESM(require("axios"));
47
44
  var import_qs = __toESM(require("qs"));
48
- const _Auth = class _Auth {
49
- api;
50
- get storagePrefix() {
51
- return this.api.storagePrefix;
52
- }
53
- get KEYS() {
54
- var _a, _b;
55
- const defaults = {
56
- locale: this.storagePrefix + "LOCALE",
57
- role: this.storagePrefix + "ROLE",
58
- token: this.storagePrefix + "TOKEN",
59
- authenticator: this.storagePrefix + "AUTH",
60
- theme: this.storagePrefix + "THEME"
61
- };
62
- if (this.api["app"]) {
63
- const appName = (_b = (_a = this.api["app"]) == null ? void 0 : _a.getName) == null ? void 0 : _b.call(_a);
64
- if (appName) {
65
- defaults["role"] = `${appName.toUpperCase()}_` + defaults["role"];
66
- defaults["locale"] = `${appName.toUpperCase()}_` + defaults["locale"];
67
- }
68
- }
69
- return defaults;
70
- }
71
- options = {
72
- locale: null,
73
- role: null,
74
- authenticator: null,
75
- token: null
76
- };
77
- constructor(api) {
78
- this.api = api;
79
- this.api.axios.interceptors.request.use(this.middleware.bind(this));
80
- }
81
- get locale() {
82
- return this.getLocale();
83
- }
84
- set locale(value) {
85
- this.setLocale(value);
86
- }
87
- get role() {
88
- return this.getRole();
89
- }
90
- set role(value) {
91
- this.setRole(value);
92
- }
93
- get token() {
94
- return this.getToken();
95
- }
96
- set token(value) {
97
- this.setToken(value);
98
- }
99
- get authenticator() {
100
- return this.getAuthenticator();
101
- }
102
- set authenticator(value) {
103
- this.setAuthenticator(value);
104
- }
105
- /**
106
- * @internal
107
- */
108
- getOption(key) {
109
- if (!this.KEYS[key]) {
110
- return;
111
- }
112
- return this.api.storage.getItem(this.KEYS[key]);
113
- }
114
- /**
115
- * @internal
116
- */
117
- setOption(key, value) {
118
- if (!this.KEYS[key]) {
119
- return;
120
- }
121
- this.options[key] = value;
122
- return this.api.storage.setItem(this.KEYS[key], value || "");
123
- }
124
- /**
125
- * @internal
126
- * use {@link Auth#locale} instead
127
- */
128
- getLocale() {
129
- return this.getOption("locale");
130
- }
131
- /**
132
- * @internal
133
- * use {@link Auth#locale} instead
134
- */
135
- setLocale(locale) {
136
- this.setOption("locale", locale);
137
- }
138
- /**
139
- * @internal
140
- * use {@link Auth#role} instead
141
- */
142
- getRole() {
143
- return this.getOption("role");
144
- }
145
- /**
146
- * @internal
147
- * use {@link Auth#role} instead
148
- */
149
- setRole(role) {
150
- this.setOption("role", role);
151
- }
152
- /**
153
- * @internal
154
- * use {@link Auth#token} instead
155
- */
156
- getToken() {
157
- return this.getOption("token");
158
- }
159
- /**
160
- * @internal
161
- * use {@link Auth#token} instead
162
- */
163
- setToken(token) {
164
- this.setOption("token", token);
165
- if (this.api["app"]) {
166
- this.api["app"].eventBus.dispatchEvent(
167
- new CustomEvent("auth:tokenChanged", { detail: { token, authenticator: this.authenticator } })
168
- );
169
- }
170
- }
171
- /**
172
- * @internal
173
- * use {@link Auth#authenticator} instead
174
- */
175
- getAuthenticator() {
176
- return this.getOption("authenticator");
177
- }
178
- /**
179
- * @internal
180
- * use {@link Auth#authenticator} instead
181
- */
182
- setAuthenticator(authenticator) {
183
- this.setOption("authenticator", authenticator);
184
- }
185
- middleware(config) {
186
- if (this.locale) {
187
- config.headers["X-Locale"] = this.locale;
188
- }
189
- if (this.role) {
190
- config.headers["X-Role"] = this.role;
191
- }
192
- if (this.authenticator && !config.headers["X-Authenticator"]) {
193
- config.headers["X-Authenticator"] = this.authenticator;
194
- }
195
- if (this.token) {
196
- config.headers["Authorization"] = `Bearer ${this.token}`;
197
- }
198
- return config;
199
- }
200
- async signIn(values, authenticator) {
201
- var _a;
202
- const response = await this.api.request({
203
- method: "post",
204
- url: "auth:signIn",
205
- data: values,
206
- headers: {
207
- "X-Authenticator": authenticator
208
- }
209
- });
210
- const data = (_a = response == null ? void 0 : response.data) == null ? void 0 : _a.data;
211
- this.setAuthenticator(authenticator);
212
- this.setToken(data == null ? void 0 : data.token);
213
- return response;
214
- }
215
- async signUp(values, authenticator) {
216
- return await this.api.request({
217
- method: "post",
218
- url: "auth:signUp",
219
- data: values,
220
- headers: {
221
- "X-Authenticator": authenticator
222
- }
223
- });
224
- }
225
- async signOut() {
226
- const response = await this.api.request({
227
- method: "post",
228
- url: "auth:signOut"
229
- });
230
- this.setToken(null);
231
- this.setRole(null);
232
- this.setAuthenticator(null);
233
- return response;
234
- }
235
- async lostPassword(values) {
236
- const searchParams = new URLSearchParams(window.location.search);
237
- const paramsObject = Object.fromEntries(searchParams.entries());
238
- const response = await this.api.request({
239
- method: "post",
240
- url: "auth:lostPassword",
241
- data: {
242
- ...values,
243
- baseURL: window.location.href.split("/forgot-password")[0]
244
- },
245
- headers: {
246
- "X-Authenticator": paramsObject.name
247
- }
248
- });
249
- return response;
250
- }
251
- async resetPassword(values) {
252
- const response = await this.api.request({
253
- method: "post",
254
- url: "auth:resetPassword",
255
- data: values
256
- });
257
- return response;
258
- }
259
- async checkResetToken(values) {
260
- const response = await this.api.request({
261
- method: "post",
262
- url: "auth:checkResetToken",
263
- data: values
264
- });
265
- return response;
266
- }
267
- };
268
- __name(_Auth, "Auth");
269
- let Auth = _Auth;
270
- const _Storage = class _Storage {
271
- };
272
- __name(_Storage, "Storage");
273
- let Storage = _Storage;
274
- const _MemoryStorage = class _MemoryStorage extends Storage {
275
- items = /* @__PURE__ */ new Map();
276
- clear() {
277
- this.items.clear();
278
- }
279
- getItem(key) {
280
- return this.items.get(key);
281
- }
282
- setItem(key, value) {
283
- return this.items.set(key, value);
284
- }
285
- removeItem(key) {
286
- return this.items.delete(key);
287
- }
288
- };
289
- __name(_MemoryStorage, "MemoryStorage");
290
- let MemoryStorage = _MemoryStorage;
45
+ var import_Auth = require("./Auth");
46
+ var import_Storage = require("./Storage");
291
47
  const _APIClient = class _APIClient {
292
48
  options;
293
49
  axios;
294
50
  auth;
295
51
  storage;
296
52
  storagePrefix = "NOCOBASE_";
53
+ baseStoragePrefix = "NOCOBASE_";
297
54
  toErrMessages(error) {
298
55
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
299
56
  if (typeof document !== "undefined" && typeof ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) === "string") {
@@ -336,8 +93,9 @@ const _APIClient = class _APIClient {
336
93
  if (typeof options === "function") {
337
94
  this.axios = options;
338
95
  } else {
339
- const { authClass, storageType, storageClass, storagePrefix = "NOCOBASE_", ...others } = options || {};
340
- this.storagePrefix = storagePrefix;
96
+ const { appName, authClass, storageType, storageClass, storagePrefix = "NOCOBASE_", ...others } = options || {};
97
+ this.baseStoragePrefix = storagePrefix;
98
+ this.storagePrefix = appName ? `${storagePrefix}${appName.toUpperCase()}_` : storagePrefix;
341
99
  this.axios = import_axios.default.create(others);
342
100
  this.initStorage(storageClass, storageType);
343
101
  if (authClass) {
@@ -348,24 +106,33 @@ const _APIClient = class _APIClient {
348
106
  this.initStorage();
349
107
  }
350
108
  if (!this.auth) {
351
- this.auth = new Auth(this);
109
+ this.auth = new import_Auth.Auth(this);
352
110
  }
353
111
  this.interceptors();
354
112
  }
113
+ createStorage(storageType) {
114
+ if (storageType === "localStorage" && typeof localStorage !== "undefined") {
115
+ return new import_Storage.LocalStorage(this.storagePrefix, this.baseStoragePrefix);
116
+ }
117
+ if (storageType === "sessionStorage" && typeof sessionStorage !== "undefined") {
118
+ return new import_Storage.SessionStorage(this.storagePrefix, this.baseStoragePrefix);
119
+ }
120
+ return new import_Storage.MemoryStorage();
121
+ }
355
122
  initStorage(storage, storageType = "localStorage") {
356
123
  if (storage) {
357
124
  this.storage = new storage(this);
358
125
  return;
359
126
  }
360
127
  if (storageType === "localStorage" && typeof localStorage !== "undefined") {
361
- this.storage = localStorage;
128
+ this.storage = new import_Storage.LocalStorage(this.storagePrefix, this.baseStoragePrefix);
362
129
  return;
363
130
  }
364
131
  if (storageType === "sessionStorage" && typeof sessionStorage !== "undefined") {
365
- this.storage = sessionStorage;
132
+ this.storage = new import_Storage.SessionStorage(this.storagePrefix, this.baseStoragePrefix);
366
133
  return;
367
134
  }
368
- this.storage = new MemoryStorage();
135
+ this.storage = new import_Storage.MemoryStorage();
369
136
  }
370
137
  interceptors() {
371
138
  this.axios.interceptors.request.use((config) => {
@@ -431,8 +198,5 @@ __name(_APIClient, "APIClient");
431
198
  let APIClient = _APIClient;
432
199
  // Annotate the CommonJS export names for ESM import in node:
433
200
  0 && (module.exports = {
434
- APIClient,
435
- Auth,
436
- MemoryStorage,
437
- Storage
201
+ APIClient
438
202
  });
package/lib/Auth.d.ts ADDED
@@ -0,0 +1,84 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { AxiosRequestConfig, AxiosResponse } from 'axios';
10
+ import { APIClient } from './APIClient';
11
+ export declare class Auth {
12
+ protected api: APIClient;
13
+ get storagePrefix(): string;
14
+ protected options: {
15
+ locale: any;
16
+ role: any;
17
+ authenticator: any;
18
+ token: any;
19
+ };
20
+ constructor(api: APIClient);
21
+ get locale(): string;
22
+ set locale(value: string);
23
+ get role(): string;
24
+ set role(value: string);
25
+ get token(): string;
26
+ set token(value: string);
27
+ get authenticator(): string;
28
+ set authenticator(value: string);
29
+ /**
30
+ * @internal
31
+ */
32
+ getOption(key: string): string;
33
+ /**
34
+ * @internal
35
+ */
36
+ setOption(key: string, value?: string): void;
37
+ /**
38
+ * @internal
39
+ * use {@link Auth#locale} instead
40
+ */
41
+ getLocale(): string;
42
+ /**
43
+ * @internal
44
+ * use {@link Auth#locale} instead
45
+ */
46
+ setLocale(locale: string): void;
47
+ /**
48
+ * @internal
49
+ * use {@link Auth#role} instead
50
+ */
51
+ getRole(): string;
52
+ /**
53
+ * @internal
54
+ * use {@link Auth#role} instead
55
+ */
56
+ setRole(role: string): void;
57
+ /**
58
+ * @internal
59
+ * use {@link Auth#token} instead
60
+ */
61
+ getToken(): string;
62
+ /**
63
+ * @internal
64
+ * use {@link Auth#token} instead
65
+ */
66
+ setToken(token: string): void;
67
+ /**
68
+ * @internal
69
+ * use {@link Auth#authenticator} instead
70
+ */
71
+ getAuthenticator(): string;
72
+ /**
73
+ * @internal
74
+ * use {@link Auth#authenticator} instead
75
+ */
76
+ setAuthenticator(authenticator: string): void;
77
+ middleware(config: AxiosRequestConfig): AxiosRequestConfig<any>;
78
+ signIn(values: any, authenticator?: string): Promise<AxiosResponse<any>>;
79
+ signUp(values: any, authenticator?: string): Promise<AxiosResponse<any>>;
80
+ signOut(): Promise<AxiosResponse<any, any>>;
81
+ lostPassword(values: any): Promise<AxiosResponse<any>>;
82
+ resetPassword(values: any): Promise<AxiosResponse<any>>;
83
+ checkResetToken(values: any): Promise<AxiosResponse<any>>;
84
+ }
package/lib/Auth.js ADDED
@@ -0,0 +1,234 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var Auth_exports = {};
29
+ __export(Auth_exports, {
30
+ Auth: () => Auth
31
+ });
32
+ module.exports = __toCommonJS(Auth_exports);
33
+ const _Auth = class _Auth {
34
+ api;
35
+ get storagePrefix() {
36
+ return this.api.storagePrefix;
37
+ }
38
+ options = {
39
+ locale: null,
40
+ role: null,
41
+ authenticator: null,
42
+ token: null
43
+ };
44
+ constructor(api) {
45
+ this.api = api;
46
+ this.api.axios.interceptors.request.use(this.middleware.bind(this));
47
+ }
48
+ get locale() {
49
+ return this.getLocale();
50
+ }
51
+ set locale(value) {
52
+ this.setLocale(value);
53
+ }
54
+ get role() {
55
+ return this.getRole();
56
+ }
57
+ set role(value) {
58
+ this.setRole(value);
59
+ }
60
+ get token() {
61
+ return this.getToken();
62
+ }
63
+ set token(value) {
64
+ this.setToken(value);
65
+ }
66
+ get authenticator() {
67
+ return this.getAuthenticator();
68
+ }
69
+ set authenticator(value) {
70
+ this.setAuthenticator(value);
71
+ }
72
+ /**
73
+ * @internal
74
+ */
75
+ getOption(key) {
76
+ return this.api.storage.getItem(key);
77
+ }
78
+ /**
79
+ * @internal
80
+ */
81
+ setOption(key, value) {
82
+ this.options[key] = value;
83
+ return this.api.storage.setItem(key, value || "");
84
+ }
85
+ /**
86
+ * @internal
87
+ * use {@link Auth#locale} instead
88
+ */
89
+ getLocale() {
90
+ return this.getOption("locale");
91
+ }
92
+ /**
93
+ * @internal
94
+ * use {@link Auth#locale} instead
95
+ */
96
+ setLocale(locale) {
97
+ this.setOption("locale", locale);
98
+ }
99
+ /**
100
+ * @internal
101
+ * use {@link Auth#role} instead
102
+ */
103
+ getRole() {
104
+ return this.getOption("role");
105
+ }
106
+ /**
107
+ * @internal
108
+ * use {@link Auth#role} instead
109
+ */
110
+ setRole(role) {
111
+ this.setOption("role", role);
112
+ }
113
+ /**
114
+ * @internal
115
+ * use {@link Auth#token} instead
116
+ */
117
+ getToken() {
118
+ return this.getOption("token");
119
+ }
120
+ /**
121
+ * @internal
122
+ * use {@link Auth#token} instead
123
+ */
124
+ setToken(token) {
125
+ this.setOption("token", token);
126
+ if (this.api["app"]) {
127
+ this.api["app"].eventBus.dispatchEvent(
128
+ new CustomEvent("auth:tokenChanged", { detail: { token, authenticator: this.authenticator } })
129
+ );
130
+ }
131
+ }
132
+ /**
133
+ * @internal
134
+ * use {@link Auth#authenticator} instead
135
+ */
136
+ getAuthenticator() {
137
+ return this.getOption("auth");
138
+ }
139
+ /**
140
+ * @internal
141
+ * use {@link Auth#authenticator} instead
142
+ */
143
+ setAuthenticator(authenticator) {
144
+ this.setOption("auth", authenticator);
145
+ }
146
+ middleware(config) {
147
+ if (this.locale) {
148
+ config.headers["X-Locale"] = this.locale;
149
+ }
150
+ if (this.role) {
151
+ config.headers["X-Role"] = this.role;
152
+ }
153
+ if (this.authenticator && !config.headers["X-Authenticator"]) {
154
+ config.headers["X-Authenticator"] = this.authenticator;
155
+ }
156
+ if (this.token) {
157
+ config.headers["Authorization"] = `Bearer ${this.token}`;
158
+ }
159
+ return config;
160
+ }
161
+ async signIn(values, authenticator) {
162
+ var _a;
163
+ const response = await this.api.request({
164
+ method: "post",
165
+ url: "auth:signIn",
166
+ data: values,
167
+ headers: {
168
+ "X-Authenticator": authenticator
169
+ }
170
+ });
171
+ const data = (_a = response == null ? void 0 : response.data) == null ? void 0 : _a.data;
172
+ this.setAuthenticator(authenticator);
173
+ this.setToken(data == null ? void 0 : data.token);
174
+ return response;
175
+ }
176
+ async signUp(values, authenticator) {
177
+ return await this.api.request({
178
+ method: "post",
179
+ url: "auth:signUp",
180
+ data: values,
181
+ headers: {
182
+ "X-Authenticator": authenticator
183
+ }
184
+ });
185
+ }
186
+ async signOut() {
187
+ const response = await this.api.request({
188
+ method: "post",
189
+ url: "auth:signOut"
190
+ });
191
+ this.setToken(null);
192
+ this.setRole(null);
193
+ this.setAuthenticator(null);
194
+ return response;
195
+ }
196
+ async lostPassword(values) {
197
+ const searchParams = new URLSearchParams(window.location.search);
198
+ const paramsObject = Object.fromEntries(searchParams.entries());
199
+ const response = await this.api.request({
200
+ method: "post",
201
+ url: "auth:lostPassword",
202
+ data: {
203
+ ...values,
204
+ baseURL: window.location.href.split("/forgot-password")[0]
205
+ },
206
+ headers: {
207
+ "X-Authenticator": paramsObject.name
208
+ }
209
+ });
210
+ return response;
211
+ }
212
+ async resetPassword(values) {
213
+ const response = await this.api.request({
214
+ method: "post",
215
+ url: "auth:resetPassword",
216
+ data: values
217
+ });
218
+ return response;
219
+ }
220
+ async checkResetToken(values) {
221
+ const response = await this.api.request({
222
+ method: "post",
223
+ url: "auth:checkResetToken",
224
+ data: values
225
+ });
226
+ return response;
227
+ }
228
+ };
229
+ __name(_Auth, "Auth");
230
+ let Auth = _Auth;
231
+ // Annotate the CommonJS export names for ESM import in node:
232
+ 0 && (module.exports = {
233
+ Auth
234
+ });
@@ -0,0 +1,38 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ export declare abstract class BaseStorage {
10
+ storagePrefix: string;
11
+ abstract clear(): void;
12
+ abstract getItem(key: string): string | null;
13
+ abstract removeItem(key: string): void;
14
+ abstract setItem(key: string, value: string): void;
15
+ toUpperCase(prefix?: string, ...arr: string[]): string;
16
+ }
17
+ export declare class MemoryStorage extends BaseStorage {
18
+ items: Map<any, any>;
19
+ clear(): void;
20
+ getItem(key: string): any;
21
+ setItem(key: string, value: string): Map<any, any>;
22
+ removeItem(key: string): boolean;
23
+ }
24
+ export declare class LocalStorage extends BaseStorage {
25
+ storagePrefix: string;
26
+ baseStoragePrefix: string;
27
+ items: Storage;
28
+ constructor(storagePrefix: string, baseStoragePrefix?: string);
29
+ clear(): void;
30
+ getItem(key: string): string;
31
+ setItem(key: string, value: string): void;
32
+ removeItem(key: string): void;
33
+ }
34
+ export declare class SessionStorage extends LocalStorage {
35
+ storagePrefix: string;
36
+ baseStoragePrefix: string;
37
+ constructor(storagePrefix: string, baseStoragePrefix?: string);
38
+ }
package/lib/Storage.js ADDED
@@ -0,0 +1,105 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var Storage_exports = {};
29
+ __export(Storage_exports, {
30
+ BaseStorage: () => BaseStorage,
31
+ LocalStorage: () => LocalStorage,
32
+ MemoryStorage: () => MemoryStorage,
33
+ SessionStorage: () => SessionStorage
34
+ });
35
+ module.exports = __toCommonJS(Storage_exports);
36
+ const _BaseStorage = class _BaseStorage {
37
+ storagePrefix;
38
+ toUpperCase(prefix = "", ...arr) {
39
+ return prefix.toUpperCase() + arr.map((str) => str.toUpperCase()).join("_");
40
+ }
41
+ };
42
+ __name(_BaseStorage, "BaseStorage");
43
+ let BaseStorage = _BaseStorage;
44
+ const _MemoryStorage = class _MemoryStorage extends BaseStorage {
45
+ items = /* @__PURE__ */ new Map();
46
+ clear() {
47
+ this.items.clear();
48
+ }
49
+ getItem(key) {
50
+ return this.items.get(key);
51
+ }
52
+ setItem(key, value) {
53
+ return this.items.set(key, value);
54
+ }
55
+ removeItem(key) {
56
+ return this.items.delete(key);
57
+ }
58
+ };
59
+ __name(_MemoryStorage, "MemoryStorage");
60
+ let MemoryStorage = _MemoryStorage;
61
+ const _LocalStorage = class _LocalStorage extends BaseStorage {
62
+ constructor(storagePrefix, baseStoragePrefix = "") {
63
+ super();
64
+ this.storagePrefix = storagePrefix;
65
+ this.baseStoragePrefix = baseStoragePrefix;
66
+ this.items = window.localStorage;
67
+ }
68
+ items;
69
+ clear() {
70
+ return this.items.clear();
71
+ }
72
+ getItem(key) {
73
+ const value = this.items.getItem(this.toUpperCase(this.storagePrefix, key));
74
+ if (key === "token" && this.baseStoragePrefix) {
75
+ return value || this.items.getItem(this.toUpperCase(this.baseStoragePrefix, key));
76
+ }
77
+ return value;
78
+ }
79
+ setItem(key, value) {
80
+ console.log("setItem", this.toUpperCase(this.storagePrefix, key), value);
81
+ return this.items.setItem(this.toUpperCase(this.storagePrefix, key), value);
82
+ }
83
+ removeItem(key) {
84
+ return this.items.removeItem(this.toUpperCase(this.storagePrefix, key));
85
+ }
86
+ };
87
+ __name(_LocalStorage, "LocalStorage");
88
+ let LocalStorage = _LocalStorage;
89
+ const _SessionStorage = class _SessionStorage extends LocalStorage {
90
+ constructor(storagePrefix, baseStoragePrefix = "") {
91
+ super(storagePrefix, baseStoragePrefix);
92
+ this.storagePrefix = storagePrefix;
93
+ this.baseStoragePrefix = baseStoragePrefix;
94
+ this.items = window.sessionStorage;
95
+ }
96
+ };
97
+ __name(_SessionStorage, "SessionStorage");
98
+ let SessionStorage = _SessionStorage;
99
+ // Annotate the CommonJS export names for ESM import in node:
100
+ 0 && (module.exports = {
101
+ BaseStorage,
102
+ LocalStorage,
103
+ MemoryStorage,
104
+ SessionStorage
105
+ });
@@ -31,6 +31,12 @@ __export(getSubAppName_exports, {
31
31
  });
32
32
  module.exports = __toCommonJS(getSubAppName_exports);
33
33
  const getSubAppName = /* @__PURE__ */ __name((publicPath = "/") => {
34
+ if (typeof window === "undefined") {
35
+ return;
36
+ }
37
+ if (!window.location.pathname) {
38
+ return;
39
+ }
34
40
  const prefix = `${publicPath}apps/`;
35
41
  if (!window.location.pathname.startsWith(prefix)) {
36
42
  return;
package/lib/index.d.ts CHANGED
@@ -7,4 +7,6 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
  export * from './APIClient';
10
+ export * from './Auth';
11
+ export * from './Storage';
10
12
  export { default as getSubAppName } from './getSubAppName';
package/lib/index.js CHANGED
@@ -41,9 +41,13 @@ __export(src_exports, {
41
41
  });
42
42
  module.exports = __toCommonJS(src_exports);
43
43
  __reExport(src_exports, require("./APIClient"), module.exports);
44
+ __reExport(src_exports, require("./Auth"), module.exports);
45
+ __reExport(src_exports, require("./Storage"), module.exports);
44
46
  var import_getSubAppName = __toESM(require("./getSubAppName"));
45
47
  // Annotate the CommonJS export names for ESM import in node:
46
48
  0 && (module.exports = {
47
49
  getSubAppName,
48
- ...require("./APIClient")
50
+ ...require("./APIClient"),
51
+ ...require("./Auth"),
52
+ ...require("./Storage")
49
53
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/sdk",
3
- "version": "2.0.0-alpha.8",
3
+ "version": "2.1.0-alpha.1",
4
4
  "license": "AGPL-3.0",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -11,5 +11,5 @@
11
11
  "devDependencies": {
12
12
  "axios-mock-adapter": "^1.20.0"
13
13
  },
14
- "gitHead": "ff91246a3914c72dc1f4000d85df10a16798ff78"
14
+ "gitHead": "d27baf21569643d6fa83f882233f4e90eb5b89f1"
15
15
  }