diff options
Diffstat (limited to 'source/library/fetcher/http-body.ts')
| -rw-r--r-- | source/library/fetcher/http-body.ts | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/source/library/fetcher/http-body.ts b/source/library/fetcher/http-body.ts new file mode 100644 index 0000000..b079d47 --- /dev/null +++ b/source/library/fetcher/http-body.ts @@ -0,0 +1,31 @@ + + + +/*** UTILITY ------------------------------------------ ***/ + +import type { FetcherOptions, FetcherRequest } from "./types.ts"; + +/*** EXPORT ------------------------------------------- ***/ + +export function buildHeaders(options: FetcherOptions, req: FetcherRequest): Record<string, string> { + return { + "Content-Type": "application/json", + ...options.headers, + ...req.headers + }; +} + +export async function postJson( + fetchImpl: typeof globalThis.fetch, + url: string, + headers: Record<string, string>, + body: unknown +): Promise<Record<string, unknown>> { + const response = await fetchImpl(url, { + body: JSON.stringify(body), + headers, + method: "POST" + }); + + return await response.json(); +} |