@@ -258,6 +258,11 @@ interface RequestOptions extends Omit<RequestInit, "headers"> {
258
258
* Headers to include in the request.
259
259
*/
260
260
headers ?: Record < string , string > ;
261
+
262
+ /**
263
+ * The URL to make the request to.
264
+ */
265
+ baseURL ?: string ;
261
266
}
262
267
263
268
/**
@@ -284,7 +289,7 @@ class ServerClient {
284
289
environment ?: string ;
285
290
secretKey : string ;
286
291
publicKey : string ;
287
- oauthClient : ClientCredentials ;
292
+ oauthClient ? : ClientCredentials ;
288
293
oauthToken ?: AccessToken ;
289
294
baseURL : string ;
290
295
@@ -339,6 +344,10 @@ class ServerClient {
339
344
}
340
345
341
346
async _oauthAuthorizationHeader ( ) : Promise < string > {
347
+ if ( ! this . oauthClient ) {
348
+ throw new Error ( "OAuth client not configured" ) ;
349
+ }
350
+
342
351
if ( ! this . oauthToken || this . oauthToken . expired ( ) ) {
343
352
this . oauthToken = await this . oauthClient . getToken ( { } ) ;
344
353
}
@@ -364,9 +373,10 @@ class ServerClient {
364
373
headers : customHeaders ,
365
374
body,
366
375
method = "GET" ,
376
+ baseURL = this . baseURL ,
367
377
...fetchOpts
368
378
} = opts ;
369
- const url = new URL ( `${ this . baseURL } ${ path } ` ) ;
379
+ const url = new URL ( `${ baseURL } ${ path } ` ) ;
370
380
371
381
if ( params ) {
372
382
Object . entries ( params ) . forEach ( ( [
@@ -615,4 +625,51 @@ class ServerClient {
615
625
method : "GET" ,
616
626
} ) ;
617
627
}
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
+ }
618
675
}
0 commit comments