Skip to content

Commit a975971

Browse files
authored
Release 6.4.0 (acacode#197)
* fix: bug with complex types; fix: bug with number enum with x-enumNames * fix: problem with non existing title property in swagger.info; chore: add enums swagger schema * chore: improve description in single api file * feat: add "onFormatRouteName" hook * chore: try to add github workflows * docs: update README.md * docs: update README * bump: up version to 6.4.0 * chore: rename gh workflow file to main
1 parent 7d38f20 commit a975971

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+781
-153
lines changed

.github/workflows/main.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- next
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v1
14+
15+
- name: use node
16+
uses: actions/setup-node@v1
17+
with:
18+
node-version: 10
19+
20+
- name: install deps
21+
run: npm i
22+
23+
- name: simple-tests
24+
run: npm run generate && npm run validate
25+
26+
- name: specific-tests
27+
run: npm run test-specific

.prettierignore

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ tests/**/*.d.js
44
swagger-test-cli
55
swagger-test-cli.*
66
templates
7-
*.md
7+
*.md
8+
.github
9+
node_modules
10+
.openapi-generator
11+
.vscode
12+
assets
13+
templates

.vscode/launch.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
"request": "launch",
1919
"cwd": "${workspaceFolder}",
2020
"runtimeExecutable": "npm",
21-
"runtimeArgs": ["run-script", "cli:debug:json"]
21+
"runtimeArgs": ["run-script", "cli:json"]
2222
},
2323
{
2424
"name": "Debug YAML CLI",
2525
"type": "node",
2626
"request": "launch",
2727
"cwd": "${workspaceFolder}",
2828
"runtimeExecutable": "npm",
29-
"runtimeArgs": ["run-script", "cli:debug:yaml"]
29+
"runtimeArgs": ["run-script", "cli:yaml"]
3030
},
3131
{
3232
"name": "Debug Node",

CHANGELOG.md

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# next release
22

3+
# 6.4.0
4+
5+
Features:
6+
- `onFormatRouteName(routeInfo: RawRouteInfo, templateRouteName: string)` hook. Allows to format route name, as you like :)
7+
8+
Fixes:
9+
- Bug with wrong complex types (anyOf, oneOf, allOf) when some child schema contains only description
10+
![example](./assets/changelog_assets/fixComplexTypeAny.jpg)
11+
- Bug with number enums which have `x-enumNames`
12+
- Problem with not existing `title` property in `info`
13+
14+
Minor:
15+
- Improve description for complex types
16+
- Improve description in single api file
17+
318
# 6.3.0
419

520
Features:

README.md

