-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormsService.ts
72 lines (63 loc) · 2.6 KB
/
FormsService.ts
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
import { Form } from '@navikt/skjemadigitalisering-shared-domain';
import { fetchWithErrorHandling } from '../../fetchUtils';
import { logger } from '../../logging/logger';
import { createHeaders, removeInnsendingFromForm } from '../utils/formsApiUtils';
import { FormPostBody, FormPutBody, FormsService } from './types';
const createFormsService = (formsApiUrl: string): FormsService => {
const formsUrl = `${formsApiUrl}/v1/forms`;
const getAll = async (select?: string): Promise<Form[]> => {
const search = select ? new URLSearchParams({ select }) : '';
const url = `${formsUrl}?${search}`;
const response = await fetchWithErrorHandling(url, { headers: createHeaders() });
return (response.data as Form[]).map((f) => removeInnsendingFromForm(f));
};
const get = async (formPath: string): Promise<Form> => {
const response = await fetchWithErrorHandling(`${formsUrl}/${formPath}`, { headers: createHeaders() });
return removeInnsendingFromForm(response.data as Form) as Form;
};
const post = async (body: FormPostBody, accessToken: string): Promise<Form> => {
logger.info(`Create new form ${body.skjemanummer} ${body.title}`);
const response = await fetchWithErrorHandling(formsUrl, {
method: 'POST',
headers: createHeaders(accessToken),
body: JSON.stringify(body),
});
return response.data as Form;
};
const put = async (formPath: string, body: FormPutBody, revision: number, accessToken: string): Promise<Form> => {
logger.info(`Update form ${formPath} (revision ${revision})`);
const response = await fetchWithErrorHandling(`${formsUrl}/${formPath}`, {
method: 'PUT',
headers: createHeaders(accessToken, revision),
body: JSON.stringify(body),
});
return response.data as Form;
};
const postLockForm = async (formPath: string, reason: string, accessToken: string): Promise<Form> => {
logger.info(`Lock form ${formPath}`);
const response = await fetchWithErrorHandling(`${formsUrl}/${formPath}/lock`, {
method: 'POST',
headers: createHeaders(accessToken),
body: JSON.stringify({ reason }),
});
return response.data as Form;
};
const deleteLockForm = async (formPath: string, accessToken: string): Promise<Form> => {
logger.info(`Unlock form ${formPath}`);
const response = await fetchWithErrorHandling(`${formsUrl}/${formPath}/lock`, {
method: 'DELETE',
headers: createHeaders(accessToken),
});
return response.data as Form;
};
return {
getAll,
get,
post,
put,
postLockForm,
deleteLockForm,
formsUrl,
};
};
export default createFormsService;