@zapier/zapier-sdk 0.45.1 → 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.
Files changed (43) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +5 -5
  3. package/dist/api/client.d.ts.map +1 -1
  4. package/dist/api/client.js +7 -1
  5. package/dist/api/schemas.d.ts +5 -5
  6. package/dist/api/types.d.ts +6 -0
  7. package/dist/api/types.d.ts.map +1 -1
  8. package/dist/auth.d.ts +27 -6
  9. package/dist/auth.d.ts.map +1 -1
  10. package/dist/auth.js +130 -92
  11. package/dist/cache.d.ts +50 -0
  12. package/dist/cache.d.ts.map +1 -0
  13. package/dist/cache.js +47 -0
  14. package/dist/index.cjs +116 -61
  15. package/dist/index.d.mts +108 -35
  16. package/dist/index.mjs +116 -62
  17. package/dist/plugins/getAction/schemas.d.ts +4 -4
  18. package/dist/plugins/getApp/index.js +1 -1
  19. package/dist/plugins/getInputFieldsSchema/schemas.d.ts +4 -4
  20. package/dist/plugins/listActions/schemas.d.ts +4 -4
  21. package/dist/plugins/listApps/index.js +2 -2
  22. package/dist/plugins/listApps/schemas.d.ts.map +1 -1
  23. package/dist/plugins/listApps/schemas.js +2 -0
  24. package/dist/plugins/listClientCredentials/index.js +1 -1
  25. package/dist/plugins/listClientCredentials/schemas.d.ts.map +1 -1
  26. package/dist/plugins/listClientCredentials/schemas.js +1 -0
  27. package/dist/plugins/listInputFieldChoices/schemas.d.ts +4 -4
  28. package/dist/plugins/listInputFields/schemas.d.ts +4 -4
  29. package/dist/plugins/runAction/schemas.d.ts +4 -4
  30. package/dist/plugins/tables/createTableFields/schemas.d.ts +21 -21
  31. package/dist/plugins/tables/listTableFields/schemas.d.ts +12 -12
  32. package/dist/plugins/tables/listTableRecords/schemas.d.ts +6 -6
  33. package/dist/schemas/Action.d.ts +1 -1
  34. package/dist/schemas/Connection.d.ts +3 -0
  35. package/dist/schemas/Connection.d.ts.map +1 -1
  36. package/dist/types/credentials.d.ts +2 -1
  37. package/dist/types/credentials.d.ts.map +1 -1
  38. package/dist/types/credentials.js +2 -1
  39. package/dist/types/properties.d.ts +1 -1
  40. package/dist/types/sdk.d.ts +2 -0
  41. package/dist/types/sdk.d.ts.map +1 -1
  42. package/dist/types/sdk.js +1 -0
  43. package/package.json +4 -6
package/dist/cache.js ADDED
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Best-effort cache for the SDK.
3
+ *
4
+ * The SDK uses this to avoid re-fetching values it can recreate, primarily
5
+ * client_credentials access tokens. Cache entries may disappear at any time:
6
+ * a miss is normal and callers must be able to rebuild the value.
7
+ *
8
+ * The default cache is in-memory. When the CLI package is installed, the SDK
9
+ * can opportunistically use its filesystem + keychain cache. Browser and
10
+ * minimal server deployments fall back to memory unless a caller injects a
11
+ * Redis, database, or environment-specific cache.
12
+ *
13
+ * Secrets: adapters receive a `secret: true` flag on `set` and decide
14
+ * what to do with it (keychain for the filesystem adapter; ignored for
15
+ * the in-memory adapter; user adapters can throw, plaintext, encrypt —
16
+ * their call). The value returned by `get` is always the plain secret.
17
+ */
18
+ /**
19
+ * Simplest possible adapter: a Map. No persistence, no locking (single
20
+ * process only), no differentiation between secrets and non-secrets.
21
+ * This is the default when cli-login isn't installed and no custom
22
+ * cache is provided.
23
+ */
24
+ export function createMemoryCache() {
25
+ const store = new Map();
26
+ return {
27
+ async get(key) {
28
+ const entry = store.get(key);
29
+ if (!entry)
30
+ return undefined;
31
+ if (entry.expiresAt !== undefined && entry.expiresAt <= Date.now()) {
32
+ store.delete(key);
33
+ return undefined;
34
+ }
35
+ return { value: entry.value, expiresAt: entry.expiresAt };
36
+ },
37
+ async set(key, value, options) {
38
+ const expiresAt = options?.ttl
39
+ ? Date.now() + options.ttl * 1000
40
+ : undefined;
41
+ store.set(key, { value, expiresAt });
42
+ },
43
+ async delete(key) {
44
+ store.delete(key);
45
+ },
46
+ };
47
+ }
package/dist/index.cjs CHANGED
@@ -1546,7 +1546,9 @@ function createPaginatedFunction(coreFn, schema, telemetry, explicitFunctionName
1546
1546
  return namedFunctions[functionName];
1547
1547
  }
