|
| 1 | +import { resolveFetch } from './helper' |
| 2 | +import { |
| 3 | + Fetch, |
| 4 | + FunctionsFetchError, |
| 5 | + FunctionsHttpError, |
| 6 | + FunctionsRelayError, |
| 7 | + FunctionsResponse, |
| 8 | +} from './types' |
| 9 | + |
| 10 | +export class FunctionsClient { |
| 11 | + protected url: string |
| 12 | + protected headers: Record<string, string> |
| 13 | + protected fetch: Fetch |
| 14 | + |
| 15 | + constructor( |
| 16 | + url: string, |
| 17 | + { |
| 18 | + headers = {}, |
| 19 | + customFetch, |
| 20 | + }: { |
| 21 | + headers?: Record<string, string> |
| 22 | + customFetch?: Fetch |
| 23 | + } = {} |
| 24 | + ) { |
| 25 | + this.url = url |
| 26 | + this.headers = headers |
| 27 | + this.fetch = resolveFetch(customFetch) |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Updates the authorization header |
| 32 | + * @params token - the new jwt token sent in the authorisation header |
| 33 | + */ |
| 34 | + setAuth(token: string) { |
| 35 | + this.headers.Authorization = `Bearer ${token}` |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Invokes a function |
| 40 | + * @param functionName - the name of the function to invoke |
| 41 | + * @param functionArgs - the arguments to the function |
| 42 | + * @param options - function invoke options |
| 43 | + * @param options.headers - headers to send with the request |
| 44 | + */ |
| 45 | + async invoke( |
| 46 | + functionName: string, |
| 47 | + functionArgs: any, |
| 48 | + { |
| 49 | + headers = {}, |
| 50 | + }: { |
| 51 | + headers?: Record<string, string> |
| 52 | + } = {} |
| 53 | + ): Promise<FunctionsResponse> { |
| 54 | + try { |
| 55 | + let _headers: Record<string, string> = {} |
| 56 | + let body: any |
| 57 | + if (functionArgs instanceof Blob || functionArgs instanceof ArrayBuffer) { |
| 58 | + // will work for File as File inherits Blob |
| 59 | + // also works for ArrayBuffer as it is the same underlying structure as a Blob |
| 60 | + _headers['Content-Type'] = 'application/octet-stream' |
| 61 | + body = functionArgs |
| 62 | + } else if (typeof functionArgs === 'string') { |
| 63 | + // plain string |
| 64 | + _headers['Content-Type'] = 'text/plain' |
| 65 | + body = functionArgs |
| 66 | + } else if (functionArgs instanceof FormData) { |
| 67 | + // don't set content-type headers |
| 68 | + // Request will automatically add the right boundary value |
| 69 | + body = functionArgs |
| 70 | + } else { |
| 71 | + // default, assume this is JSON |
| 72 | + _headers['Content-Type'] = 'application/json' |
| 73 | + body = JSON.stringify(functionArgs) |
| 74 | + } |
| 75 | + |
| 76 | + const response = await this.fetch(`${this.url}/${functionName}`, { |
| 77 | + method: 'POST', |
| 78 | + // headers priority is (high to low): |
| 79 | + // 1. invoke-level headers |
| 80 | + // 2. client-level headers |
| 81 | + // 3. default Content-Type header |
| 82 | + headers: { ..._headers, ...this.headers, ...headers }, |
| 83 | + body, |
| 84 | + }).catch((fetchError) => { |
| 85 | + throw new FunctionsFetchError(fetchError) |
| 86 | + }) |
| 87 | + |
| 88 | + const isRelayError = response.headers.get('x-relay-error') |
| 89 | + if (isRelayError && isRelayError === 'true') { |
| 90 | + throw new FunctionsRelayError(response) |
| 91 | + } |
| 92 | + |
| 93 | + if (!response.ok) { |
| 94 | + throw new FunctionsHttpError(response) |
| 95 | + } |
| 96 | + |
| 97 | + let responseType = (response.headers.get('Content-Type') ?? 'text/plain').split(';')[0].trim() |
| 98 | + let data: any |
| 99 | + if (responseType === 'application/json') { |
| 100 | + data = await response.json() |
| 101 | + } else if (responseType === 'application/octet-stream') { |
| 102 | + data = await response.blob() |
| 103 | + } else if (responseType === 'multipart/form-data') { |
| 104 | + data = await response.formData() |
| 105 | + } else { |
| 106 | + // default to text |
| 107 | + data = await response.text() |
| 108 | + } |
| 109 | + |
| 110 | + return { data, error: null } |
| 111 | + } catch (error) { |
| 112 | + return { data: null, error } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments