@@ -13,19 +13,14 @@ import {
1313 */ 
1414export  type  CreateServerClientOpts  =  { 
1515  /** 
16-    * The environment in which the server is running (e.g., "production", "development") . 
16+    * The public API key for accessing the service . 
1717   */ 
18-   environment ?: string ; 
18+   publicKey ?: string ; 
1919
2020  /** 
21-    * The public  API key for accessing the service. This key is required . 
21+    * The secret  API key for accessing the service. 
2222   */ 
23-   publicKey : string ; 
24- 
25-   /** 
26-    * The secret API key for accessing the service. This key is required. 
27-    */ 
28-   secretKey : string ; 
23+   secretKey ?: string ; 
2924
3025  /** 
3126   * The client ID of your workspace's OAuth application. 
@@ -225,7 +220,7 @@ export type Account = {
225220  updated_at : string ; 
226221
227222  /** 
228-    * The credentials associated with the account, if ` include_credentials`  was true. 
223+    * The credentials associated with the account, if include_credentials was true. 
229224   */ 
230225  credentials ?: Record < string ,  string > ; 
231226} ; 
@@ -248,7 +243,7 @@ export type ConnectAPIResponse<T> = T | ErrorResponse;
248243/** 
249244 * Options for making a request to the Pipedream API. 
250245 */ 
251- interface  RequestOptions  extends  Omit < RequestInit ,  "headers" >  { 
246+ interface  RequestOptions  extends  Omit < RequestInit ,  "headers"   |   "body" >  { 
252247  /** 
253248   * Query parameters to include in the request URL. 
254249   */ 
@@ -263,20 +258,27 @@ interface RequestOptions extends Omit<RequestInit, "headers"> {
263258   * The URL to make the request to. 
264259   */ 
265260  baseURL ?: string ; 
261+ 
262+   /** 
263+    * The body of the request. 
264+    */ 
265+   body ?: Record < string ,  unknown >  |  string  |  FormData  |  URLSearchParams  |  null ; 
266266} 
267267
268268/** 
269-  * Creates a new instance of ` ServerClient`  with the provided options. 
269+  * Creates a new instance of ServerClient with the provided options. 
270270 * 
271271 * @example  
272-  * ```typescript 
272+  * 
273+ typescript 
273274 * const client = createClient({ 
274275 *   publicKey: "your-public-key", 
275276 *   secretKey: "your-secret-key", 
276277 * }); 
277-  * ``` 
278+  * 
279+ 
278280 * @param  opts - The options for creating the server client. 
279-  * @returns  A new instance of ` ServerClient` . 
281+  * @returns  A new instance of ServerClient. 
280282 */ 
281283export  function  createClient ( opts : CreateServerClientOpts )  { 
282284  return  new  ServerClient ( opts ) ; 
@@ -286,20 +288,18 @@ export function createClient(opts: CreateServerClientOpts) {
286288 * A client for interacting with the Pipedream Connect API on the server-side. 
287289 */ 
288290class  ServerClient  { 
289-   environment ?: string ; 
290-   secretKey : string ; 
291-   publicKey : string ; 
291+   secretKey ?: string ; 
292+   publicKey ?: string ; 
292293  oauthClient ?: ClientCredentials ; 
293294  oauthToken ?: AccessToken ; 
294295  baseURL : string ; 
295296
296297  /** 
297-    * Constructs a new ` ServerClient`  instance. 
298+    * Constructs a new ServerClient instance. 
298299   * 
299300   * @param  opts - The options for configuring the server client. 
300301   */ 
301302  constructor ( opts : CreateServerClientOpts )  { 
302-     this . environment  =  opts . environment ; 
303303    this . secretKey  =  opts . secretKey ; 
304304    this . publicKey  =  opts . publicKey ; 
305305
@@ -376,6 +376,7 @@ class ServerClient {
376376      baseURL =  this . baseURL , 
377377      ...fetchOpts 
378378    }  =  opts ; 
379+ 
379380    const  url  =  new  URL ( `${ baseURL } ${ path }  ) ; 
380381
381382    if  ( params )  { 
@@ -390,10 +391,23 @@ class ServerClient {
390391    } 
391392
392393    const  headers  =  { 
393-       "Content-Type" : "application/json" , 
394394      ...customHeaders , 
395395    } ; 
396396
397+     let  processedBody : BodyInit  |  null  =  null ; 
398+ 
399+     if  ( body )  { 
400+       if  ( body  instanceof  FormData  ||  body  instanceof  URLSearchParams  ||  typeof  body  ===  "string" )  { 
401+         // For FormData, URLSearchParams, or strings, pass the body as-is 
402+         processedBody  =  body ; 
403+       }  else  { 
404+         // For objects, assume it's JSON and serialize it 
405+         processedBody  =  JSON . stringify ( body ) ; 
406+         // Set the Content-Type header to application/json if not already set 
407+         headers [ "Content-Type" ]  =  headers [ "Content-Type" ]  ||  "application/json" ; 
408+       } 
409+     } 
410+ 
397411    const  requestOptions : RequestInit  =  { 
398412      method, 
399413      headers, 
@@ -404,8 +418,8 @@ class ServerClient {
404418      "POST" , 
405419      "PUT" , 
406420      "PATCH" , 
407-     ] . includes ( method . toUpperCase ( ) )  &&  body )  { 
408-       requestOptions . body  =  body ; 
421+     ] . includes ( method . toUpperCase ( ) )  &&  processedBody )  { 
422+       requestOptions . body  =  processedBody ; 
409423    } 
410424
411425    const  response : Response  =  await  fetch ( url . toString ( ) ,  requestOptions ) ; 
@@ -414,8 +428,13 @@ class ServerClient {
414428      throw  new  Error ( `HTTP error! status: ${ response . status }  ) ; 
415429    } 
416430
417-     const  result  =  await  response . json ( )  as  unknown  as  T ; 
418-     return  result ; 
431+     // Attempt to parse JSON, fall back to raw text if it fails 
432+     const  contentType  =  response . headers . get ( "Content-Type" ) ; 
433+     if  ( contentType  &&  contentType . includes ( "application/json" ) )  { 
434+       return  await  response . json ( )  as  T ; 
435+     } 
436+ 
437+     return  await  response . text ( )  as  unknown  as  T ; 
419438  } 
420439
421440  /** 
@@ -432,6 +451,7 @@ class ServerClient {
432451    opts : RequestOptions  =  { } , 
433452  ) : Promise < T >  { 
434453    const  headers  =  { 
454+       "Content-Type" : "application/json" , 
435455      ...opts . headers  ??  { } , 
436456      "Authorization" : await  this . _oauthAuthorizationHeader ( ) , 
437457    } ; 
@@ -471,13 +491,15 @@ class ServerClient {
471491   * @returns  A promise resolving to the connect token response. 
472492   * 
473493   * @example  
474-    * ```typescript 
494+    * 
495+ typescript 
475496   * const tokenResponse = await client.connectTokenCreate({ 
476497   *   app_slug: "your-app-slug", 
477498   *   external_user_id: "external-user-id", 
478499   * }); 
479500   * console.log(tokenResponse.token); 
480-    * ``` 
501+    * 
502+ 
481503   */ 
482504  async  connectTokenCreate ( opts : ConnectTokenCreateOpts ) : Promise < ConnectTokenResponse >  { 
483505    const  body  =  { 
@@ -498,10 +520,12 @@ class ServerClient {
498520   * @returns  A promise resolving to a list of accounts. 
499521   * 
500522   * @example  
501-    * ```typescript 
523+    * 
524+ typescript 
502525   * const accounts = await client.getAccounts({ include_credentials: 1 }); 
503526   * console.log(accounts); 
504-    * ``` 
527+    * 
528+ 
505529   */ 
506530  async  getAccounts ( params : ConnectParams  =  { } ) : Promise < Account [ ] >  { 
507531    return  this . _makeConnectRequest < Account [ ] > ( "/accounts" ,  { 
@@ -517,10 +541,12 @@ class ServerClient {
517541   * @returns  A promise resolving to the account. 
518542   * 
519543   * @example  
520-    * ```typescript 
544+    * 
545+ typescript 
521546   * const account = await client.getAccount("account-id"); 
522547   * console.log(account); 
523-    * ``` 
548+    * 
549+ 
524550   */ 
525551  async  getAccount ( accountId : string ,  params : ConnectParams  =  { } ) : Promise < Account >  { 
526552    return  this . _makeConnectRequest < Account > ( `/accounts/${ accountId }  ,  { 
@@ -536,10 +562,12 @@ class ServerClient {
536562   * @returns  A promise resolving to a list of accounts. 
537563   * 
538564   * @example  
539-    * ```typescript 
565+    * 
566+ typescript 
540567   * const accounts = await client.getAccountsByApp("app-id"); 
541568   * console.log(accounts); 
542-    * ``` 
569+    * 
570+ 
543571   */ 
544572  async  getAccountsByApp ( appId : string ,  params : ConnectParams  =  { } ) : Promise < Account [ ] >  { 
545573    return  this . _makeConnectRequest < Account [ ] > ( `/accounts/app/${ appId }  ,  { 
@@ -555,10 +583,12 @@ class ServerClient {
555583   * @returns  A promise resolving to a list of accounts. 
556584   * 
557585   * @example  
558-    * ```typescript 
586+    * 
587+ typescript 
559588   * const accounts = await client.getAccountsByExternalId("external-id"); 
560589   * console.log(accounts); 
561-    * ``` 
590+    * 
591+ 
562592   */ 
563593  async  getAccountsByExternalId ( externalId : string ,  params : ConnectParams  =  { } ) : Promise < Account [ ] >  { 
564594    return  this . _makeConnectRequest < Account [ ] > ( `/accounts/external_id/${ externalId }  ,  { 
@@ -573,10 +603,12 @@ class ServerClient {
573603   * @returns  A promise resolving when the account is deleted. 
574604   * 
575605   * @example  
576-    * ```typescript 
606+    * 
607+ typescript 
577608   * await client.deleteAccount("account-id"); 
578609   * console.log("Account deleted"); 
579-    * ``` 
610+    * 
611+ 
580612   */ 
581613  async  deleteAccount ( accountId : string ) : Promise < void >  { 
582614    await  this . _makeConnectRequest ( `/accounts/${ accountId }  ,  { 
@@ -591,10 +623,12 @@ class ServerClient {
591623   * @returns  A promise resolving when all accounts are deleted. 
592624   * 
593625   * @example  
594-    * ```typescript 
626+    * 
627+ typescript 
595628   * await client.deleteAccountsByApp("app-id"); 
596629   * console.log("All accounts deleted"); 
597-    * ``` 
630+    * 
631+ 
598632   */ 
599633  async  deleteAccountsByApp ( appId : string ) : Promise < void >  { 
600634    await  this . _makeConnectRequest ( `/accounts/app/${ appId }  ,  { 
@@ -609,10 +643,12 @@ class ServerClient {
609643   * @returns  A promise resolving when all accounts are deleted. 
610644   * 
611645   * @example  
612-    * ```typescript 
646+    * 
647+ typescript 
613648   * await client.deleteExternalUser("external-id"); 
614649   * console.log("All accounts deleted"); 
615-    * ``` 
650+    * 
651+ 
616652   */ 
617653  async  deleteExternalUser ( externalId : string ) : Promise < void >  { 
618654    await  this . _makeConnectRequest ( `/users/${ externalId }  ,  { 
@@ -633,15 +669,16 @@ class ServerClient {
633669   * @param  url - The URL of the workflow's HTTP interface. 
634670   * @param  opts - The options for the request. 
635671   * @param  opts.body - The body of the request. It must be a JSON-serializable 
636-    * value (e.g. an object, ` null` , a string, etc.). 
672+    * value (e.g. an object, null, a string, etc.). 
637673   * @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 
674+    * Authorization header will always be set with an OAuth access token 
639675   * retrieved by the client. 
640676   * 
641677   * @returns  A promise resolving to the response from the workflow. 
642678   * 
643679   * @example  
644-    * ```typescript 
680+    * 
681+ typescript 
645682   * const response: JSON = await client.invokeWorkflow( 
646683   *   "https://eoy64t2rbte1u2p.m.pipedream.net", 
647684   *   { 
@@ -658,18 +695,18 @@ class ServerClient {
658695   */ 
659696  async  invokeWorkflow ( url : string ,  opts : RequestOptions  =  { } ) : Promise < unknown >  { 
660697    const  { 
661-       body  =   null , 
698+       body, 
662699      headers =  { } , 
663700    }  =  opts ; 
664701
665702    return  this . _makeRequest ( "" ,  { 
703+       ...opts , 
666704      baseURL : url , 
667-       method : "POST" , 
668705      headers : { 
669706        ...headers , 
670707        "Authorization" : await  this . _oauthAuthorizationHeader ( ) , 
671708      } , 
672-       body :  JSON . stringify ( body ) , 
709+       body, 
673710    } ) ; 
674711  } 
675712} 
0 commit comments