2023-11-22 00:37:49 +03:00

61 lines
1.6 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_UIS).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 validate({ pageUrlParams, payload }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/validate' });
return api
.post(payload, url)
.res<boolean>((res) => res.ok)
.then((res) => res)
.catch((error: WretchError) => error.json as t.HttpValidationError);
}