-
-
Notifications
You must be signed in to change notification settings - Fork 198
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 (#761)
* feat: generate path params definition when missing * chore: rename schema to schemaGenerated Co-authored-by: Manuel Spigolon <behemoth89@gmail.com> * chore: rename schema to schemaGenerated swagger Co-authored-by: Manuel Spigolon <behemoth89@gmail.com> * chore: remove paramType call Co-authored-by: Manuel Spigolon <behemoth89@gmail.com> * chore: remove paramType function Co-authored-by: Manuel Spigolon <behemoth89@gmail.com> * chore: replace match with test on url Co-authored-by: Manuel Spigolon <behemoth89@gmail.com> * fix: remove paramType from exports and tests * chore: use strictSame instead of same where needed * fix: broken website url in readme * chore(docs): add route parameters documentation --------- Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
- Loading branch information
1 parent
5b5fb42
commit a1963e5
Showing
12 changed files
with
626 additions
and
5 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
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,35 @@ | ||
'use strict' | ||
|
||
const { matchParams } = require('./match-params') | ||
|
||
const namePattern = /\{([^}]+)\}/ | ||
|
||
function paramName (param) { | ||
return param.replace(namePattern, (_, captured) => captured) | ||
} | ||
|
||
// Generates default parameters schema from the given URL. (ex: /example/{userId}) | ||
function generateParamsSchema (url) { | ||
const params = matchParams(url) | ||
const schema = { | ||
params: { | ||
type: 'object', | ||
properties: {} | ||
} | ||
} | ||
|
||
schema.params.properties = params.reduce((acc, param) => { | ||
const name = paramName(param) | ||
acc[name] = { | ||
type: 'string' | ||
} | ||
return acc | ||
}, {}) | ||
|
||
return schema | ||
} | ||
|
||
module.exports = { | ||
generateParamsSchema, | ||
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,18 @@ | ||
'use strict' | ||
|
||
const paramPattern = /\{[^{}]+\}/g | ||
|
||
function hasParams (url) { | ||
if (!url) return false | ||
return paramPattern.test(url) | ||
} | ||
|
||
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
Oops, something went wrong.