1.0.2 • Published 1 year ago

@zoeldevapps/fastify-cors-envregex v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

fastify-cors-envregex

Helper utility to parse and expand regex usually passed in as environment variables to services into a regex format supported by fastify.

const CORS_ORIGINS =
  "http://localhost:*,https://localhost:*,https://*.adahide.io";

// would be converted into
const fce = require("@zoeldevapps/fastify-cors-envregex");

fce.expandAllowedOrigins(
  "http://localhost:*,https://localhost:*,https://*.adahide.io"
);
/*
{
  origin: [
    /^http:\/\/localhost:.*$/,
    /^https:\/\/localhost:.*$/,
    /^https:\/\/.*\.adahide.io$/
  ],
  methods: [ 'GET', 'PUT', 'POST', 'OPTIONS' ],
  credentials: true
}
*/

Usage:

server.register(
  cors,
  getCorsOptions(CORS_ORIGINS, process.env.NODE_ENV !== "production")
);