6bc7c4d9c2
Related to #34185, this PR reduces the size of chunk that contains web-server.ts from 1.14mb to 210.8kb, by splitting base-http and api-utils into different environments. Only affected thing is we can't have SSG preview mode for the web runtime via `getStaticProps`. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import type { IncomingHttpHeaders } from 'http'
|
|
import type { I18NConfig } from '../config-shared'
|
|
|
|
import { PERMANENT_REDIRECT_STATUS } from '../../shared/lib/constants'
|
|
import { getCookieParser, NextApiRequestCookies } from '../api-utils'
|
|
|
|
export interface BaseNextRequestConfig {
|
|
basePath: string | undefined
|
|
i18n?: I18NConfig
|
|
trailingSlash?: boolean | undefined
|
|
}
|
|
|
|
export abstract class BaseNextRequest<Body = any> {
|
|
protected _cookies: NextApiRequestCookies | undefined
|
|
public abstract headers: IncomingHttpHeaders
|
|
|
|
constructor(public method: string, public url: string, public body: Body) {}
|
|
|
|
abstract parseBody(limit: string | number): Promise<any>
|
|
|
|
// Utils implemented using the abstract methods above
|
|
|
|
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) {}
|
|
|
|
/**
|
|
* Sets a value for the header overwriting existing values
|
|
*/
|
|
abstract setHeader(name: string, value: string | string[]): this
|
|
|
|
/**
|
|
* Appends value for the given header name
|
|
*/
|
|
abstract appendHeader(name: string, value: string): this
|
|
|
|
/**
|
|
* Get all vaues for a header as an array or undefined if no value is present
|
|
*/
|
|
abstract getHeaderValues(name: string): string[] | undefined
|
|
|
|
abstract hasHeader(name: string): boolean
|
|
|
|
/**
|
|
* Get vaues for a header concatenated using `,` or undefined if no value is present
|
|
*/
|
|
abstract getHeader(name: string): string | undefined
|
|
|
|
abstract body(value: string): this
|
|
|
|
abstract send(): void
|
|
|
|
// Utils implemented using the abstract methods above
|
|
|
|
redirect(destination: string, statusCode: number) {
|
|
this.setHeader('Location', destination)
|
|
this.statusCode = statusCode
|
|
|
|
// Since IE11 doesn't support the 308 header add backwards
|
|
// compatibility using refresh header
|
|
if (statusCode === PERMANENT_REDIRECT_STATUS) {
|
|
this.setHeader('Refresh', `0;url=${destination}`)
|
|
}
|
|
return this
|
|
}
|
|
}
|