@zapier/zapier-sdk 0.1.1 → 0.2.1
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/api/client.js +23 -6
- package/dist/api/index.d.ts +4 -2
- package/dist/api/index.js +5 -4
- package/dist/api/types.d.ts +3 -1
- package/dist/auth.d.ts +59 -0
- package/dist/auth.js +261 -0
- package/dist/functions/getAction/info.d.ts +3 -3
- package/dist/functions/getAction/schemas.d.ts +3 -3
- package/dist/functions/getAuthentication/index.d.ts +13 -0
- package/dist/functions/getAuthentication/index.js +38 -0
- package/dist/functions/getAuthentication/info.d.ts +12 -0
- package/dist/functions/getAuthentication/info.js +11 -0
- package/dist/functions/getAuthentication/schemas.d.ts +26 -0
- package/dist/functions/getAuthentication/schemas.js +16 -0
- package/dist/functions/listActions/info.d.ts +3 -3
- package/dist/functions/listActions/schemas.d.ts +3 -3
- package/dist/functions/listAuthentications/index.js +1 -4
- package/dist/functions/listAuthentications/schemas.d.ts +2 -0
- package/dist/functions/listFields/info.d.ts +3 -3
- package/dist/functions/listFields/schemas.d.ts +3 -3
- package/dist/functions/runAction/index.js +8 -79
- package/dist/functions/runAction/info.d.ts +3 -3
- package/dist/functions/runAction/schemas.d.ts +5 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/dist/sdk.js +40 -60
- package/dist/types/domain.d.ts +3 -0
- package/dist/types/events.d.ts +37 -0
- package/dist/types/events.js +8 -0
- package/dist/types/properties.d.ts +1 -1
- package/dist/types/properties.js +10 -1
- package/dist/types/sdk.d.ts +2 -1
- package/package.json +4 -3
- package/src/api/client.ts +31 -5
- package/src/api/index.ts +12 -5
- package/src/api/types.ts +11 -1
- package/src/auth.ts +340 -0
- package/src/functions/getAuthentication/index.ts +51 -0
- package/src/functions/getAuthentication/info.ts +9 -0
- package/src/functions/getAuthentication/schemas.ts +43 -0
- package/src/functions/listAuthentications/index.ts +1 -8
- package/src/functions/listAuthentications/schemas.ts +2 -0
- package/src/functions/runAction/index.ts +11 -141
- package/src/functions/runAction/schemas.ts +2 -0
- package/src/index.ts +4 -0
- package/src/sdk.ts +35 -87
- package/src/types/domain.ts +5 -0
- package/src/types/events.ts +43 -0
- package/src/types/properties.ts +10 -1
- package/src/types/sdk.ts +2 -0
package/src/api/client.ts
CHANGED
|
@@ -14,11 +14,13 @@ import type {
|
|
|
14
14
|
import { getAuthorizationHeader } from "./auth";
|
|
15
15
|
import { createDebugLogger, createDebugFetch } from "./debug";
|
|
16
16
|
import { pollUntilComplete } from "./polling";
|
|
17
|
+
import { getTokenFromEnv } from "../auth";
|
|
17
18
|
|
|
18
19
|
export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
19
20
|
const {
|
|
20
21
|
baseUrl,
|
|
21
22
|
token,
|
|
23
|
+
getToken,
|
|
22
24
|
debug = false,
|
|
23
25
|
fetch: originalFetch = globalThis.fetch,
|
|
24
26
|
} = options;
|
|
@@ -41,14 +43,29 @@ export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
// Helper to build headers
|
|
44
|
-
function buildHeaders(
|
|
46
|
+
async function buildHeaders(
|
|
47
|
+
options: RequestOptions = {},
|
|
48
|
+
): Promise<Record<string, string>> {
|
|
45
49
|
const headers: Record<string, string> = {
|
|
46
50
|
...options.headers,
|
|
47
51
|
};
|
|
48
52
|
|
|
49
53
|
// Add auth header if token provided and not explicitly disabled
|
|
50
|
-
if (
|
|
51
|
-
|
|
54
|
+
if (options.authRequired !== false) {
|
|
55
|
+
let resolvedToken = token;
|
|
56
|
+
|
|
57
|
+
// Token resolution precedence: explicit token > getToken() > env var
|
|
58
|
+
if (!resolvedToken && getToken) {
|
|
59
|
+
resolvedToken = await getToken();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!resolvedToken) {
|
|
63
|
+
resolvedToken = getTokenFromEnv();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (resolvedToken) {
|
|
67
|
+
headers.Authorization = getAuthorizationHeader(resolvedToken);
|
|
68
|
+
}
|
|
52
69
|
}
|
|
53
70
|
|
|
54
71
|
return headers;
|
|
@@ -87,7 +104,7 @@ export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
|
87
104
|
options: RequestOptions = {},
|
|
88
105
|
): Promise<any> {
|
|
89
106
|
const url = buildUrl(path, options.searchParams);
|
|
90
|
-
const headers = buildHeaders(options);
|
|
107
|
+
const headers = await buildHeaders(options);
|
|
91
108
|
|
|
92
109
|
// Add Content-Type for JSON requests with body data
|
|
93
110
|
if (data && typeof data === "object") {
|
|
@@ -130,7 +147,7 @@ export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
|
130
147
|
|
|
131
148
|
async poll(path: string, options: PollOptions = {}): Promise<any> {
|
|
132
149
|
const url = buildUrl(path, options.searchParams);
|
|
133
|
-
const headers = buildHeaders(options);
|
|
150
|
+
const headers = await buildHeaders(options);
|
|
134
151
|
|
|
135
152
|
return pollUntilComplete({
|
|
136
153
|
fetch,
|
|
@@ -144,5 +161,14 @@ export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
|
144
161
|
resultExtractor: options.resultExtractor,
|
|
145
162
|
});
|
|
146
163
|
},
|
|
164
|
+
|
|
165
|
+
requireAuthTo(operation: string): void {
|
|
166
|
+
// Check if any authentication method is available
|
|
167
|
+
if (!token && !getToken && !getTokenFromEnv()) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`Authentication token is required to ${operation}. Please provide token in options or set ZAPIER_TOKEN environment variable.`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
},
|
|
147
173
|
};
|
|
148
174
|
}
|
package/src/api/index.ts
CHANGED
|
@@ -29,6 +29,9 @@ export type {
|
|
|
29
29
|
AuthenticationsResponse,
|
|
30
30
|
} from "./types";
|
|
31
31
|
|
|
32
|
+
// Import for local use
|
|
33
|
+
import type { ApiClient } from "./types";
|
|
34
|
+
|
|
32
35
|
// Re-export authentication utilities
|
|
33
36
|
export { isJwt, getAuthorizationHeader } from "./auth";
|
|
34
37
|
|
|
@@ -41,6 +44,9 @@ export { pollUntilComplete } from "./polling";
|
|
|
41
44
|
// Re-export the main client factory
|
|
42
45
|
export { createZapierApi } from "./client";
|
|
43
46
|
|
|
47
|
+
// Import for local use
|
|
48
|
+
import { createZapierApi } from "./client";
|
|
49
|
+
|
|
44
50
|
// Utility Functions
|
|
45
51
|
export function generateRequestId(): string {
|
|
46
52
|
return Math.random().toString(36).substring(2) + Date.now().toString(36);
|
|
@@ -55,13 +61,15 @@ export function generateRequestId(): string {
|
|
|
55
61
|
export function getOrCreateApiClient(config: {
|
|
56
62
|
baseUrl?: string;
|
|
57
63
|
token?: string;
|
|
58
|
-
|
|
64
|
+
getToken?: () => Promise<string | undefined>;
|
|
65
|
+
api?: ApiClient;
|
|
59
66
|
debug?: boolean;
|
|
60
67
|
fetch?: typeof globalThis.fetch;
|
|
61
|
-
}):
|
|
68
|
+
}): ApiClient {
|
|
62
69
|
const {
|
|
63
70
|
baseUrl = "https://zapier.com",
|
|
64
|
-
token
|
|
71
|
+
token,
|
|
72
|
+
getToken,
|
|
65
73
|
api: providedApi,
|
|
66
74
|
debug = false,
|
|
67
75
|
fetch: customFetch,
|
|
@@ -72,11 +80,10 @@ export function getOrCreateApiClient(config: {
|
|
|
72
80
|
return providedApi;
|
|
73
81
|
}
|
|
74
82
|
|
|
75
|
-
// Import createZapierApi locally to avoid circular imports
|
|
76
|
-
const { createZapierApi } = require("./client");
|
|
77
83
|
return createZapierApi({
|
|
78
84
|
baseUrl,
|
|
79
85
|
token,
|
|
86
|
+
getToken,
|
|
80
87
|
debug,
|
|
81
88
|
fetch: customFetch,
|
|
82
89
|
});
|
package/src/api/types.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
export interface ApiClientOptions {
|
|
14
14
|
baseUrl: string;
|
|
15
15
|
token?: string;
|
|
16
|
+
getToken?: () => Promise<string | undefined>;
|
|
16
17
|
debug?: boolean;
|
|
17
18
|
fetch?: typeof globalThis.fetch;
|
|
18
19
|
}
|
|
@@ -23,6 +24,7 @@ export interface ApiClient {
|
|
|
23
24
|
put: (path: string, data?: any, options?: RequestOptions) => Promise<any>;
|
|
24
25
|
delete: (path: string, options?: RequestOptions) => Promise<any>;
|
|
25
26
|
poll: (path: string, options?: PollOptions) => Promise<any>;
|
|
27
|
+
requireAuthTo: (operation: string) => void;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
export interface RequestOptions {
|
|
@@ -65,7 +67,15 @@ export interface Action {
|
|
|
65
67
|
name: string;
|
|
66
68
|
description: string;
|
|
67
69
|
appKey: string;
|
|
68
|
-
type:
|
|
70
|
+
type:
|
|
71
|
+
| "read"
|
|
72
|
+
| "read_bulk"
|
|
73
|
+
| "write"
|
|
74
|
+
| "run"
|
|
75
|
+
| "search"
|
|
76
|
+
| "search_or_write"
|
|
77
|
+
| "search_and_write"
|
|
78
|
+
| "filter";
|
|
69
79
|
inputFields: Field[];
|
|
70
80
|
outputFields: Field[];
|
|
71
81
|
}
|
package/src/auth.ts
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK Authentication Utilities
|
|
3
|
+
*
|
|
4
|
+
* This module provides SDK-level authentication utilities, including
|
|
5
|
+
* token acquisition, refresh, and user information extraction.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Import type { RequestOptions } from "./api/types"; // Commented out - not used yet
|
|
9
|
+
|
|
10
|
+
let config: any;
|
|
11
|
+
|
|
12
|
+
// Import event types from common location
|
|
13
|
+
import type { EventCallback } from "./types/events";
|
|
14
|
+
|
|
15
|
+
// Re-export for backward compatibility
|
|
16
|
+
export type {
|
|
17
|
+
SdkEvent,
|
|
18
|
+
AuthEvent,
|
|
19
|
+
ApiEvent,
|
|
20
|
+
LoadingEvent,
|
|
21
|
+
EventCallback,
|
|
22
|
+
} from "./types/events";
|
|
23
|
+
|
|
24
|
+
// Options interfaces for auth functions
|
|
25
|
+
export interface AuthOptions {
|
|
26
|
+
onEvent?: EventCallback;
|
|
27
|
+
fetch?: typeof globalThis.fetch;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// JWT payload interfaces
|
|
31
|
+
interface JwtPayload {
|
|
32
|
+
payload: Record<string, any>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface LoginData {
|
|
36
|
+
access_token: string;
|
|
37
|
+
refresh_token: string;
|
|
38
|
+
expires_in: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Constants needed for token refresh
|
|
42
|
+
const ZAPIER_BASE = "https://zapier.com";
|
|
43
|
+
const LOGIN_CLIENT_ID = "K5eEnRE9TTmSFATdkkWhKF8NOKwoiOnYAyIqJjae";
|
|
44
|
+
const AUTH_MODE_HEADER = "X-Auth";
|
|
45
|
+
|
|
46
|
+
// Utility functions for config management
|
|
47
|
+
function getConfig() {
|
|
48
|
+
if (!config) {
|
|
49
|
+
const ConfModule = require("conf");
|
|
50
|
+
const Conf = ConfModule.default || ConfModule;
|
|
51
|
+
config = new Conf({ projectName: "zapier-sdk-cli" });
|
|
52
|
+
}
|
|
53
|
+
return config;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function updateLogin(loginData: LoginData): void {
|
|
57
|
+
const cfg = getConfig();
|
|
58
|
+
cfg.set("login_jwt", loginData.access_token);
|
|
59
|
+
cfg.set("login_refresh_token", loginData.refresh_token);
|
|
60
|
+
cfg.set("login_expires_at", Date.now() + loginData.expires_in * 1000);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function clearLogin(): void {
|
|
64
|
+
const cfg = getConfig();
|
|
65
|
+
cfg.delete("login_jwt");
|
|
66
|
+
cfg.delete("login_refresh_token");
|
|
67
|
+
cfg.delete("login_expires_at");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// JWT utility functions
|
|
71
|
+
function decodeJwtOrThrow(jwt: unknown): JwtPayload {
|
|
72
|
+
if (typeof jwt !== "string") {
|
|
73
|
+
throw new Error("Expected JWT to be a string");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let jsonwebtoken: any;
|
|
77
|
+
try {
|
|
78
|
+
jsonwebtoken = require("jsonwebtoken");
|
|
79
|
+
} catch {
|
|
80
|
+
throw new Error(
|
|
81
|
+
"jsonwebtoken not available - this function requires CLI dependencies",
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const decodedJwt = jsonwebtoken.decode(jwt, { complete: true });
|
|
86
|
+
|
|
87
|
+
if (!decodedJwt) {
|
|
88
|
+
throw new Error("Could not decode JWT");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (typeof decodedJwt.payload === "string") {
|
|
92
|
+
throw new Error("Did not expect JWT payload to be a string");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return decodedJwt;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Refreshes an expired JWT token using the refresh token.
|
|
100
|
+
* Returns the new access token or throws an error.
|
|
101
|
+
*/
|
|
102
|
+
async function refreshJwt(
|
|
103
|
+
refreshToken: string,
|
|
104
|
+
options: AuthOptions = {},
|
|
105
|
+
): Promise<string> {
|
|
106
|
+
const { onEvent, fetch = globalThis.fetch } = options;
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
onEvent?.({
|
|
110
|
+
type: "auth_refreshing",
|
|
111
|
+
payload: {
|
|
112
|
+
message: "Refreshing your token...",
|
|
113
|
+
operation: "token_refresh",
|
|
114
|
+
},
|
|
115
|
+
timestamp: Date.now(),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const response = await fetch(`${ZAPIER_BASE}/oauth/token/`, {
|
|
119
|
+
method: "POST",
|
|
120
|
+
headers: {
|
|
121
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
122
|
+
[AUTH_MODE_HEADER]: "no",
|
|
123
|
+
},
|
|
124
|
+
body: new URLSearchParams({
|
|
125
|
+
client_id: LOGIN_CLIENT_ID,
|
|
126
|
+
refresh_token: refreshToken,
|
|
127
|
+
grant_type: "refresh_token",
|
|
128
|
+
}),
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
if (!response.ok) {
|
|
132
|
+
throw new Error(
|
|
133
|
+
`Token refresh failed: ${response.status} ${response.statusText}`,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const data: LoginData = await response.json();
|
|
138
|
+
|
|
139
|
+
// Update stored login data
|
|
140
|
+
updateLogin(data);
|
|
141
|
+
|
|
142
|
+
onEvent?.({
|
|
143
|
+
type: "auth_success",
|
|
144
|
+
payload: {
|
|
145
|
+
message: "Token refreshed successfully",
|
|
146
|
+
operation: "token_refresh",
|
|
147
|
+
},
|
|
148
|
+
timestamp: Date.now(),
|
|
149
|
+
});
|
|
150
|
+
return data.access_token;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
// If refresh fails, clear stored login
|
|
153
|
+
clearLogin();
|
|
154
|
+
const errorMessage = `Token refresh failed: ${error instanceof Error ? error.message : "Unknown error"}`;
|
|
155
|
+
onEvent?.({
|
|
156
|
+
type: "auth_error",
|
|
157
|
+
payload: {
|
|
158
|
+
message: errorMessage,
|
|
159
|
+
error: errorMessage,
|
|
160
|
+
operation: "token_refresh",
|
|
161
|
+
},
|
|
162
|
+
timestamp: Date.now(),
|
|
163
|
+
});
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Attempts to read a valid JWT token from the CLI configuration.
|
|
170
|
+
* Automatically refreshes expired tokens when possible.
|
|
171
|
+
* Returns undefined if no valid token is found or refresh fails.
|
|
172
|
+
*/
|
|
173
|
+
export async function getTokenFromConfig(
|
|
174
|
+
options: AuthOptions = {},
|
|
175
|
+
): Promise<string | undefined> {
|
|
176
|
+
try {
|
|
177
|
+
const cfg = getConfig();
|
|
178
|
+
|
|
179
|
+
const jwt = cfg.get("login_jwt") as string | undefined;
|
|
180
|
+
const refreshToken = cfg.get("login_refresh_token") as string | undefined;
|
|
181
|
+
const expiresAt = cfg.get("login_expires_at") as number | undefined;
|
|
182
|
+
|
|
183
|
+
// Check if we have all required fields
|
|
184
|
+
if (!jwt || !refreshToken || !expiresAt) {
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Check if token is still valid (with 30 second buffer)
|
|
189
|
+
if (expiresAt > Date.now() + 30 * 1000) {
|
|
190
|
+
return jwt;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Token is expired - attempt to refresh
|
|
194
|
+
try {
|
|
195
|
+
return await refreshJwt(refreshToken, options);
|
|
196
|
+
} catch {
|
|
197
|
+
// If refresh fails, return undefined
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
} catch {
|
|
201
|
+
// If conf is not available or any other error occurs, return undefined
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Gets the ZAPIER_TOKEN from environment variables.
|
|
208
|
+
* Returns undefined if not set.
|
|
209
|
+
*/
|
|
210
|
+
export function getTokenFromEnv(): string | undefined {
|
|
211
|
+
return process.env.ZAPIER_TOKEN;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Attempts to get a token with the following precedence:
|
|
216
|
+
* 1. ZAPIER_TOKEN environment variable
|
|
217
|
+
* 2. Valid JWT from CLI configuration (with auto-refresh)
|
|
218
|
+
*
|
|
219
|
+
* Returns undefined if no valid token is found.
|
|
220
|
+
*/
|
|
221
|
+
export async function getTokenFromEnvOrConfig(
|
|
222
|
+
options: AuthOptions = {},
|
|
223
|
+
): Promise<string | undefined> {
|
|
224
|
+
// First priority: environment variable
|
|
225
|
+
const envToken = getTokenFromEnv();
|
|
226
|
+
if (envToken) {
|
|
227
|
+
return envToken;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Second priority: CLI configuration (with auto-refresh)
|
|
231
|
+
return getTokenFromConfig(options);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Gets the current JWT token, refreshing if necessary.
|
|
236
|
+
* Returns undefined if no valid token is available.
|
|
237
|
+
*/
|
|
238
|
+
export async function getValidJwt(
|
|
239
|
+
options: AuthOptions = {},
|
|
240
|
+
): Promise<string | undefined> {
|
|
241
|
+
try {
|
|
242
|
+
const cfg = getConfig();
|
|
243
|
+
|
|
244
|
+
const jwt = cfg.get("login_jwt") as string | undefined;
|
|
245
|
+
const refreshToken = cfg.get("login_refresh_token") as string | undefined;
|
|
246
|
+
const expiresAt = cfg.get("login_expires_at") as number | undefined;
|
|
247
|
+
|
|
248
|
+
// Check if we have all required fields
|
|
249
|
+
if (!jwt || !refreshToken || !expiresAt) {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Check if token is still valid (with 30 second buffer)
|
|
254
|
+
if (expiresAt > Date.now() + 30 * 1000) {
|
|
255
|
+
return jwt;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Token is expired - attempt to refresh
|
|
259
|
+
return await refreshJwt(refreshToken, options);
|
|
260
|
+
} catch {
|
|
261
|
+
return undefined;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Gets the logged-in user information from JWT token.
|
|
267
|
+
* Automatically refreshes token if expired.
|
|
268
|
+
*/
|
|
269
|
+
export async function getLoggedInUser(options: AuthOptions = {}): Promise<{
|
|
270
|
+
accountId: number;
|
|
271
|
+
customUserId: number;
|
|
272
|
+
email: string;
|
|
273
|
+
}> {
|
|
274
|
+
const jwt = await getValidJwt(options);
|
|
275
|
+
|
|
276
|
+
if (!jwt) {
|
|
277
|
+
throw new Error(
|
|
278
|
+
"No valid authentication token available. Please login first.",
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
let decodedJwt = decodeJwtOrThrow(jwt);
|
|
283
|
+
|
|
284
|
+
if (decodedJwt.payload["sub_type"] == "service") {
|
|
285
|
+
decodedJwt = decodeJwtOrThrow(decodedJwt.payload["njwt"]);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (typeof decodedJwt.payload["zap:acc"] !== "string") {
|
|
289
|
+
throw new Error("JWT payload does not contain accountId");
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const accountId = parseInt(decodedJwt.payload["zap:acc"], 10);
|
|
293
|
+
if (isNaN(accountId)) {
|
|
294
|
+
throw new Error("JWT accountId is not a number");
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (
|
|
298
|
+
decodedJwt.payload["sub_type"] !== "customuser" ||
|
|
299
|
+
typeof decodedJwt.payload["sub"] !== "string"
|
|
300
|
+
) {
|
|
301
|
+
throw new Error("JWT payload does not contain customUserId");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const customUserId = parseInt(decodedJwt.payload["sub"], 10);
|
|
305
|
+
if (isNaN(customUserId)) {
|
|
306
|
+
throw new Error("JWT customUserId is not a number");
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const email = decodedJwt.payload["zap:uname"];
|
|
310
|
+
if (typeof email !== "string") {
|
|
311
|
+
throw new Error("JWT payload does not contain email");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return {
|
|
315
|
+
accountId,
|
|
316
|
+
customUserId,
|
|
317
|
+
email,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Clears stored login information.
|
|
323
|
+
*/
|
|
324
|
+
export function logout(options: Pick<AuthOptions, "onEvent"> = {}): void {
|
|
325
|
+
const { onEvent } = options;
|
|
326
|
+
clearLogin();
|
|
327
|
+
onEvent?.({
|
|
328
|
+
type: "auth_logout",
|
|
329
|
+
payload: { message: "Logged out successfully", operation: "logout" },
|
|
330
|
+
timestamp: Date.now(),
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Gets the path to the configuration file.
|
|
336
|
+
*/
|
|
337
|
+
export function getConfigPath(): string {
|
|
338
|
+
const cfg = getConfig();
|
|
339
|
+
return cfg.path;
|
|
340
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { getOrCreateApiClient } from "../../api";
|
|
2
|
+
import type { Authentication } from "../../types/domain";
|
|
3
|
+
import type { GetAuthenticationOptions } from "./schemas";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Get a specific authentication by ID
|
|
7
|
+
*
|
|
8
|
+
* This function can be used standalone without instantiating a full SDK,
|
|
9
|
+
* which enables better tree-shaking in applications that only need this functionality.
|
|
10
|
+
*
|
|
11
|
+
* @param options - Authentication ID and API configuration options
|
|
12
|
+
* @returns Promise<Authentication> - The authentication details
|
|
13
|
+
* @throws Error if authentication not found or access denied
|
|
14
|
+
*/
|
|
15
|
+
export async function getAuthentication(
|
|
16
|
+
options: GetAuthenticationOptions,
|
|
17
|
+
): Promise<Authentication> {
|
|
18
|
+
const { authenticationId } = options;
|
|
19
|
+
const api = getOrCreateApiClient(options);
|
|
20
|
+
api.requireAuthTo("get authentication");
|
|
21
|
+
|
|
22
|
+
const data: Authentication = await api.get(
|
|
23
|
+
`/api/v4/authentications/${authenticationId}/`,
|
|
24
|
+
{
|
|
25
|
+
customErrorHandler: (response) => {
|
|
26
|
+
if (response.status === 401) {
|
|
27
|
+
return new Error(
|
|
28
|
+
`Authentication failed. Your token may not have permission to access authentications or may be expired. (HTTP ${response.status})`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
if (response.status === 403) {
|
|
32
|
+
return new Error(
|
|
33
|
+
`Access forbidden. Your token may not have the required scopes to get authentication ${authenticationId}. (HTTP ${response.status})`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
if (response.status === 404) {
|
|
37
|
+
return new Error(
|
|
38
|
+
`Authentication ${authenticationId} not found. It may not exist or you may not have access to it. (HTTP ${response.status})`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
// Coerce title from label if title is missing (API cleanup)
|
|
47
|
+
return {
|
|
48
|
+
...data,
|
|
49
|
+
title: data.title || (data as any).label || undefined,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { getAuthentication } from "./index";
|
|
2
|
+
import { GetAuthenticationSchema } from "./schemas";
|
|
3
|
+
|
|
4
|
+
// Function registry info - imports both function and schema
|
|
5
|
+
export const getAuthenticationInfo = {
|
|
6
|
+
name: getAuthentication.name,
|
|
7
|
+
inputSchema: GetAuthenticationSchema,
|
|
8
|
+
implementation: getAuthentication,
|
|
9
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { withOutputSchema } from "../../schema-utils";
|
|
3
|
+
import { AuthItemSchema } from "../../schemas/Auth";
|
|
4
|
+
import type { Authentication } from "../../types/domain";
|
|
5
|
+
|
|
6
|
+
// Pure Zod schema - no resolver metadata!
|
|
7
|
+
export const GetAuthenticationSchema = withOutputSchema(
|
|
8
|
+
z
|
|
9
|
+
.object({
|
|
10
|
+
authenticationId: z
|
|
11
|
+
.number()
|
|
12
|
+
.int()
|
|
13
|
+
.positive()
|
|
14
|
+
.describe("Authentication ID to retrieve"),
|
|
15
|
+
})
|
|
16
|
+
.describe("Get a specific authentication by ID"),
|
|
17
|
+
AuthItemSchema,
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
// Type inferred from schema + function config
|
|
21
|
+
export type GetAuthenticationOptions = z.infer<
|
|
22
|
+
typeof GetAuthenticationSchema
|
|
23
|
+
> & {
|
|
24
|
+
/** Base URL for Zapier API */
|
|
25
|
+
baseUrl?: string;
|
|
26
|
+
/** Authentication token */
|
|
27
|
+
token?: string;
|
|
28
|
+
/** Function to dynamically resolve authentication token */
|
|
29
|
+
getToken?: () => Promise<string | undefined>;
|
|
30
|
+
/** Optional pre-instantiated API client */
|
|
31
|
+
api?: any;
|
|
32
|
+
/** Enable debug logging */
|
|
33
|
+
debug?: boolean;
|
|
34
|
+
/** Custom fetch implementation */
|
|
35
|
+
fetch?: typeof globalThis.fetch;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// SDK function interface - ready to be mixed into main SDK interface
|
|
39
|
+
export interface GetAuthenticationSdkFunction {
|
|
40
|
+
getAuthentication: (
|
|
41
|
+
options: GetAuthenticationOptions,
|
|
42
|
+
) => Promise<Authentication>;
|
|
43
|
+
}
|
|
@@ -18,15 +18,8 @@ import type { ListAuthenticationsOptions } from "./schemas";
|
|
|
18
18
|
export async function listAuthentications(
|
|
19
19
|
options: Partial<ListAuthenticationsOptions> = {},
|
|
20
20
|
): Promise<Authentication[]> {
|
|
21
|
-
const { token } = options;
|
|
22
|
-
|
|
23
|
-
if (!token && !process.env.ZAPIER_TOKEN) {
|
|
24
|
-
throw new Error(
|
|
25
|
-
"Authentication token is required to list authentications. Please provide token in options or set ZAPIER_TOKEN environment variable.",
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
21
|
const api = getOrCreateApiClient(options);
|
|
22
|
+
api.requireAuthTo("list authentications");
|
|
30
23
|
|
|
31
24
|
// Local function to handle the actual API fetching
|
|
32
25
|
const listAuthenticationsInternal = async (
|
|
@@ -44,6 +44,8 @@ export type ListAuthenticationsOptions = z.infer<
|
|
|
44
44
|
baseUrl?: string;
|
|
45
45
|
/** Authentication token */
|
|
46
46
|
token?: string;
|
|
47
|
+
/** Function to dynamically resolve authentication token */
|
|
48
|
+
getToken?: () => Promise<string | undefined>;
|
|
47
49
|
/** Optional pre-instantiated API client */
|
|
48
50
|
api?: any;
|
|
49
51
|
/** Enable debug logging */
|