Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions packages/sdk/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ interface RequestOptions extends Omit<RequestInit, "headers"> {
* Headers to include in the request.
*/
headers?: Record<string, string>;

/**
* The URL to make the request to.
*/
baseURL?: string;
}

/**
Expand All @@ -284,7 +289,7 @@ class ServerClient {
environment?: string;
secretKey: string;
publicKey: string;
oauthClient: ClientCredentials;
oauthClient?: ClientCredentials;
oauthToken?: AccessToken;
baseURL: string;

Expand Down Expand Up @@ -339,6 +344,10 @@ class ServerClient {
}

async _oauthAuthorizationHeader(): Promise<string> {
if (!this.oauthClient) {
throw new Error("OAuth client not configured");
}

if (!this.oauthToken || this.oauthToken.expired()) {
this.oauthToken = await this.oauthClient.getToken({});
}
Expand All @@ -364,9 +373,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(([
Expand Down Expand Up @@ -615,4 +625,51 @@ 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 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
* ```typescript
* const response: JSON = await client.invokeWorkflow(
* "https://eoy64t2rbte1u2p.m.pipedream.net",
* {
* body: {
* foo: 123,
* bar: "abc",
* baz: null,
* },
* headers: {
* "Accept": "application/json",
* },
* },
* );
*/
async invokeWorkflow(url: string, opts: RequestOptions = {}): Promise<unknown> {
const {
body = null,
headers = {},
} = opts;

return this._makeRequest("", {
baseURL: url,
method: "POST",
headers: {
...headers,
"Authorization": await this._oauthAuthorizationHeader(),
},
body: JSON.stringify(body),
});
}
}
Loading