get-cors-origin
TypeScript icon, indicating that this package has built-in type declarations

4.0.25 • Public • Published

get-cors-origin

get-cors-origin parses any value into a cors configuration if possible.

Status

Category Status
Version npm
Dependencies David
Dev dependencies David
Build GitHub Actions
License GitHub

Installation

$ npm install get-cors-origin

Quick start

First you need to add a reference to get-cors-origin to your application:

const { getCorsOrigin } = require('get-cors-origin');

If you use TypeScript, use the following code instead:

import { getCorsOrigin } from 'get-cors-origin';

Then you can call the getCorsOrigin function to parse any value into a configuration suitable for the cors module if possible:

  • If you provide a * as parameter, you just get * back.
  • If you provide one or more domains as parameters in an array, you get an array of sanitized domains back.
  • If you provide a domain as a string that contains a regular expression, the string is converted to a regular expression.
  • If you provide anything else, an error will be thrown.

Additionally, any whitespace is removed:

const corsOrigin = getCorsOrigin('*');
// => '*'

const corsOrigin = getCorsOrigin([
  'http://www.thenativeweb.io',
  'http://www.example.com'
]);
// => [
//      'http://www.thenativeweb.io'
//      'http://www.example.com'
//    ]

const corsOrigin = getCorsOrigin([
  'http://www.thenativeweb.io',
  '/\\.thenativeweb\\.io$/'
]);
// => [
//      'http://www.thenativeweb.io'
//      /\.thenativeweb\.io$/
//    ]

const corsOrigin = getCorsOrigin([
  ' http://www.thenativeweb.io   ',
  '  /\\.thenativeweb\\.io$/  '
]);
// => [
//      'http://www.thenativeweb.io'
//      /\.thenativeweb\.io$/
//    ]

const corsOrigin = getCorsOrigin(true);
// => error

const corsOrigin = getCorsOrigin('http://www.thenativeweb.io');
// => error

const corsOrigin = getCorsOrigin(123);
// => error

Running quality assurance

To run quality assurance for this module use roboter:

$ npx roboter

/get-cors-origin/

    Package Sidebar

    Install

    npm i get-cors-origin

    Weekly Downloads

    31

    Version

    4.0.25

    License

    MIT

    Unpacked Size

    19.5 kB

    Total Files

    24

    Last publish

    Collaborators

    • thenativeweb-admin
    • goloroden