1548
1548
  var ListAppsSchema = apps.ListAppsQuerySchema.omit({
1549
- offset: true
1549
+ offset: true,
1550
+ app_keys: true,
1551
+ page_size: true
1550
1552
  }).extend({
1551
1553
  // New name for appKeys
1552
1554
  apps: AppsPropertySchema.optional().describe(
@@ -1656,10 +1658,10 @@ var listAppsPlugin = (sdk) => {
1656
1658
  });
1657
1659
  return await api.get("/api/v0/apps", {
1658
1660
  searchParams: {
1659
- appKeys: implementationIds.join(","),
1661
+ app_keys: implementationIds.join(","),
1660
1662
  ...options.search && { search: options.search },
1661
1663
  ...options.pageSize !== void 0 && {
1662
- pageSize: options.pageSize.toString()
1664
+ page_size: options.pageSize.toString()
1663
1665
  },
1664
1666
  ...options.cursor && { offset: options.cursor }
1665
1667
  }
@@ -3599,7 +3601,8 @@ var listConnectionsPlugin = (sdk) => {
3599
3601
  };
3600
3602
  };
3601
3603
  var ListClientCredentialsQuerySchema = clientCredentials.ListClientCredentialsQuerySchema.omit({
3602
- offset: true
3604
+ offset: true,
3605
+ page_size: true
3603
3606
  }).extend({
3604
3607
  // Override pageSize to make optional
3605
3608
  pageSize: zod.z.number().min(1).optional().describe("Number of credentials per page"),
@@ -3657,7 +3660,7 @@ var listClientCredentialsPlugin = (sdk) => {
3657
3660
  const { api } = sdk.context;
3658
3661
  const searchParams = {};
3659
3662
  if (options.pageSize !== void 0) {
3660
- searchParams.pageSize = options.pageSize.toString();
3663
+ searchParams.page_size = options.pageSize.toString();
3661
3664
  }
3662
3665
  if (options.cursor) {
3663
3666
  searchParams.offset = options.cursor;
@@ -3855,7 +3858,7 @@ var getAppPlugin = (sdk) => {
3855
3858
  async function getApp(options) {
3856
3859
  const appKey = "app" in options ? options.app : options.appKey;
3857
3860
  const appsIterable = sdk.listApps({
3858
- appKeys: [appKey]
3861
+ apps: [appKey]
3859
3862
  }).items();
3860
3863
  for await (const app of appsIterable) {
3861
3864
  return {
@@ -5523,43 +5526,74 @@ function getClientIdFromCredentials(credentials) {
5523
5526
  return void 0;
5524
5527
  }
5525
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
+
5526
5552
  // src/auth.ts
5527
- var tokenCache = /* @__PURE__ */ new Map();
5553
+ var DEFAULT_AUTH_BASE_URL = "https://zapier.com";
5528
5554
  var pendingExchanges = /* @__PURE__ */ new Map();
5529
- function buildCacheKey(clientId, scopes) {
5530
- const sortedScopes = [...scopes].sort().join(",");
5531
- 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}`;
5532
5559
  }
5533
5560
  function clearTokenCache() {
5534
- tokenCache.clear();
5535
5561
  pendingExchanges.clear();
5536
5562
  cachedCliLogin = void 0;
5563
+ cachedDefaultCache = void 0;
5537
5564
  }
5538
- var TOKEN_EXPIRATION_BUFFER = 5 * 60 * 1e3;
5539
- function getCachedToken(clientId, scopes) {
5540
- const cacheKey = buildCacheKey(clientId, scopes);
5541
- const cached = tokenCache.get(cacheKey);
5542
- if (!cached) return void 0;
5543
- if (cached.expiresAt > Date.now() + TOKEN_EXPIRATION_BUFFER) {
5544
- 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
+ }
5545
5577
  }
5546
- tokenCache.delete(cacheKey);
5547
- return void 0;
5578
+ const fallback = createMemoryCache();
5579
+ cachedDefaultCache = fallback;
5580
+ return fallback;
5548
5581
  }
5549
- function cacheToken(clientId, scopes, accessToken, expiresIn) {
5550
- const cacheKey = buildCacheKey(clientId, scopes);
5551
- tokenCache.set(cacheKey, {
5552
- accessToken,
5553
- expiresAt: Date.now() + expiresIn * 1e3
5554
- });
5582
+ function entryIsValid(entry) {
5583
+ if (entry.expiresAt === void 0) return true;
5584
+ return entry.expiresAt > Date.now() + TOKEN_EXPIRATION_BUFFER_MS;
5555
5585
  }
5556
- function invalidateCachedToken(clientId, scopes) {
5557
- const cacheKey = buildCacheKey(clientId, scopes);
5558
- tokenCache.delete(cacheKey);
5586
+ async function invalidateCachedToken(options) {
5587
+ const cacheKey = buildCacheKey(options);
5559
5588
  pendingExchanges.delete(cacheKey);
5589
+ const cache = await resolveCache({ cache: options.cache });
5590
+ try {
5591
+ await cache.delete(cacheKey);
5592
+ } catch {
5593
+ }
5560
5594
  }
5561
5595
  function getTokenEndpointUrl(baseUrl) {
5562
- const base = baseUrl || ZAPIER_BASE_URL;
5596
+ const base = baseUrl || DEFAULT_AUTH_BASE_URL;
5563
5597
  return `${base}/oauth/token/`;
5564
5598
  }
5565
5599
  function mergeScopes(credentialsScope, requiredScopes) {
@@ -5626,8 +5660,6 @@ async function exchangeClientCredentials(options) {
5626
5660
  if (!data.access_token) {
5627
5661
  throw new Error("Client credentials response missing access_token");
5628
5662
  }
5629
- const expiresIn = data.expires_in || 3600;
5630
- cacheToken(clientId, mergedScopes, data.access_token, expiresIn);
5631
5663
  onEvent?.({
5632
5664
  type: "auth_success",
5633
5665
  payload: {
@@ -5636,7 +5668,10 @@ async function exchangeClientCredentials(options) {
5636
5668
  },
5637
5669
  timestamp: Date.now()
5638
5670
  });
5639
- return data.access_token;
5671
+ return {
5672
+ accessToken: data.access_token,
5673
+ expiresIn: data.expires_in || 3600
5674
+ };
5640
5675
  }
5641
5676
  var cachedCliLogin;
5642
5677
  async function getCliLogin() {
@@ -5651,14 +5686,6 @@ async function getCliLogin() {
5651
5686
  }
5652
5687
  } catch {
5653
5688
  }
5654
- try {
5655
- const mod = await import('@zapier/zapier-sdk-cli-login');
5656
- if (typeof mod.getToken === "function") {
5657
- cachedCliLogin = mod;
5658
- return cachedCliLogin;
5659
- }
5660
- } catch {
5661
- }
5662
5689
  cachedCliLogin = false;
5663
5690
  return void 0;
5664
5691
  }
@@ -5696,24 +5723,41 @@ async function resolveAuthTokenFromCredentials(credentials, options) {
5696
5723
  if (isClientCredentials(credentials)) {
5697
5724
  const { clientId } = credentials;
5698
5725
  const mergedScopes = mergeScopes(credentials.scope, options.requiredScopes);
5699
- const cacheKey = buildCacheKey(clientId, mergedScopes);
5700
- const cached = getCachedToken(clientId, mergedScopes);
5701
- if (cached) {
5702
- 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;
5703
5736
  }
5704
5737
  const pending = pendingExchanges.get(cacheKey);
5705
- if (pending) {
5706
- return pending;
5707
- }
5708
- const exchangePromise = exchangeClientCredentials({
5709
- clientId: credentials.clientId,
5710
- clientSecret: credentials.clientSecret,
5711
- baseUrl: credentials.baseUrl || options.baseUrl,
5712
- scope: credentials.scope,
5713
- requiredScopes: options.requiredScopes,
5714
- fetch: options.fetch,
5715
- onEvent: options.onEvent
5716
- }).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(() => {
5717
5761
  pendingExchanges.delete(cacheKey);
5718
5762
  });
5719
5763
  pendingExchanges.set(cacheKey, exchangePromise);
@@ -5741,12 +5785,18 @@ async function invalidateCredentialsToken(options) {
5741
5785
  const clientId = getClientIdFromCredentials(resolved);
5742
5786
  if (clientId && isClientCredentials(resolved)) {
5743
5787
  const scopes = mergeScopes(resolved.scope, options.requiredScopes);
5744
- 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
+ });
5745
5795
  }
5746
5796
  }
5747
5797
 
5748
5798
  // src/sdk-version.ts
5749
- var SDK_VERSION = (typeof process !== "undefined" && process.env ? "0.45.1" : void 0) || "unknown";
5799
+ var SDK_VERSION = (typeof process !== "undefined" && process.env ? "0.46.0" : void 0) || "unknown";
5750
5800
 
5751
5801
  // src/utils/open-url.ts
5752
5802
  var nodePrefix = "node:";
@@ -6093,7 +6143,8 @@ var ZapierApiClient = class {
6093
6143
  fetch: this.options.fetch,
6094
6144
  baseUrl: this.options.baseUrl,
6095
6145
  requiredScopes: options?.requiredScopes,
6096
- debug: this.options.debug
6146
+ debug: this.options.debug,
6147
+ cache: this.options.cache
6097
6148
  });
6098
6149
  }
6099
6150
  // Helper to handle responses
@@ -6144,7 +6195,9 @@ var ZapierApiClient = class {
6144
6195
  await invalidateCredentialsToken({
6145
6196
  credentials: this.options.credentials,
6146
6197
  token: this.options.token,
6147
- requiredScopes
6198
+ baseUrl: this.options.baseUrl,
6199
+ requiredScopes,
6200
+ cache: this.options.cache
6148
6201
  });
6149
6202
  }
6150
6203
  throw new ZapierAuthenticationError(message, errorOptions);
@@ -9070,6 +9123,7 @@ var BaseSdkOptionsSchema = zod.z.object({
9070
9123
  fetch: zod.z.custom().optional().meta({ internal: true }),
9071
9124
  eventEmission: zod.z.custom().optional().meta({ internal: true }),
9072
9125
  callerPackage: zod.z.custom().optional().meta({ internal: true }),
9126
+ cache: zod.z.custom().optional().meta({ internal: true }),
9073
9127
  canIncludeSharedConnections: zod.z.boolean().optional().describe("Allow listing shared connections."),
9074
9128
  canIncludeSharedTables: zod.z.boolean().optional().describe("Allow listing shared tables."),
9075
9129
  canDeleteTables: zod.z.boolean().optional().describe("Allow deleting tables."),
@@ -9168,6 +9222,7 @@ exports.connectionsPlugin = connectionsPlugin;
9168
9222
  exports.createBaseEvent = createBaseEvent;
9169
9223
  exports.createClientCredentialsPlugin = createClientCredentialsPlugin;
9170
9224
  exports.createFunction = createFunction;
9225
+ exports.createMemoryCache = createMemoryCache;
9171
9226
  exports.createOptionsPlugin = createOptionsPlugin;
9172
9227
  exports.createSdk = createSdk;
9173
9228
  exports.createTableFieldsPlugin = createTableFieldsPlugin;