untested WIP
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { ZodError } from "zod";
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
export const apiResponse = {
|
||||
successful: (message: string, data?: any) => ({
|
||||
status: 200,
|
||||
message,
|
||||
data,
|
||||
successful: true,
|
||||
meta: {
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}),
|
||||
created: (message: string, data?: any) => ({
|
||||
status: 201,
|
||||
message,
|
||||
data,
|
||||
successful: true,
|
||||
meta: {
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}),
|
||||
error: (err: Error) => ({
|
||||
// @ts-ignore
|
||||
status: err["status"] ?? 400,
|
||||
message: err.message,
|
||||
error: err.name,
|
||||
successful: false,
|
||||
meta: {
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}),
|
||||
internalError: () => ({
|
||||
status: 500,
|
||||
message: "An Internal Server Error has occured...",
|
||||
error: "InternalServerError",
|
||||
successful: false,
|
||||
meta: {
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
}),
|
||||
zodError: (err: ZodError) => {
|
||||
const data = JSON.parse(err.message);
|
||||
return {
|
||||
status: 400,
|
||||
message: "TypeError",
|
||||
error: data,
|
||||
successful: false,
|
||||
meta: {
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
import { Handler, Hono, MiddlewareHandler } from "hono";
|
||||
import { Variables } from "../../types/HonoTypes";
|
||||
|
||||
/**
|
||||
* Create a route.
|
||||
*
|
||||
* This method exists to serve the purpose of allowing us to split all of our api routes into different files and
|
||||
* easily and quickly be able to rope them back into the main api server instance.
|
||||
*
|
||||
* One of the sortfallings of this method is that I was not able to figure out how to integrate the middleware to come
|
||||
* before the handler, so if somebody feels upto it please figure out a way to have the middleware come before the handler
|
||||
* method naturally as you would if you using a plain hono method.
|
||||
*
|
||||
* @TODO Move middleware handlers to come before primary handler naturally.
|
||||
*
|
||||
* @param method - HTTP Method
|
||||
* @param path - URL Path
|
||||
* @param handler - Handler function for Hono
|
||||
* @param middleware - Array of Middleware Handlers for Hono
|
||||
* @returns {Hono} - A new Hono instance containing the newly created route.
|
||||
*/
|
||||
export function createRoute(
|
||||
method: string | string[],
|
||||
path: string[],
|
||||
handler: Handler<{
|
||||
Variables: Variables;
|
||||
}>,
|
||||
...middleware: MiddlewareHandler<{
|
||||
Variables: Variables;
|
||||
}>[]
|
||||
): Hono<{ Variables: Variables }> {
|
||||
if (middleware)
|
||||
return new Hono<{ Variables: Variables }>().on(
|
||||
method as any,
|
||||
path,
|
||||
...middleware,
|
||||
handler
|
||||
);
|
||||
return new Hono<{ Variables: Variables }>().on(method as any, path, handler);
|
||||
}
|
||||
Reference in New Issue
Block a user