@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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @zapier/zapier-sdk
2
2
 
3
+ ## 0.46.0
4
+
5
+ ### Minor Changes
6
+
7
+ - bd887ab: Rename the client credentials persistence seam from storage to cache and expose the CLI filesystem/keychain adapter as a best-effort cache.
8
+
9
+ ## 0.45.2
10
+
11
+ ### Patch Changes
12
+
13
+ - 783e180: Convert API query params from camelCase to snake_case (appKeys -> app_keys, pageSize -> page_size) to match the updated API contract. Also updated all `zapier-sdk-*` packages to Zod to 4.3.6
14
+
3
15
  ## 0.45.1
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -1081,11 +1081,11 @@ Delete one or more fields from a table
1081
1081
 
1082
1082
  **Parameters:**
1083
1083
 
1084
- | Name | Type | Required | Default | Possible Values | Description |
1085
- | ---------- | -------- | -------- | ------- | --------------- | ---------------------------------- |
1086
- | `options` | `object` | ✅ | — | — | |
1087
- | ↳ `table` | `string` | ✅ | — | — | The unique identifier of the table |
1088
- | ↳ `fields` | `array` | ✅ | — | — | |
1084
+ | Name | Type | Required | Default | Possible Values | Description |
1085
+ | ---------- | -------- | -------- | ------- | --------------- | ----------------------------------------------------------------------------------------- |
1086
+ | `options` | `object` | ✅ | — | — | |
1087
+ | ↳ `table` | `string` | ✅ | — | — | The unique identifier of the table |
1088
+ | ↳ `fields` | `array` | ✅ | — | — | Fields to operate on. Accepts field names (e.g., "Email") or IDs (e.g., "f6", "6", or 6). |
1089
1089
 
1090
1090
  **Returns:** `Promise<any>`
1091
1091
 
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/api/client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAGjB,MAAM,SAAS,CAAC;AAihCjB,eAAO,MAAM,eAAe,GAAI,SAAS,gBAAgB,KAAG,SAW3D,CAAC"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/api/client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAGjB,MAAM,SAAS,CAAC;AAuhCjB,eAAO,MAAM,eAAe,GAAI,SAAS,gBAAgB,KAAG,SAW3D,CAAC"}
@@ -331,6 +331,7 @@ class ZapierApiClient {
331
331
  baseUrl: this.options.baseUrl,
332
332
  requiredScopes: options?.requiredScopes,
333
333
  debug: this.options.debug,
334
+ cache: this.options.cache,
334
335
  });
335
336
  }
336
337
  // Helper to handle responses
