@ctil/gql 1.0.1 → 1.0.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/dist/index.d.cts CHANGED
@@ -58,6 +58,7 @@ interface UserToken {
58
58
 
59
59
  declare class CCRequest {
60
60
  private config;
61
+ private _remember;
61
62
  private client;
62
63
  private headers;
63
64
  private deviceInfoPromise;
@@ -72,8 +73,8 @@ declare class CCRequest {
72
73
  setToken(token: string): void;
73
74
  removeToken(): void;
74
75
  setLoginInfo(loginInfo: UserToken, remember?: boolean): void;
75
- /** 加载登录信息(含访问 token 和刷新 token 过期检查) */
76
76
  /** 加载登录信息(仅负责加载,不负责刷新) */
77
+ /** 加载登录信息 */
77
78
  loadLoginInfo(): UserToken | null;
78
79
  getLoginInfo(): UserToken | null;
79
80
  removeLoginInfo(): void;
@@ -82,6 +83,7 @@ declare class CCRequest {
82
83
  setHeaders(headers: Record<string, string>): void;
83
84
  removeHeader(key: string): void;
84
85
  clearHeaders(): void;
86
+ /** 无感刷新 token */
85
87
  private ensureTokenValid;
86
88
  request<T = any>(query: string | DocumentNode, variables?: Record<string, any>): Promise<T>;
87
89
  }
package/dist/index.d.ts CHANGED
@@ -58,6 +58,7 @@ interface UserToken {
58
58
 
59
59
  declare class CCRequest {
60
60
  private config;
61
+ private _remember;
61
62
  private client;
62
63
  private headers;
63
64
  private deviceInfoPromise;
@@ -72,8 +73,8 @@ declare class CCRequest {
72
73
  setToken(token: string): void;
73
74
  removeToken(): void;
74
75
  setLoginInfo(loginInfo: UserToken, remember?: boolean): void;
75
- /** 加载登录信息(含访问 token 和刷新 token 过期检查) */
76
76
  /** 加载登录信息(仅负责加载,不负责刷新) */
77
+ /** 加载登录信息 */
77
78
  loadLoginInfo(): UserToken | null;
78
79
  getLoginInfo(): UserToken | null;
79
80
  removeLoginInfo(): void;
@@ -82,6 +83,7 @@ declare class CCRequest {
82
83
  setHeaders(headers: Record<string, string>): void;
83
84
  removeHeader(key: string): void;
84
85
  clearHeaders(): void;
86
+ /** 无感刷新 token */
85
87
  private ensureTokenValid;
86
88
  request<T = any>(query: string | DocumentNode, variables?: Record<string, any>): Promise<T>;
87
89
  }
package/dist/index.js CHANGED
@@ -1474,11 +1474,14 @@ import path from "path";
1474
1474
  var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
1475
1475
  var isNode2 = typeof process !== "undefined" && process.versions?.node != null;
1476
1476
  var NODE_LOGIN_FILE = path.resolve(process.cwd(), "loginInfo.json");
1477
+ var NODE_LOGIN_REMEBER_FILE = path.resolve(process.cwd(), "loginInfo.json.remember");
1477
1478
  var STORAGE_KEY = "GRAPHQL_LOGIN_INFO";
1479
+ var REMEMBER_KEY = "GRAPHQL_LOGIN_REMEMBER";
1478
1480
  var nodeLoginInfo = null;
1479
1481
  var CCRequest = class {
1480
1482
  constructor(config) {
1481
1483
  this.config = config;
1484
+ this._remember = false;
1482
1485
  this.interceptors = [];
1483
1486
  this.deviceInfoPromise = getDeviceInfo();
1484
1487
  this.headers = this.buildHeaders(config);
@@ -1520,22 +1523,24 @@ var CCRequest = class {
1520
1523
  }
1521
1524
  // ===== LoginInfo 管理 =====
1522
1525
  setLoginInfo(loginInfo, remember = false) {
1523
- this.config.loginInfo = loginInfo;
1526
+ this._remember = remember;
1524
1527
  if (loginInfo.token) this.setToken(loginInfo.token);
1525
1528
  if (isBrowser) {
1526
1529
  const storage = remember ? localStorage : sessionStorage;
1527
1530
  storage.setItem(STORAGE_KEY, JSON.stringify(loginInfo));
1531
+ localStorage.setItem(REMEMBER_KEY, JSON.stringify(remember));
1528
1532
  } else if (isNode2) {
1529
1533
  nodeLoginInfo = loginInfo;
1530
1534
  try {
1531
1535
  fs.writeFileSync(NODE_LOGIN_FILE, JSON.stringify(loginInfo), "utf-8");
1536
+ fs.writeFileSync(NODE_LOGIN_REMEBER_FILE, JSON.stringify(remember), "utf-8");
1532
1537
  } catch (err) {
1533
1538
  console.error("Failed to persist login info to file:", err);
1534
1539
  }
1535
1540
  }
1536
1541
  }
1537
- /** 加载登录信息(含访问 token 和刷新 token 过期检查) */
1538
1542
  /** 加载登录信息(仅负责加载,不负责刷新) */
1543
+ /** 加载登录信息 */
1539
1544
  loadLoginInfo() {
1540
1545
  let info = null;
1541
1546
  if (isBrowser) {
@@ -1544,17 +1549,17 @@ var CCRequest = class {
1544
1549
  try {
1545
1550
  info = JSON.parse(str);
1546
1551
  } catch {
1547
- info = null;
1548
1552
  }
1549
1553
  }
1554
+ const rememberStr = localStorage.getItem(REMEMBER_KEY);
1555
+ this._remember = rememberStr === "true";
1550
1556
  } else if (isNode2) {
1551
1557
  if (nodeLoginInfo) return nodeLoginInfo;
1552
1558
  if (fs.existsSync(NODE_LOGIN_FILE)) {
1553
1559
  try {
1554
- const raw = fs.readFileSync(NODE_LOGIN_FILE, "utf-8");
1555
- info = JSON.parse(raw);
1560
+ info = JSON.parse(fs.readFileSync(NODE_LOGIN_FILE, "utf-8"));
1561
+ this._remember = fs.existsSync(NODE_LOGIN_REMEBER_FILE) ? JSON.parse(fs.readFileSync(NODE_LOGIN_REMEBER_FILE, "utf-8")) : false;
1556
1562
  } catch {
1557
- info = null;
1558
1563
  }
1559
1564
  }
1560
1565
  }
@@ -1573,6 +1578,8 @@ var CCRequest = class {
1573
1578
  } else if (isNode2) {
1574
1579
  nodeLoginInfo = null;
1575
1580
  try {
1581
+ if (fs.existsSync(NODE_LOGIN_FILE)) fs.unlinkSync(NODE_LOGIN_FILE);
1582
+ if (fs.existsSync(NODE_LOGIN_REMEBER_FILE)) fs.unlinkSync(NODE_LOGIN_REMEBER_FILE);
1576
1583
  } catch {
1577
1584
  }
1578
1585
  }
@@ -1596,6 +1603,7 @@ var CCRequest = class {
1596
1603
  this.headers = { "Content-Type": "application/json" };
1597
1604
  }
1598
1605
  //无感刷新
1606
+ /** 无感刷新 token */
1599
1607
  async ensureTokenValid() {
1600
1608
  const loginInfo = this.getLoginInfo();
1601
1609
  if (!loginInfo) return;
@@ -1610,11 +1618,12 @@ var CCRequest = class {
1610
1618
  try {
1611
1619
  const refreshResult = await auth.refreshToken({
1612
1620
  refreshToken: loginInfo.refreshToken,
1613
- remember: true
1621
+ remember: this._remember
1622
+ // 使用持久化的 remember
1614
1623
  });
1615
1624
  const newInfo = refreshResult.refreshToken ?? refreshResult;
1616
- this.setLoginInfo(newInfo, true);
1617
- } catch (err) {
1625
+ this.setLoginInfo(newInfo, this._remember);
1626
+ } catch {
1618
1627
  this.removeLoginInfo();
1619
1628
  throw new Error("Failed to refresh token. Please login again.");
1620
1629
  }