aboutsummaryrefslogtreecommitdiff
path: root/source/library/fetcher/http.ts
blob: 31382268fccdebf4b73936fa9bd261b6716fcb71 (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
/*** UTILITY ------------------------------------------ ***/

import type { Fetcher, FetcherOptions } from "./types.ts";

/*** EXPORT ------------------------------------------- ***/

export function createHttpFetcher(options: FetcherOptions): Fetcher {
  const fetchImpl = options.fetch ?? globalThis.fetch;

  return async (req) => {
    const response = await fetchImpl(options.url, {
      body: JSON.stringify({
        operationName: req.operationName,
        query: req.query,
        variables: req.variables
      }),
      headers: {
        "Content-Type": "application/json",
        ...options.headers,
        ...req.headers
      },
      method: "POST"
    });

    return await response.json();
  };
}