From 93737351a272a99db269e12097516c91a6ad5324 Mon Sep 17 00:00:00 2001 From: Kirk Lin Date: Sun, 21 Jul 2024 16:09:46 +0800 Subject: [PATCH] feat: server cors --- services/admin/nitro.config.ts | 5 +++++ services/admin/plugins/logger.ts | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 services/admin/plugins/logger.ts diff --git a/services/admin/nitro.config.ts b/services/admin/nitro.config.ts index c85ef49..b982670 100644 --- a/services/admin/nitro.config.ts +++ b/services/admin/nitro.config.ts @@ -2,6 +2,11 @@ import { defineNitroConfig } from "nitropack/config"; export default defineNitroConfig({ timing: true, + routeRules: { + "/**": { + cors: true, + }, + }, esbuild: { options: { target: "esnext", diff --git a/services/admin/plugins/logger.ts b/services/admin/plugins/logger.ts new file mode 100644 index 0000000..54cb3f4 --- /dev/null +++ b/services/admin/plugins/logger.ts @@ -0,0 +1,16 @@ +import { consola } from "consola"; +import { defineNitroPlugin } from "nitropack/runtime"; + +export default defineNitroPlugin((nitroApp) => { + nitroApp.hooks.hook("request", (event) => { + consola.start(`${event.method} ${event.path}`); + }); + + nitroApp.hooks.hook("error", (event, { body }) => { + consola.error(event.message, event.message, body); + }); + + nitroApp.hooks.hook("afterResponse", (event) => { + consola.success(`${event.method} ${event.path}`); + }); +});