aboutsummaryrefslogtreecommitdiff
path: root/source/library/fetcher/apq.ts
blob: 7f9f3c155ee2d3d7ebada14367162c7babb60e7f (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*** UTILITY ------------------------------------------ ***/

import { buildHeaders, postJson } from "./http-body.ts";
import type { Fetcher, FetcherOptions } from "./types.ts";

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

export type ApqOptions = FetcherOptions & {
  disable?: boolean;
};

export function createApqFetcher(options: ApqOptions): Fetcher {
  const cache = new Map<string, string>();
  const fetchImpl = options.fetch ?? globalThis.fetch;

  return async (req) => {
    const headers = buildHeaders(options, req);
    const sha256Hash = await getHash(cache, req.query);

    const extensions = {
      persistedQuery: {
        sha256Hash,
        version: 1
      }
    };

    if (options.disable === true) {
      return await postJson(fetchImpl, options.url, headers, {
        extensions,
        operationName: req.operationName,
        query: req.query,
        variables: req.variables
      });
    }

    const firstResponse = await postJson(fetchImpl, options.url, headers, {
      extensions,
      operationName: req.operationName,
      variables: req.variables
    });

    if (!isPersistedQueryNotFound(firstResponse))
      return firstResponse;

    return await postJson(fetchImpl, options.url, headers, {
      extensions,
      operationName: req.operationName,
      query: req.query,
      variables: req.variables
    });
  };
}

/*** INTERNAL ----------------------------------------- ***/

async function getHash(cache: Map<string, string>, query: string): Promise<string> {
  const cached = cache.get(query);

  if (cached !== undefined)
    return cached;

  const hash = await sha256Hex(query);
  cache.set(query, hash);
  return hash;
}

function isPersistedQueryNotFound(response: Record<string, unknown>): boolean {
  const errors = response.errors;

  if (!Array.isArray(errors))
    return false;

  for (const entry of errors) {
    if (entry === null || typeof entry !== "object")
      continue;

    const err = entry as Record<string, unknown>;

    if (err.message === "PersistedQueryNotFound")
      return true;

    const ext = err.extensions;

    if (ext !== null && typeof ext === "object") {
      const code = (ext as Record<string, unknown>).code;

      if (code === "PERSISTED_QUERY_NOT_FOUND")
        return true;
    }
  }

  return false;
}

async function sha256Hex(input: string): Promise<string> {
  const buf = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(input));
  return Array.from(new Uint8Array(buf)).map((b) => b.toString(16).padStart(2, "0")).join("");
}