@atomm-developer/generator-sdk 1.0.9 → 1.0.11

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/README.md CHANGED
@@ -45,9 +45,15 @@ console.log(GeneratorSDK.getAppKey());
45
45
  ```javascript
46
46
  // Login
47
47
  const userInfo = await sdk.auth.login();
48
+ console.log(userInfo.roleList);
49
+ console.log(userInfo.isCommunityAdmin);
48
50
 
49
51
  // Get current status
50
52
  const { isLogin, userInfo } = sdk.auth.getStatus();
53
+ if (isLogin) {
54
+ console.log(userInfo.roleList);
55
+ console.log(userInfo.isCommunityAdmin);
56
+ }
51
57
 
52
58
  // Logout
53
59
  await sdk.auth.logout();
@@ -106,7 +112,7 @@ pnpm type-check
106
112
  pnpm test:workbench
107
113
  ```
108
114
 
109
- `pnpm deploy:cdn` uploads both `generator-sdk` and `generator-workbench` CDN assets to `static-res.atomm.com`.
115
+ `pnpm deploy:cdn` uploads the `generator-sdk` bundle, the `generator-workbench` UMD asset, the `generator-workbench/upgrade-manifest.json`, and the `generator-workbench` runtime dependencies under `atomm-pro/dist-browser` to `static-res.atomm.com`.
110
116
 
111
117
  If you need to publish both packages from the repository root, use:
112
118
 
