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 => {
|
||||
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
|
||||
|
||||
+4
-3
@@ -41,9 +41,10 @@ const Index: NextPage<Props> = props => (
|
||||
)
|
||||
|
||||
export const getStaticProps: GetStaticProps<Props> = 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())
|
||||
|
||||
+7
-5
@@ -40,14 +40,16 @@ export function createApiUrl(path: string) {
|
||||
export type FactoryFunction<I, O> =
|
||||
| ((response: I) => O)
|
||||
| ((response: I) => Promise<O>)
|
||||
export type ApiDefaultResponse<O> = () => O
|
||||
|
||||
export function apiGet<I, O>(factory: FactoryFunction<I, O>) {
|
||||
return async (url: string, defaultResponse: O) => {
|
||||
const res = await requestGet<I>(url, null as any)
|
||||
if (!res) {
|
||||
return defaultResponse
|
||||
return async (url: string, getDefaultResponse: ApiDefaultResponse<O>) => {
|
||||
const res = await fetch(url)
|
||||
if (res.ok) {
|
||||
const data = await res.json() as I
|
||||
return factory(data)
|
||||
}
|
||||
|
||||
return factory(res)
|
||||
return getDefaultResponse()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user