|
|
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http' |
|
|
import type { I18NConfig } from '../config-shared' |
|
|
|
|
|
import { RedirectStatusCode } from '../../client/components/redirect-status-code' |
|
|
import type { NextApiRequestCookies } from '../api-utils' |
|
|
import { getCookieParser } from '../api-utils/get-cookie-parser' |
|
|
|
|
|
export interface BaseNextRequestConfig { |
|
|
basePath: string | undefined |
|
|
i18n?: I18NConfig |
|
|
trailingSlash?: boolean | undefined |
|
|
} |
|
|
|
|
|
export type FetchMetric = { |
|
|
url: string |
|
|
idx: number |
|
|
end: number |
|
|
start: number |
|
|
method: string |
|
|
status: number |
|
|
cacheReason: string |
|
|
cacheStatus: 'hit' | 'miss' | 'skip' | 'hmr' |
|
|
cacheWarning?: string |
|
|
} |
|
|
|
|
|
export type FetchMetrics = Array<FetchMetric> |
|
|
|
|
|
export abstract class BaseNextRequest<Body = any> { |
|
|
protected _cookies: NextApiRequestCookies | undefined |
|
|
public abstract headers: IncomingHttpHeaders |
|
|
public abstract fetchMetrics: FetchMetric[] | undefined |
|
|
|
|
|
constructor( |
|
|
public method: string, |
|
|
public url: string, |
|
|
public body: Body |
|
|
) {} |
|
|
|
|
|
|
|
|
|
|
|
public get cookies() { |
|
|
if (this._cookies) return this._cookies |
|
|
return (this._cookies = getCookieParser(this.headers)()) |
|
|
} |
|
|
} |
|
|
|
|
|
export abstract class BaseNextResponse<Destination = any> { |
|
|
abstract statusCode: number | undefined |
|
|
abstract statusMessage: string | undefined |
|
|
abstract get sent(): boolean |
|
|
|
|
|
constructor(public destination: Destination) {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract setHeader(name: string, value: string | string[]): this |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract removeHeader(name: string): this |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract appendHeader(name: string, value: string): this |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract getHeaderValues(name: string): string[] | undefined |
|
|
|
|
|
abstract hasHeader(name: string): boolean |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract getHeader(name: string): string | undefined |
|
|
|
|
|
abstract getHeaders(): OutgoingHttpHeaders |
|
|
|
|
|
abstract body(value: string): this |
|
|
|
|
|
abstract send(): void |
|
|
|
|
|
abstract onClose(callback: () => void): void |
|
|
|
|
|
|
|
|
|
|
|
public redirect(destination: string, statusCode: number) { |
|
|
this.setHeader('Location', destination) |
|
|
this.statusCode = statusCode |
|
|
|
|
|
|
|
|
|
|
|
if (statusCode === RedirectStatusCode.PermanentRedirect) { |
|
|
this.setHeader('Refresh', `0;url=${destination}`) |
|
|
} |
|
|
|
|
|
return this |
|
|
} |
|
|
} |
|
|
|