@zapier/zapier-sdk 0.45.2 → 0.46.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/index.cjs CHANGED
@@ -5526,43 +5526,74 @@ function getClientIdFromCredentials(credentials) {
5526
5526
  return void 0;
5527
5527
  }
5528
5528
 
5529
+ // src/cache.ts
5530
+ function createMemoryCache() {
5531
+ const store = /* @__PURE__ */ new Map();
5532
+ return {
5533
+ async get(key) {
5534
+ const entry = store.get(key);
5535
+ if (!entry) return void 0;
5536
+ if (entry.expiresAt !== void 0 && entry.expiresAt <= Date.now()) {
5537
+ store.delete(key);
5538
+ return void 0;
5539
+ }
5540
+ return { value: entry.value, expiresAt: entry.expiresAt };
5541
+ },
5542
+ async set(key, value, options) {
5543
+ const expiresAt = options?.ttl ? Date.now() + options.ttl * 1e3 : void 0;
5544
+ store.set(key, { value, expiresAt });
5545
+ },
5546
+ async delete(key) {
5547
+ store.delete(key);
5548
+ }
5549
+ };
5550
+ }
5551
+
5529
5552
  // src/auth.ts
5530
- var tokenCache = /* @__PURE__ */ new Map();
5553
+ var DEFAULT_AUTH_BASE_URL = "https://zapier.com";
5531
5554
  var pendingExchanges = /* @__PURE__ */ new Map();
