@larksuiteoapi/node-sdk 1.9.0 → 1.10.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/es/index.js CHANGED
@@ -114,9 +114,17 @@ class DefaultCache {
114
114
  constructor() {
115
115
  this.values = new Map();
116
116
  }
117
- get(key) {
117
+ // When there is a namespace, splice the namespace and key to form a new key
118
+ getCacheKey(key, namespace) {
119
+ if (namespace) {
120
+ return `${namespace}/${key.toString()}`;
121
+ }
122
+ return key;
123
+ }
124
+ get(key, options) {
118
125
  return __awaiter(this, void 0, void 0, function* () {
119
- const data = this.values.get(key);
126
+ const cacheKey = this.getCacheKey(key, get(options, 'namespace'));
127
+ const data = this.values.get(cacheKey);
120
128
  if (data) {
121
129
  const { value, expiredTime } = data;
122
130
  if (!expiredTime || expiredTime - new Date().getTime() > 0) {
@@ -126,9 +134,10 @@ class DefaultCache {
126
134
  return undefined;
127
135
  });
128
136
  }
129
- set(key, value, expiredTime) {
137
+ set(key, value, expiredTime, options) {
130
138
  return __awaiter(this, void 0, void 0, function* () {
131
- this.values.set(key, {
139
+ const cacheKey = this.getCacheKey(key, get(options, 'namespace'));
140
+ this.values.set(cacheKey, {
132
141
  value,
133
142
  expiredTime,
134
143
  });
@@ -20400,7 +20409,9 @@ class AppTicketManager {
20400
20409
  var _a;
20401
20410
  return __awaiter(this, void 0, void 0, function* () {
20402
20411
  if (this.appType === AppType.ISV) {
20403
- const appTicket = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CAppTicket));
20412
+ const appTicket = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CAppTicket, {
20413
+ namespace: this.appId
20414
+ }));
20404
20415
  if (!appTicket) {
20405
20416
  this.requestAppTicket();
20406
20417
  }
@@ -20423,7 +20434,9 @@ class AppTicketManager {
20423
20434
  getAppTicket() {
20424
20435
  var _a;
20425
20436
  return __awaiter(this, void 0, void 0, function* () {
20426
- const appTicket = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CAppTicket));
20437
+ const appTicket = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CAppTicket, {
20438
+ namespace: this.appId
20439
+ }));
20427
20440
  if (appTicket) {
20428
20441
  this.logger.debug('use cache app ticket');
20429
20442
  return appTicket;
@@ -20455,7 +20468,9 @@ class TokenManager {
20455
20468
  getCustomTenantAccessToken() {
20456
20469
  var _a, _b;
20457
20470
  return __awaiter(this, void 0, void 0, function* () {
20458
- const cachedTenantAccessToken = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CTenantAccessToken));
20471
+ const cachedTenantAccessToken = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CTenantAccessToken, {
20472
+ namespace: this.appId
20473
+ }));
20459
20474
  if (cachedTenantAccessToken) {
20460
20475
  this.logger.debug('use cache token');
20461
20476
  return cachedTenantAccessToken;
@@ -20472,7 +20487,9 @@ class TokenManager {
20472
20487
  });
20473
20488
  yield ((_b = this.cache) === null || _b === void 0 ? void 0 : _b.set(CTenantAccessToken, tenant_access_token,
20474
20489
  // Due to the time-consuming network, the expiration time needs to be 3 minutes earlier
20475
- new Date().getTime() + expire * 1000 - 3 * 60 * 1000));
20490
+ new Date().getTime() + expire * 1000 - 3 * 60 * 1000, {
20491
+ namespace: this.appId
20492
+ }));
20476
20493
  return tenant_access_token;
20477
20494
  });
20478
20495
  }
@@ -20483,7 +20500,9 @@ class TokenManager {
20483
20500
  this.logger.error('market app request need tenant key');
20484
20501
  return undefined;
20485
20502
  }
20486
- const tenantAccessToken = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(`larkMarketAccessToken${tenantKey}`));
20503
+ const tenantAccessToken = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(`larkMarketAccessToken${tenantKey}`, {
20504
+ namespace: this.appId
20505
+ }));
20487
20506
  if (tenantAccessToken) {
20488
20507
  this.logger.debug('use cache token');
20489
20508
  return tenantAccessToken;
@@ -20520,7 +20539,9 @@ class TokenManager {
20520
20539
  // 设置tenant_access_token
20521
20540
  yield this.cache.set(`larkMarketAccessToken${tenantKey}`, tenant_access_token,
20522
20541
  // Due to the time-consuming network, the expiration time needs to be 3 minutes earlier
20523
- new Date().getTime() + expire * 1000 - 3 * 60 * 1000);
20542
+ new Date().getTime() + expire * 1000 - 3 * 60 * 1000, {
20543
+ namespace: this.appId
20544
+ });
20524
20545
  return tenant_access_token;
20525
20546
  });
20526
20547
  }
@@ -20746,9 +20767,11 @@ class EventDispatcher {
20746
20767
  registerAppTicketHandle() {
20747
20768
  this.register({
20748
20769
  app_ticket: (data) => __awaiter(this, void 0, void 0, function* () {
20749
- const { app_ticket } = data;
20770
+ const { app_ticket, app_id } = data;
20750
20771
  if (app_ticket) {
20751
- yield this.cache.set(CAppTicket, app_ticket);
20772
+ yield this.cache.set(CAppTicket, app_ticket, undefined, {
20773
+ namespace: app_id
20774
+ });
20752
20775
  this.logger.debug('set app ticket');
20753
20776
  }
20754
20777
  else {
@@ -20809,9 +20832,11 @@ class CardActionHandler {
20809
20832
  registerAppTicketHandle() {
20810
20833
  this.register({
20811
20834
  app_ticket: (data) => __awaiter(this, void 0, void 0, function* () {
20812
- const { app_ticket } = data;
20835
+ const { app_ticket, app_id } = data;
20813
20836
  if (app_ticket) {
20814
- yield this.cache.set(CAppTicket, app_ticket);
20837
+ yield this.cache.set(CAppTicket, app_ticket, undefined, {
20838
+ namespace: app_id
20839
+ });
20815
20840
  this.logger.debug('set app ticket');
20816
20841
  }
20817
20842
  else {
package/lib/index.js CHANGED
@@ -129,9 +129,17 @@ class DefaultCache {
129
129
  constructor() {
130
130
  this.values = new Map();
131
131
  }
132
- get(key) {
132
+ // When there is a namespace, splice the namespace and key to form a new key
133
+ getCacheKey(key, namespace) {
134
+ if (namespace) {
135
+ return `${namespace}/${key.toString()}`;
136
+ }
137
+ return key;
138
+ }
139
+ get(key, options) {
133
140
  return __awaiter(this, void 0, void 0, function* () {
134
- const data = this.values.get(key);
141
+ const cacheKey = this.getCacheKey(key, get__default["default"](options, 'namespace'));
142
+ const data = this.values.get(cacheKey);
135
143
  if (data) {
136
144
  const { value, expiredTime } = data;
137
145
  if (!expiredTime || expiredTime - new Date().getTime() > 0) {
@@ -141,9 +149,10 @@ class DefaultCache {
141
149
  return undefined;
142
150
  });
143
151
  }
144
- set(key, value, expiredTime) {
152
+ set(key, value, expiredTime, options) {
145
153
  return __awaiter(this, void 0, void 0, function* () {
146
- this.values.set(key, {
154
+ const cacheKey = this.getCacheKey(key, get__default["default"](options, 'namespace'));
155
+ this.values.set(cacheKey, {
147
156
  value,
148
157
  expiredTime,
149
158
  });
@@ -20415,7 +20424,9 @@ class AppTicketManager {
20415
20424
  var _a;
20416
20425
  return __awaiter(this, void 0, void 0, function* () {
20417
20426
  if (this.appType === exports.AppType.ISV) {
20418
- const appTicket = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CAppTicket));
20427
+ const appTicket = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CAppTicket, {
20428
+ namespace: this.appId
20429
+ }));
20419
20430
  if (!appTicket) {
20420
20431
  this.requestAppTicket();
20421
20432
  }
@@ -20438,7 +20449,9 @@ class AppTicketManager {
20438
20449
  getAppTicket() {
20439
20450
  var _a;
20440
20451
  return __awaiter(this, void 0, void 0, function* () {
20441
- const appTicket = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CAppTicket));
20452
+ const appTicket = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CAppTicket, {
20453
+ namespace: this.appId
20454
+ }));
20442
20455
  if (appTicket) {
20443
20456
  this.logger.debug('use cache app ticket');
20444
20457
  return appTicket;
@@ -20470,7 +20483,9 @@ class TokenManager {
20470
20483
  getCustomTenantAccessToken() {
20471
20484
  var _a, _b;
20472
20485
  return __awaiter(this, void 0, void 0, function* () {
20473
- const cachedTenantAccessToken = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CTenantAccessToken));
20486
+ const cachedTenantAccessToken = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(CTenantAccessToken, {
20487
+ namespace: this.appId
20488
+ }));
20474
20489
  if (cachedTenantAccessToken) {
20475
20490
  this.logger.debug('use cache token');
20476
20491
  return cachedTenantAccessToken;
@@ -20487,7 +20502,9 @@ class TokenManager {
20487
20502
  });
20488
20503
  yield ((_b = this.cache) === null || _b === void 0 ? void 0 : _b.set(CTenantAccessToken, tenant_access_token,
20489
20504
  // Due to the time-consuming network, the expiration time needs to be 3 minutes earlier
20490
- new Date().getTime() + expire * 1000 - 3 * 60 * 1000));
20505
+ new Date().getTime() + expire * 1000 - 3 * 60 * 1000, {
20506
+ namespace: this.appId
20507
+ }));
20491
20508
  return tenant_access_token;
20492
20509
  });
20493
20510
  }
@@ -20498,7 +20515,9 @@ class TokenManager {
20498
20515
  this.logger.error('market app request need tenant key');
20499
20516
  return undefined;
20500
20517
  }
20501
- const tenantAccessToken = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(`larkMarketAccessToken${tenantKey}`));
20518
+ const tenantAccessToken = yield ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(`larkMarketAccessToken${tenantKey}`, {
20519
+ namespace: this.appId
20520
+ }));
20502
20521
  if (tenantAccessToken) {
20503
20522
  this.logger.debug('use cache token');
20504
20523
  return tenantAccessToken;
@@ -20535,7 +20554,9 @@ class TokenManager {
20535
20554
  // 设置tenant_access_token
20536
20555
  yield this.cache.set(`larkMarketAccessToken${tenantKey}`, tenant_access_token,
20537
20556
  // Due to the time-consuming network, the expiration time needs to be 3 minutes earlier
20538
- new Date().getTime() + expire * 1000 - 3 * 60 * 1000);
20557
+ new Date().getTime() + expire * 1000 - 3 * 60 * 1000, {
20558
+ namespace: this.appId
20559
+ });
20539
20560
  return tenant_access_token;
20540
20561
  });
20541
20562
  }
@@ -20761,9 +20782,11 @@ class EventDispatcher {
20761
20782
  registerAppTicketHandle() {
20762
20783
  this.register({
20763
20784
  app_ticket: (data) => __awaiter(this, void 0, void 0, function* () {
20764
- const { app_ticket } = data;
20785
+ const { app_ticket, app_id } = data;
20765
20786
  if (app_ticket) {
20766
- yield this.cache.set(CAppTicket, app_ticket);
20787
+ yield this.cache.set(CAppTicket, app_ticket, undefined, {
20788
+ namespace: app_id
20789
+ });
20767
20790
  this.logger.debug('set app ticket');
20768
20791
  }
20769
20792
  else {
@@ -20824,9 +20847,11 @@ class CardActionHandler {
20824
20847
  registerAppTicketHandle() {
20825
20848
  this.register({
20826
20849
  app_ticket: (data) => __awaiter(this, void 0, void 0, function* () {
20827
- const { app_ticket } = data;
20850
+ const { app_ticket, app_id } = data;
20828
20851
  if (app_ticket) {
20829
- yield this.cache.set(CAppTicket, app_ticket);
20852
+ yield this.cache.set(CAppTicket, app_ticket, undefined, {
20853
+ namespace: app_id
20854
+ });
20830
20855
  this.logger.debug('set app ticket');
20831
20856
  }
20832
20857
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@larksuiteoapi/node-sdk",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "larksuite open sdk for nodejs",
5
5
  "keywords": [
6
6
  "feishu",
package/types/index.d.ts CHANGED
@@ -13,8 +13,12 @@
13
13
  import { AxiosRequestConfig } from 'axios';
14
14
 
15
15
  interface Cache {
16
- set: (key: string | Symbol, value: any, expire?: number) => Promise<boolean>;
17
- get: (key: string | Symbol) => Promise<any>;
16
+ set: (key: string | Symbol, value: any, expire?: number, options?: {
17
+ namespace?: string;
18
+ }) => Promise<boolean>;
19
+ get: (key: string | Symbol, options?: {
20
+ namespace?: string;
21
+ }) => Promise<any>;
18
22
  }
19
23
  interface Logger {
20
24
  error: (...msg: any[]) => void | Promise<void>;