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(
* Parse incoming message like `json` or `urlencoded`
* @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 { type, parameters } = contentType
const encoding = parameters.charset || 'utf-8'
......@@ -129,7 +132,7 @@ export async function parseBody(req: NextApiRequest, limit: string | number) {
* Parse `JSON` and handles invalid `JSON` strings
* @param str `JSON` string
*/
function parseJson(str: string) {
function parseJson(str: string): object {
if (str.length === 0) {
// special-case empty json body, as it's a common client-side mistake
return {}
......@@ -192,7 +195,10 @@ export function getCookieParser(req: IncomingMessage) {
* @param res response object
* @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
return res
}
......@@ -202,7 +208,7 @@ export function sendStatusCode(res: NextApiResponse, statusCode: number) {
* @param res response object
* @param body of response
*/
export function sendData(res: NextApiResponse, body: any) {
export function sendData(res: NextApiResponse, body: any): void {
if (body === null) {
res.end()
return
......@@ -487,7 +493,7 @@ export function sendError(
res: NextApiResponse,
statusCode: number,
message: string
) {
): void {
res.statusCode = statusCode
res.statusMessage = message
res.end(message)
......@@ -508,7 +514,7 @@ export function setLazyProp<T>(
{ req, params }: LazyProps,
prop: string,
getter: () => T
) {
): void {
const opts = { configurable: true, enumerable: true }
const optsReset = { ...opts, writable: true }
......
......@@ -11,7 +11,7 @@ const CIPHER_ALGORITHM = `aes-256-gcm`,
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 salt = crypto.randomBytes(CIPHER_SALT_LENGTH)
......@@ -42,7 +42,10 @@ export function encryptWithSecret(secret: Buffer, data: string) {
]).toString(`hex`)
}
export function decryptWithSecret(secret: Buffer, encryptedData: string) {
export function decryptWithSecret(
secret: Buffer,
encryptedData: string
): string {
const buffer = Buffer.from(encryptedData, `hex`)
const salt = buffer.slice(0, CIPHER_SALT_LENGTH)
......
......@@ -289,7 +289,7 @@ export default class Server {
return this.handleRequest.bind(this)
}
public setAssetPrefix(prefix?: string) {
public setAssetPrefix(prefix?: string): void {
this.renderOpts.assetPrefix = prefix ? prefix.replace(/\/$/, '') : ''
}
......@@ -299,7 +299,7 @@ export default class Server {
// Backwards compatibility
protected async close(): Promise<void> {}
protected setImmutableAssetCacheControl(res: ServerResponse) {
protected setImmutableAssetCacheControl(res: ServerResponse): void {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
}
......@@ -638,7 +638,7 @@ export default class Server {
}
}
private async getPagePath(pathname: string) {
private async getPagePath(pathname: string): Promise<string> {
return getPagePath(
pathname,
this.distDir,
......@@ -661,12 +661,12 @@ export default class Server {
_res: ServerResponse,
_params: Params,
_parsedUrl: UrlWithParsedQuery
) {
): Promise<boolean> {
return false
}
// 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
......@@ -679,7 +679,7 @@ export default class Server {
res: ServerResponse,
pathname: string,
query: ParsedUrlQuery
) {
): Promise<boolean> {
let page = pathname
let params: Params | boolean = false
let pageFound = await this.hasPage(page)
......@@ -776,7 +776,7 @@ export default class Server {
}))
}
private handleCompression(req: IncomingMessage, res: ServerResponse) {
private handleCompression(req: IncomingMessage, res: ServerResponse): void {
if (this.compression) {
this.compression(req, res, () => {})
}
......@@ -786,7 +786,7 @@ export default class Server {
req: IncomingMessage,
res: ServerResponse,
parsedUrl: UrlWithParsedQuery
) {
): Promise<void> {
this.handleCompression(req, res)
try {
......@@ -809,7 +809,7 @@ export default class Server {
req: IncomingMessage,
res: ServerResponse,
html: string
) {
): Promise<void> {
const { generateEtags, poweredByHeader } = this.renderOpts
return sendHTML(req, res, html, { generateEtags, poweredByHeader })
}
......@@ -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)
req.url = formatUrl({
...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