(passedOptions: Partial<Options>)
| 152 | * @returns {Configuration} - A complete configuration object. |
| 153 | */ |
| 154 | const parseOptions = (passedOptions: Partial<Options>): Configuration => { |
| 155 | // Passing undefined should be equivalent to not passing an option at all, so we'll |
| 156 | // omit all fields where their value is undefined. |
| 157 | const notUndefinedOptions: Partial<Options> = |
| 158 | omitUndefinedProperties<Partial<Options>>(passedOptions) |
| 159 | |
| 160 | const logger = passedOptions.logger ?? ConsoleLogger |
| 161 | |
| 162 | // Create the validator before even parsing the rest of the options. |
| 163 | const validations = getValidations( |
| 164 | notUndefinedOptions?.validate ?? true, |
| 165 | logger, |
| 166 | ) |
| 167 | validations.validationsConfig() |
| 168 | |
| 169 | // Warn on unknown options |
| 170 | validations.knownOptions(passedOptions) |
| 171 | |
| 172 | // Warn for the deprecated options. Note that these options have been removed |
| 173 | // from the type definitions in v7. |
| 174 | validations.draftPolliHeaders( |
| 175 | // @ts-expect-error see the note above. |
| 176 | notUndefinedOptions.draft_polli_ratelimit_headers, |
| 177 | ) |
| 178 | // @ts-expect-error see the note above. |
| 179 | validations.onLimitReached(notUndefinedOptions.onLimitReached) |
| 180 | |
| 181 | // If ipv6Subnet is set to anything other than a function, check it now |
| 182 | // (if it's a function, we'll check the output value later) |
| 183 | if ( |
| 184 | notUndefinedOptions.ipv6Subnet !== undefined && |
| 185 | typeof notUndefinedOptions.ipv6Subnet !== 'function' |
| 186 | ) { |
| 187 | validations.ipv6Subnet(notUndefinedOptions.ipv6Subnet) |
| 188 | } |
| 189 | |
| 190 | // Warn for custom keyGenerator that uses req.ip without the ipKeyGenerator helper |
| 191 | validations.keyGeneratorIpFallback(notUndefinedOptions.keyGenerator) |
| 192 | |
| 193 | // Warn for incompatible settings |
| 194 | validations.ipv6SubnetOrKeyGenerator(notUndefinedOptions) |
| 195 | |
| 196 | // The default value for the `standardHeaders` option is `false`. If set to |
| 197 | // `true`, it resolve to `draft-6`. `draft-7` and draft-8` (recommended) are |
| 198 | // used only if explicitly set. |
| 199 | let standardHeaders = notUndefinedOptions.standardHeaders ?? false |
| 200 | if (standardHeaders === true) standardHeaders = 'draft-6' |
| 201 | |
| 202 | // See ./types.ts#Options for a detailed description of the options and their |
| 203 | // defaults. |
| 204 | const config: Configuration = { |
| 205 | windowMs: 60 * 1000, |
| 206 | limit: passedOptions.max ?? 5, // `max` is deprecated, but support it anyways. |
| 207 | message: 'Too many requests, please try again later.', |
| 208 | statusCode: 429, |
| 209 | legacyHeaders: passedOptions.headers ?? true, |
| 210 | identifier(request: Request, _response: Response): string { |
| 211 | let duration = '' |
no test coverage detected
searching dependent graphs…