Skip to content

Commit 133479e

Browse files
authored
Invoke workflows via the SDK (#14040)
* 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 * Make OAuth client optional
1 parent d83f8ee commit 133479e

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

packages/sdk/src/server/index.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ interface RequestOptions extends Omit<RequestInit, "headers"> {
258258
* Headers to include in the request.
259259
*/
260260
headers?: Record<string, string>;
261+
262+
/**
263+
* The URL to make the request to.
264+
*/
265+
baseURL?: string;
261266
}
262267

263268
/**
@@ -284,7 +289,7 @@ class ServerClient {
284289
environment?: string;
285290
secretKey: string;
286291
publicKey: string;
287-
oauthClient: ClientCredentials;
292+
oauthClient?: ClientCredentials;
288293
oauthToken?: AccessToken;
289294
baseURL: string;
290295

@@ -339,6 +344,10 @@ class ServerClient {
339344
}
340345

341346
async _oauthAuthorizationHeader(): Promise<string> {
347+
if (!this.oauthClient) {
348+
throw new Error("OAuth client not configured");
349+
}
350+
342351
if (!this.oauthToken || this.oauthToken.expired()) {
343352
this.oauthToken = await this.oauthClient.getToken({});
344353
}
@@ -364,9 +373,10 @@ class ServerClient {
364373
headers: customHeaders,
365374
body,
366375
method = "GET",
376+
baseURL = this.baseURL,
367377
...fetchOpts
368378
} = opts;
369-
const url = new URL(`${this.baseURL}${path}`);
379+
const url = new URL(`${baseURL}${path}`);
370380

371381
if (params) {
372382
Object.entries(params).forEach(([
@@ -615,4 +625,51 @@ class ServerClient {
615625
method: "GET",
616626
});
617627
}
628+
629+
/**
630+
* Invokes a workflow using the URL of its HTTP interface(s), by sending an
631+
* HTTP POST request with the provided body.
632+
*
633+
* @param url - The URL of the workflow's HTTP interface.
634+
* @param opts - The options for the request.
635+
* @param opts.body - The body of the request. It must be a JSON-serializable
636+
* value (e.g. an object, `null`, a string, etc.).
637+
* @param opts.headers - The headers to include in the request. Note that the
638+
* `Authorization` header will always be set with an OAuth access token
639+
* retrieved by the client.
640+
*
641+
* @returns A promise resolving to the response from the workflow.
642+
*
643+
* @example
644+
* ```typescript
645+
* const response: JSON = await client.invokeWorkflow(
646+
* "https://eoy64t2rbte1u2p.m.pipedream.net",
647+
* {
648+
* body: {
649+
* foo: 123,
650+
* bar: "abc",
651+
* baz: null,
652+
* },
653+
* headers: {
654+
* "Accept": "application/json",
655+
* },
656+
* },
657+
* );
658+
*/
659+
async invokeWorkflow(url: string, opts: RequestOptions = {}): Promise<unknown> {
660+
const {
661+
body = null,
662+
headers = {},
663+
} = opts;
664+
665+
return this._makeRequest("", {
666+
baseURL: url,
667+
method: "POST",
668+
headers: {
669+
...headers,
670+
"Authorization": await this._oauthAuthorizationHeader(),
671+
},
672+
body: JSON.stringify(body),
673+
});
674+
}
618675
}

0 commit comments

Comments
 (0)