diff --git a/src/next-lib.ts b/src/next-lib.ts deleted file mode 100644 index 0856a13b..00000000 --- a/src/next-lib.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { NextPageContext } from "next" - -export interface IResponseItems { - items: T[] - nextPage: number - prevPage: number -} - -/** - * This wrapper prevents fetching api request in static export mode and production environment - * because nextjs call getInitialProps on the client side - * - * @param {*} fn - */ -export function wrapInitialProps(fn: (ctx: NextPageContext) => void) { - return async (ctx: NextPageContext) => { - if (process.env.NODE_ENV === "production" && process.browser) { - return window["__NEXT_DATA__"].props.pageProps - } - - return fn(ctx) - } -} - -export type FactoryFunction = - | ((response: I) => O) - | ((response: I) => Promise) -export type ApiDefaultResponse = () => O - -export function apiGet(factory: FactoryFunction) { - 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 getDefaultResponse() - } -} diff --git a/src/remote/api.ts b/src/remote/api.ts index 3b1f7002..27e31abc 100644 --- a/src/remote/api.ts +++ b/src/remote/api.ts @@ -1,9 +1,25 @@ -import { apiGet } from "@/next-lib" import { createHomeCards, createMenu, createPage, createPageUrls } from "@/remote/factory" import { IMenu, IPage, PageCardDto } from "@/types" const backendUrl = "https://hudozka.tmshv.com" +export type FactoryFunction = + | ((response: I) => O) + | ((response: I) => Promise) +export type ApiDefaultResponse = () => O + +export function apiGet(factory: FactoryFunction) { + 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 getDefaultResponse() + } +} + export async function getUrls(): Promise { let urls: string[] = [] const limit = 100