@zapier/zapier-sdk 0.18.4 → 1.0.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 (53) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/api/client.d.ts.map +1 -1
  3. package/dist/api/client.js +11 -6
  4. package/dist/api/client.test.js +82 -27
  5. package/dist/api/index.d.ts +3 -2
  6. package/dist/api/index.d.ts.map +1 -1
  7. package/dist/api/index.js +2 -3
  8. package/dist/api/schemas.d.ts +5 -5
  9. package/dist/api/types.d.ts +8 -3
  10. package/dist/api/types.d.ts.map +1 -1
  11. package/dist/auth.d.ts +54 -26
  12. package/dist/auth.d.ts.map +1 -1
  13. package/dist/auth.js +211 -39
  14. package/dist/auth.test.js +338 -64
  15. package/dist/constants.d.ts +14 -0
  16. package/dist/constants.d.ts.map +1 -1
  17. package/dist/constants.js +14 -0
  18. package/dist/credentials.d.ts +57 -0
  19. package/dist/credentials.d.ts.map +1 -0
  20. package/dist/credentials.js +174 -0
  21. package/dist/index.cjs +341 -46
  22. package/dist/index.d.mts +213 -29
  23. package/dist/index.d.ts +3 -0
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +5 -0
  26. package/dist/index.mjs +321 -45
  27. package/dist/plugins/api/index.d.ts +2 -0
  28. package/dist/plugins/api/index.d.ts.map +1 -1
  29. package/dist/plugins/api/index.js +8 -4
  30. package/dist/plugins/eventEmission/index.d.ts.map +1 -1
  31. package/dist/plugins/eventEmission/index.js +1 -3
  32. package/dist/plugins/eventEmission/index.test.js +14 -17
  33. package/dist/plugins/getAction/schemas.d.ts +1 -1
  34. package/dist/plugins/getInputFieldsSchema/schemas.d.ts +1 -1
  35. package/dist/plugins/listActions/index.test.js +1 -0
  36. package/dist/plugins/listActions/schemas.d.ts +1 -1
  37. package/dist/plugins/listInputFieldChoices/schemas.d.ts +1 -1
  38. package/dist/plugins/listInputFields/schemas.d.ts +1 -1
  39. package/dist/plugins/runAction/schemas.d.ts +1 -1
  40. package/dist/schemas/Action.d.ts +1 -1
  41. package/dist/sdk.d.ts +1 -0
  42. package/dist/sdk.d.ts.map +1 -1
  43. package/dist/sdk.test.js +5 -4
  44. package/dist/types/credentials.d.ts +65 -0
  45. package/dist/types/credentials.d.ts.map +1 -0
  46. package/dist/types/credentials.js +42 -0
  47. package/dist/types/properties.d.ts +1 -1
  48. package/dist/types/sdk.d.ts +12 -3
  49. package/dist/types/sdk.d.ts.map +1 -1
  50. package/dist/utils/logging.d.ts +13 -0
  51. package/dist/utils/logging.d.ts.map +1 -0
  52. package/dist/utils/logging.js +20 -0
  53. package/package.json +2 -2
