Skip to content

Commit

Permalink
Release 9.0.0 (#245)
Browse files Browse the repository at this point in the history
* Consider 2xx a successful status (#238)

* fix: add array params with key (#237)

* fix: add array query params with key

* chore: use encoded key and value for array params

Co-authored-by: Sascha Galley <[email protected]>

* disableProxy option, added catch to axios get (#232)

* fix(types): add silent as param in declarations (#241)

* axios file upload formData Type (#244)

* axios file upload formData Type

* fix error FormData is not undefined. when serverside request

Co-authored-by: guhyeon <[email protected]>

* 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 (#246)

* chore: refresh generated schemas; fix: form data body for axios request method

* bump: up version to 9.0.0

Co-authored-by: Joonas <[email protected]>
Co-authored-by: Sascha Galley <[email protected]>
Co-authored-by: Sascha Galley <[email protected]>
Co-authored-by: Fabio <[email protected]>
Co-authored-by: Matt R. Wilson <[email protected]>
Co-authored-by: guhyeon <[email protected]>
Co-authored-by: guhyeon <[email protected]>
  • Loading branch information
8 people authored Apr 29, 2021
1 parent 35b08b1 commit b4b11eb
Show file tree
Hide file tree
Showing 97 changed files with 3,501 additions and 1,376 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# next release

# 9.0.0

NOTE: This version is not compatible with previous templates (removed `route.request.params`, `apiConfig.props`, `apiConfig.generic`, `apiConfig.description`, `apiConfig.hasDescription`)

Fixes:
- Consider 2xx a successful status (thanks @wyozi)
- GET method query option bug (thanks @rhkdgns95, @SaschaGalley)
- `silent` property missed in `.d.ts` file (thanks @mastermatt)
- axios file upload `formData` type (thanks @guhyeon)
- make property `instance` to public in axios http client (It can be helpful in #226)
- variable name "params" doesn't uniq (thanks @mixalbl4-127 )

Features:
- `--disableProxy` option (thanks @kel666)
- `--extract-request-body` option. Allows to extract request body type to data contract
- Add TSDoc tag for deprecated route (thanks @wyozi)


# 8.0.3

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ Options:
--js generate js api module with declaration file (default: false)
--extract-request-params extract request params to data contract (default: false)
Also combine path params and query params into one object
--extract-request-body extract request body type to data contract (default: false)
--module-name-index <number> determines which path index should be used for routes separation (default: 0)
(example: GET:/fruites/getFruit -> index:0 -> moduleName -> fruites)
--module-name-first-tag splits routes based on the first tag
--modular generate separated files for http client, data contracts, and routes (default: false)
--disableStrictSSL disabled strict SSL (default: false)
--disableProxy disabled proxy (default: false)
--clean-output clean output folder before generate api. WARNING: May cause data loss (default: false)
--axios generate axios http client (default: false)
--single-http-client Ability to send HttpClient instance to Api constructor (default: false)
Expand Down Expand Up @@ -96,6 +98,7 @@ generateApi({
generateResponses: true,
toJS: false,
extractRequestParams: false,
extractRequestBody: false,
prettier: {
printWidth: 120,
tabWidth: 2,
Expand Down
70 changes: 62 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ interface GenerateApiParams {
* disabled SSL check
*/
disableStrictSSL?: boolean;
/**
* disabled Proxy
*/
disableProxy?: boolean;
/**
* generate separated files for http client, data contracts, and routes (default: false)
*/
Expand All @@ -88,6 +92,10 @@ interface GenerateApiParams {
* prettier configuration
*/
prettier?: object;
/**
* Output only errors to console (default: false)
*/
silent?: boolean;
/**
* default type for empty response schema (default: "void")
*/
Expand Down Expand Up @@ -254,6 +262,30 @@ export interface ParsedRoute {
raw: RawRouteInfo;
}

export type ModelType = {
typeIdentifier: string;
name: string;
rawContent: string;
description: string;
content: string;
};

const enum SCHEMA_TYPES {
ARRAY = "array",
OBJECT = "object",
ENUM = "enum",
REF = "$ref",
PRIMITIVE = "primitive",
COMPLEX = "complex",
COMPLEX_ONE_OF = "oneOf",
COMPLEX_ANY_OF = "anyOf",
COMPLEX_ALL_OF = "allOf",
COMPLEX_NOT = "not",
COMPLEX_UNKNOWN = "__unknown",
}

type MAIN_SCHEMA_TYPES = SCHEMA_TYPES.PRIMITIVE | SCHEMA_TYPES.OBJECT | SCHEMA_TYPES.ENUM;

export interface GenerateApiConfiguration {
apiConfig: {
baseUrl: string;
Expand All @@ -275,6 +307,7 @@ export interface GenerateApiConfiguration {
moduleNameIndex: number;
moduleNameFirstTag: boolean;
disableStrictSSL: boolean;
disableProxy: boolean;
extractRequestParams: boolean;
fileNames: {
dataContracts: string;
Expand All @@ -291,13 +324,7 @@ export interface GenerateApiConfiguration {
};
routeNameDuplicatesMap: Map<string, string>;
};
modelTypes: {
typeIdentifier: string;
name: string;
rawContent: string;
description: string;
content: string;
}[];
modelTypes: ModelType[];
rawModelTypes: SchemaComponent[];
hasFormDataRoutes: boolean;
hasSecurityRoutes: boolean;
Expand All @@ -310,7 +337,34 @@ export interface GenerateApiConfiguration {
routes: ParsedRoute[];
}[];
};
utils: typeof import("./src/render/utils");
utils: {
formatDescription: (description: string, inline?: boolean) => string;
internalCase: (value: string) => string;
classNameCase: (value: string) => string;
getInlineParseContent: (
rawTypeData: SchemaComponent["rawTypeData"],
typeName?: string,
) => string;
getParseContent: (rawTypeData: SchemaComponent["rawTypeData"], typeName?: string) => ModelType;
getComponentByRef: (ref: string) => SchemaComponent;
parseSchema: (
rawSchema: string | SchemaComponent["rawTypeData"],
typeName?: string,
formattersMap?: Record<MAIN_SCHEMA_TYPES, (content: ModelType) => string>,
) => ModelType;
formatters: Record<
MAIN_SCHEMA_TYPES,
(content: string | object | string[] | object[]) => string
>;
inlineExtraFormatters: Record<
Exclude<MAIN_SCHEMA_TYPES, SCHEMA_TYPES.PRIMITIVE>,
(schema: ModelType) => string
>;
formatModelName: (name: string) => string;
fmtToJSDocLine: (line: string, params?: { eol?: boolean }) => string;
_: import("lodash").LoDashStatic;
require: (path: string) => unknown;
};
}

export interface GenerateApiOutput {
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ program
"extract request params to data contract (Also combine path params and query params into one object)",
false,
)
.option("--extract-request-body", "extract request body type to data contract", false)
.option(
"--modular",
"generate separated files for http client, data contracts, and routes",
Expand All @@ -63,6 +64,7 @@ program
)
.option("--module-name-first-tag", "splits routes based on the first tag", false)
.option("--disableStrictSSL", "disabled strict SSL", false)
.option("--disableProxy", "disabled proxy", false)
.option("--axios", "generate axios http client", false)
.option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false)
.option("--silent", "Output only errors to console", false)
Expand Down Expand Up @@ -92,8 +94,10 @@ const {
moduleNameIndex,
moduleNameFirstTag,
extractRequestParams,
extractRequestBody,
enumNamesAsValues,
disableStrictSSL,
disableProxy,
cleanOutput,
defaultResponse,
singleHttpClient,
Expand All @@ -113,7 +117,8 @@ generateApi({
defaultResponseType: defaultResponse,
generateUnionEnums: unionEnums,
generateResponses: responses,
extractRequestParams: extractRequestParams,
extractRequestParams: !!extractRequestParams,
extractRequestBody: !!extractRequestBody,
input: resolve(process.cwd(), path),
output: resolve(process.cwd(), output || "."),
templates,
Expand All @@ -123,6 +128,7 @@ generateApi({
moduleNameIndex: +(moduleNameIndex || 0),
moduleNameFirstTag: moduleNameFirstTag,
disableStrictSSL: !!disableStrictSSL,
disableProxy: !!disableProxy,
singleHttpClient: !!singleHttpClient,
cleanOutput: !!cleanOutput,
silent: !!silent,
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger-typescript-api",
"version": "8.0.3",
"version": "9.0.0",
"description": "Generate typescript/javascript api from swagger schema",
"scripts": {
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
Expand Down
40 changes: 0 additions & 40 deletions src/apiConfig.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
const _ = require("lodash");
const { formatDescription } = require("./common");
const { TS_KEYWORDS } = require("./constants");

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

const generic = _.compact([
{
name: "SecurityDataType",
defaultValue: TS_KEYWORDS.ANY,
},
]);

const description = _.compact([
`@title ${title || "No title"}`,
version && `@version ${version}`,
serverUrl && `@baseUrl ${serverUrl}`,
_.replace(formatDescription(schemaDescription), /\n/g, "\n * "),
]);

return {
info: info || {},
servers: servers || [],
Expand All @@ -35,33 +19,9 @@ const createApiConfig = (swaggerSchema) => {
externalDocs,
),
tags: _.compact(tags),
// TODO: unused, remove!
props: _.compact([
{
name: "baseUrl",
optional: true,
type: TS_KEYWORDS.STRING,
},
{
name: "baseApiParams",
optional: true,
type: "RequestParams",
},
{
name: "securityWorker",
optional: true,
type: "(securityData: SecurityDataType) => RequestParams",
},
]),
// TODO: unused in fresh templates, remove in future
generic,
baseUrl: serverUrl,
title,
version,
// TODO: unused, remove
description,
// TODO: unused, remove
hasDescription: !!description.length,
};
};

Expand Down
54 changes: 39 additions & 15 deletions src/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ const _ = require("lodash");
const { parseSchema } = require("./schema");
const { config } = require("./config");

/**
* @typedef {"schemas" | "examples" | "headers" | "parameters" | "requestBodies" | "responses" | "securitySchemes"} ComponentName
* @typedef {object} RawTypeData
* @typedef {{ type, typeIdentifier, name, description, content }} TypeData
*
* @typedef {{
* typeName: string;
* componentName: ComponentName;
* rawTypeData: RawTypeData;
* typeData: TypeData | null;
* }} TypeInfo
*/

/**
*
* @param {ComponentName} componentName
* @param {string} typeName
* @param {RawTypeData} rawTypeData
* @returns {TypeInfo}
*/
const createComponent = (componentName, typeName, rawTypeData) => {
const $ref = `#/components/${componentName}/${typeName}`;

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

return (config.componentsMap[$ref] =
config.hooks.onCreateComponent(componentSchema) || componentSchema);
};
const usageComponent = config.hooks.onCreateComponent(componentSchema) || componentSchema;

/**
*
* @typedef TypeInfo
* {
* typeName: "Foo",
* componentName: "schemas",
* rawTypeData: {...},
* typeData: {...} (result parseSchema())
* }
*/
config.componentsMap[$ref] = usageComponent;

return usageComponent;
};

/**
* @returns {{ "#/components/schemas/Foo": TypeInfo, ... }}
* @returns {{ [key: string]: TypeInfo }}
*/
const createComponentsMap = (components) => {
config.componentsMap = {};
Expand All @@ -44,12 +56,19 @@ const createComponentsMap = (components) => {
};

/**
*
* @param {{ [key: string]: TypeInfo }} componentsMap
* @param {ComponentName} componentName
* @returns {TypeInfo[]}
*/
const filterComponentsMap = (componentsMap, componentName) =>
_.filter(componentsMap, (v, ref) => _.startsWith(ref, `#/components/${componentName}`));

/** @returns {{ type, typeIdentifier, name, description, content }} */
/**
*
* @param {TypeInfo} typeInfo
* @returns {TypeData}
*/
const getTypeData = (typeInfo) => {
if (!typeInfo.typeData) {
typeInfo.typeData = parseSchema(typeInfo.rawTypeData, typeInfo.typeName);
Expand All @@ -58,6 +77,11 @@ const getTypeData = (typeInfo) => {
return typeInfo.typeData;
};

/**
*
* @param {string} ref
* @returns {TypeInfo | undefined}
*/
const getComponentByRef = (ref) => config.componentsMap[ref];

module.exports = {
Expand Down
4 changes: 4 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { HTTP_CLIENT, TS_KEYWORDS, PRETTIER_OPTIONS } = require("./constants");
const { NameResolver } = require("./utils/resolveName");

const config = {
/** CLI flag */
Expand Down Expand Up @@ -32,7 +33,9 @@ const config = {
/** use the first tag for the module name */
moduleNameFirstTag: false,
disableStrictSSL: false,
disableProxy: false,
extractRequestParams: false,
extractRequestBody: false,
fileNames: {
dataContracts: "data-contracts",
routeTypes: "route-types",
Expand Down Expand Up @@ -79,6 +82,7 @@ const config = {
silent: false,
typePrefix: "",
typeSuffix: "",
componentTypeNameResolver: new NameResolver([]),
};

/** needs to use data everywhere in project */
Expand Down
Loading

0 comments on commit b4b11eb

Please sign in to comment.