Skip to content

Commit

Permalink
feat: server cors
Browse files Browse the repository at this point in the history
  • Loading branch information
kirklin committed Jul 21, 2024
1 parent 6913c5a commit 0124258
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions services/admin/nitro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { defineNitroConfig } from "nitropack/config";

export default defineNitroConfig({
timing: true,
routeRules: {
"/**": {
cache: process.env.NODE_ENV === "production" ? { maxAge: 60 } : undefined,

Check failure on line 7 in services/admin/nitro.config.ts

View workflow job for this annotation

GitHub Actions / LINT

Unexpected use of the global variable 'process'. Use 'require("process")' instead
cors: true,
},
},
esbuild: {
options: {
target: "esnext",
Expand Down
18 changes: 18 additions & 0 deletions services/admin/plugins/_cors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { handleCors } from "h3";
import { defineNitroPlugin } from "nitropack/runtime";

export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook("request", (event) => {
handleCors(event, {
origin: origin => true,

Check failure on line 7 in services/admin/plugins/_cors.ts

View workflow job for this annotation

GitHub Actions / LINT

'origin' is defined but never used. Allowed unused args must match /^_/u
methods: ["GET", "POST", "DELETE", "OPTIONS"],
credentials: true,
});

// End the response early for OPTIONS requests
if (event.node.req.method === "OPTIONS") {
event.node.res.statusCode = 204;
event.node.res.end();
}
});
});
16 changes: 16 additions & 0 deletions services/admin/plugins/logger.ts
Original file line number Diff line number Diff line change
@@ -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}`);
});
});

0 comments on commit 0124258

Please sign in to comment.