Skip to content

Commit ba1a687

Browse files
authored
Release 4.2.0 (#140)
* feat: new hook onCreateRequestParams; feat: response content types * bump: up version to 4.2.0
1 parent c320b9a commit ba1a687

File tree

10 files changed

+53
-8
lines changed

10 files changed

+53
-8
lines changed

CHANGELOG.md

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

3+
# 4.2.0
4+
Features:
5+
- new hook `onCreateRequestParams` which allows modify request params (`--extract-request-params` option) before sending it to route info
6+
![onCreateRequestParams](./assets/changelog_assets/onCreateRequestParamsHook.jpg)
7+
How to use:
8+
```ts
9+
generateApi({
10+
// ... your config,
11+
hooks: {
12+
onCreateRequestParams: (rawType) => {
13+
if (Object.keys(rawType.properties).length > 1) return rawType;
14+
15+
return rawType;
16+
}
17+
}
18+
})
19+
```
20+
- response content types (array of string like `application/json`, `image/png`) which allows to customize declaration of request response
21+
Exist in `procedure-call.eta` template `it.route.response.contentTypes`
22+
23+
Internal:
24+
- Difference in templates:
25+
- `procedure-call.eta`
26+
![procedureCallEta1](./assets/changelog_assets/changes_procedure_call_1.jpg)
27+
28+
329
# 4.1.0
430

531
Features:
Loading
Loading

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ interface GenerateApiParams {
7777
* prettier configuration
7878
*/
7979
prettier?: object;
80+
cleanOutput?: boolean;
8081
enumNamesAsValues?: boolean;
8182

8283
hooks?: Partial<{
@@ -87,6 +88,9 @@ interface GenerateApiParams {
8788
onInit?: <C extends GenerateApiConfiguration["config"]>(configuration: C) => C | void;
8889
/** customize configuration object before sending it to ETA templates */
8990
onPrepareConfig?: <C extends GenerateApiConfiguration>(currentConfiguration: C) => C | void;
91+
onCreateRequestParams?: (
92+
rawType: SchemaComponent["rawTypeData"],
93+
) => SchemaComponent["rawTypeData"] | void;
9094
}>;
9195
/**
9296
* extra templates

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"description": "Create typescript api module from swagger schema",
55
"scripts": {
66
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts --extract-request-params --enum-names-as-values",

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const config = {
4343
onCreateRoute: (routeData) => routeData,
4444
onInit: (config) => config,
4545
onPrepareConfig: (apiConfig) => apiConfig,
46+
onCreateRequestParams: (rawType) => {},
4647
},
4748
};
4849

src/routes.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ const createRequestParamsSchema = ({
232232
},
233233
};
234234

235+
const fixedSchema = config.hooks.onCreateRequestParams(schema);
236+
237+
if (fixedSchema) return fixedSchema;
238+
235239
if (extractRequestParams) {
236240
return createComponent("schemas", classNameCase(`${routeName.usage} Params`), { ...schema });
237241
}
@@ -264,12 +268,17 @@ const parseRoutes = ({ usageSchema, parsedSchemas, moduleNameIndex, extractReque
264268
tags,
265269
responses,
266270
requestBodyName,
271+
produces,
272+
...otherInfo
267273
} = requestInfo;
268274
const routeId = nanoid(12);
269275
const hasSecurity = !!(
270276
(globalSecurity && globalSecurity.length) ||
271277
(security && security.length)
272278
);
279+
const responseContentTypes =
280+
produces ||
281+
_.flatten(_.map(responses, (response) => response && _.keys(response.content)));
273282

274283
const formDataParams = getRouteParams(parameters, "formData");
275284
const pathParams = getRouteParams(parameters, "path");
@@ -419,7 +428,7 @@ const parseRoutes = ({ usageSchema, parsedSchemas, moduleNameIndex, extractReque
419428
hasSecurity && ` * @secure`,
420429
...(config.generateResponses && responsesTypes.length
421430
? responsesTypes.map(
422-
({ type, status, description, isSuccess }) =>
431+
({ type, status, description }) =>
423432
` * @response \`${status}\` \`${type}\` ${description}`,
424433
)
425434
: []),
@@ -428,6 +437,7 @@ const parseRoutes = ({ usageSchema, parsedSchemas, moduleNameIndex, extractReque
428437
const path = route.replace(/{/g, "${");
429438

430439
const response = {
440+
contentTypes: responseContentTypes,
431441
type: getReturnType(responses, parsedSchemas, operationId),
432442
errorType: getErrorReturnType(responses, parsedSchemas, operationId),
433443
};
@@ -459,6 +469,10 @@ const parseRoutes = ({ usageSchema, parsedSchemas, moduleNameIndex, extractReque
459469
description,
460470
tags,
461471
summary,
472+
responses,
473+
produces,
474+
requestBody,
475+
...otherInfo,
462476
},
463477
};
464478

templates/default/procedure-call.eta

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const { utils, route, config } = it;
33
const { _, getInlineParseContent, getParseContent, parseSchema, getComponentByRef, require } = utils;
44
const { parameters, path, method, payload, params, query, formData, security, requestParams } = route.request;
5-
const { type, errorType } = route.response;
5+
const { type, errorType, contentTypes } = route.response;
66
const routeDocs = includeFile("./route-docs", { config, route, utils });
77
const queryName = (query && query.name) || "query";
88
const pathParams = _.values(parameters);
@@ -14,7 +14,7 @@ const rawWrapperArgs = config.extractRequestParams ?
1414
requestParams && {
1515
name: pathParams.length ? `{ ${_.join(_.map(pathParams, "name"), ", ")}, ...${queryName} }` : queryName,
1616
optional: false,
17-
type: getParseContent(requestParams),
17+
type: getInlineParseContent(requestParams),
1818
},
1919
...(!requestParams ? pathParams : []),
2020
payload,

templates/modular/procedure-call.eta

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const { utils, route, config } = it;
33
const { _, getInlineParseContent, getParseContent, parseSchema, getComponentByRef, require } = utils;
44
const { parameters, path, method, payload, params, query, formData, security, requestParams } = route.request;
5-
const { type, errorType } = route.response;
5+
const { type, errorType, contentTypes } = route.response;
66
const routeDocs = includeFile("./route-docs", { config, route, utils });
77
const queryName = (query && query.name) || "query";
88
const pathParams = _.values(parameters);
@@ -14,7 +14,7 @@ const rawWrapperArgs = config.extractRequestParams ?
1414
requestParams && {
1515
name: pathParams.length ? `{ ${_.join(_.map(pathParams, "name"), ", ")}, ...${queryName} }` : queryName,
1616
optional: false,
17-
type: getParseContent(requestParams),
17+
type: getInlineParseContent(requestParams),
1818
},
1919
...(!requestParams ? pathParams : []),
2020
payload,

0 commit comments

Comments
 (0)