2023-11-27 15:00:23 +03:00

96 lines
2.5 KiB
TypeScript

import type * as t from './types';
import { getUrls } from '@/config/urls';
import { createUrl, type PageUrlParams } from '@/utils/url';
import type { WretchError } from 'wretch';
import wretch from 'wretch';
const urls = getUrls();
const api = wretch(urls.URL_IUS).options({ cache: 'no-store' }).errorType('json');
type Input = { pageUrlParams: PageUrlParams; payload?: unknown };
export async function getData({ pageUrlParams }: Input) {
const url = createUrl({
...pageUrlParams,
route: '',
});
return api
.get(url)
.res<t.ResponseGetData>((cb) => cb.json())
.then((res) => res);
}
export async function getMetaData({ pageUrlParams }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/meta' });
return api
.get(url)
.res<t.ResponseMetaData>((res) => res.json())
.then((res) => res);
}
export async function getConfig({ pageUrlParams }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/config' });
return api
.get(url)
.res<t.ResponseConfig>((res) => res.json())
.then((res) => res);
}
export async function getConditions({ pageUrlParams }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/conditions' });
return api
.get(url)
.res<t.ResponseConditions>((res) => res.text())
.then((res) => res);
}
export async function save({ pageUrlParams, payload }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/transfer' });
return api
.post(payload, url)
.res<boolean>((res) => res.ok)
.then((res) => res)
.catch((error: WretchError) => error.json as t.HttpValidationError);
}
export async function retract({ pageUrlParams, payload }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/return' });
return api
.post(payload, url)
.res<boolean>((res) => res.ok)
.then((res) => res)
.catch((error: WretchError) => error.json as t.HttpValidationError);
}
export async function getDocumentTypes({ pageUrlParams }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/documenttypes' });
return api
.get(url)
.res<t.ResponseDocumentTypes>((res) => res.json())
.then((res) => res);
}
export async function uploadDocument({
pageUrlParams,
document,
formData,
}: Input & { document: Pick<t.Document, 'documentTypeId'>; formData: FormData }) {
const url = createUrl({
...pageUrlParams,
route: '/document',
urlSearchParams: { ...pageUrlParams.urlSearchParams, ...document },
});
return fetch(urls.URL_IUS + url, {
body: formData,
method: 'POST',
});
}