|
4 | 4 |
|
5 | 5 | import * as oauth from "oauth4webapi"; |
6 | 6 | import { |
7 | | - Account, BaseClient, type AppInfo, type ConnectTokenResponse, |
| 7 | + Account, BaseClient, type AppInfo, type ConnectTokenResponse, type RequestOptions, |
8 | 8 | } from "../shared/index.js"; |
9 | 9 | export * from "../shared/index.js"; |
10 | 10 |
|
@@ -107,6 +107,48 @@ export type GetAccountByIdOpts = { |
107 | 107 | include_credentials?: boolean; |
108 | 108 | }; |
109 | 109 |
|
| 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 | + |
110 | 152 | /** |
111 | 153 | * Creates a new instance of BackendClient with the provided options. |
112 | 154 | * |
@@ -374,4 +416,34 @@ export class BackendClient extends BaseClient { |
374 | 416 | method: "GET", |
375 | 417 | }); |
376 | 418 | } |
| 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 | + } |
377 | 449 | } |
0 commit comments