@zapier/zapier-sdk 0.4.0 → 0.4.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/index.cjs +114 -153
- package/dist/index.d.mts +68 -16
- package/dist/index.d.ts +68 -16
- package/dist/index.mjs +112 -153
- package/package.json +1 -1
- package/src/api/client.ts +41 -28
- package/src/api/polling.ts +4 -8
- package/src/api/types.ts +1 -2
- package/src/functions/{fetch → request}/index.ts +13 -43
- package/src/functions/request/info.ts +11 -0
- package/src/functions/{fetch → request}/schemas.ts +11 -4
- package/src/index.ts +6 -2
- package/src/plugins/apps/index.ts +11 -12
- package/src/plugins/apps/types.ts +17 -1
- package/src/plugins/fetch/index.ts +50 -0
- package/src/plugins/fetch/types.ts +2 -0
- package/src/sdk.ts +11 -13
- package/src/types/sdk.ts +6 -3
- package/src/functions/fetch/info.ts +0 -8
package/src/api/client.ts
CHANGED
|
@@ -16,8 +16,12 @@ import { createDebugLogger, createDebugFetch } from "./debug";
|
|
|
16
16
|
import { pollUntilComplete } from "./polling";
|
|
17
17
|
import { getTokenFromEnvOrConfig } from "../auth";
|
|
18
18
|
|
|
19
|
-
//
|
|
20
|
-
const SUBDOMAIN_PREFIXES =
|
|
19
|
+
// Configuration for path prefixes that should be treated as subdomains
|
|
20
|
+
const SUBDOMAIN_PREFIXES = {
|
|
21
|
+
relay: {
|
|
22
|
+
authHeader: "X-Relay-Authorization",
|
|
23
|
+
},
|
|
24
|
+
} as const;
|
|
21
25
|
|
|
22
26
|
export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
23
27
|
const {
|
|
@@ -32,19 +36,26 @@ export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
|
32
36
|
const debugLog = createDebugLogger(debug);
|
|
33
37
|
const fetch = createDebugFetch({ originalFetch, debugLog });
|
|
34
38
|
|
|
35
|
-
// Helper to build full URLs
|
|
39
|
+
// Helper to build full URLs and return routing info
|
|
36
40
|
function buildUrl(
|
|
37
41
|
path: string,
|
|
38
42
|
searchParams?: Record<string, string>,
|
|
39
|
-
):
|
|
43
|
+
): {
|
|
44
|
+
url: string;
|
|
45
|
+
subdomainConfig?: (typeof SUBDOMAIN_PREFIXES)[keyof typeof SUBDOMAIN_PREFIXES];
|
|
46
|
+
} {
|
|
40
47
|
// Check if this is a path that needs subdomain routing
|
|
41
48
|
const pathSegments = path.split("/").filter(Boolean);
|
|
42
49
|
let finalBaseUrl = baseUrl;
|
|
50
|
+
let subdomainConfig:
|
|
51
|
+
| (typeof SUBDOMAIN_PREFIXES)[keyof typeof SUBDOMAIN_PREFIXES]
|
|
52
|
+
| undefined;
|
|
43
53
|
|
|
44
|
-
if (pathSegments.length > 0 &&
|
|
54
|
+
if (pathSegments.length > 0 && pathSegments[0] in SUBDOMAIN_PREFIXES) {
|
|
45
55
|
// Transform paths to use subdomain routing
|
|
46
56
|
// /prefix/domain/path -> prefix.zapier.com/domain/path
|
|
47
|
-
const subdomain = pathSegments[0];
|
|
57
|
+
const subdomain = pathSegments[0] as keyof typeof SUBDOMAIN_PREFIXES;
|
|
58
|
+
subdomainConfig = SUBDOMAIN_PREFIXES[subdomain];
|
|
48
59
|
const baseUrlObj = new URL(baseUrl);
|
|
49
60
|
baseUrlObj.hostname = `${subdomain}.${baseUrlObj.hostname}`;
|
|
50
61
|
finalBaseUrl = baseUrlObj.toString();
|
|
@@ -59,12 +70,13 @@ export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
|
59
70
|
url.searchParams.set(key, value);
|
|
60
71
|
});
|
|
61
72
|
}
|
|
62
|
-
return url.toString();
|
|
73
|
+
return { url: url.toString(), subdomainConfig };
|
|
63
74
|
}
|
|
64
75
|
|
|
65
76
|
// Helper to build headers
|
|
66
77
|
async function buildHeaders(
|
|
67
78
|
options: RequestOptions = {},
|
|
79
|
+
subdomainConfig?: (typeof SUBDOMAIN_PREFIXES)[keyof typeof SUBDOMAIN_PREFIXES],
|
|
68
80
|
): Promise<Record<string, string>> {
|
|
69
81
|
const headers: Record<string, string> = {
|
|
70
82
|
...options.headers,
|
|
@@ -87,7 +99,8 @@ export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
|
87
99
|
}
|
|
88
100
|
|
|
89
101
|
if (resolvedToken) {
|
|
90
|
-
|
|
102
|
+
const authHeaderName = subdomainConfig?.authHeader || "Authorization";
|
|
103
|
+
headers[authHeaderName] = getAuthorizationHeader(resolvedToken);
|
|
91
104
|
}
|
|
92
105
|
}
|
|
93
106
|
|
|
@@ -127,21 +140,23 @@ export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
|
127
140
|
}
|
|
128
141
|
}
|
|
129
142
|
|
|
130
|
-
// Plain fetch method
|
|
143
|
+
// Plain fetch method for API paths (must start with /)
|
|
131
144
|
async function plainFetch(
|
|
132
|
-
|
|
145
|
+
path: string,
|
|
133
146
|
init?: RequestInit & {
|
|
134
147
|
searchParams?: Record<string, string>;
|
|
135
148
|
authRequired?: boolean;
|
|
136
|
-
customErrorHandler?: (response: Response) => Error | undefined;
|
|
137
149
|
},
|
|
138
150
|
): Promise<Response> {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
151
|
+
if (!path.startsWith("/")) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
`plainFetch expects a path starting with '/', got: ${path}`,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const { url, subdomainConfig } = buildUrl(path, init?.searchParams);
|
|
143
158
|
|
|
144
|
-
const headers = await buildHeaders(init as RequestOptions);
|
|
159
|
+
const headers = await buildHeaders(init as RequestOptions, subdomainConfig);
|
|
145
160
|
|
|
146
161
|
// Merge headers from init with our built headers
|
|
147
162
|
const finalHeaders = {
|
|
@@ -179,11 +194,10 @@ export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
|
179
194
|
headers,
|
|
180
195
|
searchParams: options.searchParams,
|
|
181
196
|
authRequired: options.authRequired,
|
|
182
|
-
customErrorHandler: options.customErrorHandler,
|
|
183
197
|
});
|
|
184
198
|
|
|
185
|
-
|
|
186
|
-
return handleResponse(response, options.customErrorHandler
|
|
199
|
+
// plainFetch already handled all auth and headers
|
|
200
|
+
return handleResponse(response, options.customErrorHandler);
|
|
187
201
|
}
|
|
188
202
|
|
|
189
203
|
return {
|
|
@@ -212,13 +226,13 @@ export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
|
212
226
|
},
|
|
213
227
|
|
|
214
228
|
async poll(path: string, options: PollOptions = {}): Promise<any> {
|
|
215
|
-
const url = buildUrl(path, options.searchParams);
|
|
216
|
-
const headers = await buildHeaders(options);
|
|
217
|
-
|
|
218
229
|
return pollUntilComplete({
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
230
|
+
fetchPoll: () =>
|
|
231
|
+
plainFetch(path, {
|
|
232
|
+
method: "GET",
|
|
233
|
+
searchParams: options.searchParams,
|
|
234
|
+
authRequired: options.authRequired,
|
|
235
|
+
}),
|
|
222
236
|
maxAttempts: options.maxAttempts,
|
|
223
237
|
initialDelay: options.initialDelay,
|
|
224
238
|
maxDelay: options.maxDelay,
|
|
@@ -229,14 +243,13 @@ export function createZapierApi(options: ApiClientOptions): ApiClient {
|
|
|
229
243
|
},
|
|
230
244
|
|
|
231
245
|
async fetch(
|
|
232
|
-
|
|
246
|
+
path: string,
|
|
233
247
|
init?: RequestInit & {
|
|
234
248
|
searchParams?: Record<string, string>;
|
|
235
249
|
authRequired?: boolean;
|
|
236
|
-
customErrorHandler?: (response: Response) => Error | undefined;
|
|
237
250
|
},
|
|
238
251
|
): Promise<Response> {
|
|
239
|
-
return plainFetch(
|
|
252
|
+
return plainFetch(path, init);
|
|
240
253
|
},
|
|
241
254
|
};
|
|
242
255
|
}
|
package/src/api/polling.ts
CHANGED
|
@@ -6,9 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
export async function pollUntilComplete(options: {
|
|
9
|
-
|
|
10
|
-
url: string;
|
|
11
|
-
headers?: Record<string, string>;
|
|
9
|
+
fetchPoll: () => Promise<Response>;
|
|
12
10
|
maxAttempts?: number;
|
|
13
11
|
initialDelay?: number;
|
|
14
12
|
maxDelay?: number;
|
|
@@ -17,9 +15,7 @@ export async function pollUntilComplete(options: {
|
|
|
17
15
|
resultExtractor?: (response: any) => any;
|
|
18
16
|
}): Promise<any> {
|
|
19
17
|
const {
|
|
20
|
-
|
|
21
|
-
url,
|
|
22
|
-
headers = {},
|
|
18
|
+
fetchPoll,
|
|
23
19
|
maxAttempts = 30,
|
|
24
20
|
initialDelay = 50,
|
|
25
21
|
maxDelay = 1000,
|
|
@@ -31,7 +27,7 @@ export async function pollUntilComplete(options: {
|
|
|
31
27
|
let delay = initialDelay;
|
|
32
28
|
|
|
33
29
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
34
|
-
const response = await
|
|
30
|
+
const response = await fetchPoll();
|
|
35
31
|
|
|
36
32
|
if (response.status === successStatus) {
|
|
37
33
|
// Success - extract and return results
|
|
@@ -47,7 +43,7 @@ export async function pollUntilComplete(options: {
|
|
|
47
43
|
} else {
|
|
48
44
|
// Error occurred
|
|
49
45
|
throw new Error(
|
|
50
|
-
`
|
|
46
|
+
`Poll request failed: ${response.status} ${response.statusText}`,
|
|
51
47
|
);
|
|
52
48
|
}
|
|
53
49
|
}
|
package/src/api/types.ts
CHANGED
|
@@ -26,11 +26,10 @@ export interface ApiClient {
|
|
|
26
26
|
delete: (path: string, options?: RequestOptions) => Promise<any>;
|
|
27
27
|
poll: (path: string, options?: PollOptions) => Promise<any>;
|
|
28
28
|
fetch: (
|
|
29
|
-
|
|
29
|
+
path: string,
|
|
30
30
|
init?: RequestInit & {
|
|
31
31
|
searchParams?: Record<string, string>;
|
|
32
32
|
authRequired?: boolean;
|
|
33
|
-
customErrorHandler?: (response: Response) => Error | undefined;
|
|
34
33
|
},
|
|
35
34
|
) => Promise<Response>;
|
|
36
35
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { getOrCreateApiClient } from "../../api";
|
|
2
2
|
import type { FunctionOptions } from "../../types/domain";
|
|
3
3
|
|
|
4
|
-
export interface
|
|
4
|
+
export interface RelayRequestOptions {
|
|
5
5
|
/** HTTP method */
|
|
6
6
|
method?: string;
|
|
7
|
-
/** Request body
|
|
8
|
-
body?:
|
|
7
|
+
/** Request body (can be string, FormData, Blob, etc.) */
|
|
8
|
+
body?: BodyInit | null;
|
|
9
9
|
/** Request headers */
|
|
10
10
|
headers?: HeadersInit;
|
|
11
11
|
/** Zapier authentication ID to use for the request */
|
|
@@ -16,6 +16,9 @@ export interface RelayFetchOptions {
|
|
|
16
16
|
authenticationTemplate?: string;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
// Legacy type alias
|
|
20
|
+
export type RelayFetchOptions = RelayRequestOptions;
|
|
21
|
+
|
|
19
22
|
export interface RelayFetchConfig extends FunctionOptions {
|
|
20
23
|
/** Event callback function */
|
|
21
24
|
onEvent?: (event: any) => void;
|
|
@@ -49,14 +52,14 @@ function transformUrlToRelayPath(url: string | URL): string {
|
|
|
49
52
|
* @returns Promise<Response> - The response from the request
|
|
50
53
|
*/
|
|
51
54
|
/**
|
|
52
|
-
* Schema-compatible wrapper function for the
|
|
55
|
+
* Schema-compatible wrapper function for the request functionality
|
|
53
56
|
*/
|
|
54
|
-
export async function
|
|
57
|
+
export async function request(
|
|
55
58
|
options: {
|
|
56
59
|
url: string;
|
|
57
60
|
method?: string;
|
|
58
|
-
body?:
|
|
59
|
-
headers?:
|
|
61
|
+
body?: BodyInit | null;
|
|
62
|
+
headers?: HeadersInit;
|
|
60
63
|
authenticationId?: number;
|
|
61
64
|
callbackUrl?: string;
|
|
62
65
|
authenticationTemplate?: string;
|
|
@@ -92,7 +95,7 @@ export async function fetch(
|
|
|
92
95
|
*/
|
|
93
96
|
export async function relayFetch(
|
|
94
97
|
url: string | URL,
|
|
95
|
-
options:
|
|
98
|
+
options: RelayRequestOptions = {},
|
|
96
99
|
config: RelayFetchConfig = {},
|
|
97
100
|
): Promise<Response> {
|
|
98
101
|
const api = getOrCreateApiClient(config);
|
|
@@ -138,43 +141,10 @@ export async function relayFetch(
|
|
|
138
141
|
headers["X-Authentication-Template"] = authenticationTemplate;
|
|
139
142
|
}
|
|
140
143
|
|
|
141
|
-
//
|
|
142
|
-
const requestOptions = {
|
|
143
|
-
headers,
|
|
144
|
-
authRequired: false, // Disable automatic Authorization header
|
|
145
|
-
customErrorHandler: (response: Response) => {
|
|
146
|
-
if (!response.ok) {
|
|
147
|
-
return new Error(
|
|
148
|
-
`Relay request failed: ${response.status} ${response.statusText}`,
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
return undefined;
|
|
152
|
-
},
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
// Add X-Relay-Authorization header manually
|
|
156
|
-
let token = config.token;
|
|
157
|
-
if (!token && config.getToken) {
|
|
158
|
-
token = await config.getToken();
|
|
159
|
-
}
|
|
160
|
-
if (!token) {
|
|
161
|
-
const { getTokenFromEnvOrConfig } = await import("../../auth");
|
|
162
|
-
token = await getTokenFromEnvOrConfig({
|
|
163
|
-
onEvent: config.onEvent,
|
|
164
|
-
fetch: config.fetch,
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
if (token) {
|
|
168
|
-
const { getAuthorizationHeader } = await import("../../api/auth");
|
|
169
|
-
headers["X-Relay-Authorization"] = getAuthorizationHeader(token);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// Use the API client's fetch method directly
|
|
144
|
+
// Use the API client's fetch method directly - let it handle auth automatically
|
|
173
145
|
return await api.fetch(relayPath, {
|
|
174
146
|
method,
|
|
175
147
|
body,
|
|
176
|
-
headers
|
|
177
|
-
authRequired: requestOptions.authRequired,
|
|
178
|
-
customErrorHandler: requestOptions.customErrorHandler,
|
|
148
|
+
headers,
|
|
179
149
|
});
|
|
180
150
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { request } from "./index";
|
|
2
|
+
import { RelayRequestSchema } from "./schemas";
|
|
3
|
+
|
|
4
|
+
export const requestInfo = {
|
|
5
|
+
name: "request",
|
|
6
|
+
inputSchema: RelayRequestSchema,
|
|
7
|
+
implementation: request,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// Legacy export for compatibility
|
|
11
|
+
export const fetchInfo = requestInfo;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import type { FunctionOptions } from "../../types/domain";
|
|
3
3
|
|
|
4
|
-
// Pure Zod schema for the
|
|
5
|
-
export const
|
|
4
|
+
// Pure Zod schema for the request function
|
|
5
|
+
export const RelayRequestSchema = z
|
|
6
6
|
.object({
|
|
7
7
|
url: z
|
|
8
8
|
.string()
|
|
9
9
|
.url()
|
|
10
|
-
.describe("The URL to
|
|
10
|
+
.describe("The URL to request (will be proxied through Relay)"),
|
|
11
11
|
method: z
|
|
12
12
|
.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"])
|
|
13
13
|
.optional()
|
|
@@ -34,13 +34,20 @@ export const RelayFetchSchema = z
|
|
|
34
34
|
.describe("Make authenticated HTTP requests through Zapier's Relay service");
|
|
35
35
|
|
|
36
36
|
// Type inferred from schema + function config
|
|
37
|
-
export type
|
|
37
|
+
export type RelayRequestOptions = z.infer<typeof RelayRequestSchema> &
|
|
38
38
|
FunctionOptions & {
|
|
39
39
|
/** Base URL for Relay service */
|
|
40
40
|
relayBaseUrl?: string;
|
|
41
41
|
};
|
|
42
42
|
|
|
43
43
|
// SDK function interface
|
|
44
|
+
export interface RelayRequestSdkFunction {
|
|
45
|
+
request: (options: RelayRequestOptions) => Promise<Response>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Legacy exports for compatibility
|
|
49
|
+
export const RelayFetchSchema = RelayRequestSchema;
|
|
50
|
+
export type RelayFetchOptions = RelayRequestOptions;
|
|
44
51
|
export interface RelayFetchSdkFunction {
|
|
45
52
|
fetch: (options: RelayFetchOptions) => Promise<Response>;
|
|
46
53
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
export * from "./types/domain";
|
|
3
3
|
export * from "./types/properties";
|
|
4
4
|
export * from "./plugins/apps";
|
|
5
|
+
export * from "./plugins/fetch";
|
|
5
6
|
|
|
6
7
|
// Export schema utilities for CLI
|
|
7
8
|
export { isPositional } from "./schema-utils";
|
|
@@ -27,8 +28,11 @@ export { runAction } from "./functions/runAction";
|
|
|
27
28
|
export { listFields } from "./functions/listFields";
|
|
28
29
|
export { generateTypes } from "./functions/generateTypes";
|
|
29
30
|
export { bundleCode } from "./functions/bundleCode";
|
|
30
|
-
export {
|
|
31
|
-
export {
|
|
31
|
+
export { request, relayFetch } from "./functions/request";
|
|
32
|
+
export {
|
|
33
|
+
RelayRequestSchema,
|
|
34
|
+
RelayFetchSchema,
|
|
35
|
+
} from "./functions/request/schemas";
|
|
32
36
|
|
|
33
37
|
// Export the main combined SDK
|
|
34
38
|
export { createZapierSdk, ZapierSdkOptions } from "./sdk";
|
|
@@ -55,19 +55,18 @@ function createActionTypeProxy(
|
|
|
55
55
|
) {
|
|
56
56
|
// Special handling for "fetch" action type
|
|
57
57
|
if (actionType === "fetch") {
|
|
58
|
-
return async (
|
|
59
|
-
url: string
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}) => {
|
|
58
|
+
return async (
|
|
59
|
+
url: string | URL,
|
|
60
|
+
init?: RequestInit & {
|
|
61
|
+
authenticationId?: number;
|
|
62
|
+
callbackUrl?: string;
|
|
63
|
+
authenticationTemplate?: string;
|
|
64
|
+
},
|
|
65
|
+
) => {
|
|
67
66
|
const { sdk } = options;
|
|
68
67
|
|
|
69
68
|
// Use pinned auth ID first, then provided auth ID
|
|
70
|
-
const authenticationId = pinnedAuthId ||
|
|
69
|
+
const authenticationId = pinnedAuthId || init?.authenticationId;
|
|
71
70
|
|
|
72
71
|
if (!authenticationId) {
|
|
73
72
|
throw new Error(
|
|
@@ -76,8 +75,8 @@ function createActionTypeProxy(
|
|
|
76
75
|
}
|
|
77
76
|
|
|
78
77
|
// Call sdk.fetch with the resolved authenticationId
|
|
79
|
-
return sdk.fetch({
|
|
80
|
-
...
|
|
78
|
+
return sdk.fetch(url, {
|
|
79
|
+
...init,
|
|
81
80
|
authenticationId,
|
|
82
81
|
});
|
|
83
82
|
};
|
|
@@ -7,12 +7,28 @@ interface AppFactoryOptions {
|
|
|
7
7
|
authenticationId: number;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
// Base action type proxy for regular actions
|
|
11
|
+
interface BaseActionTypeProxy {
|
|
11
12
|
[action: string]: (
|
|
12
13
|
options?: ActionExecutionOptions,
|
|
13
14
|
) => Promise<ActionExecutionResult>;
|
|
14
15
|
}
|
|
15
16
|
|
|
17
|
+
// Special fetch function type
|
|
18
|
+
interface FetchActionType {
|
|
19
|
+
fetch: (
|
|
20
|
+
url: string | URL,
|
|
21
|
+
init?: RequestInit & {
|
|
22
|
+
authenticationId?: number;
|
|
23
|
+
callbackUrl?: string;
|
|
24
|
+
authenticationTemplate?: string;
|
|
25
|
+
},
|
|
26
|
+
) => Promise<Response>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Combined action type proxy
|
|
30
|
+
type ActionTypeProxy = BaseActionTypeProxy & Partial<FetchActionType>;
|
|
31
|
+
|
|
16
32
|
interface AppProxy {
|
|
17
33
|
[type: string]: ActionTypeProxy;
|
|
18
34
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ZapierSdk } from "../../types/sdk";
|
|
2
|
+
|
|
3
|
+
export interface FetchPluginOptions {
|
|
4
|
+
sdk: ZapierSdk;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a fetch function with standard fetch signature that delegates to sdk.request
|
|
9
|
+
*/
|
|
10
|
+
export function createFetchPlugin(options: FetchPluginOptions) {
|
|
11
|
+
const { sdk } = options;
|
|
12
|
+
|
|
13
|
+
return async function fetch(
|
|
14
|
+
url: string | URL,
|
|
15
|
+
init?: RequestInit & {
|
|
16
|
+
authenticationId?: number;
|
|
17
|
+
callbackUrl?: string;
|
|
18
|
+
authenticationTemplate?: string;
|
|
19
|
+
},
|
|
20
|
+
): Promise<Response> {
|
|
21
|
+
const {
|
|
22
|
+
authenticationId,
|
|
23
|
+
callbackUrl,
|
|
24
|
+
authenticationTemplate,
|
|
25
|
+
...fetchInit
|
|
26
|
+
} = init || {};
|
|
27
|
+
|
|
28
|
+
return sdk.request({
|
|
29
|
+
url: url.toString(),
|
|
30
|
+
method: fetchInit.method as any,
|
|
31
|
+
body: fetchInit.body as any,
|
|
32
|
+
headers: fetchInit.headers as any,
|
|
33
|
+
authenticationId,
|
|
34
|
+
callbackUrl,
|
|
35
|
+
authenticationTemplate,
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Export types for TypeScript
|
|
41
|
+
export interface FetchPluginSdkExtension {
|
|
42
|
+
fetch: (
|
|
43
|
+
url: string | URL,
|
|
44
|
+
init?: RequestInit & {
|
|
45
|
+
authenticationId?: number;
|
|
46
|
+
callbackUrl?: string;
|
|
47
|
+
authenticationTemplate?: string;
|
|
48
|
+
},
|
|
49
|
+
) => Promise<Response>;
|
|
50
|
+
}
|
package/src/sdk.ts
CHANGED
|
@@ -13,7 +13,7 @@ import { findUniqueAuthentication } from "./functions/findUniqueAuthentication";
|
|
|
13
13
|
import { listFields } from "./functions/listFields";
|
|
14
14
|
import { generateTypes } from "./functions/generateTypes";
|
|
15
15
|
import { bundleCode } from "./functions/bundleCode";
|
|
16
|
-
import {
|
|
16
|
+
import { request } from "./functions/request";
|
|
17
17
|
|
|
18
18
|
// Import function registry info objects (only for CLI registry)
|
|
19
19
|
import { listAppsInfo } from "./functions/listApps/info";
|
|
@@ -28,7 +28,7 @@ import { findUniqueAuthenticationInfo } from "./functions/findUniqueAuthenticati
|
|
|
28
28
|
import { listFieldsInfo } from "./functions/listFields/info";
|
|
29
29
|
import { generateTypesInfo } from "./functions/generateTypes/info";
|
|
30
30
|
import { bundleCodeInfo } from "./functions/bundleCode/info";
|
|
31
|
-
import {
|
|
31
|
+
import { requestInfo } from "./functions/request/info";
|
|
32
32
|
|
|
33
33
|
// Function registry as array - uses names from function info objects
|
|
34
34
|
const functionRegistry = [
|
|
@@ -44,7 +44,7 @@ const functionRegistry = [
|
|
|
44
44
|
listFieldsInfo,
|
|
45
45
|
generateTypesInfo,
|
|
46
46
|
bundleCodeInfo,
|
|
47
|
-
|
|
47
|
+
requestInfo,
|
|
48
48
|
];
|
|
49
49
|
|
|
50
50
|
// Import the properly typed SDK interfaces
|
|
@@ -52,6 +52,7 @@ import type { BaseZapierSdk, ZapierSdk } from "./types/sdk";
|
|
|
52
52
|
|
|
53
53
|
// Import plugin functions
|
|
54
54
|
import { createAppsPlugin } from "./plugins/apps/index";
|
|
55
|
+
import { createFetchPlugin } from "./plugins/fetch/index";
|
|
55
56
|
|
|
56
57
|
// TODO: Add plugin registry back when needed for CLI compatibility
|
|
57
58
|
|
|
@@ -101,18 +102,15 @@ export function createZapierSdk(options: ZapierSdkOptions = {}): ZapierSdk {
|
|
|
101
102
|
listFields: (options) => listFields({ ...options, api }),
|
|
102
103
|
generateTypes: (options) => generateTypes({ ...options, api }),
|
|
103
104
|
bundleCode: (options) => bundleCode(options), // No API config needed
|
|
104
|
-
|
|
105
|
+
request: (options) => request({ ...options, api }),
|
|
105
106
|
};
|
|
106
107
|
|
|
107
|
-
//
|
|
108
|
-
const fullSdk
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
sdk: baseSdk as ZapierSdk,
|
|
114
|
-
}),
|
|
115
|
-
};
|
|
108
|
+
// Mutate baseSdk to add plugins so they can access each other
|
|
109
|
+
const fullSdk = baseSdk as ZapierSdk;
|
|
110
|
+
|
|
111
|
+
// Add plugins to the SDK - they now have access to the full SDK including other plugins
|
|
112
|
+
fullSdk.apps = createAppsPlugin({ sdk: fullSdk });
|
|
113
|
+
fullSdk.fetch = createFetchPlugin({ sdk: fullSdk });
|
|
116
114
|
|
|
117
115
|
return fullSdk;
|
|
118
116
|
}
|
package/src/types/sdk.ts
CHANGED
|
@@ -11,10 +11,11 @@ import type { FindUniqueAuthenticationSdkFunction } from "../functions/findUniqu
|
|
|
11
11
|
import type { GenerateTypesSdkFunction } from "../functions/generateTypes/schemas";
|
|
12
12
|
import type { ListAppsSdkFunction } from "../functions/listApps/schemas";
|
|
13
13
|
import type { BundleCodeSdkFunction } from "../functions/bundleCode/schemas";
|
|
14
|
-
import type {
|
|
14
|
+
import type { RelayRequestSdkFunction } from "../functions/request/schemas";
|
|
15
15
|
|
|
16
16
|
// Plugin interfaces
|
|
17
17
|
import type { AppsPluginSdkExtension } from "../plugins/apps/types";
|
|
18
|
+
import type { FetchPluginSdkExtension } from "../plugins/fetch/types";
|
|
18
19
|
|
|
19
20
|
// Compose SDK functions from individual function interfaces
|
|
20
21
|
export interface ZapierSdkFunctions
|
|
@@ -30,12 +31,14 @@ export interface ZapierSdkFunctions
|
|
|
30
31
|
GenerateTypesSdkFunction,
|
|
31
32
|
ListAppsSdkFunction,
|
|
32
33
|
BundleCodeSdkFunction,
|
|
33
|
-
|
|
34
|
+
RelayRequestSdkFunction {
|
|
34
35
|
// All functions now properly typed!
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
// Compose SDK plugins from individual plugin interfaces
|
|
38
|
-
export interface ZapierSdkPlugins
|
|
39
|
+
export interface ZapierSdkPlugins
|
|
40
|
+
extends AppsPluginSdkExtension,
|
|
41
|
+
FetchPluginSdkExtension {
|
|
39
42
|
// All plugins now properly typed!
|
|
40
43
|
}
|
|
41
44
|
|