apiGet update
This commit is contained in:
1 parent
42fdf429e2
commit
794281c02c
3 files changed
+14
-10
No files matched your search
+3
-2
@@ -151,14 +151,15 @@ const Index: NextPage<Props> = props => {
|
|||||||
export const getStaticProps: GetStaticProps<Props> = async ctx => {
|
export const getStaticProps: GetStaticProps<Props> = async ctx => {
|
||||||
const slug = paramsToSlug(ctx.params.slug)
|
const slug = paramsToSlug(ctx.params.slug)
|
||||||
const url = `https://hudozka.tmshv.com/pages?slug=${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) {
|
if (!page) {
|
||||||
return {
|
return {
|
||||||
notFound: true,
|
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 description = page.description ?? undefined
|
||||||
const breadcrumbSize = page?.breadcrumb?.length ?? 0
|
const breadcrumbSize = page?.breadcrumb?.length ?? 0
|
||||||
|
|||||||
+4
-3
@@ -41,9 +41,10 @@ const Index: NextPage<Props> = props => (
|
|||||||
)
|
)
|
||||||
|
|
||||||
export const getStaticProps: GetStaticProps<Props> = async () => {
|
export const getStaticProps: GetStaticProps<Props> = async () => {
|
||||||
const url = 'https://hudozka.tmshv.com/home'
|
const items = await apiGet(createHomeCards)
|
||||||
const items = await apiGet(createHomeCards)(url, [])
|
('https://hudozka.tmshv.com/home', async () => [])
|
||||||
const menu = await apiGet(createMenu)('https://hudozka.tmshv.com/menu', [])
|
const menu = await apiGet(createMenu)
|
||||||
|
('https://hudozka.tmshv.com/menu', () => [])
|
||||||
|
|
||||||
const title = 'Шлиссельбургская ДХШ'
|
const title = 'Шлиссельбургская ДХШ'
|
||||||
const meta = (new MetaBuilder())
|
const meta = (new MetaBuilder())
|
||||||
|
|||||||
+7
-5
@@ -40,14 +40,16 @@ export function createApiUrl(path: string) {
|
|||||||
export type FactoryFunction<I, O> =
|
export type FactoryFunction<I, O> =
|
||||||
| ((response: I) => O)
|
| ((response: I) => O)
|
||||||
| ((response: I) => Promise<O>)
|
| ((response: I) => Promise<O>)
|
||||||
|
export type ApiDefaultResponse<O> = () => O
|
||||||
|
|
||||||
export function apiGet<I, O>(factory: FactoryFunction<I, O>) {
|
export function apiGet<I, O>(factory: FactoryFunction<I, O>) {
|
||||||
return async (url: string, defaultResponse: O) => {
|
return async (url: string, getDefaultResponse: ApiDefaultResponse<O>) => {
|
||||||
const res = await requestGet<I>(url, null as any)
|
const res = await fetch(url)
|
||||||
if (!res) {
|
if (res.ok) {
|
||||||
return defaultResponse
|
const data = await res.json() as I
|
||||||
|
return factory(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return factory(res)
|
return getDefaultResponse()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user