diff --git a/packages/sdk/CHANGELOG.md b/packages/sdk/CHANGELOG.md index 3bc01d2347fab..b18403d0bb351 100644 --- a/packages/sdk/CHANGELOG.md +++ b/packages/sdk/CHANGELOG.md @@ -2,6 +2,12 @@ # Changelog +## [1.3.3] - 2025-02-5 + +### Changed + +- Add makeProxyRequest function to BaseClient + ## [1.3.2] - 2025-02-3 ### Changed diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 13ad64555d8b7..40c7645b2b206 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,7 +1,7 @@ { "name": "@pipedream/sdk", "type": "module", - "version": "1.3.2", + "version": "1.3.3", "description": "Pipedream SDK", "main": "./dist/server.js", "module": "./dist/server.js", diff --git a/packages/sdk/src/server/index.ts b/packages/sdk/src/server/index.ts index c61003fff8b58..1ad8500f9cb83 100644 --- a/packages/sdk/src/server/index.ts +++ b/packages/sdk/src/server/index.ts @@ -4,7 +4,7 @@ import * as oauth from "oauth4webapi"; import { - Account, BaseClient, type AppInfo, type ConnectTokenResponse, + Account, BaseClient, type AppInfo, type ConnectTokenResponse, type RequestOptions, } from "../shared/index.js"; export * from "../shared/index.js"; @@ -107,6 +107,48 @@ export type GetAccountByIdOpts = { include_credentials?: boolean; }; +/** + * Options used to determine the external user and account to be used in Connect Proxy API + */ +export type ProxyApiOpts = { + /** + * Search parameters to be added to the proxy request. external_user_id and account_id are required. + */ + searchParams: Record; +}; + +/** + * fetch-like options for the Target of the Connect Proxy Api Request + */ +export type ProxyTargetApiOpts = { + /** + * http method for the request + */ + method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; + /** + * http headers for the request + */ + headers?: Record; + /** + * http body for the request + */ + body?: string; +}; + +/** + * object that contains the url and options for the target of the Connect Proxy Api Request + */ +export type ProxyTargetApiRequest = { + /** + * URL for the target of the request. Search parameters must be included here. + */ + url: string; + /** + * fetch-like options for the target of the Connect Proxy Request + */ + options: ProxyTargetApiOpts; +}; + /** * Creates a new instance of BackendClient with the provided options. * @@ -374,4 +416,34 @@ export class BackendClient extends BaseClient { method: "GET", }); } + + /** + * Makes a proxy request to the target app API with the specified query parameters and options. + * + * @returns A promise resolving to the response from the downstream service + */ + public makeProxyRequest(proxyOptions: ProxyApiOpts, targetRequest: ProxyTargetApiRequest): Promise { + const url64 = btoa(targetRequest.url).replace(/\+/g, "-") + .replace(/\//g, "_") + .replace(/=+$/, ""); + + const headers = targetRequest.options.headers || {}; + + const newHeaders = Object.keys(headers).reduce<{ [key: string]: string }>((acc, key) => { + acc[`x-pd-proxy-${key}`] = headers[key]; + return acc; + }, {}); + + const newOpts: RequestOptions = { + method: targetRequest.options.method, + headers: newHeaders, + params: proxyOptions.searchParams, + } + + if (targetRequest.options.body) { + newOpts.body = targetRequest.options.body + } + + return this.makeConnectRequest(`/proxy/${url64}`, newOpts); + } } diff --git a/packages/sdk/src/shared/index.ts b/packages/sdk/src/shared/index.ts index 7fcf185e742d0..dd9c855ccacb7 100644 --- a/packages/sdk/src/shared/index.ts +++ b/packages/sdk/src/shared/index.ts @@ -995,8 +995,8 @@ export abstract class BaseClient { }; return this.makeRequest(path, { - headers, ...opts, + headers, }); }