Skip to content

Commit

Permalink
refactor: move fail to outer scope, rename to failOriginParam
Browse files Browse the repository at this point in the history
  • Loading branch information
Lordfirespeed committed Jul 12, 2024
1 parent 7ca7d79 commit 78645fc
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export interface AccessControlOptions {

const isIterable = (obj: unknown): obj is Iterable<unknown> => typeof obj[Symbol.iterator] === 'function'

const getOriginHeaderHandler = (origin: unknown): ((req: Request, res: Response) => void) => {
const fail = () => {
throw new TypeError('No other objects allowed. Allowed types is array of strings or RegExp')
}
const failOriginParam = () => {
throw new TypeError('No other objects allowed. Allowed types is array of strings or RegExp')
}

const getOriginHeaderHandler = (origin: unknown): ((req: Request, res: Response) => void) => {
if (typeof origin === 'boolean') {
return origin
? (_, res) => {
Expand All @@ -40,11 +40,11 @@ const getOriginHeaderHandler = (origin: unknown): ((req: Request, res: Response)
}
}

if (typeof origin !== 'object') fail()
if (typeof origin !== 'object') failOriginParam()

if (isIterable(origin)) {
const originArray = Array.from(origin)
if (originArray.some((element) => typeof element !== 'string')) fail()
if (originArray.some((element) => typeof element !== 'string')) failOriginParam()

const originSet = new Set(origin)

Expand All @@ -71,7 +71,7 @@ const getOriginHeaderHandler = (origin: unknown): ((req: Request, res: Response)
}
}

fail()
failOriginParam()
}

/**
Expand Down

0 comments on commit 78645fc

Please sign in to comment.