@@ -0,0 +1,174 @@
1
+ /**
2
+ * Credentials Resolution
3
+ *
4
+ * This module provides the core logic for resolving credentials from various sources:
5
+ * - Explicit credentials option
6
+ * - Deprecated token option
7
+ * - Environment variables (new and deprecated)
8
+ * - CLI login stored tokens
9
+ */
10
+ import { isCredentialsFunction, isCredentialsObject, } from "./types/credentials";
11
+ import { logDeprecation } from "./utils/logging";
12
+ import { getZapierBaseUrl } from "./utils/url-utils";
13
+ import { ZAPIER_CREDENTIALS, ZAPIER_CREDENTIALS_CLIENT_ID, ZAPIER_CREDENTIALS_CLIENT_SECRET, ZAPIER_CREDENTIALS_BASE_URL, ZAPIER_CREDENTIALS_SCOPE, ZAPIER_TOKEN, ZAPIER_AUTH_BASE_URL, ZAPIER_AUTH_CLIENT_ID, } from "./constants";
14
+ /**
15
+ * Derive the auth base URL from the SDK base URL.
16
+ * Returns the Zapier root domain (e.g., https://zapier.com from https://api.zapier.com).
17
+ */
18
+ function deriveAuthBaseUrl(sdkBaseUrl) {
19
+ if (!sdkBaseUrl)
20
+ return undefined;
21
+ return getZapierBaseUrl(sdkBaseUrl);
22
+ }
23
+ /**
24
+ * Normalize a credentials object by ensuring it has a type and resolved baseUrl.
25
+ */
26
+ function normalizeCredentialsObject(obj, sdkBaseUrl) {
27
+ // Resolve baseUrl: use credentials baseUrl, or derive from SDK baseUrl
28
+ const resolvedBaseUrl = obj.baseUrl || deriveAuthBaseUrl(sdkBaseUrl);
29
+ // Explicitly construct the correct typed object to satisfy TypeScript
30
+ if (obj.type === "client_credentials" ||
31
+ ("clientSecret" in obj && obj.clientSecret)) {
32
+ return {
33
+ type: "client_credentials",
34
+ clientId: obj.clientId,
35
+ clientSecret: obj.clientSecret,
36
+ baseUrl: resolvedBaseUrl,
37
+ scope: obj.scope,
38
+ };
39
+ }
40
+ return {
41
+ type: "pkce",
42
+ clientId: obj.clientId,
43
+ baseUrl: resolvedBaseUrl,
44
+ scope: obj.scope,
45
+ };
46
+ }
47
+ /**
48
+ * Resolve credentials from environment variables.
49
+ *
50
+ * Precedence:
51
+ * 1. ZAPIER_CREDENTIALS (string token)
52
+ * 2. ZAPIER_CREDENTIALS_CLIENT_ID + ZAPIER_CREDENTIALS_CLIENT_SECRET (client credentials)
53
+ * 3. ZAPIER_CREDENTIALS_CLIENT_ID alone (PKCE)
54
+ * 4. Deprecated ZAPIER_TOKEN, ZAPIER_AUTH_* vars (with warnings)
55
+ *
56
+ * @param sdkBaseUrl - SDK base URL used to derive auth base URL if not specified
57
+ */
58
+ export function resolveCredentialsFromEnv(sdkBaseUrl) {
59
+ // 1. Check ZAPIER_CREDENTIALS (string token only)
60
+ if (ZAPIER_CREDENTIALS) {
61
+ return ZAPIER_CREDENTIALS;
62
+ }
63
+ // 2. Check ZAPIER_CREDENTIALS_* individual vars
64
+ if (ZAPIER_CREDENTIALS_CLIENT_ID) {
65
+ const resolvedBaseUrl = ZAPIER_CREDENTIALS_BASE_URL || deriveAuthBaseUrl(sdkBaseUrl);
66
+ // Infer type from presence of clientSecret
67
+ if (ZAPIER_CREDENTIALS_CLIENT_SECRET) {
68
+ return {
69
+ type: "client_credentials",
70
+ clientId: ZAPIER_CREDENTIALS_CLIENT_ID,
71
+ clientSecret: ZAPIER_CREDENTIALS_CLIENT_SECRET,
72
+ baseUrl: resolvedBaseUrl,
73
+ scope: ZAPIER_CREDENTIALS_SCOPE,
74
+ };
75
+ }
76
+ else {
77
+ return {
78
+ type: "pkce",
79
+ clientId: ZAPIER_CREDENTIALS_CLIENT_ID,
80
+ baseUrl: resolvedBaseUrl,
81
+ scope: ZAPIER_CREDENTIALS_SCOPE,
82
+ };
83
+ }
84
+ }
85
+ // 3. Check deprecated env vars (with warnings)
86
+ if (ZAPIER_TOKEN) {
87
+ logDeprecation("ZAPIER_TOKEN is deprecated. Use ZAPIER_CREDENTIALS instead.");
88
+ return ZAPIER_TOKEN;
89
+ }
90
+ if (ZAPIER_AUTH_CLIENT_ID) {
91
+ logDeprecation("ZAPIER_AUTH_CLIENT_ID is deprecated. Use ZAPIER_CREDENTIALS_CLIENT_ID instead.");
92
+ if (ZAPIER_AUTH_BASE_URL) {
93
+ logDeprecation("ZAPIER_AUTH_BASE_URL is deprecated. Use ZAPIER_CREDENTIALS_BASE_URL instead.");
94
+ }
95
+ const resolvedBaseUrl = ZAPIER_AUTH_BASE_URL || deriveAuthBaseUrl(sdkBaseUrl);
96
+ return {
97
+ type: "pkce",
98
+ clientId: ZAPIER_AUTH_CLIENT_ID,
99
+ baseUrl: resolvedBaseUrl,
100
+ };
101
+ }
102
+ return undefined;
103
+ }
104
+ /**
105
+ * Resolve credentials from all possible sources.
106
+ *
107
+ * Precedence:
108
+ * 1. Explicit credentials option
109
+ * 2. Deprecated token option (with warning)
110
+ * 3. Environment variables
111
+ * 4. CLI login stored token (handled separately in auth.ts)
112
+ *
113
+ * If credentials is a function, it is called and must return
114
+ * a string or credentials object (not another function).
115
+ *
116
+ * The baseUrl option is used to derive the auth base URL if not
117
+ * specified in credentials.
118
+ */
119
+ export async function resolveCredentials(options = {}) {
120
+ const { baseUrl } = options;
121
+ // 1. Check explicit credentials option
122
+ if (options.credentials !== undefined) {
123
+ // If it's a function, call it
124
+ if (isCredentialsFunction(options.credentials)) {
125
+ const resolved = await options.credentials();
126
+ // Validate that the function didn't return another function
127
+ if (typeof resolved === "function") {
128
+ throw new Error("Credentials function returned another function. " +
129
+ "Credentials functions must return a string or credentials object.");
130
+ }
131
+ // Normalize object if needed
132
+ if (isCredentialsObject(resolved)) {
133
+ return normalizeCredentialsObject(resolved, baseUrl);
134
+ }
135
+ return resolved;
136
+ }
137
+ // If it's an object, normalize it
138
+ if (isCredentialsObject(options.credentials)) {
139
+ return normalizeCredentialsObject(options.credentials, baseUrl);
140
+ }
141
+ // It's a string token
142
+ return options.credentials;
143
+ }
144
+ // 2. Check deprecated token option (with warning)
145
+ if (options.token !== undefined) {
146
+ logDeprecation("The `token` option is deprecated. Use `credentials` instead.");
147
+ return options.token;
148
+ }
149
+ // 3. Check environment variables
150
+ const envCredentials = resolveCredentialsFromEnv(baseUrl);
151
+ if (envCredentials !== undefined) {
152
+ return envCredentials;
153
+ }
154
+ // 4. CLI login is handled separately in auth.ts
155
+ return undefined;
156
+ }
157
+ /**
158
+ * Extract the base URL from credentials for use in auth flows.
159
+ */
160
+ export function getBaseUrlFromCredentials(credentials) {
161
+ if (credentials && isCredentialsObject(credentials)) {
162
+ return credentials.baseUrl;
163
+ }
164
+ return undefined;
165
+ }
166
+ /**
167
+ * Extract the client ID from credentials for use in auth flows.
168
+ */
169
+ export function getClientIdFromCredentials(credentials) {
170
+ if (credentials && isCredentialsObject(credentials)) {
171
+ return credentials.clientId;
172
+ }
173
+ return undefined;
174
+ }
package/dist/index.cjs CHANGED
@@ -58,6 +58,14 @@ function isPositional(schema) {
58
58
  // src/constants.ts
59
59
  var ZAPIER_BASE_URL = process.env.ZAPIER_BASE_URL || "https://zapier.com";
60
60
  var MAX_PAGE_LIMIT = 1e4;
61
+ var ZAPIER_CREDENTIALS = process.env.ZAPIER_CREDENTIALS;
62
+ var ZAPIER_CREDENTIALS_CLIENT_ID = process.env.ZAPIER_CREDENTIALS_CLIENT_ID;
63
+ var ZAPIER_CREDENTIALS_CLIENT_SECRET = process.env.ZAPIER_CREDENTIALS_CLIENT_SECRET;
64
+ var ZAPIER_CREDENTIALS_BASE_URL = process.env.ZAPIER_CREDENTIALS_BASE_URL;
65
+ var ZAPIER_CREDENTIALS_SCOPE = process.env.ZAPIER_CREDENTIALS_SCOPE;
66
+ var ZAPIER_TOKEN = process.env.ZAPIER_TOKEN;
67
+ var ZAPIER_AUTH_BASE_URL = process.env.ZAPIER_AUTH_BASE_URL;
68
+ var ZAPIER_AUTH_CLIENT_ID = process.env.ZAPIER_AUTH_CLIENT_ID;
61
69
 
62
70
  // src/types/properties.ts
63
71
  var AppKeyPropertySchema = withPositional(
@@ -3465,36 +3473,29 @@ async function pollUntilComplete(options) {
3465
3473
  }
3466
3474
  }
3467
3475
 
3468
- // src/auth.ts
3469
- function getTokenFromEnv() {
3470
- return process.env.ZAPIER_TOKEN;
3476
+ // src/types/credentials.ts
3477
+ function isClientCredentials(credentials) {
3478
+ return typeof credentials === "object" && credentials !== null && "clientId" in credentials && "clientSecret" in credentials;
3471
3479
  }
3472
- async function getTokenFromCliLogin(options = {}) {
3473
- try {
3474
- const { getToken } = await import('@zapier/zapier-sdk-cli-login');
3475
- return await getToken(options);
3476
- } catch {
3477
- return void 0;
3478
- }
3480
+ function isPkceCredentials(credentials) {
3481
+ return typeof credentials === "object" && credentials !== null && "clientId" in credentials && !("clientSecret" in credentials);
3479
3482
  }
3480
- async function getTokenFromEnvOrConfig(options = {}) {
3481
- const envToken = getTokenFromEnv();
3482
- if (envToken) {
3483
- return envToken;
3484
- }
3485
- return getTokenFromCliLogin(options);
3483
+ function isCredentialsObject(credentials) {
3484
+ return typeof credentials === "object" && credentials !== null && "clientId" in credentials;
3486
3485
  }
3487
- async function resolveAuthToken(options = {}) {
3488
- if (options.token) {
3489
- return options.token;
3490
- }
3491
- if (options.getToken) {
3492
- const token = await options.getToken();
3493
- if (token) {
3494
- return token;
3495
- }
3496
- }
3497
- return getTokenFromEnvOrConfig(options);
3486
+ function isCredentialsFunction(credentials) {
3487
+ return typeof credentials === "function";
3488
+ }
3489
+
3490
+ // src/utils/logging.ts
3491
+ var loggedDeprecations = /* @__PURE__ */ new Set();
3492
+ function logDeprecation(message) {
3493
+ if (loggedDeprecations.has(message)) return;
3494
+ loggedDeprecations.add(message);
3495
+ console.warn(`[zapier-sdk] Deprecation: ${message}`);
3496
+ }
3497
+ function resetDeprecationWarnings() {
3498
+ loggedDeprecations.clear();
3498
3499
  }
3499
3500
 
3500
3501
  // src/utils/url-utils.ts
@@ -3543,6 +3544,278 @@ function getTrackingBaseUrl({
3543
3544
  return ZAPIER_BASE_URL;
3544
3545
  }
3545
3546
 
3547
+ // src/credentials.ts
3548
+ function deriveAuthBaseUrl(sdkBaseUrl) {
3549
+ if (!sdkBaseUrl) return void 0;
3550
+ return getZapierBaseUrl(sdkBaseUrl);
3551
+ }
3552
+ function normalizeCredentialsObject(obj, sdkBaseUrl) {
3553
+ const resolvedBaseUrl = obj.baseUrl || deriveAuthBaseUrl(sdkBaseUrl);
3554
+ if (obj.type === "client_credentials" || "clientSecret" in obj && obj.clientSecret) {
3555
+ return {
3556
+ type: "client_credentials",
3557
+ clientId: obj.clientId,
3558
+ clientSecret: obj.clientSecret,
3559
+ baseUrl: resolvedBaseUrl,
3560
+ scope: obj.scope
3561
+ };
3562
+ }
3563
+ return {
3564
+ type: "pkce",
3565
+ clientId: obj.clientId,
3566
+ baseUrl: resolvedBaseUrl,
3567
+ scope: obj.scope
3568
+ };
3569
+ }
3570
+ function resolveCredentialsFromEnv(sdkBaseUrl) {
3571
+ if (ZAPIER_CREDENTIALS) {
3572
+ return ZAPIER_CREDENTIALS;
3573
+ }
3574
+ if (ZAPIER_CREDENTIALS_CLIENT_ID) {
3575
+ const resolvedBaseUrl = ZAPIER_CREDENTIALS_BASE_URL || deriveAuthBaseUrl(sdkBaseUrl);
3576
+ if (ZAPIER_CREDENTIALS_CLIENT_SECRET) {
3577
+ return {
3578
+ type: "client_credentials",
3579
+ clientId: ZAPIER_CREDENTIALS_CLIENT_ID,
3580
+ clientSecret: ZAPIER_CREDENTIALS_CLIENT_SECRET,
3581
+ baseUrl: resolvedBaseUrl,
3582
+ scope: ZAPIER_CREDENTIALS_SCOPE
3583
+ };
3584
+ } else {
3585
+ return {
3586
+ type: "pkce",
3587
+ clientId: ZAPIER_CREDENTIALS_CLIENT_ID,
3588
+ baseUrl: resolvedBaseUrl,
3589
+ scope: ZAPIER_CREDENTIALS_SCOPE
3590
+ };
3591
+ }
3592
+ }
3593
+ if (ZAPIER_TOKEN) {
3594
+ logDeprecation(
3595
+ "ZAPIER_TOKEN is deprecated. Use ZAPIER_CREDENTIALS instead."
3596
+ );
3597
+ return ZAPIER_TOKEN;
3598
+ }
3599
+ if (ZAPIER_AUTH_CLIENT_ID) {
3600
+ logDeprecation(
3601
+ "ZAPIER_AUTH_CLIENT_ID is deprecated. Use ZAPIER_CREDENTIALS_CLIENT_ID instead."
3602
+ );
3603
+ if (ZAPIER_AUTH_BASE_URL) {
3604
+ logDeprecation(
3605
+ "ZAPIER_AUTH_BASE_URL is deprecated. Use ZAPIER_CREDENTIALS_BASE_URL instead."
3606
+ );
3607
+ }
3608
+ const resolvedBaseUrl = ZAPIER_AUTH_BASE_URL || deriveAuthBaseUrl(sdkBaseUrl);
3609
+ return {
3610
+ type: "pkce",
3611
+ clientId: ZAPIER_AUTH_CLIENT_ID,
3612
+ baseUrl: resolvedBaseUrl
3613
+ };
3614
+ }
3615
+ return void 0;
3616
+ }
3617
+ async function resolveCredentials(options = {}) {
3618
+ const { baseUrl } = options;
3619
+ if (options.credentials !== void 0) {
3620
+ if (isCredentialsFunction(options.credentials)) {
3621
+ const resolved = await options.credentials();
3622
+ if (typeof resolved === "function") {
3623
+ throw new Error(
3624
+ "Credentials function returned another function. Credentials functions must return a string or credentials object."
3625
+ );
3626
+ }
3627
+ if (isCredentialsObject(resolved)) {
3628
+ return normalizeCredentialsObject(resolved, baseUrl);
3629
+ }
3630
+ return resolved;
3631
+ }
3632
+ if (isCredentialsObject(options.credentials)) {
3633
+ return normalizeCredentialsObject(options.credentials, baseUrl);
3634
+ }
3635
+ return options.credentials;
3636
+ }
3637
+ if (options.token !== void 0) {
3638
+ logDeprecation(
3639
+ "The `token` option is deprecated. Use `credentials` instead."
3640
+ );
3641
+ return options.token;
3642
+ }
3643
+ const envCredentials = resolveCredentialsFromEnv(baseUrl);
3644
+ if (envCredentials !== void 0) {
3645
+ return envCredentials;
3646
+ }
3647
+ return void 0;
3648
+ }
3649
+ function getBaseUrlFromCredentials(credentials) {
3650
+ if (credentials && isCredentialsObject(credentials)) {
3651
+ return credentials.baseUrl;
3652
+ }
3653
+ return void 0;
3654
+ }
3655
+ function getClientIdFromCredentials(credentials) {
3656
+ if (credentials && isCredentialsObject(credentials)) {
3657
+ return credentials.clientId;
3658
+ }
3659
+ return void 0;
3660
+ }
3661
+
3662
+ // src/auth.ts
3663
+ var tokenCache = /* @__PURE__ */ new Map();
3664
+ var pendingExchanges = /* @__PURE__ */ new Map();
3665
+ function clearTokenCache() {
3666
+ tokenCache.clear();
3667
+ pendingExchanges.clear();
3668
+ }
3669
+ var TOKEN_EXPIRATION_BUFFER = 5 * 60 * 1e3;
3670
+ function getCachedToken(clientId) {
3671
+ const cached = tokenCache.get(clientId);
3672
+ if (!cached) return void 0;
3673
+ if (cached.expiresAt > Date.now() + TOKEN_EXPIRATION_BUFFER) {
3674
+ return cached.accessToken;
3675
+ }
3676
+ tokenCache.delete(clientId);
3677
+ return void 0;
3678
+ }
3679
+ function cacheToken(clientId, accessToken, expiresIn) {
3680
+ tokenCache.set(clientId, {
3681
+ accessToken,
3682
+ expiresAt: Date.now() + expiresIn * 1e3
3683
+ });
3684
+ }
3685
+ function invalidateCachedToken(clientId) {
3686
+ tokenCache.delete(clientId);
3687
+ }
3688
+ function getTokenEndpointUrl(baseUrl) {
3689
+ const base = baseUrl || ZAPIER_BASE_URL;
3690
+ return `${base}/oauth/token/`;
3691
+ }
3692
+ async function exchangeClientCredentials(options) {
3693
+ const { clientId, clientSecret, baseUrl, scope, onEvent } = options;
3694
+ const fetchFn = options.fetch || globalThis.fetch;
3695
+ const tokenUrl = getTokenEndpointUrl(baseUrl);
3696
+ onEvent?.({
3697
+ type: "auth_exchanging",
3698
+ payload: {
3699
+ message: "Exchanging client credentials for token...",
3700
+ operation: "client_credentials"
3701
+ },
3702
+ timestamp: Date.now()
3703
+ });
3704
+ const response = await fetchFn(tokenUrl, {
3705
+ method: "POST",
3706
+ headers: {
3707
+ "Content-Type": "application/x-www-form-urlencoded"
3708
+ },
3709
+ body: new URLSearchParams({
3710
+ grant_type: "client_credentials",
3711
+ client_id: clientId,
3712
+ client_secret: clientSecret,
3713
+ scope: scope || "external",
3714
+ audience: "zapier.com"
3715
+ })
3716
+ });
3717
+ if (!response.ok) {
3718
+ const errorText = await response.text();
3719
+ onEvent?.({
3720
+ type: "auth_error",
3721
+ payload: {
3722
+ message: `Client credentials exchange failed: ${response.status}`,
3723
+ error: errorText,
3724
+ operation: "client_credentials"
3725
+ },
3726
+ timestamp: Date.now()
3727
+ });
3728
+ throw new Error(
3729
+ `Client credentials exchange failed: ${response.status} ${response.statusText}`
3730
+ );
3731
+ }
3732
+ const data = await response.json();
3733
+ if (!data.access_token) {
3734
+ throw new Error("Client credentials response missing access_token");
3735
+ }
3736
+ const expiresIn = data.expires_in || 3600;
3737
+ cacheToken(clientId, data.access_token, expiresIn);
3738
+ onEvent?.({
3739
+ type: "auth_success",
3740
+ payload: {
3741
+ message: "Client credentials exchange successful",
3742
+ operation: "client_credentials"
3743
+ },
3744
+ timestamp: Date.now()
3745
+ });
3746
+ return data.access_token;
3747
+ }
3748
+ async function getTokenFromCliLogin(options) {
3749
+ try {
3750
+ const cliLogin = await import('@zapier/zapier-sdk-cli-login');
3751
+ return await cliLogin.getToken(options);
3752
+ } catch {
3753
+ return void 0;
3754
+ }
3755
+ }
3756
+ async function resolveAuthToken(options = {}) {
3757
+ const credentials = await resolveCredentials({
3758
+ credentials: options.credentials,
3759
+ token: options.token
3760
+ });
3761
+ if (credentials !== void 0) {
3762
+ return resolveAuthTokenFromCredentials(credentials, options);
3763
+ }
3764
+ return getTokenFromCliLogin({
3765
+ onEvent: options.onEvent,
3766
+ fetch: options.fetch
3767
+ });
3768
+ }
3769
+ async function resolveAuthTokenFromCredentials(credentials, options) {
3770
+ if (typeof credentials === "string") {
3771
+ return credentials;
3772
+ }
3773
+ if (isClientCredentials(credentials)) {
3774
+ const { clientId } = credentials;
3775
+ const cached = getCachedToken(clientId);
3776
+ if (cached) {
3777
+ return cached;
3778
+ }
3779
+ const pending = pendingExchanges.get(clientId);
3780
+ if (pending) {
3781
+ return pending;
3782
+ }
3783
+ const exchangePromise = exchangeClientCredentials({
3784
+ clientId: credentials.clientId,
3785
+ clientSecret: credentials.clientSecret,
3786
+ baseUrl: credentials.baseUrl || options.baseUrl,
3787
+ scope: credentials.scope,
3788
+ fetch: options.fetch,
3789
+ onEvent: options.onEvent
3790
+ }).finally(() => {
3791
+ pendingExchanges.delete(clientId);
3792
+ });
3793
+ pendingExchanges.set(clientId, exchangePromise);
3794
+ return exchangePromise;
3795
+ }
3796
+ if (isPkceCredentials(credentials)) {
3797
+ const storedToken = await getTokenFromCliLogin({
3798
+ onEvent: options.onEvent,
3799
+ fetch: options.fetch,
3800
+ credentials
3801
+ });
3802
+ if (storedToken) {
3803
+ return storedToken;
3804
+ }
3805
+ throw new Error(
3806
+ "PKCE credentials require interactive login. Please run the 'login' command with the CLI first, or use client_credentials flow."
3807
+ );
3808
+ }
3809
+ throw new Error("Unknown credentials type");
3810
+ }
3811
+ async function invalidateCredentialsToken(options) {
3812
+ const resolved = await resolveCredentials(options);
3813
+ const clientId = getClientIdFromCredentials(resolved);
3814
+ if (clientId) {
3815
+ invalidateCachedToken(clientId);
3816
+ }
3817
+ }
3818
+
3546
3819
  // src/api/client.ts
3547
3820
  var pathConfig = {
3548
3821
  // e.g. /relay -> https://sdkapi.zapier.com/api/v0/sdk/relay/...
@@ -3600,13 +3873,11 @@ var ZapierApiClient = class {
3600
3873
  // Helper to get a token from the different places it could be gotten
3601
3874
  async getAuthToken() {
3602
3875
  return resolveAuthToken({
3876
+ credentials: this.options.credentials,
3603
3877
  token: this.options.token,
3604
- getToken: this.options.getToken,
3605
3878
  onEvent: this.options.onEvent,
3606
3879
  fetch: this.options.fetch,
3607
- baseUrl: this.options.baseUrl,
3608
- authBaseUrl: this.options.authBaseUrl,
3609
- authClientId: this.options.authClientId
3880
+ baseUrl: this.options.baseUrl
3610
3881
  });
3611
3882
  }
3612
3883
  // Helper to handle responses
@@ -3644,10 +3915,16 @@ var ZapierApiClient = class {
3644
3915
  if (response.status === 401 || response.status === 403) {
3645
3916
  if (wasMissingAuthToken) {
3646
3917
  throw new ZapierAuthenticationError(
3647
- `Authentication required (HTTP ${response.status}). Please provide a token in options or set ZAPIER_TOKEN environment variable.`,
3918
+ `Authentication required (HTTP ${response.status}). Please provide credentials in options or set ZAPIER_CREDENTIALS environment variable.`,
3648
3919
  errorOptions
3649
3920
  );
3650
3921
  }
3922
+ if (response.status === 401) {
3923
+ await invalidateCredentialsToken({
3924
+ credentials: this.options.credentials,
3925
+ token: this.options.token
3926
+ });
3927
+ }
3651
3928
  throw new ZapierAuthenticationError(message, errorOptions);
3652
3929
  }
3653
3930
  if (response.status === 400) {
@@ -3783,7 +4060,7 @@ var ZapierApiClient = class {
3783
4060
  if (options.authRequired) {
3784
4061
  if (headers.get("Authorization") == null && authToken == null) {
3785
4062
  throw new ZapierAuthenticationError(
3786
- `Authentication required but no token available. Please set ZAPIER_TOKEN, or run the 'login' command with the CLI.`
4063
+ `Authentication required but no credentials available. Please set ZAPIER_CREDENTIALS, or run the 'login' command with the CLI.`
3787
4064
  );
3788
4065
  }
3789
4066
  }
@@ -3859,27 +4136,28 @@ var apiPlugin = (params) => {
3859
4136
  const {
3860
4137
  fetch: customFetch = globalThis.fetch,
3861
4138
  baseUrl = ZAPIER_BASE_URL,
3862
- authBaseUrl,
3863
- authClientId,
4139
+ credentials,
3864
4140
  token,
3865
- getToken,
3866
4141
  onEvent,
3867
4142
  debug = false
3868
4143
  } = params.context.options;
3869
4144
  const api = createZapierApi({
3870
4145
  baseUrl,
3871
- authBaseUrl,
3872
- authClientId,
4146
+ credentials,
3873
4147
  token,
3874
- getToken,
3875
4148
  debug,
3876
4149
  fetch: customFetch,
3877
4150
  onEvent
3878
4151
  });
3879
4152
  return {
3880
4153
  context: {
3881
- api
4154
+ api,
3882
4155
  // Provide API client in context for other plugins to use
4156
+ resolveCredentials: () => resolveCredentials({
4157
+ credentials,
4158
+ token,
4159
+ baseUrl
4160
+ })
3883
4161
  }
3884
4162
  };
3885
4163
  };
@@ -4473,7 +4751,7 @@ function getCpuTime() {
4473
4751
 
4474
4752
  // package.json
4475
4753
  var package_default = {
4476
- version: "0.18.4"};
4754
+ version: "1.0.0"};
4477
4755
 
4478
4756
  // src/plugins/eventEmission/builders.ts
4479
4757
  function createBaseEvent(context = {}) {
@@ -4646,11 +4924,9 @@ var eventEmissionPlugin = ({ context }) => {
4646
4924
  const getUserContext = (async () => {
4647
4925
  try {
4648
4926
  const token = await resolveAuthToken({
4927
+ credentials: context.options.credentials,
4649
4928
  token: context.options.token,
4650
- getToken: context.options.getToken,
4651
4929
  baseUrl: context.options.baseUrl,
4652
- authBaseUrl: context.options.authBaseUrl,
4653
- authClientId: context.options.authClientId,
4654
4930
  onEvent: context.options.onEvent,
4655
4931
  fetch: context.options.fetch
4656
4932
  });
@@ -4929,7 +5205,15 @@ exports.OutputPropertySchema = OutputPropertySchema;
4929
5205
  exports.ParamsPropertySchema = ParamsPropertySchema;
4930
5206
  exports.RelayFetchSchema = RelayFetchSchema;
4931
5207
  exports.RelayRequestSchema = RelayRequestSchema;
5208
+ exports.ZAPIER_AUTH_BASE_URL = ZAPIER_AUTH_BASE_URL;
5209
+ exports.ZAPIER_AUTH_CLIENT_ID = ZAPIER_AUTH_CLIENT_ID;
4932
5210
  exports.ZAPIER_BASE_URL = ZAPIER_BASE_URL;
5211
+ exports.ZAPIER_CREDENTIALS = ZAPIER_CREDENTIALS;
5212
+ exports.ZAPIER_CREDENTIALS_BASE_URL = ZAPIER_CREDENTIALS_BASE_URL;
5213
+ exports.ZAPIER_CREDENTIALS_CLIENT_ID = ZAPIER_CREDENTIALS_CLIENT_ID;
5214
+ exports.ZAPIER_CREDENTIALS_CLIENT_SECRET = ZAPIER_CREDENTIALS_CLIENT_SECRET;
5215
+ exports.ZAPIER_CREDENTIALS_SCOPE = ZAPIER_CREDENTIALS_SCOPE;
5216
+ exports.ZAPIER_TOKEN = ZAPIER_TOKEN;
4933
5217
  exports.ZapierActionError = ZapierActionError;
4934
5218
  exports.ZapierApiError = ZapierApiError;
4935
5219
  exports.ZapierAppNotFoundError = ZapierAppNotFoundError;
@@ -4954,6 +5238,7 @@ exports.buildApplicationLifecycleEvent = buildApplicationLifecycleEvent;
4954
5238
  exports.buildErrorEvent = buildErrorEvent;
4955
5239
  exports.buildErrorEventWithContext = buildErrorEventWithContext;
4956
5240
  exports.buildMethodCalledEvent = buildMethodCalledEvent;
5241
+ exports.clearTokenCache = clearTokenCache;
4957
5242
  exports.createBaseEvent = createBaseEvent;
4958
5243
  exports.createFunction = createFunction;
4959
5244
  exports.createSdk = createSdk;
@@ -4968,7 +5253,9 @@ exports.generateEventId = generateEventId;
4968
5253
  exports.getActionPlugin = getActionPlugin;
4969
5254
  exports.getAppPlugin = getAppPlugin;
4970
5255
  exports.getAuthenticationPlugin = getAuthenticationPlugin;
5256
+ exports.getBaseUrlFromCredentials = getBaseUrlFromCredentials;
4971
5257
  exports.getCiPlatform = getCiPlatform;
5258
+ exports.getClientIdFromCredentials = getClientIdFromCredentials;
4972
5259
  exports.getCpuTime = getCpuTime;
4973
5260
  exports.getCurrentTimestamp = getCurrentTimestamp;
4974
5261
  exports.getMemoryUsage = getMemoryUsage;
@@ -4978,22 +5265,30 @@ exports.getPreferredManifestEntryKey = getPreferredManifestEntryKey;
4978
5265
  exports.getProfilePlugin = getProfilePlugin;
4979
5266
  exports.getReleaseId = getReleaseId;
4980
5267
  exports.getTokenFromCliLogin = getTokenFromCliLogin;
4981
- exports.getTokenFromEnv = getTokenFromEnv;
4982
- exports.getTokenFromEnvOrConfig = getTokenFromEnvOrConfig;
4983
5268
  exports.inputFieldKeyResolver = inputFieldKeyResolver;
4984
5269
  exports.inputsAllOptionalResolver = inputsAllOptionalResolver;
4985
5270
  exports.inputsResolver = inputsResolver;
5271
+ exports.invalidateCachedToken = invalidateCachedToken;
5272
+ exports.invalidateCredentialsToken = invalidateCredentialsToken;
4986
5273
  exports.isCi = isCi;
5274
+ exports.isClientCredentials = isClientCredentials;
5275
+ exports.isCredentialsFunction = isCredentialsFunction;
5276
+ exports.isCredentialsObject = isCredentialsObject;
5277
+ exports.isPkceCredentials = isPkceCredentials;
4987
5278
  exports.isPositional = isPositional;
4988
5279
  exports.listActionsPlugin = listActionsPlugin;
4989
5280
  exports.listAppsPlugin = listAppsPlugin;
4990
5281
  exports.listAuthenticationsPlugin = listAuthenticationsPlugin;
4991
5282
  exports.listInputFieldsPlugin = listInputFieldsPlugin;
5283
+ exports.logDeprecation = logDeprecation;
4992
5284
  exports.manifestPlugin = manifestPlugin;
4993
5285
  exports.readManifestFromFile = readManifestFromFile;
4994
5286
  exports.registryPlugin = registryPlugin;
4995
5287
  exports.requestPlugin = requestPlugin;
5288
+ exports.resetDeprecationWarnings = resetDeprecationWarnings;
4996
5289
  exports.resolveAuthToken = resolveAuthToken;
5290
+ exports.resolveCredentials = resolveCredentials;
5291
+ exports.resolveCredentialsFromEnv = resolveCredentialsFromEnv;
4997
5292
  exports.runActionPlugin = runActionPlugin;
4998
5293
  exports.toSnakeCase = toSnakeCase;
4999
5294
  exports.toTitleCase = toTitleCase;