lansenger-sdk-ts 1.1.0 → 1.2.0

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/auth.d.ts CHANGED
@@ -19,11 +19,13 @@ export declare class UserTokenManager {
19
19
  private userToken;
20
20
  private refreshToken;
21
21
  private userTokenExpiry;
22
+ private refreshTokenExpiry;
22
23
  private staffId;
23
24
  constructor(config: LansengerConfig, fetchFn: FetchFn, appTokenManager: TokenManager, store?: CredentialStore | null);
24
25
  getToken(): Promise<string>;
25
- setTokens(userToken: string, refreshToken: string, expiresIn?: number, staffId?: string): void;
26
+ setTokens(userToken: string, refreshToken: string, expiresIn?: number, staffId?: string, refreshExpiresIn?: number): void;
26
27
  get staff_id(): string | null;
27
28
  get refresh_token(): string | null;
29
+ get refresh_token_expiry(): number;
28
30
  invalidate(): void;
29
31
  }
package/dist/auth.js CHANGED
@@ -74,6 +74,7 @@ class UserTokenManager {
74
74
  this.userToken = null;
75
75
  this.refreshToken = null;
76
76
  this.userTokenExpiry = 0;
77
+ this.refreshTokenExpiry = 0;
77
78
  this.staffId = null;
78
79
  this.config = config;
79
80
  this.fetchFn = fetchFn;
@@ -88,6 +89,7 @@ class UserTokenManager {
88
89
  this.userToken = ut;
89
90
  this.refreshToken = rt;
90
91
  this.userTokenExpiry = expiry;
92
+ this.refreshTokenExpiry = cached.refresh_token_expiry || 0;
91
93
  }
92
94
  }
93
95
  }
@@ -117,14 +119,18 @@ class UserTokenManager {
117
119
  const tokenData = data.data || {};
118
120
  this.userToken = tokenData.userToken;
119
121
  const expiresIn = tokenData.expiresIn || 7200;
120
- this.refreshToken = tokenData.refreshToken;
122
+ const newRefreshToken = tokenData.refreshToken;
123
+ if (newRefreshToken) {
124
+ this.refreshToken = newRefreshToken;
125
+ }
121
126
  this.staffId = tokenData.staffId;
122
127
  this.userTokenExpiry = Math.floor(Date.now() / 1000) + expiresIn - USER_TOKEN_REFRESH_MARGIN;
123
128
  if (!this.userToken) {
124
129
  throw new exceptions_1.LansengerAuthError("Refresh response missing userToken field");
125
130
  }
126
131
  if (this.store) {
127
- this.store.saveUserToken(this.userToken, this.refreshToken || "", expiresIn);
132
+ const refreshExpiresIn = tokenData.refreshExpiresIn || 0;
133
+ this.store.saveUserToken(this.userToken, this.refreshToken || "", expiresIn, USER_TOKEN_REFRESH_MARGIN, refreshExpiresIn);
128
134
  }
129
135
  return this.userToken;
130
136
  }
@@ -134,14 +140,16 @@ class UserTokenManager {
134
140
  throw new exceptions_1.LansengerNetworkError(`userToken refresh failed: ${e instanceof Error ? e.message : String(e)}`);
135
141
  }
136
142
  }
