From 1ebeef221d56b0dbfc90c3835b397630b6ce6e21 Mon Sep 17 00:00:00 2001 From: Jay Vercellone Date: Thu, 19 Sep 2024 16:40:35 -0700 Subject: [PATCH 1/3] Invoke workflows via the SDK * Refactor `_makeRequest` to accept an optional `baseURL` argument, so that it can target URLs other than the API * Implement `invokeWorkflow` to make a request to the HTTP interface of a workflow --- packages/sdk/src/server/index.ts | 38 +++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/sdk/src/server/index.ts b/packages/sdk/src/server/index.ts index bf9a6f9521a84..6665414cf77e5 100644 --- a/packages/sdk/src/server/index.ts +++ b/packages/sdk/src/server/index.ts @@ -258,6 +258,11 @@ interface RequestOptions extends Omit { * Headers to include in the request. */ headers?: Record; + + /** + * The URL to make the request to. + */ + baseURL?: string; } /** @@ -364,9 +369,10 @@ class ServerClient { headers: customHeaders, body, method = "GET", + baseURL = this.baseURL, ...fetchOpts } = opts; - const url = new URL(`${this.baseURL}${path}`); + const url = new URL(`${baseURL}${path}`); if (params) { Object.entries(params).forEach(([ @@ -615,4 +621,34 @@ class ServerClient { method: "GET", }); } + + /** + * Invokes a workflow using the URL of its HTTP interface(s), by sending an + * HTTP POST request with the provided body. + * + * @param url - The URL of the workflow's HTTP interface. + * @param body - The body to send with the request. + * @returns A promise resolving to the response from the workflow. + * + * @example + * ```typescript + * const response: JSON = await client.invokeWorkflow( + * "https://eoy64t2rbte1u2p.m.pipedream.net", + * { + * foo: 123, + * bar: "abc", + * baz: null, + * }, + * ); + */ + async invokeWorkflow(url: string, body: unknown = null): Promise { + return this._makeRequest("", { + baseURL: url, + method: "POST", + headers: { + "Authorization": await this._oauthAuthorizationHeader(), + }, + body: JSON.stringify(body), + }); + } } From 23e75e5fc0d65a0ccca0184759af6b807bc5a7fb Mon Sep 17 00:00:00 2001 From: Jay Vercellone Date: Fri, 20 Sep 2024 08:34:07 -0700 Subject: [PATCH 2/3] Can customize headers --- packages/sdk/src/server/index.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/sdk/src/server/index.ts b/packages/sdk/src/server/index.ts index 6665414cf77e5..6c07f67fb5124 100644 --- a/packages/sdk/src/server/index.ts +++ b/packages/sdk/src/server/index.ts @@ -627,7 +627,13 @@ class ServerClient { * HTTP POST request with the provided body. * * @param url - The URL of the workflow's HTTP interface. - * @param body - The body to send with the request. + * @param opts - The options for the request. + * @param opts.body - The body of the request. It must be a JSON-serializable + * value (e.g. an object, `null`, a string, etc.). + * @param opts.headers - The headers to include in the request. Note that the + * `Authorization` header will always be set with an OAuth access token + * retrieved by the client. + * * @returns A promise resolving to the response from the workflow. * * @example @@ -635,17 +641,28 @@ class ServerClient { * const response: JSON = await client.invokeWorkflow( * "https://eoy64t2rbte1u2p.m.pipedream.net", * { - * foo: 123, - * bar: "abc", - * baz: null, + * body: { + * foo: 123, + * bar: "abc", + * baz: null, + * }, + * headers: { + * "Accept": "application/json", + * }, * }, * ); */ - async invokeWorkflow(url: string, body: unknown = null): Promise { + async invokeWorkflow(url: string, opts: RequestOptions = {}): Promise { + const { + body = null, + headers = {}, + } = opts; + return this._makeRequest("", { baseURL: url, method: "POST", headers: { + ...headers, "Authorization": await this._oauthAuthorizationHeader(), }, body: JSON.stringify(body), From b15cb05d7bc30cedee5e5ebf2d6175b5d21a7989 Mon Sep 17 00:00:00 2001 From: Jay Vercellone Date: Fri, 20 Sep 2024 10:26:33 -0700 Subject: [PATCH 3/3] Make OAuth client optional --- packages/sdk/src/server/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/sdk/src/server/index.ts b/packages/sdk/src/server/index.ts index 6c07f67fb5124..d7ed3209c3cb8 100644 --- a/packages/sdk/src/server/index.ts +++ b/packages/sdk/src/server/index.ts @@ -289,7 +289,7 @@ class ServerClient { environment?: string; secretKey: string; publicKey: string; - oauthClient: ClientCredentials; + oauthClient?: ClientCredentials; oauthToken?: AccessToken; baseURL: string; @@ -344,6 +344,10 @@ class ServerClient { } async _oauthAuthorizationHeader(): Promise { + if (!this.oauthClient) { + throw new Error("OAuth client not configured"); + } + if (!this.oauthToken || this.oauthToken.expired()) { this.oauthToken = await this.oauthClient.getToken({}); }