Skip to content

Commit b4b11eb

Browse files
js2mewyoziSaschaGalleykel666mastermatt
authored
Release 9.0.0 (acacode#245)
* Consider 2xx a successful status (acacode#238) * fix: add array params with key (acacode#237) * fix: add array query params with key * chore: use encoded key and value for array params Co-authored-by: Sascha Galley <sg@troop.at> * disableProxy option, added catch to axios get (acacode#232) * fix(types): add silent as param in declarations (acacode#241) * axios file upload formData Type (acacode#244) * axios file upload formData Type * fix error FormData is not undefined. when serverside request Co-authored-by: guhyeon <guhyeon77@gmail.com> * docs: update CHANGELOG * chore: make property instance public * chore: refresh generated schemas * fix: variable name "params" doesn't uniq (thanks @mixalbl4-127) * chore: replace log to errorLog * refactor: resolving specific arg names * docs: update CHANGELOG * feat: --extract-request-body cli option (extractRequestBody: false); refactor: logger; BREAKING_CHANGE: remove deprecated code in apiConfig * fix: utils type in .d.ts * Add TSDoc tag for deprecated routes (acacode#246) * chore: refresh generated schemas; fix: form data body for axios request method * bump: up version to 9.0.0 Co-authored-by: Joonas <wyozi@users.noreply.github.com> Co-authored-by: Sascha Galley <me@xash.at> Co-authored-by: Sascha Galley <sg@troop.at> Co-authored-by: Fabio <kelfabio@gmail.com> Co-authored-by: Matt R. Wilson <github@mattw.co> Co-authored-by: guhyeon <guhyun77@naver.com> Co-authored-by: guhyeon <guhyeon77@gmail.com>
1 parent 35b08b1 commit b4b11eb

File tree

97 files changed

+3501
-1376
lines changed

Some content is hidden

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

97 files changed

+3501
-1376
lines changed

CHANGELOG.md

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

3+
# 9.0.0
4+
5+
NOTE: This version is not compatible with previous templates (removed `route.request.params`, `apiConfig.props`, `apiConfig.generic`, `apiConfig.description`, `apiConfig.hasDescription`)
6+
7+
Fixes:
8+
- Consider 2xx a successful status (thanks @wyozi)
9+
- GET method query option bug (thanks @rhkdgns95, @SaschaGalley)
10+
- `silent` property missed in `.d.ts` file (thanks @mastermatt)
11+
- axios file upload `formData` type (thanks @guhyeon)
12+
- make property `instance` to public in axios http client (It can be helpful in #226)
13+
- variable name "params" doesn't uniq (thanks @mixalbl4-127 )
14+
15+
Features:
16+
- `--disableProxy` option (thanks @kel666)
17+
- `--extract-request-body` option. Allows to extract request body type to data contract
18+
- Add TSDoc tag for deprecated route (thanks @wyozi)
19+
320

421
# 8.0.3
522

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ Options:
4949
--js generate js api module with declaration file (default: false)
5050
--extract-request-params extract request params to data contract (default: false)
5151
Also combine path params and query params into one object
52+
--extract-request-body extract request body type to data contract (default: false)
5253
--module-name-index <number> determines which path index should be used for routes separation (default: 0)
5354
(example: GET:/fruites/getFruit -> index:0 -> moduleName -> fruites)
5455
--module-name-first-tag splits routes based on the first tag
5556
--modular generate separated files for http client, data contracts, and routes (default: false)
5657
--disableStrictSSL disabled strict SSL (default: false)
58+
--disableProxy disabled proxy (default: false)
5759
--clean-output clean output folder before generate api. WARNING: May cause data loss (default: false)
5860
--axios generate axios http client (default: false)
5961
--single-http-client Ability to send HttpClient instance to Api constructor (default: false)
@@ -96,6 +98,7 @@ generateApi({
9698
generateResponses: true,
9799
toJS: false,
98100
extractRequestParams: false,
101+
extractRequestBody: false,
99102
prettier: {
100103
printWidth: 120,
101104
tabWidth: 2,

index.d.ts

+62-8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ interface GenerateApiParams {
7676
* disabled SSL check
7777
*/
7878
disableStrictSSL?: boolean;
79+
/**
80+
* disabled Proxy
81+
*/
82+
disableProxy?: boolean;
7983
/**
8084
* generate separated files for http client, data contracts, and routes (default: false)
8185
*/
@@ -88,6 +92,10 @@ interface GenerateApiParams {
8892
* prettier configuration
8993
*/
9094
prettier?: object;
95+
/**
96+
* Output only errors to console (default: false)
97+
*/
98+
silent?: boolean;
9199
/**
92100
* default type for empty response schema (default: "void")
93101
*/
@@ -254,6 +262,30 @@ export interface ParsedRoute {
254262
raw: RawRouteInfo;
255263
}
256264

265+
export type ModelType = {
266+
typeIdentifier: string;
267+
name: string;
268+
rawContent: string;
269+
description: string;
270+
content: string;
271+
};
272+
273+
const enum SCHEMA_TYPES {
274+
ARRAY = "array",
275+
OBJECT = "object",
276+
ENUM = "enum",
277+
REF = "$ref",
278+
PRIMITIVE = "primitive",
279+
COMPLEX = "complex",
280+
COMPLEX_ONE_OF = "oneOf",
281+
COMPLEX_ANY_OF = "anyOf",
282+
COMPLEX_ALL_OF = "allOf",
283+
COMPLEX_NOT = "not",
284+
COMPLEX_UNKNOWN = "__unknown",
285+
}
286+
287+
type MAIN_SCHEMA_TYPES = SCHEMA_TYPES.PRIMITIVE | SCHEMA_TYPES.OBJECT | SCHEMA_TYPES.ENUM;
288+
257289
export interface GenerateApiConfiguration {
258290
apiConfig: {
259291
baseUrl: string;
@@ -275,6 +307,7 @@ export interface GenerateApiConfiguration {
275307
moduleNameIndex: number;
276308
moduleNameFirstTag: boolean;
277309
disableStrictSSL: boolean;
310+
disableProxy: boolean;
278311
extractRequestParams: boolean;
279312
fileNames: {
280313
dataContracts: string;
@@ -291,13 +324,7 @@ export interface GenerateApiConfiguration {
291324
};
292325
routeNameDuplicatesMap: Map<string, string>;
293326
};
294-
modelTypes: {
295-
typeIdentifier: string;
296-
name: string;
297-
rawContent: string;
298-
description: string;
299-
content: string;
300-
}[];
327+
modelTypes: ModelType[];
301328
rawModelTypes: SchemaComponent[];
302329
hasFormDataRoutes: boolean;
303330
hasSecurityRoutes: boolean;
@@ -310,7 +337,34 @@ export interface GenerateApiConfiguration {
310337
routes: ParsedRoute[];
311338
}[];
312339
};
313-
utils: typeof import("./src/render/utils");
340+
utils: {
341+
formatDescription: (description: string, inline?: boolean) => string;
342+
internalCase: (value: string) => string;
343+
classNameCase: (value: string) => string;
344+
getInlineParseContent: (
345+
rawTypeData: SchemaComponent["rawTypeData"],
346+
typeName?: string,
347+
) => string;
348+
getParseContent: (rawTypeData: SchemaComponent["rawTypeData"], typeName?: string) => ModelType;
349+
getComponentByRef: (ref: string) => SchemaComponent;
350+
parseSchema: (
351+
rawSchema: string | SchemaComponent["rawTypeData"],
352+
typeName?: string,
353+
formattersMap?: Record<MAIN_SCHEMA_TYPES, (content: ModelType) => string>,
354+
) => ModelType;
355+
formatters: Record<
356+
MAIN_SCHEMA_TYPES,
357+
(content: string | object | string[] | object[]) => string
358+
>;
359+
inlineExtraFormatters: Record<
360+
Exclude<MAIN_SCHEMA_TYPES, SCHEMA_TYPES.PRIMITIVE>,
361+
(schema: ModelType) => string
362+
>;
363+
formatModelName: (name: string) => string;
364+
fmtToJSDocLine: (line: string, params?: { eol?: boolean }) => string;
365+
_: import("lodash").LoDashStatic;
366+
require: (path: string) => unknown;
367+
};
314368
}
315369

316370
export interface GenerateApiOutput {

index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ program
5050
"extract request params to data contract (Also combine path params and query params into one object)",
5151
false,
5252
)
53+
.option("--extract-request-body", "extract request body type to data contract", false)
5354
.option(
5455
"--modular",
5556
"generate separated files for http client, data contracts, and routes",
@@ -63,6 +64,7 @@ program
6364
)
6465
.option("--module-name-first-tag", "splits routes based on the first tag", false)
6566
.option("--disableStrictSSL", "disabled strict SSL", false)
67+
.option("--disableProxy", "disabled proxy", false)
6668
.option("--axios", "generate axios http client", false)
6769
.option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false)
6870
.option("--silent", "Output only errors to console", false)
@@ -92,8 +94,10 @@ const {
9294
moduleNameIndex,
9395
moduleNameFirstTag,
9496
extractRequestParams,
97+
extractRequestBody,
9598
enumNamesAsValues,
9699
disableStrictSSL,
100+
disableProxy,
97101
cleanOutput,
98102
defaultResponse,
99103
singleHttpClient,
@@ -113,7 +117,8 @@ generateApi({
113117
defaultResponseType: defaultResponse,
114118
generateUnionEnums: unionEnums,
115119
generateResponses: responses,
116-
extractRequestParams: extractRequestParams,
120+
extractRequestParams: !!extractRequestParams,
121+
extractRequestBody: !!extractRequestBody,
117122
input: resolve(process.cwd(), path),
118123
output: resolve(process.cwd(), output || "."),
119124
templates,
@@ -123,6 +128,7 @@ generateApi({
123128
moduleNameIndex: +(moduleNameIndex || 0),
124129
moduleNameFirstTag: moduleNameFirstTag,
125130
disableStrictSSL: !!disableStrictSSL,
131+
disableProxy: !!disableProxy,
126132
singleHttpClient: !!singleHttpClient,
127133
cleanOutput: !!cleanOutput,
128134
silent: !!silent,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "8.0.3",
3+
"version": "9.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",

src/apiConfig.js

-40
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
const _ = require("lodash");
2-
const { formatDescription } = require("./common");
3-
const { TS_KEYWORDS } = require("./constants");
42

53
const createApiConfig = (swaggerSchema) => {
64
const { info, servers, host, basePath, externalDocs, tags } = swaggerSchema;
75
const server = (servers && servers[0]) || { url: "" };
86
const { title = "No title", version, description: schemaDescription = "" } = info || {};
97
const { url: serverUrl } = server;
108

11-
const generic = _.compact([
12-
{
13-
name: "SecurityDataType",
14-
defaultValue: TS_KEYWORDS.ANY,
15-
},
16-
]);
17-
18-
const description = _.compact([
19-
`@title ${title || "No title"}`,
20-
version && `@version ${version}`,
21-
serverUrl && `@baseUrl ${serverUrl}`,
22-
_.replace(formatDescription(schemaDescription), /\n/g, "\n * "),
23-
]);
24-
259
return {
2610
info: info || {},
2711
servers: servers || [],
@@ -35,33 +19,9 @@ const createApiConfig = (swaggerSchema) => {
3519
externalDocs,
3620
),
3721
tags: _.compact(tags),
38-
// TODO: unused, remove!
39-
props: _.compact([
40-
{
41-
name: "baseUrl",
42-
optional: true,
43-
type: TS_KEYWORDS.STRING,
44-
},
45-
{
46-
name: "baseApiParams",
47-
optional: true,
48-
type: "RequestParams",
49-
},
50-
{
51-
name: "securityWorker",
52-
optional: true,
53-
type: "(securityData: SecurityDataType) => RequestParams",
54-
},
55-
]),
56-
// TODO: unused in fresh templates, remove in future
57-
generic,
5822
baseUrl: serverUrl,
5923
title,
6024
version,
61-
// TODO: unused, remove
62-
description,
63-
// TODO: unused, remove
64-
hasDescription: !!description.length,
6525
};
6626
};
6727

src/components.js

+39-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@ const _ = require("lodash");
22
const { parseSchema } = require("./schema");
33
const { config } = require("./config");
44

5+
/**
6+
* @typedef {"schemas" | "examples" | "headers" | "parameters" | "requestBodies" | "responses" | "securitySchemes"} ComponentName
7+
* @typedef {object} RawTypeData
8+
* @typedef {{ type, typeIdentifier, name, description, content }} TypeData
9+
*
10+
* @typedef {{
11+
* typeName: string;
12+
* componentName: ComponentName;
13+
* rawTypeData: RawTypeData;
14+
* typeData: TypeData | null;
15+
* }} TypeInfo
16+
*/
17+
18+
/**
19+
*
20+
* @param {ComponentName} componentName
21+
* @param {string} typeName
22+
* @param {RawTypeData} rawTypeData
23+
* @returns {TypeInfo}
24+
*/
525
const createComponent = (componentName, typeName, rawTypeData) => {
626
const $ref = `#/components/${componentName}/${typeName}`;
727

@@ -13,23 +33,15 @@ const createComponent = (componentName, typeName, rawTypeData) => {
1333
typeData: null,
1434
};
1535

16-
return (config.componentsMap[$ref] =
17-
config.hooks.onCreateComponent(componentSchema) || componentSchema);
18-
};
36+
const usageComponent = config.hooks.onCreateComponent(componentSchema) || componentSchema;
1937

20-
/**
21-
*
22-
* @typedef TypeInfo
23-
* {
24-
* typeName: "Foo",
25-
* componentName: "schemas",
26-
* rawTypeData: {...},
27-
* typeData: {...} (result parseSchema())
28-
* }
29-
*/
38+
config.componentsMap[$ref] = usageComponent;
39+
40+
return usageComponent;
41+
};
3042

3143
/**
32-
* @returns {{ "#/components/schemas/Foo": TypeInfo, ... }}
44+
* @returns {{ [key: string]: TypeInfo }}
3345
*/
3446
const createComponentsMap = (components) => {
3547
config.componentsMap = {};
@@ -44,12 +56,19 @@ const createComponentsMap = (components) => {
4456
};
4557

4658
/**
59+
*
60+
* @param {{ [key: string]: TypeInfo }} componentsMap
61+
* @param {ComponentName} componentName
4762
* @returns {TypeInfo[]}
4863
*/
4964
const filterComponentsMap = (componentsMap, componentName) =>
5065
_.filter(componentsMap, (v, ref) => _.startsWith(ref, `#/components/${componentName}`));
5166

52-
/** @returns {{ type, typeIdentifier, name, description, content }} */
67+
/**
68+
*
69+
* @param {TypeInfo} typeInfo
70+
* @returns {TypeData}
71+
*/
5372
const getTypeData = (typeInfo) => {
5473
if (!typeInfo.typeData) {
5574
typeInfo.typeData = parseSchema(typeInfo.rawTypeData, typeInfo.typeName);
@@ -58,6 +77,11 @@ const getTypeData = (typeInfo) => {
5877
return typeInfo.typeData;
5978
};
6079

80+
/**
81+
*
82+
* @param {string} ref
83+
* @returns {TypeInfo | undefined}
84+
*/
6185
const getComponentByRef = (ref) => config.componentsMap[ref];
6286

6387
module.exports = {

src/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { HTTP_CLIENT, TS_KEYWORDS, PRETTIER_OPTIONS } = require("./constants");
2+
const { NameResolver } = require("./utils/resolveName");
23

34
const config = {
45
/** CLI flag */
@@ -32,7 +33,9 @@ const config = {
3233
/** use the first tag for the module name */
3334
moduleNameFirstTag: false,
3435
disableStrictSSL: false,
36+
disableProxy: false,
3537
extractRequestParams: false,
38+
extractRequestBody: false,
3639
fileNames: {
3740
dataContracts: "data-contracts",
3841
routeTypes: "route-types",
@@ -79,6 +82,7 @@ const config = {
7982
silent: false,
8083
typePrefix: "",
8184
typeSuffix: "",
85+
componentTypeNameResolver: new NameResolver([]),
8286
};
8387

8488
/** needs to use data everywhere in project */

0 commit comments

Comments
 (0)