From e560380c2961bff921fc17e67bc18e2325cd9c0e Mon Sep 17 00:00:00 2001 From: Jason Endo Date: Mon, 13 Jan 2025 20:21:12 -0800 Subject: [PATCH 1/7] + proxy request --- packages/sdk/src/server/index.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/sdk/src/server/index.ts b/packages/sdk/src/server/index.ts index c2776294302f0..c6b6a23ce39cf 100644 --- a/packages/sdk/src/server/index.ts +++ b/packages/sdk/src/server/index.ts @@ -350,4 +350,14 @@ export class BackendClient extends BaseClient { method: "GET", }); } + public getProxyRequest(url: string, method: string, body: string, params: any): Promise { + const url64 = btoa(url).replace(/\+/g, "-") + .replace(/\//g, "_") + .replace(/=+$/, ""); + return this.makeConnectRequest(`/proxy/${url64}`, { + method, + body, + params, + }); + } } From 37320f816d858b9dbbef664423f10b4fcfa066ca Mon Sep 17 00:00:00 2001 From: Jason Endo Date: Wed, 15 Jan 2025 08:52:26 -0800 Subject: [PATCH 2/7] makeProxyRequest --- packages/sdk/src/server/index.ts | 35 ++++++++++++++++++++++++++------ packages/sdk/src/shared/index.ts | 2 +- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/sdk/src/server/index.ts b/packages/sdk/src/server/index.ts index c6b6a23ce39cf..de237a06f3cf9 100644 --- a/packages/sdk/src/server/index.ts +++ b/packages/sdk/src/server/index.ts @@ -109,6 +109,19 @@ export type GetAccountByIdOpts = { include_credentials?: boolean; }; +/** + * Parameters for the retrieval of an account from the Connect API + */ +export type MakeProxyRequestOpts = { + /** + * Whether to retrieve the account's credentials or not. + */ + method: string; + headers: Record; + //headers: Record; + body: any +}; + /** * Creates a new instance of BackendClient with the provided options. * @@ -350,14 +363,24 @@ export class BackendClient extends BaseClient { method: "GET", }); } - public getProxyRequest(url: string, method: string, body: string, params: any): Promise { + //public getProxyRequest(url: string, method: string, body: string, params: any): Promise { + public makeProxyRequest(url: string, query: any, opts: MakeProxyRequestOpts): Promise { const url64 = btoa(url).replace(/\+/g, "-") .replace(/\//g, "_") .replace(/=+$/, ""); - return this.makeConnectRequest(`/proxy/${url64}`, { - method, - body, - params, - }); + + const newHeaders = Object.keys(opts.headers).reduce<{ [key: string]: string }>((acc, key) => { + acc[`x-pd-proxy-${key}`] = opts.headers[key]; + return acc; + }, {}); + + const newOpts = { + method: opts.method, + body: opts.body, + headers: newHeaders, + params: query, + } + + return this.makeConnectRequest(`/proxy/${url64}`, newOpts); } } diff --git a/packages/sdk/src/shared/index.ts b/packages/sdk/src/shared/index.ts index 44e2bbde21b41..65c11f3dfdd21 100644 --- a/packages/sdk/src/shared/index.ts +++ b/packages/sdk/src/shared/index.ts @@ -738,8 +738,8 @@ export abstract class BaseClient { }; return this.makeRequest(path, { - headers, ...opts, + headers, }); } From 5e45ac5a95c0538d4d4ac5785169197ee71740db Mon Sep 17 00:00:00 2001 From: Jason Endo Date: Tue, 28 Jan 2025 15:54:36 -0800 Subject: [PATCH 3/7] remove header and body requirements --- packages/sdk/src/server/index.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/sdk/src/server/index.ts b/packages/sdk/src/server/index.ts index de237a06f3cf9..7aa3736907cf1 100644 --- a/packages/sdk/src/server/index.ts +++ b/packages/sdk/src/server/index.ts @@ -6,7 +6,7 @@ import { AccessToken, ClientCredentials, } from "simple-oauth2"; import { - Account, BaseClient, type AppInfo, type ConnectTokenResponse, + Account, BaseClient, type AppInfo, type ConnectTokenResponse, type RequestOptions } from "../shared"; export * from "../shared"; @@ -117,9 +117,9 @@ export type MakeProxyRequestOpts = { * Whether to retrieve the account's credentials or not. */ method: string; - headers: Record; + headers?: Record; //headers: Record; - body: any + body?: any }; /** @@ -369,18 +369,23 @@ export class BackendClient extends BaseClient { .replace(/\//g, "_") .replace(/=+$/, ""); - const newHeaders = Object.keys(opts.headers).reduce<{ [key: string]: string }>((acc, key) => { - acc[`x-pd-proxy-${key}`] = opts.headers[key]; + const headers = opts.headers || {}; + + const newHeaders = Object.keys(headers).reduce<{ [key: string]: string }>((acc, key) => { + acc[`x-pd-proxy-${key}`] = headers[key]; return acc; }, {}); - const newOpts = { + const newOpts: RequestOptions = { method: opts.method, - body: opts.body, headers: newHeaders, params: query, } + if (opts.body) { + newOpts.body = opts.body + } + return this.makeConnectRequest(`/proxy/${url64}`, newOpts); } } From 3ec43b5d9fceceec65db56ea60186f2198ce727f Mon Sep 17 00:00:00 2001 From: Jason Endo Date: Mon, 3 Feb 2025 16:37:57 -0800 Subject: [PATCH 4/7] clean up --- packages/sdk/src/server/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/sdk/src/server/index.ts b/packages/sdk/src/server/index.ts index d3d91318e9feb..1ff5aeadd41bc 100644 --- a/packages/sdk/src/server/index.ts +++ b/packages/sdk/src/server/index.ts @@ -381,8 +381,13 @@ export class BackendClient extends BaseClient { method: "GET", }); } - //public getProxyRequest(url: string, method: string, body: string, params: any): Promise { - public makeProxyRequest(url: string, query: any, opts: MakeProxyRequestOpts): Promise { + + /** + * Makes a proxy request to a URL with the specified query parameters and options. + * + * @returns A promise resolving to the response from the downstream service + */ + public makeProxyRequest(url: string, query: any, opts: MakeProxyRequestOpts): Promise { const url64 = btoa(url).replace(/\+/g, "-") .replace(/\//g, "_") .replace(/=+$/, ""); From f73fe7984888ce94abf2f92819870a47965e254a Mon Sep 17 00:00:00 2001 From: Jason Endo Date: Tue, 4 Feb 2025 11:11:30 -0800 Subject: [PATCH 5/7] fix linting --- packages/sdk/src/server/index.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/sdk/src/server/index.ts b/packages/sdk/src/server/index.ts index be2f57b3f8862..1f870fc1345b2 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, type RequestOptions + Account, BaseClient, type AppInfo, type ConnectTokenResponse, type RequestOptions, } from "../shared/index.js"; export * from "../shared/index.js"; @@ -116,8 +116,7 @@ export type MakeProxyRequestOpts = { */ method: string; headers?: Record; - //headers: Record; - body?: any + body?: string; }; /** @@ -393,10 +392,10 @@ export class BackendClient extends BaseClient { * * @returns A promise resolving to the response from the downstream service */ - public makeProxyRequest(url: string, query: any, opts: MakeProxyRequestOpts): Promise { + public makeProxyRequest(url: string, query: Record, opts: MakeProxyRequestOpts): Promise { const url64 = btoa(url).replace(/\+/g, "-") - .replace(/\//g, "_") - .replace(/=+$/, ""); + .replace(/\//g, "_") + .replace(/=+$/, ""); const headers = opts.headers || {}; From 9ab11138f8313bacaa73d30ba8e88d0b9a0c8c27 Mon Sep 17 00:00:00 2001 From: Jason Endo Date: Wed, 5 Feb 2025 12:06:21 -0800 Subject: [PATCH 6/7] change the function header to be more fetch-like --- packages/sdk/src/server/index.ts | 52 +++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/packages/sdk/src/server/index.ts b/packages/sdk/src/server/index.ts index 1f870fc1345b2..9a88148633a18 100644 --- a/packages/sdk/src/server/index.ts +++ b/packages/sdk/src/server/index.ts @@ -108,17 +108,47 @@ export type GetAccountByIdOpts = { }; /** - * Parameters for the retrieval of an account from the Connect API + * Options used to determine the external user and account to be used in Connect Proxy API */ -export type MakeProxyRequestOpts = { +export type ProxyApiOpts = { /** - * Whether to retrieve the account's credentials or not. + * 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: string; + /** + * 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. * @@ -388,16 +418,16 @@ export class BackendClient extends BaseClient { } /** - * Makes a proxy request to a URL with the specified query parameters and options. + * 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(url: string, query: Record, opts: MakeProxyRequestOpts): Promise { - const url64 = btoa(url).replace(/\+/g, "-") + public makeProxyRequest(proxyOptions: ProxyApiOpts, targetRequest: ProxyTargetApiRequest): Promise { + const url64 = btoa(targetRequest.url).replace(/\+/g, "-") .replace(/\//g, "_") .replace(/=+$/, ""); - const headers = opts.headers || {}; + const headers = targetRequest.options.headers || {}; const newHeaders = Object.keys(headers).reduce<{ [key: string]: string }>((acc, key) => { acc[`x-pd-proxy-${key}`] = headers[key]; @@ -405,13 +435,13 @@ export class BackendClient extends BaseClient { }, {}); const newOpts: RequestOptions = { - method: opts.method, + method: targetRequest.options.method, headers: newHeaders, - params: query, + params: proxyOptions.searchParams, } - if (opts.body) { - newOpts.body = opts.body + if (targetRequest.options.body) { + newOpts.body = targetRequest.options.body } return this.makeConnectRequest(`/proxy/${url64}`, newOpts); From 76a8ff76209a6a06a6d9570c87cc822927f02911 Mon Sep 17 00:00:00 2001 From: Jason Endo Date: Wed, 5 Feb 2025 13:18:40 -0800 Subject: [PATCH 7/7] PR feedback --- packages/sdk/CHANGELOG.md | 6 ++++++ packages/sdk/package.json | 2 +- packages/sdk/src/server/index.ts | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) 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 9a88148633a18..1ad8500f9cb83 100644 --- a/packages/sdk/src/server/index.ts +++ b/packages/sdk/src/server/index.ts @@ -124,7 +124,7 @@ export type ProxyTargetApiOpts = { /** * http method for the request */ - method: string; + method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; /** * http headers for the request */