fal-js/libs/client/src/request.ts
Daniel Rochetti c3a3c3d21a
chore: upgrade dependencies and tooling (#81)
* chore: remove uuid-random dependency

* chore: upgrade prettier

* chore(client): bump version for release
2024-08-07 15:45:15 -07:00

60 lines
1.6 KiB
TypeScript

import { getConfig } from "./config";
import { ResponseHandler } from "./response";
import { getUserAgent, isBrowser } from "./runtime";
const isCloudflareWorkers =
typeof navigator !== "undefined" &&
navigator?.userAgent === "Cloudflare-Workers";
type RequestOptions = {
responseHandler?: ResponseHandler<any>;
};
export async function dispatchRequest<Input, Output>(
method: string,
targetUrl: string,
input: Input,
options: RequestOptions & RequestInit = {},
): Promise<Output> {
const {
credentials: credentialsValue,
requestMiddleware,
responseHandler,
fetch,
} = getConfig();
const userAgent = isBrowser() ? {} : { "User-Agent": getUserAgent() };
const credentials =
typeof credentialsValue === "function"
? credentialsValue()
: credentialsValue;
const { url, headers } = await requestMiddleware({
url: targetUrl,
});
const authHeader = credentials ? { Authorization: `Key ${credentials}` } : {};
const requestHeaders = {
...authHeader,
Accept: "application/json",
"Content-Type": "application/json",
...userAgent,
...(headers ?? {}),
} as HeadersInit;
const { responseHandler: customResponseHandler, ...requestInit } = options;
const response = await fetch(url, {
...requestInit,
method,
headers: {
...requestHeaders,
...(requestInit.headers ?? {}),
},
...(!isCloudflareWorkers && { mode: "cors" }),
body:
method.toLowerCase() !== "get" && input
? JSON.stringify(input)
: undefined,
});
const handleResponse = customResponseHandler ?? responseHandler;
return await handleResponse(response);
}