merge next-lib to remote/api
This commit is contained in:
1 parent
780e9ac0e8
commit
7c8fd851b1
2 files changed
+17
-41
No files matched your search
@@ -1,40 +0,0 @@
|
||||
import { NextPageContext } from "next"
|
||||
|
||||
export interface IResponseItems<T> {
|
||||
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<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, getDefaultResponse: ApiDefaultResponse<O>) => {
|
||||
const res = await fetch(url)
|
||||
if (res.ok) {
|
||||
const data = await res.json() as I
|
||||
return factory(data)
|
||||
}
|
||||
|
||||
return getDefaultResponse()
|
||||
}
|
||||
}
|
||||
+17
-1
@@ -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<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, getDefaultResponse: ApiDefaultResponse<O>) => {
|
||||
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<string[]> {
|
||||
let urls: string[] = []
|
||||
const limit = 100
|
||||
|
||||
Reference in new issue
Block a user