137
- setTokens(userToken, refreshToken, expiresIn = 7200, staffId = "") {
143
+ setTokens(userToken, refreshToken, expiresIn = 7200, staffId = "", refreshExpiresIn = 0) {
138
144
  this.userToken = userToken;
139
145
  this.refreshToken = refreshToken;
140
146
  this.userTokenExpiry = Math.floor(Date.now() / 1000) + expiresIn - USER_TOKEN_REFRESH_MARGIN;
147
+ if (refreshExpiresIn)
148
+ this.refreshTokenExpiry = Math.floor(Date.now() / 1000) + refreshExpiresIn;
141
149
  if (staffId)
142
150
  this.staffId = staffId;
143
151
  if (this.store) {
144
- this.store.saveUserToken(userToken, refreshToken, expiresIn);
152
+ this.store.saveUserToken(userToken, refreshToken, expiresIn, USER_TOKEN_REFRESH_MARGIN, refreshExpiresIn);
145
153
  }
146
154
  }
147
155
  get staff_id() {
@@ -150,6 +158,9 @@ class UserTokenManager {
150
158
  get refresh_token() {
151
159
  return this.refreshToken;
152
160
  }
161
+ get refresh_token_expiry() {
162
+ return this.refreshTokenExpiry;
163
+ }
153
164
  invalidate() {
154
165
  this.userToken = null;
155
166
  this.userTokenExpiry = 0;
package/dist/chats.js CHANGED
@@ -9,13 +9,13 @@ async function fetchChatList(config, appToken, opts) {
9
9
  const userToken = opts?.user_token || "";
10
10
  const url = (0, urlHelpers_1.buildApiUrl)(config, "chats", "fetch", appToken, { userToken });
11
11
  const payload = {};
12
- if (opts?.chat_type)
12
+ if (opts?.chat_type != null)
13
13
  payload.chatType = opts.chat_type;
14
14
  if (opts?.keyword)
15
15
  payload.keyword = opts.keyword;
16
- if (opts?.start_time)
16
+ if (opts?.start_time != null)
17
17
  payload.startTime = opts.start_time;
18
- if (opts?.end_time)
18
+ if (opts?.end_time != null)
19
19
  payload.endTime = opts.end_time;
20
20
  const [data, httpErr] = await (0, http_1.doPost)(url, payload, opts?.fetchFn);
21
21
  if (httpErr)
@@ -54,9 +54,9 @@ async function fetchChatMessages(config, appToken, opts) {
54
54
  payload.staffId = opts.staff_id;
55
55
  if (opts?.group_id)
56
56
  payload.groupId = opts.group_id;
57
- if (opts?.start_time)
57
+ if (opts?.start_time != null)
58
58
  payload.startTime = opts.start_time;
59
- if (opts?.end_time)
59
+ if (opts?.end_time != null)
60
60
  payload.endTime = opts.end_time;
61
61
  if (opts?.sender_id)
62
62
  payload.senderId = opts.sender_id;
package/dist/client.js CHANGED
@@ -432,9 +432,9 @@ class LansengerClient {
432
432
  const result = await (0, oauth_1.exchangeCodeForUserToken)(this._config, token, code, { redirect_uri: opts?.redirect_uri, fetchFn: this._fetchFn });
433
433
  if (result.success) {
434
434
  if (this._store) {
435
- this._store.saveUserToken(result.user_token || "", result.refresh_token || "", result.expires_in);
435
+ this._store.saveUserToken(result.user_token || "", result.refresh_token || "", result.expires_in, undefined, result.refresh_expires_in || 0);
436
436
  }
437
- this._userTokenManager.setTokens(result.user_token || "", result.refresh_token || "", result.expires_in, result.staff_id || "");
437
+ this._userTokenManager.setTokens(result.user_token || "", result.refresh_token || "", result.expires_in, result.staff_id || "", result.refresh_expires_in || 0);
438
438
  }
439
439
  return result;
440
440
  }
@@ -27,4 +27,4 @@ export declare const REMINDER_TYPE_PHONE = 3;
27
27
  export declare const CALLBACK_EVENT_TYPES: Record<string, string>;
28
28
  export declare function guessMediaType(filePath: string): number;
29
29
  export declare function guessAppMediaType(filePath: string): string;
30
- export declare const VERSION = "1.4.1";
30
+ export declare const VERSION = "1.1.0";
package/dist/constants.js CHANGED
@@ -139,31 +139,31 @@ exports.REMINDER_TYPE_POPUP = 1;
139
139
  exports.REMINDER_TYPE_SMS = 2;
140
140
  exports.REMINDER_TYPE_PHONE = 3;
141
141
  exports.CALLBACK_EVENT_TYPES = {
142
+ account_message: "public_account",
142
143
  account_subscribe: "public_account",
143
144
  account_unsubscribe: "public_account",
144
- account_message: "public_account",
145
145
  staff_info: "staff",
146
146
  staff_modify: "staff",
147
147
  staff_create: "staff",
148
148
  staff_delete: "staff",
149
- telephone_track: "telephone_track",
150
- dept_create: "department",
151
149
  dept_modify: "department",
150
+ dept_create: "department",
152
151
  dept_delete: "department",
153
152
  tag_member: "tag",
154
- app_install_org: "application",
155
- app_uninstall_org: "application",
153
+ app_install_org: "app",
154
+ app_uninstall_org: "app",
156
155
  bot_private_message: "bot",
157
156
  bot_group_message: "bot",
158
157
  group_create_approve: "group",
159
- ua_cert_create: "ua_cert",
160
- ua_cert_delete: "ua_cert",
158
+ telephone_track: "notification",
159
+ ua_cert_create: "certificate",
160
+ ua_cert_delete: "certificate",
161
161
  report_location: "location",
162
- user_logout: "user",
162
+ user_logout: "auth",
163
163
  data_scope: "data_scope",
164
164
  wb_visible_config: "workbench",
165
- schedule_modify: "schedule",
166
- schedule_delete: "schedule",
165
+ schedule_modify: "calendar",
166
+ schedule_delete: "calendar",
167
167
  };
168
168
  function guessMediaType(filePath) {
169
169
  const ext = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
@@ -183,5 +183,5 @@ function guessAppMediaType(filePath) {
183
183
  return exports.APP_MEDIA_TYPE_AUDIO;
184
184
  return exports.APP_MEDIA_TYPE_FILE;
185
185
  }
186
- exports.VERSION = "1.4.1";
186
+ exports.VERSION = "1.1.0";
187
187
  //# sourceMappingURL=constants.js.map
package/dist/oauth.js CHANGED
@@ -87,7 +87,9 @@ function parseAuthorizeCallback(queryString) {
87
87
  params = {};
88
88
  for (const part of queryString.replace(/^\?/, "").split("&")) {
89
89
  if (part.includes("=")) {
90
- const [key, value] = part.split("=", 1);
90
+ const eqIdx = part.indexOf("=");
91
+ const key = part.substring(0, eqIdx);
92
+ const value = part.substring(eqIdx + 1);
91
93
  params[key] = value || "";
92
94
  }
93
95
  }
@@ -15,7 +15,7 @@ export declare class CredentialStore {
15
15
  loadAppToken(): string | null;
16
16
  saveAppToken(token: string, expiresIn?: number, margin?: number): void;
17
17
  loadUserToken(): Record<string, any>;
18
- saveUserToken(userToken: string, refreshToken?: string, expiresIn?: number): void;
18
+ saveUserToken(userToken: string, refreshToken?: string, expiresIn?: number, margin?: number, refreshExpiresIn?: number): void;
19
19
  clearProfile(): void;
20
20
  clear(): void;
21
21
  listProfiles(): string[];
@@ -160,15 +160,18 @@ class CredentialStore {
160
160
  user_token: data.user_token || "",
161
161
  refresh_token: data.refresh_token || "",
162
162
  user_token_expiry: data.user_token_expiry || 0,
163
+ refresh_token_expiry: data.refresh_token_expiry || 0,
163
164
  };
164
165
  }
165
- saveUserToken(userToken, refreshToken = "", expiresIn = 0) {
166
+ saveUserToken(userToken, refreshToken = "", expiresIn = 0, margin = 300, refreshExpiresIn = 0) {
166
167
  const state = this.load();
167
168
  const data = this.getProfileData(state);
168
169
  data.user_token = userToken;
169
170
  data.refresh_token = refreshToken;
170
171
  if (expiresIn)
171
- data.user_token_expiry = Date.now() / 1000 + expiresIn;
172
+ data.user_token_expiry = Date.now() / 1000 + expiresIn - margin;
173
+ if (refreshExpiresIn)
174
+ data.refresh_token_expiry = Date.now() / 1000 + refreshExpiresIn;
172
175
  this.save(this.setProfileData(state, data));
173
176
  }
174
177
  clearProfile() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lansenger-sdk-ts",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Lansenger SDK — TypeScript SDK for Lansenger Smart Bot API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",