aboutsummaryrefslogtreecommitdiff
path: root/source/library/fetcher/http-body.ts
blob: b079d471ecf920e8cb6ea397cc8d1e652f88c489 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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();
}