From 794281c02cdc11245cfea3ed354f0ab079269a98 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Wed, 22 Sep 2021 14:05:45 +0300 Subject: [PATCH] apiGet update --- pages/[...slug].tsx | 5 +++-- pages/index.tsx | 7 ++++--- src/next-lib.ts | 12 +++++++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pages/[...slug].tsx b/pages/[...slug].tsx index 454df34b..cc86f01e 100644 --- a/pages/[...slug].tsx +++ b/pages/[...slug].tsx @@ -151,14 +151,15 @@ const Index: NextPage = props => { export const getStaticProps: GetStaticProps = async ctx => { const slug = paramsToSlug(ctx.params.slug) const url = `https://hudozka.tmshv.com/pages?slug=${slug}` - const page = await apiGet(createPage)(url, null) + const page = await apiGet(createPage) (url, () => null) if (!page) { return { notFound: true, } } - const menu = await apiGet(createMenu)('https://hudozka.tmshv.com/menu', []) + const menu = await apiGet(createMenu) + ('https://hudozka.tmshv.com/menu', () => []) const description = page.description ?? undefined const breadcrumbSize = page?.breadcrumb?.length ?? 0 diff --git a/pages/index.tsx b/pages/index.tsx index b67aae78..9401666b 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -41,9 +41,10 @@ const Index: NextPage = props => ( ) export const getStaticProps: GetStaticProps = async () => { - const url = 'https://hudozka.tmshv.com/home' - const items = await apiGet(createHomeCards)(url, []) - const menu = await apiGet(createMenu)('https://hudozka.tmshv.com/menu', []) + const items = await apiGet(createHomeCards) + ('https://hudozka.tmshv.com/home', async () => []) + const menu = await apiGet(createMenu) + ('https://hudozka.tmshv.com/menu', () => []) const title = 'Шлиссельбургская ДХШ' const meta = (new MetaBuilder()) diff --git a/src/next-lib.ts b/src/next-lib.ts index f2720c7f..22873813 100644 --- a/src/next-lib.ts +++ b/src/next-lib.ts @@ -40,14 +40,16 @@ export function createApiUrl(path: string) { export type FactoryFunction = | ((response: I) => O) | ((response: I) => Promise) +export type ApiDefaultResponse = () => O export function apiGet(factory: FactoryFunction) { - return async (url: string, defaultResponse: O) => { - const res = await requestGet(url, null as any) - if (!res) { - return defaultResponse + return async (url: string, getDefaultResponse: ApiDefaultResponse) => { + const res = await fetch(url) + if (res.ok) { + const data = await res.json() as I + return factory(data) } - return factory(res) + return getDefaultResponse() } }