Skip to content

Commit 746069d

Browse files
authored
chore: faster checks (#905)
1 parent 5f364d1 commit 746069d

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

lib/spec/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
CUSTOM_HEADER: 'x-'
3+
}

lib/spec/openapi/utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { xResponseDescription, xConsume, xExamples } = require('../../constants')
88
const { rawRequired } = require('../../symbols')
99
const { generateParamsSchema } = require('../../util/generate-params-schema')
1010
const { hasParams } = require('../../util/match-params')
11+
const { CUSTOM_HEADER } = require('../constants')
1112

1213
function prepareDefaultOptions (opts) {
1314
const openapi = opts.openapi
@@ -26,7 +27,7 @@ function prepareDefaultOptions (opts) {
2627
const convertConstToEnum = opts.convertConstToEnum
2728

2829
for (const [key, value] of Object.entries(opts.openapi)) {
29-
if (key.startsWith('x-')) {
30+
if (key.startsWith(CUSTOM_HEADER)) {
3031
extensions.push([key, value])
3132
}
3233
}
@@ -82,7 +83,7 @@ function prepareOpenapiObject (opts) {
8283
function normalizeUrl (url, serverUrls, stripBasePath) {
8384
if (!stripBasePath) return formatParamUrl(url)
8485
serverUrls.forEach(function (serverUrl) {
85-
const basePath = serverUrl.startsWith('/') ? serverUrl : new URL(serverUrl).pathname
86+
const basePath = serverUrl[0] === '/' ? serverUrl : new URL(serverUrl).pathname
8687
if (url.startsWith(basePath) && basePath !== '/') {
8788
url = url.replace(basePath, '')
8889
}
@@ -489,7 +490,7 @@ function prepareOpenapiMethod (opts, schema, ref, openapiObject, url) {
489490
if (schema.servers) openapiMethod.servers = schema.servers
490491
if (schema.callbacks) openapiMethod.callbacks = resolveCallbacks(opts, schema.callbacks, ref)
491492
for (const key of Object.keys(schema)) {
492-
if (key.startsWith('x-')) {
493+
if (key.startsWith(CUSTOM_HEADER)) {
493494
openapiMethod[key] = schema[key]
494495
}
495496
}

lib/spec/swagger/utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { resolveSchemaReference } = require('../../util/resolve-schema-reference'
77
const { xResponseDescription, xConsume } = require('../../constants')
88
const { generateParamsSchema } = require('../../util/generate-params-schema')
99
const { hasParams } = require('../../util/match-params')
10+
const { CUSTOM_HEADER } = require('../constants')
1011

1112
function prepareDefaultOptions (opts) {
1213
const swagger = opts.swagger
@@ -30,7 +31,7 @@ function prepareDefaultOptions (opts) {
3031
const extensions = []
3132

3233
for (const [key, value] of Object.entries(opts.swagger)) {
33-
if (key.startsWith('x-')) {
34+
if (key.startsWith(CUSTOM_HEADER)) {
3435
extensions.push([key, value])
3536
}
3637
}
@@ -97,7 +98,7 @@ function normalizeUrl (url, basePath, stripBasePath) {
9798
} else {
9899
path = url
99100
}
100-
if (!path.startsWith('/')) {
101+
if (path[0] !== '/') {
101102
path = '/' + String(path)
102103
}
103104
return formatParamUrl(path)
@@ -310,7 +311,7 @@ function prepareSwaggerMethod (schema, ref, swaggerObject, url) {
310311
if (schema.deprecated) swaggerMethod.deprecated = schema.deprecated
311312
if (schema.security) swaggerMethod.security = schema.security
312313
for (const key of Object.keys(schema)) {
313-
if (key.startsWith('x-')) {
314+
if (key.startsWith(CUSTOM_HEADER)) {
314315
swaggerMethod[key] = schema[key]
315316
}
316317
}

0 commit comments

Comments
 (0)