2023-11-22 11:30:08 +03:00

71 lines
1.9 KiB
TypeScript

'use server';
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).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);
}