Skip to content

Commit

Permalink
update typing
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 23, 2022
1 parent 466ac2c commit dd2dd7b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 44 deletions.
29 changes: 16 additions & 13 deletions packages/next/server/app-render.tsx
Expand Up @@ -195,6 +195,14 @@ function createErrorHandler(
}
}

class NextFetchRequestImpl extends globalThis.Request {
next?: NextFetchRequestConfig | undefined
constructor(input: RequestInfo | URL, init?: RequestInit | undefined) {
super(input, init)
this.next = init?.next
}
}

let isFetchPatched = false

// we patch fetch to collect cache information used for
Expand All @@ -208,19 +216,14 @@ function patchFetch(ComponentMod: any) {

const staticGenerationAsyncStorage = ComponentMod.staticGenerationAsyncStorage

const originFetch = global.fetch
const OriginalRequest = global.Request
global.Request = class NextFetchRequestImpl extends OriginalRequest {
next?: NextFetchRequestConfig | undefined
constructor(
input: RequestInfo | URL,
init?: NextFetchRequestOptions | undefined
) {
super(input, init)
this.next = init?.next
}
}
global.fetch = async (input, init) => {
Object.defineProperty(globalThis, 'Request', {
get() {
return NextFetchRequestImpl
},
})

const originFetch = globalThis.fetch
globalThis.fetch = async (input, init) => {
const staticGenerationStore =
'getStore' in staticGenerationAsyncStorage
? staticGenerationAsyncStorage.getStore()
Expand Down
39 changes: 8 additions & 31 deletions packages/next/types/global.d.ts
@@ -1,18 +1,6 @@
/// <reference types="node" />

// Extend the NodeJS namespace with Next.js-defined properties

type NextFetchRequestConfig = {
revalidate?: number
}

type NextFetchRequestOptions = RequestInit & {
next?: NextFetchRequestConfig | undefined
}

interface RequestInit {
next?: NextFetchRequestConfig | undefined
}
declare namespace NodeJS {
interface Process {
/**
Expand All @@ -25,28 +13,13 @@ declare namespace NodeJS {
readonly NODE_ENV: 'development' | 'production' | 'test'
}

// Typing `global.fetch` for overriding in app-render
interface Global {
fetch(
url: RequestInfo,
init: NextFetchRequestOptions | undefined
): Promise<Response>
Request: {
prototype: NextFetchRequest
new (
input: RequestInfo | URL,
init?: NextFetchRequestOptions
): NextFetchRequest
}
interface RequestInit extends globalThis.RequestInit {
next?: NextFetchRequestConfig | undefined
}
}

declare function fetch(
url: RequestInfo,
init: NextFetchRequestOptions | undefined
): Promise<Response>
interface NextFetchRequest extends Request {
readonly next?: NextFetchRequestConfig | undefined
interface NextFetchRequestConfig {
revalidate?: number
}

declare module '*.module.css' {
Expand All @@ -68,3 +41,7 @@ interface Window {
MSInputMethodContext?: unknown
__NEXT_HMR_CB?: null | ((message?: string) => void)
}

interface RequestInit {
next?: { revalidate?: number } | undefined
}

0 comments on commit dd2dd7b

Please sign in to comment.