apps/web: fix pass cookie to api request

This commit is contained in:
vchikalkin 2024-02-21 11:51:29 +03:00
parent 4733f2bb61
commit 4d5ab7fd36
2 changed files with 22 additions and 12 deletions

View File

@ -7,42 +7,46 @@ import wretch from 'wretch';
const urls = getUrls(); const urls = getUrls();
const api = wretch(urls.URL_IUS).options({ cache: 'no-store' }).errorType('json'); const api = wretch(urls.URL_IUS).options({ cache: 'no-store' }).errorType('json');
type Input = { pageUrlParams: PageUrlParams; payload?: unknown }; type Input = { cookie?: string; pageUrlParams: PageUrlParams; payload?: unknown };
export async function getData({ pageUrlParams }: Input) { export async function getData({ pageUrlParams, cookie = '' }: Input) {
const url = createUrl({ const url = createUrl({
...pageUrlParams, ...pageUrlParams,
route: '', route: '',
}); });
return api return api
.headers({ cookie })
.get(url) .get(url)
.res<t.ResponseGetData>((cb) => cb.json()) .res<t.ResponseGetData>((cb) => cb.json())
.then((res) => res); .then((res) => res);
} }
export async function getMetaData({ pageUrlParams }: Input) { export async function getMetaData({ pageUrlParams, cookie = '' }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/meta' }); const url = createUrl({ ...pageUrlParams, route: '/meta' });
return api return api
.headers({ cookie })
.get(url) .get(url)
.res<t.ResponseMetaData>((res) => res.json()) .res<t.ResponseMetaData>((res) => res.json())
.then((res) => res); .then((res) => res);
} }
export async function getConfig({ pageUrlParams }: Input) { export async function getConfig({ pageUrlParams, cookie = '' }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/config' }); const url = createUrl({ ...pageUrlParams, route: '/config' });
return api return api
.headers({ cookie })
.get(url) .get(url)
.res<t.ResponseConfig>((res) => res.json()) .res<t.ResponseConfig>((res) => res.json())
.then((res) => res); .then((res) => res);
} }
export async function getConditions({ pageUrlParams }: Input) { export async function getConditions({ pageUrlParams, cookie = '' }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/conditions' }); const url = createUrl({ ...pageUrlParams, route: '/conditions' });
return api return api
.headers({ cookie })
.get(url) .get(url)
.res<t.ResponseConditions>((res) => res.text()) .res<t.ResponseConditions>((res) => res.text())
.then((res) => res); .then((res) => res);
@ -68,19 +72,21 @@ export async function retract({ pageUrlParams, payload }: Input) {
.catch((error: WretchError) => error.json as t.HttpValidationError | t.HttpError); .catch((error: WretchError) => error.json as t.HttpValidationError | t.HttpError);
} }
export async function getDocumentTypes({ pageUrlParams }: Input) { export async function getDocumentTypes({ pageUrlParams, cookie = '' }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/documenttypes' }); const url = createUrl({ ...pageUrlParams, route: '/documenttypes' });
return api return api
.headers({ cookie })
.get(url) .get(url)
.res<t.ResponseDocumentTypes>((res) => res.json()) .res<t.ResponseDocumentTypes>((res) => res.json())
.then((res) => res); .then((res) => res);
} }
export async function getDocuments({ pageUrlParams }: Input) { export async function getDocuments({ pageUrlParams, cookie = '' }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/documents' }); const url = createUrl({ ...pageUrlParams, route: '/documents' });
return api return api
.headers({ cookie })
.get(url) .get(url)
.res<t.ResponseDocuments>((res) => res.json()) .res<t.ResponseDocuments>((res) => res.json())
.then((res) => res); .then((res) => res);

View File

@ -6,6 +6,7 @@ import type { PageProps } from '@/types/page';
import { withError } from '@/utils/error'; import { withError } from '@/utils/error';
import { getPageUrlParams } from '@/utils/url'; import { getPageUrlParams } from '@/utils/url';
import type { Metadata } from 'next'; import type { Metadata } from 'next';
import { headers } from 'next/headers';
export async function generateMetadata(pageProps: PageProps): Promise<Metadata> { export async function generateMetadata(pageProps: PageProps): Promise<Metadata> {
const pageUrlParams = getPageUrlParams(pageProps); const pageUrlParams = getPageUrlParams(pageProps);
@ -27,12 +28,15 @@ export default async function Page(pageProps: PageProps) {
render: async () => { render: async () => {
const pageUrlParams = getPageUrlParams(pageProps); const pageUrlParams = getPageUrlParams(pageProps);
const headersList = headers();
const cookie = headersList.get('cookie') ?? '';
return Promise.all([ return Promise.all([
apiIUS.getData({ pageUrlParams }), apiIUS.getData({ cookie, pageUrlParams }),
apiIUS.getMetaData({ pageUrlParams }), apiIUS.getMetaData({ cookie, pageUrlParams }),
apiIUS.getConfig({ pageUrlParams }), apiIUS.getConfig({ cookie, pageUrlParams }),
apiIUS.getDocumentTypes({ pageUrlParams }), apiIUS.getDocumentTypes({ cookie, pageUrlParams }),
apiIUS.getDocuments({ pageUrlParams }), apiIUS.getDocuments({ cookie, pageUrlParams }),
]).then(([data, metaData, { title }, documentTypes, documents]) => { ]).then(([data, metaData, { title }, documentTypes, documents]) => {
const combinedDocuments = combineDocuments({ documentTypes, documents }); const combinedDocuments = combineDocuments({ documentTypes, documents });
const props: FormComponentProps = { const props: FormComponentProps = {