+38-34
Original file line numberDiff line numberDiff line change
@@ -73,49 +73,53 @@ Also you can use `npx`:
7373
You can use this package from nodejs:
7474
```js
7575
const { generateApi } = require('swagger-typescript-api');
76+
const path = require("path");
77+
const fs = require("fs");
7678

77-
// example with url
79+
/* NOTE: all fields are optional expect one of `output`, `url`, `spec` */
7880
generateApi({
79-
name: "MySuperbApi.ts", // name of output typescript file
80-
url: 'http://api.com/swagger.json', // url where located swagger schema
81-
})
82-
.then(({ files, configuration }) => {
83-
files.forEach(({ content, name }) => {
84-
fs.writeFile(path, content);
85-
});
86-
})
87-
.catch(e => console.error(e))
88-
89-
// example with local file
90-
generateApi({
91-
name: "ApiModule.ts", // name of output typescript file
92-
input: resolve(process.cwd(), './foo/swagger.json') // path to swagger schema
93-
})
94-
.then(({ files, configuration }) => {
95-
files.forEach(({ content, name }) => {
96-
fs.writeFile(path, content);
97-
});
98-
})
99-
.catch(e => console.error(e))
100-
101-
// example with parsed schema
102-
generateApi({
103-
name: "ApiModule.ts", // name of output typescript file
81+
name: "MySuperbApi.ts",
82+
output: path.resolve(process.cwd(), "./src/__generated__"),
83+
url: 'http://api.com/swagger.json',
84+
input: path.resolve(process.cwd(), './foo/swagger.json'),
10485
spec: {
10586
swagger: "2.0",
10687
info: {
10788
version: "1.0.0",
10889
title: "Swagger Petstore",
10990
},
110-
host: "petstore.swagger.io",
111-
basePath: "/api",
112-
schemes: ["http"],
113-
consumes: ["application/json"],
114-
produces: ["application/json"],
115-
paths: {
116-
// ...
117-
}
11891
// ...
92+
},
93+
templates: path.resolve(process.cwd(), './api-templates'),
94+
httpClientType: "axios", // or "fetch"
95+
defaultResponseAsSuccess: false,
96+
generateRouteTypes: false,
97+
generateResponses: true,
98+
toJS: false,
99+
extractRequestParams: false,
100+
prettier: {
101+
printWidth: 120,
102+
tabWidth: 2,
103+
trailingComma: "all",
104+
parser: "typescript",
105+
},
106+
defaultResponseType: "void",
107+
singleHttpClient: true,
108+
cleanOutput: false,
109+
enumNamesAsValues: false,
110+
moduleNameFirstTag: false,
111+
generateUnionEnums: false,
112+
extraTemplates: [],
113+
hooks: {
114+
onCreateComponent: (component) => {},
115+
onCreateRequestParams: (rawType) => {},
116+
onCreateRoute: (routeData) => {},
117+
onCreateRouteName: (routeNameInfo, rawRouteInfo) => {},
118+
onFormatRouteName: (routeInfo, templateRouteName) => {},
119+
onFormatTypeName: (typeName, rawTypeName) => {},
120+
onInit: (configuration) => {},
121+
onParseSchema: (originalSchema, parsedSchema) => {},
122+
onPrepareConfig: (currentConfiguration) => {},
119123
}
120124
})
121125
.then(({ files, configuration }) => {
24.4 KB
Loading

index.d.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ interface GenerateApiParams {
8484
* extract request params to data contract (Also combine path params and query params into one object)
8585
*/
8686
extractRequestParams?: boolean;
87-
prepareConfig?: <C extends GenerateApiConfiguration>(currentConfiguration: C) => C;
8887
/**
8988
* prettier configuration
9089
*/
@@ -100,29 +99,32 @@ interface GenerateApiParams {
10099
cleanOutput?: boolean;
101100
enumNamesAsValues?: boolean;
102101

103-
hooks?: Partial<{
104-
onCreateComponent: (component: SchemaComponent) => SchemaComponent | void;
105-
onParseSchema: (originalSchema: any, parsedSchema: any) => any | void;
106-
onCreateRoute: (routeData: ParsedRoute) => ParsedRoute | void;
107-
/** Start point of work this tool (after fetching schema) */
108-
onInit?: <C extends GenerateApiConfiguration["config"]>(configuration: C) => C | void;
109-
/** customize configuration object before sending it to ETA templates */
110-
onPrepareConfig?: <C extends GenerateApiConfiguration>(currentConfiguration: C) => C | void;
111-
onCreateRouteName?: (
112-
routeNameInfo: RouteNameInfo,
113-
rawRouteInfo: RawRouteInfo,
114-
) => RouteNameInfo | void;
115-
onCreateRequestParams?: (
116-
rawType: SchemaComponent["rawTypeData"],
117-
) => SchemaComponent["rawTypeData"] | void;
118-
onFormatTypeName?: (typeName: string, rawTypeName?: string) => string | void;
119-
}>;
102+
hooks?: Partial<Hooks>;
120103
/**
121104
* extra templates
122105
*/
123106
extraTemplates?: { name: string; path: string }[];
124107
}
125108

109+
export interface Hooks {
110+
onCreateComponent: (component: SchemaComponent) => SchemaComponent | void;
111+
onParseSchema: (originalSchema: any, parsedSchema: any) => any | void;
112+
onCreateRoute: (routeData: ParsedRoute) => ParsedRoute | void;
113+
/** Start point of work this tool (after fetching schema) */
114+
onInit?: <C extends GenerateApiConfiguration["config"]>(configuration: C) => C | void;
115+
/** customize configuration object before sending it to ETA templates */
116+
onPrepareConfig?: <C extends GenerateApiConfiguration>(currentConfiguration: C) => C | void;
117+
onCreateRouteName?: (
118+
routeNameInfo: RouteNameInfo,
119+
rawRouteInfo: RawRouteInfo,
120+
) => RouteNameInfo | void;
121+
onCreateRequestParams?: (
122+
rawType: SchemaComponent["rawTypeData"],
123+
) => SchemaComponent["rawTypeData"] | void;
124+
onFormatTypeName?: (typeName: string, rawTypeName?: string) => string | void;
125+
onFormatRouteName?: (routeInfo: RawRouteInfo, templateRouteName: string) => string | void;
126+
}
127+
126128
export interface RouteNameRouteInfo {}
127129

128130
export type RouteNameInfo = {

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "6.3.0",
4-
"description": "Create typescript api module from swagger schema",
3+
"version": "6.4.0",
4+
"description": "Generate typescript/javascript api from swagger schema",
55
"scripts": {
6-
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts --extract-request-params --enum-names-as-values",
6+
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
77
"cli:yaml": "node index.js -r -d -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
8-
"cli:debug:json": "node --nolazy index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts --extract-request-params --enum-names-as-values",
9-
"cli:debug:yaml": "node --nolazy index.js -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
108
"node": "node swagger-test-cli/generate.js",
119
"node:debug": "node --nolazy swagger-test-cli/generate.js",
1210
"contributors": "all-contributors generate",
1311
"cli:help": "node index.js -h",
1412
"test-all": "node ./node_modules/npm-run-all/bin/npm-run-all/index.js generate validate test:* --continue-on-error",
13+
"test-specific": "node ./node_modules/npm-run-all/bin/npm-run-all/index.js test:* --continue-on-error",
1514
"generate": "node tests/generate.js",
1615
"generate:debug": "node --nolazy tests/generate.js",
1716
"validate": "node tests/validate.js",

src/apiConfig.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { formatDescription } = require("./common");
33
const { TS_KEYWORDS } = require("./constants");
44

55
const createApiConfig = (swaggerSchema) => {
6-
const { info, servers, host, basePath } = swaggerSchema;
6+
const { info, servers, host, basePath, externalDocs, tags } = swaggerSchema;
77
const server = (servers && servers[0]) || { url: "" };
88
const { title = "No title", version, description: schemaDescription = "" } = info || {};
99
const { url: serverUrl } = server;
@@ -27,6 +27,14 @@ const createApiConfig = (swaggerSchema) => {
2727
servers: servers || [],
2828
basePath,
2929
host,
30+
externalDocs: _.merge(
31+
{
32+
url: "",
33+
description: "",
34+
},
35+
externalDocs,
36+
),
37+
tags: _.compact(tags),
3038
// TODO: unused, remove!
3139
props: _.compact([
3240
{
@@ -50,7 +58,9 @@ const createApiConfig = (swaggerSchema) => {
5058
baseUrl: serverUrl,
5159
title,
5260
version,
61+
// TODO: unused, remove
5362
description,
63+
// TODO: unused, remove
5464
hasDescription: !!description.length,
5565
};
5666
};

src/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const config = {
5050
onCreateRequestParams: (rawType) => {},
5151
onCreateRouteName: () => {},
5252
onFormatTypeName: (typeName, rawTypeName) => {},
53+
onFormatRouteName: (routeInfo, templateRouteName) => {},
5354
},
5455
defaultResponseType: TS_KEYWORDS.VOID,
5556
singleHttpClient: false,

src/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const SCHEMA_TYPES = {
2929
COMPLEX_ANY_OF: "anyOf",
3030
COMPLEX_ALL_OF: "allOf",
3131
COMPLEX_NOT: "not",
32+
COMPLEX_UNKNOWN: "__unknown",
3233
};
3334

3435
const HTTP_CLIENT = {

src/routeNames.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ const getRouteName = (routeInfo) => {
66
const { routeNameDuplicatesMap, templatesToRender } = config;
77
const routeNameTemplate = templatesToRender.routeName;
88

9-
if (!routeNameTemplate) {
10-
throw new Error("🥵 route name template (route-name.eta) not found");
11-
}
12-
13-
const routeName = renderTemplate(routeNameTemplate, {
9+
const routeNameFromTemplate = renderTemplate(routeNameTemplate, {
1410
routeInfo: routeInfo,
1511
utils: require("./render/utils"),
12+
config,
1613
});
1714

15+
const routeName =
16+
config.hooks.onFormatRouteName(routeInfo, routeNameFromTemplate) || routeNameFromTemplate;
17+
1818
const duplicateIdentifier = `${moduleName}|${routeName}`;
1919

2020
if (routeNameDuplicatesMap.has(duplicateIdentifier)) {

0 commit comments

Comments
 (0)