aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/pages.ts
blob: 0781d44e4967be5fb775e32270bea171ab15e04d (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
import {
  FetchPageByUriReturn,
  GetPageReturn,
  Page,
  PageResponse,
  RawPage,
} from '@ts/types/pages';
import { gql } from 'graphql-request';
import { getGraphQLClient } from './client';

const fetchPageByUri: FetchPageByUriReturn = async (uri: string) => {
  const client = getGraphQLClient();
  const query = gql`
    query PageByUri($uri: String!) {
      pageBy(uri: $uri) {
        contentParts {
          afterMore
          beforeMore
        }
        date
        modified
        title
      }
    }
  `;

  const variables = { uri };

  try {
    const response: PageResponse = await client.request(query, variables);
    return response.pageBy;
  } catch (error) {
    console.error(JSON.stringify(error, undefined, 2));
    process.exit(1);
  }
};

const getFormattedPage = (page: RawPage) => {
  const formattedPage: Page = {
    ...page,
    content: page.contentParts.afterMore,
    intro: page.contentParts.beforeMore,
  };

  return formattedPage;
};

export const getCVPage: GetPageReturn = async () => {
  const rawCV = await fetchPageByUri('/cv/');
  const formattedCV = getFormattedPage(rawCV);

  return formattedCV;
};

export const getLegalNoticePage: GetPageReturn = async () => {
  const rawLegalNotice = await fetchPageByUri('/mentions-legales');
  const formattedLegalNotice = getFormattedPage(rawLegalNotice);

  return formattedLegalNotice;
};