next.js/packages/next/src/next-devtools/server/middleware-response.ts
middleware-response.ts44 lines1.1 KB
import type { ServerResponse } from 'http'
import { inspect } from 'util'

export const middlewareResponse = {
  noContent(res: ServerResponse) {
    res.statusCode = 204
    res.end('No Content')
  },
  badRequest(res: ServerResponse, reason?: string) {
    res.statusCode = 400
    if (reason !== undefined) {
      res.setHeader('Content-Type', 'text/plain')
      res.end(reason)
    } else {
      res.end()
    }
  },
  notFound(res: ServerResponse) {
    res.statusCode = 404
    res.end('Not Found')
  },
  methodNotAllowed(res: ServerResponse) {
    res.statusCode = 405
    res.end('Method Not Allowed')
  },
  internalServerError(res: ServerResponse, error?: unknown) {
    res.statusCode = 500
    res.setHeader('Content-Type', 'text/plain')
    res.end(
      error !== undefined
        ? inspect(error, { colors: false })
        : 'Internal Server Error'
    )
  },
  json(res: ServerResponse, data: any) {
    res
      .setHeader('Content-Type', 'application/json')
      .end(Buffer.from(JSON.stringify(data)))
  },
  jsonString(res: ServerResponse, data: string) {
    res.setHeader('Content-Type', 'application/json').end(Buffer.from(data))
  },
}
Quest for Codev2.0.0
/
SIGN IN