-
-
Notifications
You must be signed in to change notification settings - Fork 210
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 74a53c4
Showing
11 changed files
with
503 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,40 @@ | ||
'use strict' | ||
|
||
const { matchParams } = require('./match-params') | ||
|
||
const namePattern = /\{([^}]+)\}/ | ||
|
||
function paramType () { | ||
return 'string' | ||
} | ||
|
||
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: paramType() | ||
} | ||
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
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.