179 lines
6.5 KiB
TypeScript
179 lines
6.5 KiB
TypeScript
import { Fetch, FunctionInvokeOptions, FunctionRegion, FunctionsResponse } from './types';
|
|
/**
|
|
* Client for invoking Supabase Edge Functions.
|
|
*/
|
|
export declare class FunctionsClient {
|
|
protected url: string;
|
|
protected headers: Record<string, string>;
|
|
protected region: FunctionRegion;
|
|
protected fetch: Fetch;
|
|
/**
|
|
* Creates a new Functions client bound to an Edge Functions URL.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* import { FunctionsClient, FunctionRegion } from '@supabase/functions-js'
|
|
*
|
|
* const functions = new FunctionsClient('https://xyzcompany.supabase.co/functions/v1', {
|
|
* headers: { apikey: 'public-anon-key' },
|
|
* region: FunctionRegion.UsEast1,
|
|
* })
|
|
* ```
|
|
*
|
|
* @category Functions
|
|
*
|
|
* @example Creating a Functions client
|
|
* ```ts
|
|
* import { FunctionsClient, FunctionRegion } from '@supabase/functions-js'
|
|
*
|
|
* const functions = new FunctionsClient('https://xyzcompany.supabase.co/functions/v1', {
|
|
* headers: { apikey: 'public-anon-key' },
|
|
* region: FunctionRegion.UsEast1,
|
|
* })
|
|
* ```
|
|
*/
|
|
constructor(url: string, { headers, customFetch, region, }?: {
|
|
headers?: Record<string, string>;
|
|
customFetch?: Fetch;
|
|
region?: FunctionRegion;
|
|
});
|
|
/**
|
|
* Updates the authorization header
|
|
* @param token - the new jwt token sent in the authorisation header
|
|
*
|
|
* @category Functions
|
|
*
|
|
* @example Setting the authorization header
|
|
* ```ts
|
|
* functions.setAuth(session.access_token)
|
|
* ```
|
|
*/
|
|
setAuth(token: string): void;
|
|
/**
|
|
* Invokes a function
|
|
* @param functionName - The name of the Function to invoke.
|
|
* @param options - Options for invoking the Function.
|
|
* @example
|
|
* ```ts
|
|
* const { data, error } = await functions.invoke('hello-world', {
|
|
* body: { name: 'Ada' },
|
|
* })
|
|
* ```
|
|
*
|
|
* @category Functions
|
|
*
|
|
* @remarks
|
|
* - Requires an Authorization header.
|
|
* - Invoke params generally match the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) spec.
|
|
* - When you pass in a body to your function, we automatically attach the Content-Type header for `Blob`, `ArrayBuffer`, `File`, `FormData` and `String`. If it doesn't match any of these types we assume the payload is `json`, serialize it and attach the `Content-Type` header as `application/json`. You can override this behavior by passing in a `Content-Type` header of your own.
|
|
* - Responses are automatically parsed as `json`, `blob` and `form-data` depending on the `Content-Type` header sent by your function. Responses are parsed as `text` by default.
|
|
*
|
|
* @example Basic invocation
|
|
* ```js
|
|
* const { data, error } = await supabase.functions.invoke('hello', {
|
|
* body: { foo: 'bar' }
|
|
* })
|
|
* ```
|
|
*
|
|
* @exampleDescription Error handling
|
|
* A `FunctionsHttpError` error is returned if your function throws an error, `FunctionsRelayError` if the Supabase Relay has an error processing your function and `FunctionsFetchError` if there is a network error in calling your function.
|
|
*
|
|
* @example Error handling
|
|
* ```js
|
|
* import { FunctionsHttpError, FunctionsRelayError, FunctionsFetchError } from "@supabase/supabase-js";
|
|
*
|
|
* const { data, error } = await supabase.functions.invoke('hello', {
|
|
* headers: {
|
|
* "my-custom-header": 'my-custom-header-value'
|
|
* },
|
|
* body: { foo: 'bar' }
|
|
* })
|
|
*
|
|
* if (error instanceof FunctionsHttpError) {
|
|
* const errorMessage = await error.context.json()
|
|
* console.log('Function returned an error', errorMessage)
|
|
* } else if (error instanceof FunctionsRelayError) {
|
|
* console.log('Relay error:', error.message)
|
|
* } else if (error instanceof FunctionsFetchError) {
|
|
* console.log('Fetch error:', error.message)
|
|
* }
|
|
* ```
|
|
*
|
|
* @exampleDescription Passing custom headers
|
|
* You can pass custom headers to your function. Note: supabase-js automatically passes the `Authorization` header with the signed in user's JWT.
|
|
*
|
|
* @example Passing custom headers
|
|
* ```js
|
|
* const { data, error } = await supabase.functions.invoke('hello', {
|
|
* headers: {
|
|
* "my-custom-header": 'my-custom-header-value'
|
|
* },
|
|
* body: { foo: 'bar' }
|
|
* })
|
|
* ```
|
|
*
|
|
* @exampleDescription Calling with DELETE HTTP verb
|
|
* You can also set the HTTP verb to `DELETE` when calling your Edge Function.
|
|
*
|
|
* @example Calling with DELETE HTTP verb
|
|
* ```js
|
|
* const { data, error } = await supabase.functions.invoke('hello', {
|
|
* headers: {
|
|
* "my-custom-header": 'my-custom-header-value'
|
|
* },
|
|
* body: { foo: 'bar' },
|
|
* method: 'DELETE'
|
|
* })
|
|
* ```
|
|
*
|
|
* @exampleDescription Invoking a Function in the UsEast1 region
|
|
* Here are the available regions:
|
|
* - `FunctionRegion.Any`
|
|
* - `FunctionRegion.ApNortheast1`
|
|
* - `FunctionRegion.ApNortheast2`
|
|
* - `FunctionRegion.ApSouth1`
|
|
* - `FunctionRegion.ApSoutheast1`
|
|
* - `FunctionRegion.ApSoutheast2`
|
|
* - `FunctionRegion.CaCentral1`
|
|
* - `FunctionRegion.EuCentral1`
|
|
* - `FunctionRegion.EuWest1`
|
|
* - `FunctionRegion.EuWest2`
|
|
* - `FunctionRegion.EuWest3`
|
|
* - `FunctionRegion.SaEast1`
|
|
* - `FunctionRegion.UsEast1`
|
|
* - `FunctionRegion.UsWest1`
|
|
* - `FunctionRegion.UsWest2`
|
|
*
|
|
* @example Invoking a Function in the UsEast1 region
|
|
* ```js
|
|
* import { createClient, FunctionRegion } from '@supabase/supabase-js'
|
|
*
|
|
* const { data, error } = await supabase.functions.invoke('hello', {
|
|
* body: { foo: 'bar' },
|
|
* region: FunctionRegion.UsEast1
|
|
* })
|
|
* ```
|
|
*
|
|
* @exampleDescription Calling with GET HTTP verb
|
|
* You can also set the HTTP verb to `GET` when calling your Edge Function.
|
|
*
|
|
* @example Calling with GET HTTP verb
|
|
* ```js
|
|
* const { data, error } = await supabase.functions.invoke('hello', {
|
|
* headers: {
|
|
* "my-custom-header": 'my-custom-header-value'
|
|
* },
|
|
* method: 'GET'
|
|
* })
|
|
* ```
|
|
*
|
|
* @example Example 7
|
|
* ```ts
|
|
* const { data, error } = await functions.invoke('hello-world', {
|
|
* body: { name: 'Ada' },
|
|
* })
|
|
* ```
|
|
*/
|
|
invoke<T = any>(functionName: string, options?: FunctionInvokeOptions): Promise<FunctionsResponse<T>>;
|
|
}
|
|
//# sourceMappingURL=FunctionsClient.d.ts.map
|