getAllowedHosts.ts 589 B

12345678910111213141516171819202122
  1. export default function getAllowedHosts(
  2. hosts: string | RegExp | Array<string | RegExp>,
  3. url: string,
  4. ): string | RegExp | Array<string | RegExp> {
  5. if (hosts) {
  6. if (
  7. typeof hosts !== 'string' &&
  8. !Array.isArray(hosts) &&
  9. !(hosts instanceof RegExp)
  10. ) {
  11. throw new TypeError(
  12. `The option "companionAllowedHosts" must be one of string, Array, RegExp`,
  13. )
  14. }
  15. return hosts
  16. }
  17. // does not start with https://
  18. if (/^(?!https?:\/\/).*$/i.test(url)) {
  19. return `https://${url.replace(/^\/\//, '')}`
  20. }
  21. return new URL(url).origin
  22. }