UNPKG

@nhost/nhost-js

Version:

Nhost JavaScript SDK

1 lines 6.12 kB
{"version":3,"file":"functions.cjs","names":[],"sources":["../../src/functions/client.ts"],"sourcesContent":["/**\n * Functions client for the Nhost JavaScript SDK.\n *\n * This module provides functionality for executing serverless function calls\n * against Nhost serverless functions.\n */\n\nimport {\n type ChainFunction,\n createEnhancedFetch,\n FetchError,\n type FetchResponse,\n} from '../fetch';\n\n/**\n * Functions client interface providing methods for executing serverless function calls\n */\nexport interface Client {\n baseURL: string;\n\n /** Add a middleware function to the fetch chain\n * @param chainFunction - The middleware function to add\n */\n pushChainFunction(chainFunction: ChainFunction): void;\n\n /**\n * Execute a request to a serverless function\n * The response body will be automatically parsed based on the content type into the following types:\n * - Object if the response is application/json\n * - string text string if the response is text/*\n * - Blob if the response is any other type\n *\n * @param path - The path to the serverless function\n * @param options - Additional fetch options to apply to the request\n * @returns Promise with the function response and metadata.\n */\n fetch<T = unknown>(\n path: string,\n options?: RequestInit,\n ): Promise<FetchResponse<T>>;\n\n /**\n * Executes a POST request to a serverless function with a JSON body\n *\n * This is a convenience method assuming the request is a POST with JSON body\n * setting the `Content-Type` and 'Accept' headers to `application/json` and\n * automatically stringifying the body.\n *\n * For a more generic request, use the `fetch` method instead.\n *\n * @param path - The path to the serverless function\n * @param body - The JSON body to send in the request\n * @param options - Additional fetch options to apply to the request\n * @returns Promise with the function response and metadata\n */\n post<T = unknown>(\n path: string,\n body?: unknown,\n options?: RequestInit,\n ): Promise<FetchResponse<T>>;\n}\n\n/**\n * Creates a Functions API client for interacting with serverless functions.\n *\n * This client provides methods for executing requests against serverless functions,\n * with support for middleware functions to handle authentication, error handling,\n * and other cross-cutting concerns.\n *\n * @param baseURL - Base URL for the functions endpoint\n * @param chainFunctions - Array of middleware functions for the fetch chain\n * @returns Functions client with fetch method\n */\nexport const createAPIClient = (\n baseURL: string,\n chainFunctions: ChainFunction[] = [],\n): Client => {\n let enhancedFetch = createEnhancedFetch(chainFunctions);\n\n const pushChainFunction = (chainFunction: ChainFunction) => {\n chainFunctions.push(chainFunction);\n enhancedFetch = createEnhancedFetch(chainFunctions);\n };\n\n /**\n * Executes a request to a serverless function and processes the response\n *\n * @param path - The path to the serverless function\n * @param options - Additional fetch options to apply to the request\n * @returns Promise with the function response and metadata. Body will be either\n * - JSON object if the response is application/json\n - text string if the response is text/*\n - Blob if the response is any other type\n */\n const fetch = async <T = unknown>(\n path: string,\n options?: RequestInit,\n ): Promise<FetchResponse<T | string | Blob>> => {\n const resp = await enhancedFetch(`${baseURL}${path}`, options);\n\n let body: T | string | Blob;\n // Process response based on content type\n if (resp.headers.get('content-type')?.includes('application/json')) {\n body = (await resp.json()) as T;\n } else if (resp.headers.get('content-type')?.startsWith('text/')) {\n body = await resp.text();\n } else {\n body = await resp.blob();\n }\n\n // Throw error for non-OK responses\n if (!resp.ok) {\n throw new FetchError(body, resp.status, resp.headers);\n }\n\n return {\n status: resp.status,\n body,\n headers: resp.headers,\n };\n };\n\n /**\n * Executes a POST request to a serverless function with a JSON body\n *\n * This is a convenience method assuming the request is a POST with JSON body\n * setting the `Content-Type` and 'Accept' headers to `application/json` and\n * automatically stringifying the body.\n *\n * For a more generic request, use the `fetch` method instead.\n *\n * @param path - The path to the serverless function\n * @param body - The JSON body to send in the request\n * @param options - Additional fetch options to apply to the request\n * @returns Promise with the function response and metadata\n */\n const post = async <T = unknown>(\n path: string,\n body?: unknown,\n options: RequestInit = {},\n ): Promise<FetchResponse<T | string | Blob>> => {\n // Ensure the method is POST and set the body\n const requestOptions: RequestInit = {\n ...options,\n method: 'POST',\n headers: {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n ...options.headers,\n },\n body: body ? JSON.stringify(body) : undefined,\n };\n\n return fetch<T>(path, requestOptions);\n };\n\n // Return client object with the fetch method\n return {\n baseURL,\n fetch,\n post,\n pushChainFunction,\n } as Client;\n};\n"],"mappings":"oIAyEa,CACX,EACA,EAAkC,MAElC,IAAI,EAAgB,EAAA,oBAAoB,GAExC,MAeM,EAAQ,MACZ,EACA,KAEA,MAAM,QAAa,EAAc,GAAG,IAAU,IAAQ,GAEtD,IAAI,EAWJ,GARE,EADE,EAAK,QAAQ,IAAI,iBAAiB,SAAS,0BAC/B,EAAK,OACV,EAAK,QAAQ,IAAI,iBAAiB,WAAW,eACzC,EAAK,aAEL,EAAK,QAIf,EAAK,GACR,MAAM,IAAI,EAAA,WAAW,EAAM,EAAK,OAAQ,EAAK,SAG/C,MAAO,CACL,OAAQ,EAAK,OACb,OACA,QAAS,EAAK,QAChB,EAsCF,MAAO,CACL,UACA,QACA,KAxBW,MACX,EACA,EACA,EAAuB,CAAC,IAcjB,EAAS,EAAM,IAVjB,EACH,OAAQ,OACR,QAAS,CACP,OAAQ,mBACR,eAAgB,sBACb,EAAQ,SAEb,KAAM,EAAO,KAAK,UAAU,QAAQ,IAWtC,kBAlFyB,IACzB,EAAe,KAAK,GACpB,EAAgB,EAAA,oBAAoB,EAAc,EAiFpD"}