@@ -1,7 +1,7 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- import { t as toUtf8, f as fromUtf8 } from "./index-T0kU38cY.js";
4
+ import { t as toUtf8, f as fromUtf8 } from "./index-DuQGCcOW.js";
5
5
  class EventStreamSerde {
6
6
  constructor({ marshaller, serializer, deserializer, serdeContext, defaultContentType }) {
7
7
  __publicField(this, "marshaller");
@@ -2627,23 +2627,41 @@ const getBaseURL = (env) => {
2627
2627
  return BASE_URL_MAP[env] ?? BASE_URL_MAP.prod;
2628
2628
  };
2629
2629
  const TOKEN_KEY_PREFIX = "__atomm_sdk_token__";
2630
+ const TOKEN_COOKIE_DOMAIN = "";
2631
+ const TOKEN_COOKIE_PATH = "/";
2632
+ const TOKEN_COOKIE_MAX_AGE_SECONDS = 15 * 24 * 60 * 60;
2630
2633
  const getTokenKey = (appKey) => `${TOKEN_KEY_PREFIX}${appKey}`;
2634
+ const buildCookieBase = () => `Domain=${TOKEN_COOKIE_DOMAIN}; Path=${TOKEN_COOKIE_PATH}; SameSite=Lax`;
2631
2635
  const setToken = (appKey, token) => {
2632
2636
  try {
2633
- localStorage.setItem(getTokenKey(appKey), token);
2637
+ document.cookie = [
2638
+ `${getTokenKey(appKey)}=${encodeURIComponent(token)}`,
2639
+ buildCookieBase(),
2640
+ `Max-Age=${TOKEN_COOKIE_MAX_AGE_SECONDS}`
2641
+ ].join("; ");
2634
2642
  } catch {
2635
2643
  }
2636
2644
  };
2637
2645
  const getToken = (appKey) => {
2638
2646
  try {
2639
- return localStorage.getItem(getTokenKey(appKey)) || "";
2647
+ const tokenKey = getTokenKey(appKey);
2648
+ const tokenPair = document.cookie.split("; ").find((item) => item.startsWith(`${tokenKey}=`));
2649
+ if (!tokenPair) {
2650
+ return "";
2651
+ }
2652
+ return decodeURIComponent(tokenPair.slice(tokenKey.length + 1));
2640
2653
  } catch {
2641
2654
  return "";
2642
2655
  }
2643
2656
  };
2644
2657
  const clearToken = (appKey) => {
2645
2658
  try {
2646
- localStorage.removeItem(getTokenKey(appKey));
2659
+ document.cookie = [
2660
+ `${getTokenKey(appKey)}=`,
2661
+ buildCookieBase(),
2662
+ "Expires=Thu, 01 Jan 1970 00:00:00 GMT",
2663
+ "Max-Age=0"
2664
+ ].join("; ");
2647
2665
  } catch {
2648
2666
  }
2649
2667
  };
@@ -9990,7 +10008,7 @@ class AuthModule {
9990
10008
  // 公开 API
9991
10009
  // ─────────────────────────────────────────────
9992
10010
  /**
9993
- * 获取当前登录状态(同步,读本地缓存)
10011
+ * 获取当前登录状态(同步,读本地 cookie 缓存)
9994
10012
  */
9995
10013
  getStatus() {
9996
10014
  return { ...this._status };
@@ -10003,7 +10021,7 @@ class AuthModule {
10003
10021
  return getToken(this.appKey);
10004
10022
  }
10005
10023
  /**
10006
- * 外部传入 token,同步到本地缓存并刷新登录状态
10024
+ * 外部传入 token,同步到本地 cookie 缓存并刷新登录状态
10007
10025
  */
10008
10026
  async syncToken(token) {
10009
10027
  const normalizedToken = token.trim();
@@ -10069,7 +10087,7 @@ class AuthModule {
10069
10087
  // 内部方法
10070
10088
  // ─────────────────────────────────────────────
10071
10089
  /**
10072
- * 初始化时从 localStorage 恢复登录状态
10090
+ * 初始化时从 cookie 恢复登录状态
10073
10091
  * 如果 token 存在,尝试拉取用户信息验证有效性
10074
10092
  */
10075
10093
  async _restoreFromStorage() {
@@ -10089,11 +10107,17 @@ class AuthModule {
10089
10107
  */
10090
10108
  async _fetchUserInfo(token) {
10091
10109
  this.passportClient.token = token;
10092
- const response = await this.passportClient.userInfo();
10110
+ const [response, roleList] = await Promise.all([
10111
+ this.passportClient.userInfo(),
10112
+ this._fetchUserRoles()
10113
+ ]);
10093
10114
  const raw = (response == null ? void 0 : response.data) ?? response;
10094
10115
  if (!(raw == null ? void 0 : raw.uid)) {
10095
10116
  throw new Error("Invalid user info: missing uid");
10096
10117
  }
10118
+ const isCommunityAdmin = roleList.some(
10119
+ (item) => item.code === "community_admin"
10120
+ );
10097
10121
  return {
10098
10122
  uid: raw.uid,
10099
10123
  uuid: raw.uuid ?? String(raw.uid),
@@ -10104,9 +10128,24 @@ class AuthModule {
10104
10128
  phoneZone: raw.phoneZone ?? "",
10105
10129
  gender: raw.gender ?? 0,
10106
10130
  signature: raw.signature ?? "",
10107
- createTime: raw.createTime ?? 0
10131
+ createTime: raw.createTime ?? 0,
10132
+ roleList,
10133
+ isCommunityAdmin
10108
10134
  };
10109
10135
  }
10136
+ async _fetchUserRoles() {
10137
+ try {
10138
+ const roleList = await this.http.request({
10139
+ method: "GET",
10140
+ url: "/efficacy/v1/roles",
10141
+ params: { d: "xtool" }
10142
+ });
10143
+ return Array.isArray(roleList) ? roleList : [];
10144
+ } catch (error) {
10145
+ console.warn("[generator-sdk] fetch user roles failed", error);
10146
+ return [];
10147
+ }
10148
+ }
10110
10149
  _setLoginStatus(userInfo) {
10111
10150
  this._status = { isLogin: true, userInfo };
10112
10151
  this.emitter.emit("change", this.getStatus());
@@ -12287,7 +12326,7 @@ class HttpProtocol extends SerdeContext {
12287
12326
  });
12288
12327
  }
12289
12328
  async loadEventStreamCapability() {
12290
- const { EventStreamSerde } = await import("./index-DyTtU447.js");
12329
+ const { EventStreamSerde } = await import("./index-DcHLmTaB.js");
12291
12330
  return new EventStreamSerde({
12292
12331
  marshaller: this.getEventStreamMarshaller(),
12293
12332
  serializer: this.serializer,
package/dist/index.d.ts CHANGED
@@ -15,7 +15,7 @@ declare class AuthModule {
15
15
  private _status;
16
16
  constructor(appKey: string, env: SdkEnv, http: AxiosInstance);
17
17
  /**
18
- * 获取当前登录状态(同步,读本地缓存)
18
+ * 获取当前登录状态(同步,读本地 cookie 缓存)
19
19
  */
20
20
  getStatus(): AuthStatus;
21
21
  /**
@@ -24,7 +24,7 @@ declare class AuthModule {
24
24
  */
25
25
  getToken(): string;
26
26
  /**
27
- * 外部传入 token,同步到本地缓存并刷新登录状态
27
+ * 外部传入 token,同步到本地 cookie 缓存并刷新登录状态
28
28
  */
29
29
  syncToken(token: string): Promise<AuthStatus>;
30
30
  /**
@@ -41,7 +41,7 @@ declare class AuthModule {
41
41
  */
42
42
  onChange(callback: AuthChangeCallback): () => void;
43
43
  /**
44
- * 初始化时从 localStorage 恢复登录状态
44
+ * 初始化时从 cookie 恢复登录状态
45
45
  * 如果 token 存在,尝试拉取用户信息验证有效性
46
46
  */
47
47
  private _restoreFromStorage;
@@ -50,6 +50,7 @@ declare class AuthModule {
50
50
  * 接口响应格式:{ code: 0, data: { uid, userName, headpic, uuid, email, ... } }
51
51
  */
52
52
  private _fetchUserInfo;
53
+ private _fetchUserRoles;
53
54
  private _setLoginStatus;
54
55
  private _clearLoginStatus;
55
56
  }
@@ -745,6 +746,16 @@ export declare interface UserInfo {
745
746
  signature: string;
746
747
  /** 账号创建时间(Unix 秒) */
747
748
  createTime: number;
749
+ /** 用户角色列表 */
750
+ roleList: UserRole[];
751
+ /** 是否为社区管理员 */
752
+ isCommunityAdmin: boolean;
753
+ }
754
+
755
+ /** 用户信息 */
756
+ declare interface UserRole {
757
+ code: string;
758
+ [key: string]: unknown;
748
759
  }
749
760
 
750
761
  /**
package/dist/index.es.js CHANGED
@@ -1,4 +1,4 @@
1
- import { G, S, T, w } from "./index-T0kU38cY.js";
1
+ import { G, S, T, w } from "./index-DuQGCcOW.js";
2
2
  export {
3
3
  G as GeneratorSDK,
4
4
  S as SdkError,