Skip to content

Commit 721b35e

Browse files
authored
Release 1.6.0 (#30)
* chore(refactor): make query params collect function more readable * bump: up project version to 1.6.0 (see changes in changelog) Co-authored-by: svolkov <[email protected]>
1 parent 7056e26 commit 721b35e

31 files changed

+922
-391
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# next release
22

3+
# 1.6.0
4+
5+
Features:
6+
7+
- Improvenment in optional request params (request body, query params, path params)
8+
9+
Fixes:
10+
11+
- Fix bug when `path` request param have the same name as `query`
12+
- Fix bug when `path` request param have the same name as `params`
13+
14+
Minor/Internal:
15+
16+
- changed `addQueryParams()` method
17+
- up `swagger2openapi` dependency version to `5.3.4`
18+
319
# 1.5.0
420

521
Features:

package-lock.json

Lines changed: 125 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "Create typescript api module from swagger schema",
55
"scripts": {
66
"cli": "node index.js -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
@@ -35,7 +35,7 @@
3535
"js-yaml": "^3.13.1",
3636
"lodash": "^4.17.15",
3737
"mustache": "^4.0.0",
38-
"swagger2openapi": "^5.3.3",
38+
"swagger2openapi": "^5.3.4",
3939
"prettier": "^1.19.1"
4040
},
4141
"bin": {

src/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ module.exports = {
8181
generateRouteTypes ? mustache.render(routeTypesTemplate, configuration) : "",
8282
generateClient ? mustache.render(clientTemplate, configuration) : "",
8383
].join(""),
84-
{
85-
printWidth: 120,
86-
tabWidth: 2,
87-
trailingComma: "all",
88-
parser: "typescript",
89-
},
84+
prettierConfig,
9085
);
9186

9287
if (pathIsExist(output)) {

src/routes.js

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,20 +216,59 @@ const parseRoutes = ({ paths }, parsedSchemas) =>
216216
? getTypeFromRequestInfo(requestBody, parsedSchemas, operationId)
217217
: null;
218218

219-
const args = [
220-
...pathParams.map(param => ({
221-
name: param.name,
222-
type: parseSchema(param.schema, null, inlineExtraFormatters).content,
223-
})),
224-
queryType && {
225-
name: "query",
226-
type: queryType,
219+
const pathArgs = _.map(pathParams, param => ({
220+
name: param.name,
221+
optional: !param.required,
222+
type: parseSchema(param.schema, null, inlineExtraFormatters).content,
223+
}));
224+
225+
const specificArgs = {
226+
query: queryType
227+
? {
228+
name: pathArgs.some(pathArg => pathArg.name === "query") ? "queryParams" : "query",
229+
optional: parseSchema(queryObjectSchema, null).allFieldsAreOptional,
230+
type: queryType,
231+
}
232+
: void 0,
233+
body: bodyType
234+
? {
235+
name: bodyParamName,
236+
optional:
237+
typeof requestBody.required === "undefined" ? false : !requestBody.required,
238+
type: bodyType,
239+
}
240+
: void 0,
241+
requestParams: {
242+
name: pathArgs.some(pathArg => pathArg.name === "params") ? "requestParams" : "params",
243+
optional: true,
244+
type: "RequestParams",
227245
},
228-
bodyType && {
229-
name: bodyParamName,
230-
type: bodyType,
231-
},
232-
].filter(Boolean);
246+
};
247+
248+
let routeArgs = [...pathArgs, specificArgs.query, specificArgs.body].filter(Boolean);
249+
250+
if (routeArgs.some(pathArg => pathArg.optional)) {
251+
const { optionalArgs, requiredArgs } = _.reduce(
252+
[...routeArgs],
253+
(acc, pathArg) => {
254+
if (pathArg.optional) {
255+
acc.optionalArgs.push(pathArg);
256+
} else {
257+
acc.requiredArgs.push(pathArg);
258+
}
259+
260+
return acc;
261+
},
262+
{
263+
optionalArgs: [],
264+
requiredArgs: [],
265+
},
266+
);
267+
268+
routeArgs = [...requiredArgs, ...optionalArgs];
269+
}
270+
271+
routeArgs.push(specificArgs.requestParams);
233272

234273
// TODO: get args for formData
235274
// "name": "file",
@@ -275,7 +314,8 @@ const parseRoutes = ({ paths }, parsedSchemas) =>
275314
name,
276315
pascalName: _.upperFirst(name),
277316
comments,
278-
args,
317+
routeArgs,
318+
specificArgs,
279319
method: _.upperCase(method),
280320
path: route.replace(/{/g, "${"),
281321
returnType: getReturnType(responses, parsedSchemas, operationId),

0 commit comments

Comments
 (0)