Passed
Push — feature/improve-applicant-list ( 3fd796...cbd310 )
by Chris
04:06
created

resources/assets/js/api/base.ts   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 52
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 39
mnd 2
bc 2
fnc 0
dl 0
loc 52
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import dayjs from "dayjs";
2
import rootAxios from "axios";
3
import { baseApiUrl } from "../helpers/routes";
4
5
export interface ResponseData {
6
  [key: string]: string & ResponseData & [ResponseData];
7
}
8
export interface ApiResponse {
9
  data: ResponseData;
10
}
11
12
export const baseUrl = baseApiUrl;
13
14
export const parseDateStrict = (date: string): Date => dayjs(date).toDate();
15
16
export const parseDate = (date: string | null): Date | null => {
17
  return date !== null ? parseDateStrict(date) : null;
18
};
19
20
export const addQueryParameters = (
21
  url: string,
22
  parameters: Map<string, string>,
23
): string => {
24
  const toKeyValuePairs = ([key, value]: string[]): string =>
25
    `${encodeURI(key)}=${encodeURI(value)}`;
26
  const parameterString = Array.from(parameters.entries())
27
    .map(toKeyValuePairs)
28
    .join("&");
29
  return `${url}?${parameterString}`;
30
};
31
32
/**
33
 * Get the page's CSRF token, which laravel uses to validate requests.
34
 */
35
// TODO: is this the best way to get this?
36
const csrfElement = document.head.querySelector('meta[name="csrf-token"]');
37
const csrfToken: string =
38
  csrfElement && csrfElement.getAttribute("content")
39
    ? (csrfElement.getAttribute("content") as string)
40
    : "";
41
const axiosConfig = {
42
  headers: {
43
    "X-Requested-With": "XMLHttpRequest",
44
    "X-CSRF-TOKEN": csrfToken,
45
  },
46
};
47
export const axios = rootAxios.create(axiosConfig);
48
49
export default {
50
  baseUrl,
51
};
52