UNPKG

@nhost/nhost-js

Version:

Nhost JavaScript SDK

107 lines (106 loc) 3.66 kB
import { NhostClientConstructorParams } from '../../utils/types'; import { NhostFunctionCallConfig, NhostFunctionCallResponse, NhostFunctionsConstructorParams } from './types'; /** * Creates a client for Functions from either a subdomain or a URL */ export declare function createFunctionsClient(params: NhostClientConstructorParams): NhostFunctionsClient; /** * @alias Functions */ export declare class NhostFunctionsClient { readonly url: string; private accessToken; private adminSecret?; private headers; constructor(params: NhostFunctionsConstructorParams); /** * Use `nhost.functions.call` to call (sending a POST request to) a serverless function. Use generic * types to specify the expected response data, request body and error message. * * @example * ### Without generic types * ```ts * await nhost.functions.call('send-welcome-email', { email: 'joe@example.com', name: 'Joe Doe' }) * ``` * * @example * ### Using generic types * ```ts * type Data = { * message: string * } * * type Body = { * email: string * name: string * } * * type ErrorMessage = { * details: string * } * * // The function will only accept a body of type `Body` * const { res, error } = await nhost.functions.call<Data, Body, ErrorMessage>( * 'send-welcome-email', * { email: 'joe@example.com', name: 'Joe Doe' } * ) * * // Now the response data is typed as `Data` * console.log(res?.data.message) * * // Now the error message is typed as `ErrorMessage` * console.log(error?.message.details) * ``` * * @docs https://docs.nhost.io/reference/javascript/nhost-js/functions/call */ call<TData = unknown, TBody = any, TErrorMessage = any>(url: string, body?: TBody | null, config?: NhostFunctionCallConfig): Promise<NhostFunctionCallResponse<TData, TErrorMessage>>; /** * Use `nhost.functions.setAccessToken` to a set an access token to be used in subsequent functions requests. Note that if you're signin in users with `nhost.auth.signIn()` the access token will be set automatically. * * @example * ```ts * nhost.functions.setAccessToken('some-access-token') * ``` * * @docs https://docs.nhost.io/reference/javascript/nhost-js/functions/set-access-token */ setAccessToken(accessToken: string | undefined): void; /** * Use `nhost.functions.getHeaders` to get the global headers sent with all functions requests. * * @example * ```ts * nhost.functions.getHeaders() * ``` * * @docs https://docs.nhost.io/reference/javascript/nhost-js/functions/get-headers */ getHeaders(): Record<string, string>; /** * Use `nhost.functions.setHeaders` to a set global headers to be sent in all subsequent functions requests. * * @example * ```ts * nhost.functions.setHeaders({ * 'x-hasura-role': 'admin' * }) * ``` * * @docs https://docs.nhost.io/reference/javascript/nhost-js/functions/set-headers */ setHeaders(headers?: Record<string, string>): void; /** * Use `nhost.functions.unsetHeaders` to a unset global headers sent with all functions requests. * * @example * ```ts * nhost.functions.unsetHeaders() * ``` * * @docs https://docs.nhost.io/reference/javascript/nhost-js/functions/unset-headers */ unsetHeaders(): void; generateAccessTokenHeaders(): NhostFunctionCallConfig['headers']; } //# sourceMappingURL=index.d.ts.map