Skip to content

Commit 4e6a586

Browse files
authored
Je/connect proxy support (#15495)
* + proxy request * makeProxyRequest * remove header and body requirements * clean up * fix linting * change the function header to be more fetch-like * PR feedback
1 parent e170fd1 commit 4e6a586

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

packages/sdk/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
# Changelog
44

5+
## [1.3.3] - 2025-02-5
6+
7+
### Changed
8+
9+
- Add makeProxyRequest function to BaseClient
10+
511
## [1.3.2] - 2025-02-3
612

713
### Changed

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@pipedream/sdk",
33
"type": "module",
4-
"version": "1.3.2",
4+
"version": "1.3.3",
55
"description": "Pipedream SDK",
66
"main": "./dist/server.js",
77
"module": "./dist/server.js",

packages/sdk/src/server/index.ts

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import * as oauth from "oauth4webapi";
66
import {
7-
Account, BaseClient, type AppInfo, type ConnectTokenResponse,
7+
Account, BaseClient, type AppInfo, type ConnectTokenResponse, type RequestOptions,
88
} from "../shared/index.js";
99
export * from "../shared/index.js";
1010

@@ -107,6 +107,48 @@ export type GetAccountByIdOpts = {
107107
include_credentials?: boolean;
108108
};
109109

110+
/**
111+
* Options used to determine the external user and account to be used in Connect Proxy API
112+
*/
113+
export type ProxyApiOpts = {
114+
/**
115+
* Search parameters to be added to the proxy request. external_user_id and account_id are required.
116+
*/
117+
searchParams: Record<string, string>;
118+
};
119+
120+
/**
121+
* fetch-like options for the Target of the Connect Proxy Api Request
122+
*/
123+
export type ProxyTargetApiOpts = {
124+
/**
125+
* http method for the request
126+
*/
127+
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
128+
/**
129+
* http headers for the request
130+
*/
131+
headers?: Record<string, string>;
132+
/**
133+
* http body for the request
134+
*/
135+
body?: string;
136+
};
137+
138+
/**
139+
* object that contains the url and options for the target of the Connect Proxy Api Request
140+
*/
141+
export type ProxyTargetApiRequest = {
142+
/**
143+
* URL for the target of the request. Search parameters must be included here.
144+
*/
145+
url: string;
146+
/**
147+
* fetch-like options for the target of the Connect Proxy Request
148+
*/
149+
options: ProxyTargetApiOpts;
150+
};
151+
110152
/**
111153
* Creates a new instance of BackendClient with the provided options.
112154
*
@@ -374,4 +416,34 @@ export class BackendClient extends BaseClient {
374416
method: "GET",
375417
});
376418
}
419+
420+
/**
421+
* Makes a proxy request to the target app API with the specified query parameters and options.
422+
*
423+
* @returns A promise resolving to the response from the downstream service
424+
*/
425+
public makeProxyRequest(proxyOptions: ProxyApiOpts, targetRequest: ProxyTargetApiRequest): Promise<string> {
426+
const url64 = btoa(targetRequest.url).replace(/\+/g, "-")
427+
.replace(/\//g, "_")
428+
.replace(/=+$/, "");
429+
430+
const headers = targetRequest.options.headers || {};
431+
432+
const newHeaders = Object.keys(headers).reduce<{ [key: string]: string }>((acc, key) => {
433+
acc[`x-pd-proxy-${key}`] = headers[key];
434+
return acc;
435+
}, {});
436+
437+
const newOpts: RequestOptions = {
438+
method: targetRequest.options.method,
439+
headers: newHeaders,
440+
params: proxyOptions.searchParams,
441+
}
442+
443+
if (targetRequest.options.body) {
444+
newOpts.body = targetRequest.options.body
445+
}
446+
447+
return this.makeConnectRequest(`/proxy/${url64}`, newOpts);
448+
}
377449
}

packages/sdk/src/shared/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,8 +995,8 @@ export abstract class BaseClient {
995995
};
996996

997997
return this.makeRequest(path, {
998-
headers,
999998
...opts,
999+
headers,
10001000
});
10011001
}
10021002

0 commit comments

Comments
 (0)