@nocobase/sdk 2.2.0-beta.9 → 2.3.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.
@@ -44,6 +44,7 @@ export declare class APIClient {
44
44
  storagePrefix: string;
45
45
  baseStoragePrefix: string;
46
46
  shareToken: boolean;
47
+ appName?: string;
47
48
  toErrMessages(error: any): any;
48
49
  getHeaders(): {};
49
50
  constructor(options?: APIClientOptions);
package/lib/APIClient.js CHANGED
@@ -52,6 +52,7 @@ const _APIClient = class _APIClient {
52
52
  storagePrefix = "NOCOBASE_";
53
53
  baseStoragePrefix = "NOCOBASE_";
54
54
  shareToken = false;
55
+ appName;
55
56
  toErrMessages(error) {
56
57
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
57
58
  if (typeof document !== "undefined" && typeof ((_a = error == null ? void 0 : error.response) == null ? void 0 : _a.data) === "string") {
@@ -103,6 +104,7 @@ const _APIClient = class _APIClient {
103
104
  shareToken = false,
104
105
  ...others
105
106
  } = options || {};
107
+ this.appName = appName;
106
108
  this.shareToken = shareToken;
107
109
  this.baseStoragePrefix = storagePrefix;
108
110
  this.storagePrefix = appName ? `${storagePrefix}${appName.toUpperCase()}_` : storagePrefix;
package/lib/Auth.d.ts CHANGED
@@ -18,6 +18,7 @@ export declare class Auth {
18
18
  token: any;
19
19
  };
20
20
  constructor(api: APIClient);
21
+ protected getCookieAppName(): string;
21
22
  get locale(): string;
22
23
  set locale(value: string);
23
24
  get role(): string;
@@ -77,6 +78,7 @@ export declare class Auth {
77
78
  middleware(config: AxiosRequestConfig): AxiosRequestConfig<any>;
78
79
  signIn(values: any, authenticator?: string): Promise<AxiosResponse<any>>;
79
80
  signUp(values: any, authenticator?: string): Promise<AxiosResponse<any>>;
81
+ syncCookies(): Promise<AxiosResponse<unknown> | undefined>;
80
82
  signOut(): Promise<AxiosResponse<any, any>>;
81
83
  lostPassword(values: any): Promise<AxiosResponse<any>>;
82
84
  resetPassword(values: any): Promise<AxiosResponse<any>>;
package/lib/Auth.js CHANGED
@@ -30,7 +30,9 @@ __export(Auth_exports, {
30
30
  Auth: () => Auth
31
31
  });
32
32
  module.exports = __toCommonJS(Auth_exports);
33
+ var import_auth_cookie = require("./auth-cookie");
33
34
  var import_headers = require("./headers");
35
+ const SAFE_METHODS = /* @__PURE__ */ new Set(["get", "head", "options"]);
34
36
  const _Auth = class _Auth {
35
37
  api;
36
38
  get storagePrefix() {
@@ -46,6 +48,10 @@ const _Auth = class _Auth {
46
48
  this.api = api;
47
49
  this.api.axios.interceptors.request.use(this.middleware.bind(this));
48
50
  }
51
+ getCookieAppName() {
52
+ var _a;
53
+ return typeof this.api.options !== "function" && ((_a = this.api.options) == null ? void 0 : _a.appName) || this.api.appName || "main";
54
+ }
49
55
  get locale() {
50
56
  return this.getLocale();
51
57
  }
@@ -110,6 +116,7 @@ const _Auth = class _Auth {
110
116
  */
111
117
  setRole(role) {
112
118
  this.setOption("role", role);
119
+ (0, import_auth_cookie.setRoleCookie)(this.getCookieAppName(), role);
113
120
  }
114
121
  /**
115
122
  * @internal
@@ -157,6 +164,11 @@ const _Auth = class _Auth {
157
164
  if (this.token && !(0, import_headers.hasHeaderValue)(config.headers, "Authorization") && config.headers) {
158
165
  config.headers["Authorization"] = `Bearer ${this.token}`;
159
166
  }
167
+ const method = (config.method || "get").toLowerCase();
168
+ const csrfToken = (0, import_auth_cookie.getAuthCookieValue)("csrfToken", this.getCookieAppName());
169
+ if (!SAFE_METHODS.has(method) && csrfToken && !(0, import_headers.hasHeaderValue)(config.headers, "X-CSRF-Token") && config.headers) {
170
+ config.headers["X-CSRF-Token"] = csrfToken;
171
+ }
160
172
  return config;
161
173
  }
162
174
  async signIn(values, authenticator) {
@@ -184,6 +196,16 @@ const _Auth = class _Auth {
184
196
  }
185
197
  });
186
198
  }
199
+ async syncCookies() {
200
+ if (!this.token) {
201
+ return void 0;
202
+ }
203
+ return await this.api.request({
204
+ method: "post",
205
+ url: "auth:syncCookies",
206
+ skipNotify: true
207
+ });
208
+ }
187
209
  async signOut() {
188
210
  const response = await this.api.request({
189
211
  method: "post",
@@ -0,0 +1,11 @@
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 type { AuthCookieType } from '@nocobase/utils/client';
10
+ export declare function setRoleCookie(appName: string | undefined, role?: string | null): void;
11
+ export declare function getAuthCookieValue(type: AuthCookieType, appName: string | undefined): string;
@@ -0,0 +1,61 @@
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_cookie_exports = {};
29
+ __export(auth_cookie_exports, {
30
+ getAuthCookieValue: () => getAuthCookieValue,
31
+ setRoleCookie: () => setRoleCookie
32
+ });
33
+ module.exports = __toCommonJS(auth_cookie_exports);
34
+ var import_client = require("@nocobase/utils/client");
35
+ function setRoleCookie(appName, role) {
36
+ var _a, _b;
37
+ if (typeof document === "undefined") {
38
+ return;
39
+ }
40
+ const name = (0, import_client.getAuthCookieName)("role", appName);
41
+ const expires = role ? "" : "; Max-Age=0";
42
+ const value = role ? encodeURIComponent(role) : "";
43
+ const secure = ((_a = globalThis.location) == null ? void 0 : _a.protocol) === "https:" ? "; Secure" : "";
44
+ const publicPath = ((_b = window.__nocobase_public_path__) == null ? void 0 : _b.replace(/\/+$/g, "")) || "/";
45
+ document.cookie = `${name}=${value}; Path=${publicPath || "/"}; SameSite=Lax${secure}${expires}`;
46
+ }
47
+ __name(setRoleCookie, "setRoleCookie");
48
+ function getAuthCookieValue(type, appName) {
49
+ var _a;
50
+ if (typeof document === "undefined") {
51
+ return;
52
+ }
53
+ const name = `${(0, import_client.getAuthCookieName)(type, appName)}=`;
54
+ return (_a = document.cookie.split(";").map((item) => item.trim()).find((item) => item.startsWith(name))) == null ? void 0 : _a.slice(name.length);
55
+ }
56
+ __name(getAuthCookieValue, "getAuthCookieValue");
57
+ // Annotate the CommonJS export names for ESM import in node:
58
+ 0 && (module.exports = {
59
+ getAuthCookieValue,
60
+ setRoleCookie
61
+ });
package/lib/index.d.ts CHANGED
@@ -7,6 +7,7 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
  export * from './APIClient';
10
+ export * from './auth-cookie';
10
11
  export * from './Auth';
11
12
  export * from './headers';
12
13
  export * from './Storage';
package/lib/index.js CHANGED
@@ -41,6 +41,7 @@ __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-cookie"), module.exports);
44
45
  __reExport(src_exports, require("./Auth"), module.exports);
45
46
  __reExport(src_exports, require("./headers"), module.exports);
46
47
  __reExport(src_exports, require("./Storage"), module.exports);
@@ -49,6 +50,7 @@ var import_getSubAppName = __toESM(require("./getSubAppName"));
49
50
  0 && (module.exports = {
50
51
  getSubAppName,
51
52
  ...require("./APIClient"),
53
+ ...require("./auth-cookie"),
52
54
  ...require("./Auth"),
53
55
  ...require("./headers"),
54
56
  ...require("./Storage")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/sdk",
3
- "version": "2.2.0-beta.9",
3
+ "version": "2.3.0-alpha.1",
4
4
  "license": "Apache-2.0",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -9,7 +9,8 @@
9
9
  "qs": "^6.10.1"
10
10
  },
11
11
  "devDependencies": {
12
+ "@nocobase/utils": "2.x",
12
13
  "axios-mock-adapter": "^1.20.0"
13
14
  },
14
- "gitHead": "60e3d7abbaa0c7cead76f71a4f3d5eedb6b8acdb"
15
+ "gitHead": "2377df8ceb12549149017f7f14a61207bf6e49a2"
15
16
  }