aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/api.ts
blob: e587ccc735ef297fb95ad449b61bd1676f491b68 (plain)
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
import {
  Mutations,
  MutationsInputMap,
  MutationsResponseMap,
} from '@ts/types/graphql/mutations';
import {
  Queries,
  QueriesInputMap,
  QueriesResponseMap,
} from '@ts/types/graphql/queries';
import { settings } from '@utils/config';

/**
 * Retrieve the API url from settings.
 *
 * @returns {string} The API url.
 */
export const getAPIUrl = (): string => {
  const { url } = settings.api;

  if (!url) {
    throw new Error('API url is not defined.');
  }

  return url;
};

export type ResponseMap<T, K extends Mutations | Queries> = K extends Mutations
  ? MutationsResponseMap<T>
  : QueriesResponseMap<T>;

export type InputMap<T extends Mutations | Queries> = T extends Mutations
  ? MutationsInputMap
  : QueriesInputMap;

type FetchAPIVariables<T> = T extends Queries
  ? QueriesInputMap[T]
  : T extends Mutations
  ? MutationsInputMap[T]
  : never;

type FetchAPIProps<Q extends Queries | Mutations, V = FetchAPIVariables<Q>> = {
  query: Q;
  variables?: V;
};

type FetchAPIResponse<T, K extends Queries | Mutations> = K extends Queries
  ? QueriesResponseMap<T>[K]
  : K extends Mutations
  ? MutationsResponseMap<T>[K]
  : never;

export const fetchAPI = async <T, K extends Queries | Mutations>({
  query,
  variables,
}: FetchAPIProps<K>): Promise<FetchAPIResponse<T, K>> => {
  const response = await fetch(getAPIUrl(), {
    method: 'POST',
    headers: {
      'content-type': 'application/json;charset=UTF-8',
    },
    body: JSON.stringify({
      query,
      variables,
    }),
  });

  type JSONResponse = {
    data?: FetchAPIResponse<T, K>;
    errors?: Array<{ message: string }>;
  };

  const { data, errors }: JSONResponse = await response.json();

  if (response.ok) {
    if (!data) return Promise.reject(new Error(`No data found"`));

    return data;
  } else {
    console.error('Failed to fetch API');
    const error = new Error(
      errors?.map((e) => e.message).join('\n') ?? 'unknown'
    );
    return Promise.reject(error);
  }
};