@@ -10,8 +10,9 @@ import { Fetch, resolveFetch } from '../utils/fetch'
1010 * @param headers - The headers to pass to the function.
1111 */
1212interface FunctionInvokeOptions {
13- args : any [ ]
13+ params : Record < string , any >
1414 headers ?: Record < string , string >
15+ apiKey ?: string
1516}
1617
1718/**
@@ -27,13 +28,15 @@ export class FunctionsClient {
2728 constructor (
2829 connectionString : string ,
2930 options : {
30- customFetch ?: Fetch ,
31+ fetch ?: Fetch ,
3132 headers ?: Record < string , string >
32- } = { }
33+ } = {
34+ headers : { }
35+ }
3336 ) {
3437 this . url = getAPIUrl ( connectionString , FUNCTIONS_ROOT_PATH )
35- this . fetch = resolveFetch ( options . customFetch )
36- this . headers = options . headers ? { ...DEFAULT_HEADERS , ...options . headers } : { ... DEFAULT_HEADERS }
38+ this . fetch = resolveFetch ( options . fetch )
39+ this . headers = { ...DEFAULT_HEADERS , ...options . headers }
3740 }
3841 // TODO: check authorization and api key setup in Gateway
3942 setAuth ( token : string ) {
@@ -43,56 +46,42 @@ export class FunctionsClient {
4346 async invoke ( functionId : string , options : FunctionInvokeOptions ) {
4447 let body ;
4548 let _headers : Record < string , string > = { }
46- if ( options . args &&
49+ if ( options . params &&
4750 ( ( options . headers && ! Object . prototype . hasOwnProperty . call ( options . headers , 'Content-Type' ) ) || ! options . headers )
4851 ) {
4952 if (
50- ( typeof Blob !== 'undefined' && options . args instanceof Blob ) ||
51- options . args instanceof ArrayBuffer
53+ ( typeof Blob !== 'undefined' && options . params instanceof Blob ) ||
54+ options . params instanceof ArrayBuffer
5255 ) {
5356 // will work for File as File inherits Blob
5457 // also works for ArrayBuffer as it is the same underlying structure as a Blob
5558 _headers [ 'Content-Type' ] = 'application/octet-stream'
56- body = options . args
57- } else if ( typeof options . args === 'string' ) {
59+ body = options . params
60+ } else if ( typeof options . params === 'string' ) {
5861 // plain string
5962 _headers [ 'Content-Type' ] = 'text/plain'
60- body = options . args
61- } else if ( typeof FormData !== 'undefined' && options . args instanceof FormData ) {
63+ body = options . params
64+ } else if ( typeof FormData !== 'undefined' && options . params instanceof FormData ) {
6265 _headers [ 'Content-Type' ] = 'multipart/form-data'
63- body = options . args
66+ body = options . params
6467 } else {
6568 // default, assume this is JSON
6669 _headers [ 'Content-Type' ] = 'application/json'
67- body = JSON . stringify ( options . args )
70+ body = JSON . stringify ( options . params )
6871 }
6972 }
7073
7174 try {
7275 const response = await this . fetch ( `${ this . url } /${ functionId } ` , {
7376 method : 'POST' ,
74- body : JSON . stringify ( options . args ) ,
77+ body,
7578 headers : { ..._headers , ...this . headers , ...options . headers }
7679 } )
7780
7881 if ( ! response . ok ) {
7982 throw new SQLiteCloudError ( `Failed to invoke function: ${ response . statusText } ` )
8083 }
81-
82- let responseType = ( response . headers . get ( 'Content-Type' ) ?? 'text/plain' ) . split ( ';' ) [ 0 ] . trim ( )
83- let data : any
84- if ( responseType === 'application/json' ) {
85- data = await response . json ( )
86- } else if ( responseType === 'application/octet-stream' ) {
87- data = await response . blob ( )
88- } else if ( responseType === 'text/event-stream' ) {
89- data = response
90- } else if ( responseType === 'multipart/form-data' ) {
91- data = await response . formData ( )
92- } else {
93- data = await response . text ( )
94- }
95- return { ...data , error : null }
84+ return { error : null , ...( await response . json ( ) ) }
9685 } catch ( error ) {
9786 return { data : null , error }
9887 }
0 commit comments