Skip to content

Commit 44c6f54

Browse files
js2mehaedaal
andauthored
Release 7.0.0 (#208)
* feat: add "onFormatRouteName" hook * chore: try to add github workflows * chore: rename gh workflow file to main * chore: fix changelog after rebase * fix incorrect working --route-types with --modular option (#207) * chore: try to fix modular route types problem * fix: --route-types with --modular option; fix: better format namespace name (--route-types) * feat: add extra output util functions * chore: improve getTemplate function * fix: getting template content by getTemplate by path * fix: getTemplate by path * chore: fix typings for rawModelTypes * feat: add formatTSContent util function * Fix boolean enum handling (#210) * Add boolean enum example * Fix boolean enum handling * chore: up lodash to latest * bump: up version to 7.0.0; feat: add returning `false` in `onCreateRoute` hook Co-authored-by: Minjae Kim <[email protected]>
1 parent 888249b commit 44c6f54

27 files changed

+28659
-453
lines changed

.vscode/launch.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151
"cwd": "${workspaceFolder}",
5252
"runtimeExecutable": "npm",
5353
"runtimeArgs": ["run-script", "test:--templates"]
54+
},
55+
{
56+
"name": "Debug test-all",
57+
"type": "node",
58+
"request": "launch",
59+
"cwd": "${workspaceFolder}",
60+
"runtimeExecutable": "npm",
61+
"runtimeArgs": ["run-script", "test-all"]
5462
}
5563
]
5664
}

CHANGELOG.md

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

3+
# 7.0.0
4+
5+
BREAKING_CHANGES:
6+
- format `namespace` name in `--route-types` as camelCase with upper first capitalized letter
7+
`foo_bar` -> `FooBar`
8+
9+
Fixes:
10+
- Incorrect working the `--route-types` option with `--modular` option (route types should be splitted on files)
11+
- Fix critical bug linked with enums with boolean type (thanks @haedaal)
12+
13+
Features:
14+
- Ability to return `false` in `onCreateRoute` hook, it allow to ignore route
15+
- Add output util functions
16+
```ts
17+
createFile: (params: {
18+
path: string;
19+
fileName: string;
20+
content: string;
21+
withPrefix?: boolean;
22+
}) => void;
23+
renderTemplate: (
24+
templateContent: string,
25+
data: Record<string, unknown>,
26+
etaOptions?: import("eta/dist/types/config").PartialConfig
27+
) => string;
28+
getTemplate: (params: {
29+
fileName?: string;
30+
name?: string;
31+
path?: string;
32+
}) => string
33+
formatTSContent: (content: string) => string;
34+
35+
36+
// ...
37+
38+
generateApi({ /* ... */ }).then(({ createFile, renderTemplate, getTemplate }) => {
39+
// do something
40+
})
41+
```
42+
343
# 6.4.2
444

545
Fixes:

index.d.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,28 @@ interface GenerateApiParams {
107107
}
108108

109109
export interface Hooks {
110+
/** calls after parse schema component */
110111
onCreateComponent: (component: SchemaComponent) => SchemaComponent | void;
112+
/** calls after parse any kind of schema */
111113
onParseSchema: (originalSchema: any, parsedSchema: any) => any | void;
112-
onCreateRoute: (routeData: ParsedRoute) => ParsedRoute | void;
114+
/** calls after parse route (return type: customized route (ParsedRoute), nothing change (void), false (ignore this route)) */
115+
onCreateRoute: (routeData: ParsedRoute) => ParsedRoute | void | false;
113116
/** Start point of work this tool (after fetching schema) */
114117
onInit?: <C extends GenerateApiConfiguration["config"]>(configuration: C) => C | void;
115118
/** customize configuration object before sending it to ETA templates */
116119
onPrepareConfig?: <C extends GenerateApiConfiguration>(currentConfiguration: C) => C | void;
120+
/** customize route name as you need */
117121
onCreateRouteName?: (
118122
routeNameInfo: RouteNameInfo,
119123
rawRouteInfo: RawRouteInfo,
120124
) => RouteNameInfo | void;
125+
/** customize request params (path params, query params) */
121126
onCreateRequestParams?: (
122127
rawType: SchemaComponent["rawTypeData"],
123128
) => SchemaComponent["rawTypeData"] | void;
129+
/** customize name of model type */
124130
onFormatTypeName?: (typeName: string, rawTypeName?: string) => string | void;
131+
/** customize name of route (operationId), you can do it with using onCreateRouteName too */
125132
onFormatRouteName?: (routeInfo: RawRouteInfo, templateRouteName: string) => string | void;
126133
}
127134