@@ -373,12 +374,17 @@ class ZapierApiClient {
373
374
  if (wasMissingAuthToken) {
374
375
  throw new ZapierAuthenticationError(`Authentication required (HTTP ${response.status}). Please provide credentials in options or set ZAPIER_CREDENTIALS environment variable.`, errorOptions);
375
376
  }
376
- // On 401, invalidate cached token so next request gets a fresh one
377
+ // On 401, invalidate the cached token so the next request fires
378
+ // a fresh exchange. Passing baseUrl + cache explicitly so the
379
+ // invalidation targets the same cache key the token was stored
380
+ // under (both are part of the key identity).
377
381
  if (response.status === 401) {
378
382
  await invalidateCredentialsToken({
379
383
  credentials: this.options.credentials,
380
384
  token: this.options.token,
385
+ baseUrl: this.options.baseUrl,
381
386
  requiredScopes,
387
+ cache: this.options.cache,
382
388
  });
383
389
  }
384
390
  throw new ZapierAuthenticationError(message, errorOptions);
@@ -43,8 +43,8 @@ export declare const NeedSchema: z.ZodObject<{
43
43
  string: "string";
44
44
  boolean: "boolean";
45
45
  file: "file";
46
- integer: "integer";
47
46
  filter: "filter";
47
+ integer: "integer";
48
48
  text: "text";
49
49
  datetime: "datetime";
50
50
  decimal: "decimal";
@@ -65,11 +65,11 @@ export declare const ActionPermissionsSchema: z.ZodObject<{
65
65
  export declare const ActionSchema: z.ZodObject<{
66
66
  id: z.ZodOptional<z.ZodString>;
67
67
  type: z.ZodEnum<{
68
- search: "search";
69
68
  filter: "filter";
70
69
  read: "read";
71
70
  read_bulk: "read_bulk";
72
71
  run: "run";
72
+ search: "search";
73
73
  search_and_write: "search_and_write";
74
74
  search_or_write: "search_or_write";
75
75
  write: "write";
@@ -288,8 +288,8 @@ export declare const NeedsResponseSchema: z.ZodObject<{
288
288
  string: "string";
289
289
  boolean: "boolean";
290
290
  file: "file";
291
- integer: "integer";
292
291
  filter: "filter";
292
+ integer: "integer";
293
293
  text: "text";
294
294
  datetime: "datetime";
295
295
  decimal: "decimal";
@@ -313,11 +313,11 @@ export declare const ImplementationSchema: z.ZodObject<{
313
313
  actions: z.ZodOptional<z.ZodArray<z.ZodObject<{
314
314
  id: z.ZodOptional<z.ZodString>;
315
315
  type: z.ZodEnum<{
316
- search: "search";
317
316
  filter: "filter";
318
317
  read: "read";
319
318
  read_bulk: "read_bulk";
320
319
  run: "run";
320
+ search: "search";
321
321
  search_and_write: "search_and_write";
322
322
  search_or_write: "search_or_write";
323
323
  write: "write";
@@ -355,11 +355,11 @@ export declare const ImplementationsResponseSchema: z.ZodObject<{
355
355
  actions: z.ZodOptional<z.ZodArray<z.ZodObject<{
356
356
  id: z.ZodOptional<z.ZodString>;
357
357
  type: z.ZodEnum<{
358
- search: "search";
359
358
  filter: "filter";
360
359
  read: "read";
361
360
  read_bulk: "read_bulk";
362
361
  run: "run";
362
+ search: "search";
363
363
  search_and_write: "search_and_write";
364
364
  search_or_write: "search_or_write";
365
365
  write: "write";
@@ -13,6 +13,7 @@ import type { ImplementationMetaSchema, ImplementationsMetaResponseSchema } from
13
13
  import type { SdkEvent } from "../types/events";
14
14
  import type { Credentials } from "../types/credentials";
15
15
  import type { RequestContext } from "@zapier/policy-context";
16
+ import type { ZapierCache } from "../cache";
16
17
  import type { z } from "zod";
17
18
  import type { NeedChoicesSchema, NeedSchema, ActionLinksSchema, ActionPermissionsSchema, ActionSchema, ChoiceSchema, FieldSchema, ActionExecutionResultSchema, ActionFieldChoiceSchema, ActionFieldSchema, UserProfileSchema, AppSchema, NeedsRequestSchema, NeedsResponseSchema, ImplementationSchema, ImplementationsResponseSchema, ServiceSchema, ServicesResponseSchema, NeedChoicesRequestSchema, NeedChoicesResponseSchema, NeedChoicesResponseMetaSchema, NeedChoicesResponseLinksSchema } from "./schemas";
18
19
  export interface ApiClientOptions {
@@ -71,6 +72,11 @@ export interface ApiClientOptions {
71
72
  name: string;
72
73
  version: string;
73
74
  };
75
+ /**
76
+ * Pluggable key-value cache used to cache access tokens across
77
+ * invocations. See ZapierCache in ../cache.ts.
78
+ */
79
+ cache?: ZapierCache;
74
80
  }
75
81
  export interface ApiClient {
76
82
  get: <T = unknown>(path: string, options?: RequestOptions) => Promise<T>;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/api/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EACV,gBAAgB,EAChB,yBAAyB,EAC1B,MAAM,gDAAgD,CAAC;AACxD,OAAO,KAAK,EACV,wBAAwB,EACxB,iCAAiC,EAClC,MAAM,oDAAoD,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EACV,iBAAiB,EACjB,UAAU,EACV,iBAAiB,EACjB,uBAAuB,EACvB,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,2BAA2B,EAC3B,uBAAuB,EACvB,iBAAiB,EACjB,iBAAiB,EACjB,SAAS,EACT,kBAAkB,EAClB,m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AC"}
package/dist/auth.d.ts CHANGED
@@ -6,10 +6,13 @@
6
6
  * and handles different credential types appropriately.
7
7
  *
8
8
  * CLI-specific functionality like login/logout is handled by the
9
- * @zapier/zapier-sdk-cli-login package.
9
+ * @zapier/zapier-sdk-cli package (imported via its `./login` subpath).
10
10
  */
11
11
  import type { EventCallback } from "./types/events";
12
12
  import type { Credentials } from "./types/credentials";
13
+ import { type ZapierCache } from "./cache";
14
+ export type { ZapierCache, ZapierCacheEntry, ZapierCacheSetOptions, } from "./cache";
15
+ export { createMemoryCache } from "./cache";
13
16
  export type { SdkEvent, AuthEvent, ApiEvent, LoadingEvent, EventCallback, } from "./types/events";
14
17
  export type { Credentials, ResolvedCredentials, CredentialsObject, ClientCredentialsObject, PkceCredentialsObject, } from "./types/credentials";
15
18
  export { isClientCredentials, isPkceCredentials, isCredentialsObject, isCredentialsFunction, } from "./types/credentials";
@@ -31,16 +34,32 @@ export interface ResolveAuthTokenOptions {
31
34
  requiredScopes?: string[];
32
35
  /** Enable debug logging for auth operations. */
33
36
  debug?: boolean;
37
+ /**
38
+ * Pluggable key-value cache used to cache access tokens across
39
+ * process boundaries. When omitted, the SDK tries to load a default
40
+ * adapter from the cli-login package (file system + keychain) and
41
+ * falls back to in-memory cache if none is available.
42
+ */
43
+ cache?: ZapierCache;
34
44
  }
35
45
  /**
36
- * Clear the token cache. Useful for testing or forcing re-authentication.
46
+ * Clear in-process caches. Useful for testing or to force the next
47
+ * resolve to re-import the default cache adapter. Does not touch
48
+ * persistent cache — use `invalidateCachedToken` for that.
37
49
  */
38
50
  export declare function clearTokenCache(): void;
39
51
  /**
40
- * Invalidate a cached token for a specific clientId and scope combination.
41
- * Called when we get a 401 response.
52
+ * Invalidate the cached token for a given client_credentials identity.
53
+ * Called on 401 so the next request re-exchanges. Clears both the
54
+ * in-process pending-exchange map and the persistent layer via the
55
+ * resolved cache adapter.
42
56
  */
43
- export declare function invalidateCachedToken(clientId: string, scopes: string[]): void;
57
+ export declare function invalidateCachedToken(options: {
58
+ clientId: string;
59
+ scopes: string[];
60
+ baseUrl: string;
61
+ cache?: ZapierCache;
62
+ }): Promise<void>;
44
63
  /**
45
64
  * Options for getTokenFromCliLogin.
46
65
  */
@@ -55,7 +74,7 @@ interface CliLoginOptions {
55
74
  };
56
75
  debug?: boolean;
57
76
  }
58
- type CliLoginModule = typeof import("@zapier/zapier-sdk-cli-login");
77
+ type CliLoginModule = typeof import("@zapier/zapier-sdk-cli/login");
59
78
  /**
60
79
  * Inject an already-loaded CLI login module so the SDK skips its dynamic import.
61
80
  * This guarantees CLI and SDK share the same module instance in the same process.
@@ -97,6 +116,8 @@ export declare function resolveAuthToken(options?: ResolveAuthTokenOptions): Pro
97
116
  export declare function invalidateCredentialsToken(options: {
98
117
  credentials?: Credentials;
99
118
  token?: string;
119
+ baseUrl?: string;
100
120
  requiredScopes?: string[];
121
+ cache?: ZapierCache;
101
122
  }): Promise<void>;
102
123
  //# sourceMappingURL=auth.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,KAAK,EAAE,WAAW,EAAuB,MAAM,qBAAqB,CAAC;AAM5E,YAAY,EACV,QAAQ,EACR,SAAS,EACT,QAAQ,EACR,YAAY,EACZ,aAAa,GACd,MAAM,gBAAgB,CAAC;AAGxB,YAAY,EACV,WAAW,EACX,mBAAmB,EACnB,iBAAiB,EACjB,uBAAuB,EACvB,qBAAqB,GACtB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,qBAAqB,CAAC;AAE7B;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,4CAA4C;IAC5C,KAAK,CAAC,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IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,KAAK,CAAC,EAAE,WAAW,CAAC;CACrB,GAAG,OAAO,CAAC,IAAI,CAAC,CAehB"}
package/dist/auth.js CHANGED
@@ -6,80 +6,100 @@
6
6
  * and handles different credential types appropriately.
7
7
  *
8
8
  * CLI-specific functionality like login/logout is handled by the
9
- * @zapier/zapier-sdk-cli-login package.
9
+ * @zapier/zapier-sdk-cli package (imported via its `./login` subpath).
10
10
  */
11
11
  import { isClientCredentials, isPkceCredentials } from "./types/credentials";
12
12
  import { resolveCredentials, getClientIdFromCredentials } from "./credentials";
13
- import { ZAPIER_BASE_URL } from "./constants";
13
+ import { createMemoryCache } from "./cache";
14
+ const DEFAULT_AUTH_BASE_URL = "https://zapier.com";
15
+ export { createMemoryCache } from "./cache";
14
16
  export { isClientCredentials, isPkceCredentials, isCredentialsObject, isCredentialsFunction, } from "./types/credentials";
15
17
  /**
16
- * In-memory cache for tokens obtained via client credentials flow.
17
- * Keyed by clientId + sorted scopes.
18
+ * In-flight token exchange promises to prevent duplicate exchanges
19
+ * inside the same process. When an exchange is in progress, subsequent
20
+ * calls await the same promise. Cache backends are responsible for
21
+ * cross-process dedup via their optional `withLock` method.
18
22
  */
19
- const tokenCache = new Map();
23
+ const pendingExchanges = new Map();
20
24
  /**
21
- * In-flight token exchange promises to prevent duplicate exchanges.
22
- * When an exchange is in progress, subsequent calls await the same promise.
23
- * Keyed by clientId + sorted scopes.
25
+ * The resolved default cache adapter. Cached at module scope so
26
+ * repeated calls to `resolveAuthToken` reuse the same provider.
24
27
  */
25
- const pendingExchanges = new Map();
28
+ let cachedDefaultCache;
26
29
  /**
27
- * Build a cache key from clientId and scopes.
30
+ * Build the cache key for a client_credentials identity. baseUrl is
31
+ * part of the key so the same clientId+scopes against different auth
32
+ * hosts (prod vs staging) never alias.
28
33
  */
29
- function buildCacheKey(clientId, scopes) {
30
- const sortedScopes = [...scopes].sort().join(",");
31
- return `${clientId}:${sortedScopes}`;
34
+ function buildCacheKey(options) {
35
+ const sortedScopes = [...options.scopes].sort().join(",");
36
+ return `zapier-sdk/client-credentials/${options.clientId}:${sortedScopes}:${options.baseUrl}`;
32
37
  }
33
38
  /**
34
- * Clear the token cache. Useful for testing or forcing re-authentication.
39
+ * Clear in-process caches. Useful for testing or to force the next
40
+ * resolve to re-import the default cache adapter. Does not touch
41
+ * persistent cache — use `invalidateCachedToken` for that.
35
42
  */
36
43
  export function clearTokenCache() {
37
- tokenCache.clear();
38
44
  pendingExchanges.clear();
39
45
  cachedCliLogin = undefined;
46
+ cachedDefaultCache = undefined;
40
47
  }
41
- const TOKEN_EXPIRATION_BUFFER = 5 * 60 * 1000; // 5 minutes
48
+ const TOKEN_EXPIRATION_BUFFER_MS = 5 * 60 * 1000;
42
49
  /**
43
- * Get a cached token if it exists and is not expired.
44
- * Returns undefined if no valid cached token exists.
50
+ * Decide which cache adapter to use for this call. Priority:
51
+ * 1. explicit `options.cache`
52
+ * 2. CLI's default cache provider (if installed)
53
+ * 3. in-memory fallback
45
54
  */
46
- function getCachedToken(clientId, scopes) {
47
- const cacheKey = buildCacheKey(clientId, scopes);
48
- const cached = tokenCache.get(cacheKey);
49
- if (!cached)
50
- return undefined;
51
- // Check if token is still valid (with expiration buffer)
52
- if (cached.expiresAt > Date.now() + TOKEN_EXPIRATION_BUFFER) {
53
- return cached.accessToken;
55
+ async function resolveCache(options) {
56
+ if (options.cache)
57
+ return options.cache;
58
+ if (cachedDefaultCache !== undefined)
59
+ return cachedDefaultCache;
60
+ const cliLogin = await getCliLogin();
61
+ if (cliLogin?.createCache) {
62
+ try {
63
+ const cache = cliLogin.createCache();
64
+ cachedDefaultCache = cache;
65
+ return cache;
66
+ }
67
+ catch {
68
+ // Fall through to in-memory if the CLI provider can't be constructed.
69
+ }
54
70
  }
55
- // Token expired, remove from cache
56
- tokenCache.delete(cacheKey);
57
- return undefined;
71
+ const fallback = createMemoryCache();
72
+ cachedDefaultCache = fallback;
73
+ return fallback;
58
74
  }
59
- /**
60
- * Cache a token obtained from client credentials flow.
61
- */
62
- function cacheToken(clientId, scopes, accessToken, expiresIn) {
63
- const cacheKey = buildCacheKey(clientId, scopes);
64
- tokenCache.set(cacheKey, {
65
- accessToken,
66
- expiresAt: Date.now() + expiresIn * 1000,
67
- });
75
+ function entryIsValid(entry) {
76
+ if (entry.expiresAt === undefined)
77
+ return true;
78
+ return entry.expiresAt > Date.now() + TOKEN_EXPIRATION_BUFFER_MS;
68
79
  }
69
80
  /**
70
- * Invalidate a cached token for a specific clientId and scope combination.
71
- * Called when we get a 401 response.
81
+ * Invalidate the cached token for a given client_credentials identity.
82
+ * Called on 401 so the next request re-exchanges. Clears both the
83
+ * in-process pending-exchange map and the persistent layer via the
84
+ * resolved cache adapter.
72
85
  */
73
- export function invalidateCachedToken(clientId, scopes) {
74
- const cacheKey = buildCacheKey(clientId, scopes);
75
- tokenCache.delete(cacheKey);
86
+ export async function invalidateCachedToken(options) {
87
+ const cacheKey = buildCacheKey(options);
76
88
  pendingExchanges.delete(cacheKey);
89
+ const cache = await resolveCache({ cache: options.cache });
90
+ try {
91
+ await cache.delete(cacheKey);
92
+ }
93
+ catch {
94
+ // Best-effort: the caller is about to surface a 401 to its own
95
+ // caller; a failed invalidation shouldn't add noise on top of that.
96
+ }
77
97
  }
78
98
  /**
79
99
  * Get the token endpoint URL for client credentials exchange.
80
100
  */
81
101
  function getTokenEndpointUrl(baseUrl) {
82
- const base = baseUrl || ZAPIER_BASE_URL;
102
+ const base = baseUrl || DEFAULT_AUTH_BASE_URL;
83
103
  return `${base}/oauth/token/`;
84
104
  }
85
105
  /**
@@ -109,9 +129,6 @@ function mergeScopes(credentialsScope, requiredScopes) {
109
129
  }
110
130
  return [...scopeSet].sort();
111
131
  }
112
- /**
113
- * Exchange client credentials for an access token.
114
- */
115
132
  async function exchangeClientCredentials(options) {
116
133
  const { clientId, clientSecret, baseUrl, scope, requiredScopes, onEvent } = options;
117
134
  const fetchFn = options.fetch || globalThis.fetch;
@@ -157,9 +174,6 @@ async function exchangeClientCredentials(options) {
157
174
  if (!data.access_token) {
158
175
  throw new Error("Client credentials response missing access_token");
159
176
  }
160
- // Cache the token with the scopes used
161
- const expiresIn = data.expires_in || 3600; // Default to 1 hour
162
- cacheToken(clientId, mergedScopes, data.access_token, expiresIn);
163
177
  onEvent?.({
164
178
  type: "auth_success",
165
179
  payload: {
@@ -168,18 +182,17 @@ async function exchangeClientCredentials(options) {
168
182
  },
169
183
  timestamp: Date.now(),
170
184
  });
171
- return data.access_token;
185
+ return {
186
+ accessToken: data.access_token,
187
+ expiresIn: data.expires_in || 3600,
188
+ };
172
189
  }
173
190
  let cachedCliLogin;
174
191
  /**
175
- * Dynamically imports a CLI login package with caching.
176
- *
177
- * Tries `@zapier/zapier-sdk-cli/login` first (available when the CLI is a
178
- * direct dependency), then falls back to `@zapier/zapier-sdk-cli-login` (for
179
- * users who install the lightweight login package directly).
180
- *
181
- * Returns the module if found, or `undefined` if neither package is available.
182
- * The result is cached after the first attempt.
192
+ * Dynamically imports the CLI's login/cache entrypoint with caching.
193
+ * Returns the module if the CLI package is installed in the consumer's
194
+ * environment, or `undefined` if not (e.g. in a browser or minimal
195
+ * server deployment). The result is cached after the first attempt.
183
196
  */
184
197
  async function getCliLogin() {
185
198
  if (cachedCliLogin !== undefined) {
@@ -193,17 +206,7 @@ async function getCliLogin() {
193
206
  }
194
207
  }
195
208
  catch {
196
- // Not available, try fallback
197
- }
198
- try {
199
- const mod = await import("@zapier/zapier-sdk-cli-login");
200
- if (typeof mod.getToken === "function") {
201
- cachedCliLogin = mod;
202
- return cachedCliLogin;
203
- }
204
- }
205
- catch {
206
- // Not available
209
+ // Not available (no CLI installed in this environment).
207
210
  }
208
211
  cachedCliLogin = false;
209
212
  return undefined;
@@ -277,33 +280,62 @@ async function resolveAuthTokenFromCredentials(credentials, options) {
277
280
  if (typeof credentials === "string") {
278
281
  return credentials;
279
282
  }
280
- // Client credentials: exchange for token
283
+ // Client credentials: exchange + cache through a pluggable cache
284
+ // adapter. Resolution order:
285
+ // 1. in-process pending exchange (dedup concurrent same-process calls)
286
+ // 2. cache.get (hits keychain + config under the filesystem adapter,
287
+ // RAM under the in-memory adapter, whatever the user adapter does)
288
+ // 3. cache.withLock (if available) -> re-read, exchange, persist
289
+ // (so N parallel processes collapse to 1 network exchange)
290
+ // 4. plain exchange + persist when no lock is available
281
291
  if (isClientCredentials(credentials)) {
282
292
  const { clientId } = credentials;
283
- // Compute merged scopes for cache lookup
284
293
  const mergedScopes = mergeScopes(credentials.scope, options.requiredScopes);
285
- const cacheKey = buildCacheKey(clientId, mergedScopes);
286
- // Check cache first
287
- const cached = getCachedToken(clientId, mergedScopes);
288
- if (cached) {
289
- return cached;
294
+ const resolvedBaseUrl = credentials.baseUrl || options.baseUrl || DEFAULT_AUTH_BASE_URL;
295
+ const cacheKey = buildCacheKey({
296
+ clientId,
297
+ scopes: mergedScopes,
298
+ baseUrl: resolvedBaseUrl,
299
+ });
300
+ const cache = await resolveCache(options);
301
+ // Fast-path read
302
+ const cached = await cache.get(cacheKey);
303
+ if (cached && entryIsValid(cached)) {
304
+ return cached.value;
290
305
  }
291
- // Check if there's already an exchange in progress for this clientId + scopes
306
+ // In-process dedup
292
307
  const pending = pendingExchanges.get(cacheKey);
293
- if (pending) {
308
+ if (pending)
294
309
  return pending;
295
- }
296
- // Start new exchange and cache the promise to prevent duplicate exchanges
297
- const exchangePromise = exchangeClientCredentials({
298
- clientId: credentials.clientId,
299
- clientSecret: credentials.clientSecret,
300
- baseUrl: credentials.baseUrl || options.baseUrl,
301
- scope: credentials.scope,
302
- requiredScopes: options.requiredScopes,
303
- fetch: options.fetch,
304
- onEvent: options.onEvent,
305
- }).finally(() => {
306
- // Remove from pending when done (success or failure)
310
+ // Serialize exchange + persist inside the lock (when the adapter
311
+ // supports cross-process locking). The second acquirer re-reads the
312
+ // cache under the lock and uses the first's token instead of firing
313
+ // another exchange.
314
+ const runLocked = async () => {
315
+ const recheck = await cache.get(cacheKey);
316
+ if (recheck && entryIsValid(recheck))
317
+ return recheck.value;
318
+ const { accessToken, expiresIn } = await exchangeClientCredentials({
319
+ clientId: credentials.clientId,
320
+ clientSecret: credentials.clientSecret,
321
+ baseUrl: credentials.baseUrl || options.baseUrl,
322
+ scope: credentials.scope,
323
+ requiredScopes: options.requiredScopes,
324
+ fetch: options.fetch,
325
+ onEvent: options.onEvent,
326
+ });
327
+ try {
328
+ await cache.set(cacheKey, accessToken, {
329
+ secret: true,
330
+ ttl: expiresIn,
331
+ });
332
+ }
333
+ catch {
334
+ // Best-effort persistence: next process will re-exchange.
335
+ }
336
+ return accessToken;
337
+ };
338
+ const exchangePromise = (cache.withLock ? cache.withLock(cacheKey, runLocked) : runLocked()).finally(() => {
307
339
  pendingExchanges.delete(cacheKey);
308
340
  });
309
341
  pendingExchanges.set(cacheKey, exchangePromise);
@@ -340,6 +372,12 @@ export async function invalidateCredentialsToken(options) {
340
372
  const clientId = getClientIdFromCredentials(resolved);
341
373
  if (clientId && isClientCredentials(resolved)) {
342
374
  const scopes = mergeScopes(resolved.scope, options.requiredScopes);
343
- invalidateCachedToken(clientId, scopes);
375
+ const baseUrl = resolved.baseUrl || options.baseUrl || DEFAULT_AUTH_BASE_URL;
376
+ await invalidateCachedToken({
377
+ clientId,
378
+ scopes,
379
+ baseUrl,
380
+ cache: options.cache,
381
+ });
344
382
  }
345
383
  }
@@ -0,0 +1,50 @@
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
+ export interface ZapierCacheEntry {
19
+ value: string;
20
+ /** Millisecond epoch. Undefined means "no expiration recorded." */
21
+ expiresAt?: number;
22
+ }
23
+ export interface ZapierCacheSetOptions {
24
+ /**
25
+ * Hint that this value is sensitive. Adapters may use a more secure
26
+ * backend (e.g. the OS keychain for the filesystem adapter); adapters
27
+ * without a secure backend may throw, warn, or accept — their call.
28
+ */
29
+ secret?: boolean;
30
+ /** Time-to-live in seconds from now. */
31
+ ttl?: number;
32
+ }
33
+ export interface ZapierCache {
34
+ get(key: string): Promise<ZapierCacheEntry | undefined>;
35
+ set(key: string, value: string, options?: ZapierCacheSetOptions): Promise<void>;
36
+ delete(key: string): Promise<void>;
37
+ /**
38
+ * Optional mutex for caches that can coordinate beyond one JavaScript
39
+ * process. The SDK re-checks the cache inside the lock before rebuilding.
40
+ */
41
+ withLock?<T>(key: string, fn: () => Promise<T>): Promise<T>;
42
+ }
43
+ /**
44
+ * Simplest possible adapter: a Map. No persistence, no locking (single
45
+ * process only), no differentiation between secrets and non-secrets.
46
+ * This is the default when cli-login isn't installed and no custom
47
+ * cache is provided.
48
+ */
49
+ export declare function createMemoryCache(): ZapierCache;
50
+ //# sourceMappingURL=cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,mEAAmE;IACnE,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,wCAAwC;IACxC,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,WAAW;IAC1B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAAC;IACxD,GAAG,CACD,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,qBAAqB,GAC9B,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACnC;;;OAGG;IACH,QAAQ,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CAC7D;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,IAAI,WAAW,CAsB/C"}