-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate path params definition when missing
- Loading branch information
1 parent
5b5fb42
commit e6046d2
Showing
9 changed files
with
451 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
|
||
const { matchParams } = require('./match-params') | ||
|
||
const schema = { | ||
params: { | ||
type: 'object', | ||
properties: {} | ||
} | ||
} | ||
|
||
const namePattern = /\{([^}]+)\}/ | ||
|
||
function paramType (param) { | ||
return param ? 'string' : undefined | ||
} | ||
|
||
function paramName (param) { | ||
return param.replace(namePattern, (_, captured) => captured) | ||
} | ||
|
||
// Generates default parameters schema from the given URL. | ||
function generateParamsSchema (url) { | ||
const params = matchParams(url) | ||
schema.params.properties = params.reduce((acc, param) => { | ||
acc[paramName(param)] = { | ||
type: paramType(paramName(param)) | ||
} | ||
return acc | ||
}, {}) | ||
|
||
return schema | ||
} | ||
|
||
module.exports = { | ||
generateParamsSchema, | ||
paramType, | ||
paramName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
const paramPattern = /\{[^{}]+\}/g | ||
|
||
function hasParams (url) { | ||
if (!url) return false | ||
const matches = url.match(paramPattern) | ||
return matches !== null && matches.length > 0 | ||
} | ||
|
||
function matchParams (url) { | ||
if (!url) return [] | ||
return url.match(paramPattern) || [] | ||
} | ||
|
||
module.exports = { | ||
hasParams, | ||
matchParams | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.