@@ -291,6 +298,7 @@ export interface GenerateApiConfiguration {
291298
description: string;
292299
content: string;
293300
}[];
301+
modelTypes: SchemaComponent[];
294302
hasFormDataRoutes: boolean;
295303
hasSecurityRoutes: boolean;
296304
hasQueryRoutes: boolean;
@@ -308,6 +316,19 @@ export interface GenerateApiConfiguration {
308316
export interface GenerateApiOutput {
309317
configuration: GenerateApiConfiguration;
310318
files: { name: string; content: string; declaration: { name: string; content: string } | null }[];
319+
createFile: (params: {
320+
path: string;
321+
fileName: string;
322+
content: string;
323+
withPrefix?: boolean;
324+
}) => void;
325+
renderTemplate: (
326+
templateContent: string,
327+
data: Record<string, unknown>,
328+
etaOptions?: import("eta/dist/types/config").PartialConfig,
329+
) => string;
330+
getTemplate: (params: { fileName?: string; name?: string; path?: string }) => string;
331+
formatTSContent: (content: string) => string;
311332
}
312333

313334
export declare function generateApi(

package-lock.json

Lines changed: 3 additions & 3 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": "6.4.2",
3+
"version": "7.0.0",
44
"description": "Generate typescript/javascript api from swagger schema",
55
"scripts": {
66
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
@@ -55,7 +55,7 @@
5555
"commander": "^6.2.1",
5656
"eta": "^1.12.1",
5757
"js-yaml": "^4.0.0",
58-
"lodash": "^4.17.20",
58+
"lodash": "^4.17.21",
5959
"make-dir": "^3.1.0",
6060
"nanoid": "^3.1.20",
6161
"prettier": "^2.2.1",

src/config.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ const config = {
6868
custom: "",
6969
},
7070
/** Record<templateName, templateContent> */
71-
templatesToRender: {},
71+
templatesToRender: {
72+
api: "",
73+
dataContracts: "",
74+
httpClient: "",
75+
routeTypes: "",
76+
routeName: "",
77+
},
7278
toJS: false,
7379
silent: false,
7480
typePrefix: "",

src/files.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ const cleanDir = (path) => {
3838

3939
const pathIsExist = (path) => path && fs.existsSync(path);
4040

41-
const createFile = (pathTo, fileName, content) =>
42-
fs.writeFileSync(resolve(__dirname, pathTo, `./${fileName}`), `${filePrefix}${content}`, _.noop);
41+
const createFile = ({ path, fileName, content, withPrefix }) =>
42+
fs.writeFileSync(
43+
resolve(__dirname, path, `./${fileName}`),
44+
`${withPrefix ? filePrefix : ""}${content}`,
45+
_.noop,
46+
);
4347

4448
module.exports = {
4549
createFile,

src/index.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ const { getSwaggerObject, fixSwaggerScheme, convertSwaggerObject } = require("./
1515
const { createComponentsMap, filterComponentsMap } = require("./components");
1616
const { createFile, pathIsExist, pathIsDir, createDir, cleanDir } = require("./files");
1717
const { addToConfig, config } = require("./config");
18-
const { getTemplates, getTemplatePaths } = require("./templates");
18+
const { getTemplates, getTemplatePaths, renderTemplate, getTemplate } = require("./templates");
1919
const constants = require("./constants");
2020
const { generateOutputFiles } = require("./output");
21+
const formatFileContent = require("./formatFileContent");
2122

2223
module.exports = {
24+
constants: constants,
2325
generateApi: ({
2426
input,
2527
output,
@@ -116,10 +118,13 @@ module.exports = {
116118
const hasQueryRoutes = routes.some((route) => route.hasQuery);
117119
const hasFormDataRoutes = routes.some((route) => route.hasFormDataParams);
118120

121+
const componentSchemas = filterComponentsMap(componentsMap, "schemas");
122+
119123
const rawConfiguration = {
120124
apiConfig: createApiConfig(usageSchema, hasSecurityRoutes),
121125
config,
122-
modelTypes: _.map(filterComponentsMap(componentsMap, "schemas"), prepareModelType),
126+
modelTypes: _.map(componentSchemas, prepareModelType),
127+
rawModelTypes: componentSchemas,
123128
hasFormDataRoutes,
124129
hasSecurityRoutes,
125130
hasQueryRoutes,
@@ -156,12 +161,27 @@ module.exports = {
156161
if (!isDirPath) return file;
157162

158163
if (translateToJavaScript) {
159-
createFile(output, file.name, file.content);
160-
createFile(output, file.declaration.name, file.declaration.content);
164+
createFile({
165+
path: output,
166+
fileName: file.name,
167+
content: file.content,
168+
withPrefix: true,
169+
});
170+
createFile({
171+
path: output,
172+
fileName: file.declaration.name,
173+
content: file.declaration.content,
174+
withPrefix: true,
175+
});
161176
if (!config.silent)
162177
console.log(`✔️ your javascript api file created in "${output}"`);
163178
} else {
164-
createFile(output, file.name, file.content);
179+
createFile({
180+
path: output,
181+
fileName: file.name,
182+
content: file.content,
183+
withPrefix: true,
184+
});
165185
if (!config.silent)
166186
console.log(`✔️ your typescript api file created in "${output}"`);
167187
}
@@ -172,6 +192,10 @@ module.exports = {
172192
resolve({
173193
files: generatedFiles,
174194
configuration,
195+
getTemplate,
196+
renderTemplate,
197+
createFile,
198+
formatTSContent: formatFileContent,
175199
});
176200
})
177201
.catch((e) => {

src/modelTypes.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ const prepareModelType = (typeInfo) => {
77
const typeData = getTypeData(typeInfo);
88
let { typeIdentifier, name: originalName, content, type, description } = typeData;
99

10-
if (config.generateUnionEnums && typeIdentifier === "enum") {
11-
typeIdentifier = "type";
12-
}
13-
1410
const resultContent = formatters[type] ? formatters[type](content) : content;
1511
const name = formatModelName(originalName);
1612

src/output.js

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,61 @@ const createMultipleFileInfos = (templatesToRender, configuration) => {
4747
const { fileNames, generateRouteTypes, generateClient } = configuration.config;
4848
const modularApiFileInfos = [];
4949

50-
if (generateClient && routes.$outOfModule) {
51-
const outOfModuleApiContent = renderTemplate(templatesToRender.api, {
52-
...configuration,
53-
route: configuration.routes.$outOfModule,
54-
});
55-
56-
modularApiFileInfos.push(
57-
createFileInfo(configuration, fileNames.outOfModuleApi, outOfModuleApiContent),
58-
);
50+
if (routes.$outOfModule) {
51+
if (generateRouteTypes) {
52+
const outOfModuleRouteContent = renderTemplate(templatesToRender.routeTypes, {
53+
...configuration,
54+
route: configuration.routes.$outOfModule,
55+
});
56+
57+
modularApiFileInfos.push(
58+
createFileInfo(configuration, fileNames.outOfModuleApi, outOfModuleRouteContent),
59+
);
60+
}
61+
if (generateClient) {
62+
const outOfModuleApiContent = renderTemplate(templatesToRender.api, {
63+
...configuration,
64+
route: configuration.routes.$outOfModule,
65+
});
66+
67+
modularApiFileInfos.push(
68+
createFileInfo(configuration, fileNames.outOfModuleApi, outOfModuleApiContent),
69+
);
70+
}
5971
}
6072

61-
if (generateClient && routes.combined) {
73+
if (routes.combined) {
6274
modularApiFileInfos.push(
6375
..._.reduce(
6476
routes.combined,
6577
(apiFileInfos, route) => {
66-
const apiModuleContent = renderTemplate(templatesToRender.api, {
67-
...configuration,
68-
route,
69-
});
70-
71-
return [
72-
...apiFileInfos,
73-
createFileInfo(configuration, classNameCase(route.moduleName), apiModuleContent),
74-
];
78+
if (generateRouteTypes) {
79+
const routeModuleContent = renderTemplate(templatesToRender.routeTypes, {
80+
...configuration,
81+
route,
82+
});
83+
84+
apiFileInfos.push(
85+
createFileInfo(
86+
configuration,
87+
classNameCase(`${route.moduleName}_Route`),
88+
routeModuleContent,
89+
),
90+
);
91+
}
92+
93+
if (generateClient) {
94+
const apiModuleContent = renderTemplate(templatesToRender.api, {
95+
...configuration,
96+
route,
97+
});
98+
99+
apiFileInfos.push(
100+
createFileInfo(configuration, classNameCase(route.moduleName), apiModuleContent),
101+
);
102+
}
103+
104+
return apiFileInfos;
75105
},
76106
[],
77107
),
@@ -84,12 +114,6 @@ const createMultipleFileInfos = (templatesToRender, configuration) => {
84114
fileNames.dataContracts,
85115
renderTemplate(templatesToRender.dataContracts, configuration),
86116
),
87-
generateRouteTypes &&
88-
createFileInfo(
89-
configuration,
90-
fileNames.routeTypes,
91-
renderTemplate(templatesToRender.routeTypes, configuration),
92-
),
93117
generateClient &&
94118
createFileInfo(
95119
configuration,

src/render/utils/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require("path");
33
const { classNameCase, formatDescription, internalCase } = require("../../common");
44
const { getComponentByRef } = require("../../components");
55
const { config } = require("../../config");
6+
const { formatModelName } = require("../../modelNames");
67
const { getInlineParseContent, getParseContent, parseSchema } = require("../../schema");
78
const { formatters, inlineExtraFormatters } = require("../../typeFormatters");
89

@@ -16,6 +17,7 @@ module.exports = {
1617
parseSchema,
1718
formatters,
1819
inlineExtraFormatters,
20+
formatModelName,
1921
fmtToJSDocLine: require("./fmtToJSDocLine"),
2022
_: _,
2123
require: (packageOrPath) => {

0 commit comments

Comments
 (0)