@ctil/gql 1.1.7 → 1.1.8

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.cjs CHANGED
@@ -4516,6 +4516,7 @@ var CCRequest = class {
4516
4516
  this._remember = false;
4517
4517
  this.interceptors = [];
4518
4518
  this.refreshPromise = null;
4519
+ this.logoutPromise = null;
4519
4520
  this.deviceInfoPromise = getDeviceInfo();
4520
4521
  this.headers = this.buildHeaders(config);
4521
4522
  const loginInfo = this.loadLoginInfo();
@@ -4657,65 +4658,79 @@ var CCRequest = class {
4657
4658
  clearHeaders() {
4658
4659
  this.headers = { "Content-Type": "application/json" };
4659
4660
  }
4660
- //无感刷新
4661
+ async logoutOnce() {
4662
+ if (!this.logoutPromise) {
4663
+ this.logoutPromise = (async () => {
4664
+ await this.removeLoginInfo();
4665
+ })();
4666
+ }
4667
+ return this.logoutPromise;
4668
+ }
4661
4669
  /** 无感刷新 token */
4662
4670
  async ensureTokenValid() {
4663
- const loginInfo = this.getLoginInfo();
4671
+ const loginInfo = await this.getLoginInfo();
4664
4672
  if (!loginInfo) return;
4665
4673
  const now = Date.now();
4666
- const accessExpired = new Date(loginInfo.expireAt).getTime() - now <= 6e5;
4674
+ const accessExpire = new Date(loginInfo.expireAt).getTime() <= now + 6e5;
4667
4675
  const refreshExpired = new Date(loginInfo.refreshExpireAt).getTime() <= now;
4668
4676
  if (refreshExpired) {
4669
- this.removeLoginInfo();
4677
+ await this.logoutOnce();
4670
4678
  throw new Error("Login expired. Please login again.");
4671
4679
  }
4672
- if (!accessExpired) return;
4673
- if (this.refreshPromise) {
4674
- return this.refreshPromise;
4680
+ if (accessExpire) {
4681
+ await this.refreshToken();
4682
+ }
4683
+ }
4684
+ async refreshToken() {
4685
+ const loginInfo = await this.getLoginInfo();
4686
+ if (!loginInfo) return;
4687
+ if (!this.refreshPromise) {
4688
+ this.refreshPromise = (async () => {
4689
+ try {
4690
+ const newToken = await auth.refreshToken({
4691
+ refreshToken: loginInfo.refreshToken,
4692
+ remember: this._remember
4693
+ });
4694
+ this.setToken(newToken.refreshToken.token);
4695
+ } catch (e) {
4696
+ await this.logoutOnce();
4697
+ throw e;
4698
+ } finally {
4699
+ this.refreshPromise = null;
4700
+ }
4701
+ })();
4675
4702
  }
4676
- this.refreshPromise = (async () => {
4677
- try {
4678
- const result = await auth.refreshToken({
4679
- refreshToken: loginInfo.refreshToken,
4680
- remember: this._remember
4681
- });
4682
- const newInfo = result.refreshToken ?? result;
4683
- this.setLoginInfo(newInfo, this._remember);
4684
- } finally {
4685
- this.refreshPromise = null;
4686
- }
4687
- })();
4688
4703
  return this.refreshPromise;
4689
4704
  }
4690
4705
  // ===== 请求逻辑 =====
4691
4706
  async request(query2, variables) {
4692
4707
  let queryStr = typeof query2 === "string" ? query2 : print(query2);
4693
- const { deviceId, deviceName } = await this.deviceInfoPromise;
4694
- let headersWithDevice = Object.fromEntries(
4695
- Object.entries({
4696
- ...this.headers,
4697
- "X-Device-Id": deviceId,
4698
- "X-Device-Name": deviceName
4699
- }).filter(([_, v]) => v !== void 0)
4700
- );
4701
- for (const interceptor of this.interceptors) {
4702
- if (interceptor.onRequest) {
4703
- const result = await interceptor.onRequest({
4704
- query: queryStr,
4705
- variables,
4706
- headers: headersWithDevice
4707
- });
4708
- queryStr = result.query;
4709
- variables = result.variables;
4710
- headersWithDevice = Object.fromEntries(
4711
- Object.entries(result.headers).filter(([_, v]) => v !== void 0)
4712
- );
4713
- }
4714
- }
4715
4708
  try {
4716
4709
  if (!/refreshToken/i.test(queryStr)) {
4717
4710
  await this.ensureTokenValid();
4718
4711
  }
4712
+ const { deviceId, deviceName } = await this.deviceInfoPromise;
4713
+ let headersWithDevice = Object.fromEntries(
4714
+ Object.entries({
4715
+ ...this.headers,
4716
+ "X-Device-Id": deviceId,
4717
+ "X-Device-Name": deviceName
4718
+ }).filter(([_, v]) => v !== void 0)
4719
+ );
4720
+ for (const interceptor of this.interceptors) {
4721
+ if (interceptor.onRequest) {
4722
+ const result = await interceptor.onRequest({
4723
+ query: queryStr,
4724
+ variables,
4725
+ headers: headersWithDevice
4726
+ });
4727
+ queryStr = result.query;
4728
+ variables = result.variables;
4729
+ headersWithDevice = Object.fromEntries(
4730
+ Object.entries(result.headers).filter(([_, v]) => v !== void 0)
4731
+ );
4732
+ }
4733
+ }
4719
4734
  const res = await this.client.rawRequest(
4720
4735
  queryStr,
4721
4736
  variables,
@@ -4731,6 +4746,14 @@ var CCRequest = class {
4731
4746
  } catch (err) {
4732
4747
  const message = err.response?.errors?.[0]?.message ?? err.message;
4733
4748
  const status = err.response?.errors?.[0]?.extensions?.code ?? 500;
4749
+ if (status === 401 && !/refreshToken/i.test(queryStr)) {
4750
+ try {
4751
+ await this.refreshToken();
4752
+ return this.request(queryStr, variables);
4753
+ } catch {
4754
+ await this.logoutOnce();
4755
+ }
4756
+ }
4734
4757
  const formattedError = {
4735
4758
  message,
4736
4759
  status,