add nextjs and migrated api to next api

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-10-12 23:19:02 +02:00
parent 7d474e8444
commit cef7fc6965
47 changed files with 1466 additions and 1 deletions

View File

@@ -0,0 +1,55 @@
import {
MixanIssue,
MixanErrorResponse
} from '@mixan/types'
import { NextApiResponse } from 'next'
export class HttpError extends Error {
public status: number
public message: string
public issues: MixanIssue[]
constructor(status: number, message: string | Error, issues?: MixanIssue[]) {
super(message instanceof Error ? message.message : message)
this.status = status
this.message = message instanceof Error ? message.message : message
this.issues = issues || []
}
toJson(): MixanErrorResponse {
return {
code: this.status,
status: 'error',
message: this.message,
issues: this.issues.length ? this.issues : undefined,
stack: process.env.NODE_ENV !== 'production' ? this.stack : undefined,
}
}
}
export function createIssues(arr: Array<MixanIssue>) {
throw new HttpError(400, 'Issues', arr)
}
export function createError(status = 500, error: unknown | Error | string) {
if(error instanceof Error || typeof error === 'string') {
return new HttpError(status, error)
}
return new HttpError(500, 'Unexpected error occured')
}
export function handleError(res: NextApiResponse, error: Error | HttpError | unknown) {
if(error instanceof HttpError) {
return res.status(error.status).json(error.toJson())
}
if(error instanceof Error) {
const httpError = createError(500, error)
res.status(httpError.status).json(httpError.toJson())
}
const httpError = createError(500, error)
res.status(httpError.status).json(httpError.toJson())
}