Skip to content

Instantly share code, notes, and snippets.

@ahsanzizan
Created January 9, 2024 11:43
Show Gist options
  • Save ahsanzizan/8e1ab21658305a8bd432d8e9e99d4823 to your computer and use it in GitHub Desktop.
Save ahsanzizan/8e1ab21658305a8bd432d8e9e99d4823 to your computer and use it in GitHub Desktop.
Next.js responses template
import { NextResponse } from "next/server";
export function Success(response: Object) {
return NextResponse.json({ status: 200, ...response }, { status: 200 });
}
export function Created(response: Object) {
return NextResponse.json({ status: 201, ...response }, { status: 201 });
}
export function BadRequest(message: string) {
return NextResponse.json({ status: 400, message }, { status: 400 });
}
export function Unauthorized(message: string) {
return NextResponse.json({ status: 401, message }, { status: 401 });
}
export function Forbidden(message: string) {
return NextResponse.json({ status: 403, message }, { status: 403 });
}
export function NotFound(message: string) {
return NextResponse.json({ status: 404, message }, { status: 404 });
}
export function MethodNotAllowed(message: string) {
return NextResponse.json({ status: 405, message }, { status: 405 });
}
export function InternalServerError(message: string = "Internal Server Error") {
return NextResponse.json({ status: 500, message }, { status: 500 });
}
export function NotImplemented(message: string) {
return NextResponse.json({ status: 501, message }, { status: 501 });
}
export function BadGateway(message: string) {
return NextResponse.json({ status: 502, message }, { status: 502 });
}
export function ServiceUnavailable(message: string) {
return NextResponse.json({ status: 503, message }, { status: 503 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment