Skip to content

Commit 1ebeef2

Browse files
committed
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
1 parent 64a3bc0 commit 1ebeef2

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

packages/sdk/src/server/index.ts

Lines changed: 37 additions & 1 deletion
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
/**
@@ -364,9 +369,10 @@ class ServerClient {
364369
headers: customHeaders,
365370
body,
366371
method = "GET",
372+
baseURL = this.baseURL,
367373
...fetchOpts
368374
} = opts;
369-
const url = new URL(`${this.baseURL}${path}`);
375+
const url = new URL(`${baseURL}${path}`);
370376

371377
if (params) {
372378
Object.entries(params).forEach(([
@@ -615,4 +621,34 @@ class ServerClient {
615621
method: "GET",
616622
});
617623
}
624+
625+
/**
626+
* Invokes a workflow using the URL of its HTTP interface(s), by sending an
627+
* HTTP POST request with the provided body.
628+
*
629+
* @param url - The URL of the workflow's HTTP interface.
630+
* @param body - The body to send with the request.
631+
* @returns A promise resolving to the response from the workflow.
632+
*
633+
* @example
634+
* ```typescript
635+
* const response: JSON = await client.invokeWorkflow(
636+
* "https://eoy64t2rbte1u2p.m.pipedream.net",
637+
* {
638+
* foo: 123,
639+
* bar: "abc",
640+
* baz: null,
641+
* },
642+
* );
643+
*/
644+
async invokeWorkflow(url: string, body: unknown = null): Promise<unknown> {
645+
return this._makeRequest("", {
646+
baseURL: url,
647+
method: "POST",
648+
headers: {
649+
"Authorization": await this._oauthAuthorizationHeader(),
650+
},
651+
body: JSON.stringify(body),
652+
});
653+
}
618654
}

0 commit comments

Comments
 (0)