Unverified Commit c01a93ad authored by Maxi Gimenez's avatar Maxi Gimenez Committed by GitHub

chore(next-server): api-utils,crypto-utils,next-server missing types (#13431)

parent aef7f270
...@@ -96,7 +96,10 @@ export async function apiResolver( ...@@ -96,7 +96,10 @@ export async function apiResolver(
* Parse incoming message like `json` or `urlencoded` * Parse incoming message like `json` or `urlencoded`
* @param req request object * @param req request object
*/ */
export async function parseBody(req: NextApiRequest, limit: string | number) { export async function parseBody(
req: NextApiRequest,
limit: string | number
): Promise<any> {
const contentType = parse(req.headers['content-type'] || 'text/plain') const contentType = parse(req.headers['content-type'] || 'text/plain')
const { type, parameters } = contentType const { type, parameters } = contentType
const encoding = parameters.charset || 'utf-8' const encoding = parameters.charset || 'utf-8'
...@@ -129,7 +132,7 @@ export async function parseBody(req: NextApiRequest, limit: string | number) { ...@@ -129,7 +132,7 @@ export async function parseBody(req: NextApiRequest, limit: string | number) {
* Parse `JSON` and handles invalid `JSON` strings * Parse `JSON` and handles invalid `JSON` strings
* @param str `JSON` string * @param str `JSON` string
*/ */
function parseJson(str: string) { function parseJson(str: string): object {
if (str.length === 0) { if (str.length === 0) {
// special-case empty json body, as it's a common client-side mistake // special-case empty json body, as it's a common client-side mistake
return {} return {}
...@@ -192,7 +195,10 @@ export function getCookieParser(req: IncomingMessage) { ...@@ -192,7 +195,10 @@ export function getCookieParser(req: IncomingMessage) {
* @param res response object * @param res response object
* @param statusCode `HTTP` status code of response * @param statusCode `HTTP` status code of response
*/ */
export function sendStatusCode(res: NextApiResponse, statusCode: number) { export function sendStatusCode(
res: NextApiResponse,
statusCode: number
): NextApiResponse<any> {
res.statusCode = statusCode res.statusCode = statusCode
return res return res
} }
...@@ -202,7 +208,7 @@ export function sendStatusCode(res: NextApiResponse, statusCode: number) { ...@@ -202,7 +208,7 @@ export function sendStatusCode(res: NextApiResponse, statusCode: number) {
* @param res response object * @param res response object
* @param body of response * @param body of response
*/ */
export function sendData(res: NextApiResponse, body: any) { export function sendData(res: NextApiResponse, body: any): void {
if (body === null) { if (body === null) {
res.end() res.end()
return return
...@@ -487,7 +493,7 @@ export function sendError( ...@@ -487,7 +493,7 @@ export function sendError(
res: NextApiResponse, res: NextApiResponse,
statusCode: number, statusCode: number,
message: string message: string
) { ): void {
res.statusCode = statusCode res.statusCode = statusCode
res.statusMessage = message res.statusMessage = message
res.end(message) res.end(message)
...@@ -508,7 +514,7 @@ export function setLazyProp<T>( ...@@ -508,7 +514,7 @@ export function setLazyProp<T>(
{ req, params }: LazyProps, { req, params }: LazyProps,
prop: string, prop: string,
getter: () => T getter: () => T
) { ): void {
const opts = { configurable: true, enumerable: true } const opts = { configurable: true, enumerable: true }
const optsReset = { ...opts, writable: true } const optsReset = { ...opts, writable: true }
......
...@@ -11,7 +11,7 @@ const CIPHER_ALGORITHM = `aes-256-gcm`, ...@@ -11,7 +11,7 @@ const CIPHER_ALGORITHM = `aes-256-gcm`,
const PBKDF2_ITERATIONS = 100_000 // https://support.1password.com/pbkdf2/ const PBKDF2_ITERATIONS = 100_000 // https://support.1password.com/pbkdf2/
export function encryptWithSecret(secret: Buffer, data: string) { export function encryptWithSecret(secret: Buffer, data: string): string {
const iv = crypto.randomBytes(CIPHER_IV_LENGTH) const iv = crypto.randomBytes(CIPHER_IV_LENGTH)
const salt = crypto.randomBytes(CIPHER_SALT_LENGTH) const salt = crypto.randomBytes(CIPHER_SALT_LENGTH)
...@@ -42,7 +42,10 @@ export function encryptWithSecret(secret: Buffer, data: string) { ...@@ -42,7 +42,10 @@ export function encryptWithSecret(secret: Buffer, data: string) {
]).toString(`hex`) ]).toString(`hex`)
} }
export function decryptWithSecret(secret: Buffer, encryptedData: string) { export function decryptWithSecret(
secret: Buffer,
encryptedData: string
): string {
const buffer = Buffer.from(encryptedData, `hex`) const buffer = Buffer.from(encryptedData, `hex`)
const salt = buffer.slice(0, CIPHER_SALT_LENGTH) const salt = buffer.slice(0, CIPHER_SALT_LENGTH)
......
...@@ -289,7 +289,7 @@ export default class Server { ...@@ -289,7 +289,7 @@ export default class Server {
return this.handleRequest.bind(this) return this.handleRequest.bind(this)
} }
public setAssetPrefix(prefix?: string) { public setAssetPrefix(prefix?: string): void {
this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : '' this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : ''
} }
...@@ -299,7 +299,7 @@ export default class Server { ...@@ -299,7 +299,7 @@ export default class Server {
// Backwards compatibility // Backwards compatibility
protected async close(): Promise<void> {} protected async close(): Promise<void> {}
protected setImmutableAssetCacheControl(res: ServerResponse) { protected setImmutableAssetCacheControl(res: ServerResponse): void {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable') res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
} }
...@@ -638,7 +638,7 @@ export default class Server { ...@@ -638,7 +638,7 @@ export default class Server {
} }
} }
private async getPagePath(pathname: string) { private async getPagePath(pathname: string): Promise<string> {
return getPagePath( return getPagePath(
pathname, pathname,
this.distDir, this.distDir,
...@@ -661,12 +661,12 @@ export default class Server { ...@@ -661,12 +661,12 @@ export default class Server {
_res: ServerResponse, _res: ServerResponse,
_params: Params, _params: Params,
_parsedUrl: UrlWithParsedQuery _parsedUrl: UrlWithParsedQuery
) { ): Promise<boolean> {
return false return false
} }
// Used to build API page in development // Used to build API page in development
protected async ensureApiPage(pathname: string) {} protected async ensureApiPage(pathname: string): Promise<void> {}
/** /**
* Resolves `API` request, in development builds on demand * Resolves `API` request, in development builds on demand
...@@ -679,7 +679,7 @@ export default class Server { ...@@ -679,7 +679,7 @@ export default class Server {
res: ServerResponse, res: ServerResponse,
pathname: string, pathname: string,
query: ParsedUrlQuery query: ParsedUrlQuery
) { ): Promise<boolean> {
let page = pathname let page = pathname
let params: Params | boolean = false let params: Params | boolean = false
let pageFound = await this.hasPage(page) let pageFound = await this.hasPage(page)
...@@ -776,7 +776,7 @@ export default class Server { ...@@ -776,7 +776,7 @@ export default class Server {
})) }))
} }
private handleCompression(req: IncomingMessage, res: ServerResponse) { private handleCompression(req: IncomingMessage, res: ServerResponse): void {
if (this.compression) { if (this.compression) {
this.compression(req, res, () => {}) this.compression(req, res, () => {})
} }
...@@ -786,7 +786,7 @@ export default class Server { ...@@ -786,7 +786,7 @@ export default class Server {
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
parsedUrl: UrlWithParsedQuery parsedUrl: UrlWithParsedQuery
) { ): Promise<void> {
this.handleCompression(req, res) this.handleCompression(req, res)
try { try {
...@@ -809,7 +809,7 @@ export default class Server { ...@@ -809,7 +809,7 @@ export default class Server {
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
html: string html: string
) { ): Promise<void> {
const { generateEtags, poweredByHeader } = this.renderOpts const { generateEtags, poweredByHeader } = this.renderOpts
return sendHTML(req, res, html, { generateEtags, poweredByHeader }) return sendHTML(req, res, html, { generateEtags, poweredByHeader })
} }
...@@ -1436,7 +1436,10 @@ export default class Server { ...@@ -1436,7 +1436,10 @@ export default class Server {
} }
} }
function prepareServerlessUrl(req: IncomingMessage, query: ParsedUrlQuery) { function prepareServerlessUrl(
req: IncomingMessage,
query: ParsedUrlQuery
): void {
const curUrl = parseUrl(req.url!, true) const curUrl = parseUrl(req.url!, true)
req.url = formatUrl({ req.url = formatUrl({
...curUrl, ...curUrl,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment