7 changed files with 61 additions and 7 deletions
@ -0,0 +1,12 @@ |
|||||
|
import { dbGlobal } from "drizzle-pkg/lib/db"; |
||||
|
import { usersTable } from "drizzle-pkg/lib/schema/schema"; |
||||
|
import { eq } from "drizzle-orm"; |
||||
|
import log4js from "logger"; |
||||
|
|
||||
|
const logger = log4js.getLogger("AUTH") |
||||
|
|
||||
|
export async function getUsers(id: number) { |
||||
|
const users = await dbGlobal.select().from(usersTable) |
||||
|
logger.info("users (formatted): %s \n", JSON.stringify(users, null, 2)); |
||||
|
return users; |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
|
||||
|
interface IConfig { |
||||
|
auth?: boolean; |
||||
|
authType?: 'token' | 'basic'; |
||||
|
} |
||||
|
|
||||
|
export const defineWrappedResponseHandler = <T extends EventHandlerRequest, D>( |
||||
|
handlerOrConfig?: EventHandler<T, D> | IConfig, |
||||
|
_handler?: EventHandler<T, D>, |
||||
|
): EventHandler<T, D> => { |
||||
|
const handler = typeof handlerOrConfig === 'function' ? handlerOrConfig : _handler; |
||||
|
if (!handler) { |
||||
|
throw new Error('handler or config is required'); |
||||
|
} |
||||
|
const config = typeof handlerOrConfig === 'object' ? handlerOrConfig : {}; |
||||
|
return defineEventHandler<T>(async (event) => { |
||||
|
try { |
||||
|
// do something before the route handler
|
||||
|
const response = await handler(event) |
||||
|
// do something after the route handler
|
||||
|
return { response } |
||||
|
} catch (err) { |
||||
|
// Error handling
|
||||
|
return { err } |
||||
|
} |
||||
|
}) |
||||
|
} |
||||
Loading…
Reference in new issue