5532
- function buildCacheKey(clientId, scopes) {
5533
- const sortedScopes = [...scopes].sort().join(",");
5534
- return `${clientId}:${sortedScopes}`;
5555
+ var cachedDefaultCache;
5556
+ function buildCacheKey(options) {
5557
+ const sortedScopes = [...options.scopes].sort().join(",");
5558
+ return `zapier-sdk/client-credentials/${options.clientId}:${sortedScopes}:${options.baseUrl}`;
5535
5559
  }
5536
5560
  function clearTokenCache() {
5537
- tokenCache.clear();
5538
5561
  pendingExchanges.clear();
5539
5562
  cachedCliLogin = void 0;
5563
+ cachedDefaultCache = void 0;
5540
5564
  }
5541
- var TOKEN_EXPIRATION_BUFFER = 5 * 60 * 1e3;
5542
- function getCachedToken(clientId, scopes) {
5543
- const cacheKey = buildCacheKey(clientId, scopes);
5544
- const cached = tokenCache.get(cacheKey);
5545
- if (!cached) return void 0;
5546
- if (cached.expiresAt > Date.now() + TOKEN_EXPIRATION_BUFFER) {
5547
- return cached.accessToken;
5565
+ var TOKEN_EXPIRATION_BUFFER_MS = 5 * 60 * 1e3;
5566
+ async function resolveCache(options) {
5567
+ if (options.cache) return options.cache;
5568
+ if (cachedDefaultCache !== void 0) return cachedDefaultCache;
5569
+ const cliLogin = await getCliLogin();
5570
+ if (cliLogin?.createCache) {
5571
+ try {
5572
+ const cache = cliLogin.createCache();
5573
+ cachedDefaultCache = cache;
5574
+ return cache;
5575
+ } catch {
5576
+ }
5548
5577
  }
5549
- tokenCache.delete(cacheKey);
5550
- return void 0;
5578
+ const fallback = createMemoryCache();
5579
+ cachedDefaultCache = fallback;
5580
+ return fallback;
5551
5581
  }
5552
- function cacheToken(clientId, scopes, accessToken, expiresIn) {
5553
- const cacheKey = buildCacheKey(clientId, scopes);
5554
- tokenCache.set(cacheKey, {
5555
- accessToken,
5556
- expiresAt: Date.now() + expiresIn * 1e3
5557
- });
5582
+ function entryIsValid(entry) {
5583
+ if (entry.expiresAt === void 0) return true;
5584
+ return entry.expiresAt > Date.now() + TOKEN_EXPIRATION_BUFFER_MS;
5558
5585
  }
5559
- function invalidateCachedToken(clientId, scopes) {
5560
- const cacheKey = buildCacheKey(clientId, scopes);
5561
- tokenCache.delete(cacheKey);
5586
+ async function invalidateCachedToken(options) {
5587
+ const cacheKey = buildCacheKey(options);
5562
5588
  pendingExchanges.delete(cacheKey);
5589
+ const cache = await resolveCache({ cache: options.cache });
5590
+ try {
5591
+ await cache.delete(cacheKey);
5592
+ } catch {
5593
+ }
5563
5594
  }
5564
5595
  function getTokenEndpointUrl(baseUrl) {
5565
- const base = baseUrl || ZAPIER_BASE_URL;
5596
+ const base = baseUrl || DEFAULT_AUTH_BASE_URL;
5566
5597
  return `${base}/oauth/token/`;
5567
5598
  }
5568
5599
  function mergeScopes(credentialsScope, requiredScopes) {
@@ -5629,8 +5660,6 @@ async function exchangeClientCredentials(options) {
5629
5660
  if (!data.access_token) {
5630
5661
  throw new Error("Client credentials response missing access_token");
5631
5662
  }
5632
- const expiresIn = data.expires_in || 3600;
5633
- cacheToken(clientId, mergedScopes, data.access_token, expiresIn);
5634
5663
  onEvent?.({
5635
5664
  type: "auth_success",
5636
5665
  payload: {
@@ -5639,7 +5668,10 @@ async function exchangeClientCredentials(options) {
5639
5668
  },
5640
5669
  timestamp: Date.now()
5641
5670
  });
5642
- return data.access_token;
5671
+ return {
5672
+ accessToken: data.access_token,
5673
+ expiresIn: data.expires_in || 3600
5674
+ };
5643
5675
  }
5644
5676
  var cachedCliLogin;
5645
5677
  async function getCliLogin() {
@@ -5654,14 +5686,6 @@ async function getCliLogin() {
5654
5686
  }
5655
5687
  } catch {
5656
5688
  }
5657
- try {
5658
- const mod = await import('@zapier/zapier-sdk-cli-login');
5659
- if (typeof mod.getToken === "function") {
5660
- cachedCliLogin = mod;
5661
- return cachedCliLogin;
5662
- }
5663
- } catch {
5664
- }
5665
5689
  cachedCliLogin = false;
5666
5690
  return void 0;
5667
5691
  }
@@ -5699,24 +5723,41 @@ async function resolveAuthTokenFromCredentials(credentials, options) {
5699
5723
  if (isClientCredentials(credentials)) {
5700
5724
  const { clientId } = credentials;
5701
5725
  const mergedScopes = mergeScopes(credentials.scope, options.requiredScopes);
5702
- const cacheKey = buildCacheKey(clientId, mergedScopes);
5703
- const cached = getCachedToken(clientId, mergedScopes);
5704
- if (cached) {
5705
- return cached;
5726
+ const resolvedBaseUrl = credentials.baseUrl || options.baseUrl || DEFAULT_AUTH_BASE_URL;
5727
+ const cacheKey = buildCacheKey({
5728
+ clientId,
5729
+ scopes: mergedScopes,
5730
+ baseUrl: resolvedBaseUrl
5731
+ });
5732
+ const cache = await resolveCache(options);
5733
+ const cached = await cache.get(cacheKey);
5734
+ if (cached && entryIsValid(cached)) {
5735
+ return cached.value;
5706
5736
  }
5707
5737
  const pending = pendingExchanges.get(cacheKey);
5708
- if (pending) {
5709
- return pending;
5710
- }
5711
- const exchangePromise = exchangeClientCredentials({
5712
- clientId: credentials.clientId,
5713
- clientSecret: credentials.clientSecret,
5714
- baseUrl: credentials.baseUrl || options.baseUrl,
5715
- scope: credentials.scope,
5716
- requiredScopes: options.requiredScopes,
5717
- fetch: options.fetch,
5718
- onEvent: options.onEvent
5719
- }).finally(() => {
5738
+ if (pending) return pending;
5739
+ const runLocked = async () => {
5740
+ const recheck = await cache.get(cacheKey);
5741
+ if (recheck && entryIsValid(recheck)) return recheck.value;
5742
+ const { accessToken, expiresIn } = await exchangeClientCredentials({
5743
+ clientId: credentials.clientId,
5744
+ clientSecret: credentials.clientSecret,
5745
+ baseUrl: credentials.baseUrl || options.baseUrl,
5746
+ scope: credentials.scope,
5747
+ requiredScopes: options.requiredScopes,
5748
+ fetch: options.fetch,
5749
+ onEvent: options.onEvent
5750
+ });
5751
+ try {
5752
+ await cache.set(cacheKey, accessToken, {
5753
+ secret: true,
5754
+ ttl: expiresIn
5755
+ });
5756
+ } catch {
5757
+ }
5758
+ return accessToken;
5759
+ };
5760
+ const exchangePromise = (cache.withLock ? cache.withLock(cacheKey, runLocked) : runLocked()).finally(() => {
5720
5761
  pendingExchanges.delete(cacheKey);
5721
5762
  });
5722
5763
  pendingExchanges.set(cacheKey, exchangePromise);
@@ -5744,12 +5785,18 @@ async function invalidateCredentialsToken(options) {
5744
5785
  const clientId = getClientIdFromCredentials(resolved);
5745
5786
  if (clientId && isClientCredentials(resolved)) {
5746
5787
  const scopes = mergeScopes(resolved.scope, options.requiredScopes);
5747
- invalidateCachedToken(clientId, scopes);
5788
+ const baseUrl = resolved.baseUrl || options.baseUrl || DEFAULT_AUTH_BASE_URL;
5789
+ await invalidateCachedToken({
5790
+ clientId,
5791
+ scopes,
5792
+ baseUrl,
5793
+ cache: options.cache
5794
+ });
5748
5795
  }
5749
5796
  }
5750
5797
 
5751
5798
  // src/sdk-version.ts
5752
- var SDK_VERSION = (typeof process !== "undefined" && process.env ? "0.45.2" : void 0) || "unknown";
5799
+ var SDK_VERSION = (typeof process !== "undefined" && process.env ? "0.46.0" : void 0) || "unknown";
5753
5800
 
5754
5801
  // src/utils/open-url.ts
5755
5802
  var nodePrefix = "node:";
@@ -6096,7 +6143,8 @@ var ZapierApiClient = class {
6096
6143
  fetch: this.options.fetch,
6097
6144
  baseUrl: this.options.baseUrl,
6098
6145
  requiredScopes: options?.requiredScopes,
6099
- debug: this.options.debug
6146
+ debug: this.options.debug,
6147
+ cache: this.options.cache
6100
6148
  });
6101
6149
  }
6102
6150
  // Helper to handle responses
@@ -6147,7 +6195,9 @@ var ZapierApiClient = class {
6147
6195
  await invalidateCredentialsToken({
6148
6196
  credentials: this.options.credentials,
6149
6197
  token: this.options.token,
6150
- requiredScopes
6198
+ baseUrl: this.options.baseUrl,
6199
+ requiredScopes,
6200
+ cache: this.options.cache
6151
6201
  });
6152
6202
  }
6153
6203
  throw new ZapierAuthenticationError(message, errorOptions);
@@ -9073,6 +9123,7 @@ var BaseSdkOptionsSchema = zod.z.object({
9073
9123
  fetch: zod.z.custom().optional().meta({ internal: true }),
9074
9124
  eventEmission: zod.z.custom().optional().meta({ internal: true }),
9075
9125
  callerPackage: zod.z.custom().optional().meta({ internal: true }),
9126
+ cache: zod.z.custom().optional().meta({ internal: true }),
9076
9127
  canIncludeSharedConnections: zod.z.boolean().optional().describe("Allow listing shared connections."),
9077
9128
  canIncludeSharedTables: zod.z.boolean().optional().describe("Allow listing shared tables."),
9078
9129
  canDeleteTables: zod.z.boolean().optional().describe("Allow deleting tables."),
@@ -9171,6 +9222,7 @@ exports.connectionsPlugin = connectionsPlugin;
9171
9222
  exports.createBaseEvent = createBaseEvent;
9172
9223
  exports.createClientCredentialsPlugin = createClientCredentialsPlugin;
9173
9224
  exports.createFunction = createFunction;
9225
+ exports.createMemoryCache = createMemoryCache;
9174
9226
  exports.createOptionsPlugin = createOptionsPlugin;
9175
9227
  exports.createSdk = createSdk;
9176
9228
  exports.createTableFieldsPlugin = createTableFieldsPlugin;