Skip to content

Commit

Permalink
Release 6.3.0 (#196)
Browse files Browse the repository at this point in the history
* feat: --type-suffix, --type-prefix options (issue #191, thanks @the-ult); feat: onFormatTypeName hook

* bump: up version to 6.3.0
  • Loading branch information
js2me authored Mar 4, 2021
1 parent c9753aa commit 7d38f20
Show file tree
Hide file tree
Showing 15 changed files with 30,033 additions and 34 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# next release

# 6.3.0

Features:
- `--type-suffix` option. Allows to set suffix for data contract name. (issue #191, thanks @the-ult)
- `--type-prefix` option. Allows to set prefix for data contract name. (issue #191, thanks @the-ult)
Examples [here](./spec/typeSuffixPrefix/schema.ts)
- `onFormatTypeName(usageTypeName, rawTypeName)` hook. Allow to format data contract names as you want.

Internal:
- rename and split `checkAndRenameModelName` -> `formatModelName`, `fixModelName`

# 6.2.1

Fixes:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Options:
--single-http-client Ability to send HttpClient instance to Api constructor (default: false)
--silent Output only errors to console (default: false)
--default-response <type> default type for empty response schema (default: "void")
--type-prefix <string> data contract name prefix (default: "")
--type-suffix <string> data contract name suffix (default: "")
-h, --help display help for command
```

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ interface GenerateApiParams {
onCreateRequestParams?: (
rawType: SchemaComponent["rawTypeData"],
) => SchemaComponent["rawTypeData"] | void;
onFormatTypeName?: (typeName: string, rawTypeName?: string) => string | void;
}>;
/**
* extra templates
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ program
.option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false)
.option("--silent", "Output only errors to console", false)
.option("--default-response <type>", "default type for empty response schema", TS_KEYWORDS.VOID)
.option("--type-prefix <string>", "data contract name prefix", "")
.option("--type-suffix <string>", "data contract name suffix", "")
.option(
"--clean-output",
"clean output folder before generate api. WARNING: May cause data loss",
Expand Down Expand Up @@ -97,6 +99,8 @@ const {
singleHttpClient,
axios,
silent,
typePrefix,
typeSuffix,
} = program;

generateApi({
Expand All @@ -122,4 +126,6 @@ generateApi({
singleHttpClient: !!singleHttpClient,
cleanOutput: !!cleanOutput,
silent: !!silent,
typePrefix,
typeSuffix,
});
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.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swagger-typescript-api",
"version": "6.2.1",
"version": "6.3.0",
"description": "Create typescript api module from swagger schema",
"scripts": {
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts --extract-request-params --enum-names-as-values",
Expand Down Expand Up @@ -34,6 +34,7 @@
"test:--js--axios": "node tests/spec/jsAxios/test.js",
"test:--axios": "node tests/spec/axios/test.js",
"test:--axios--single-http-client": "node tests/spec/axiosSingleHttpClient/test.js",
"test:--type-suffix--type-prefix": "node tests/spec/typeSuffixPrefix/test.js",
"test:partialBaseTemplate": "node tests/spec/partialBaseTemplate/test.js",
"test:partialDefaultTemplate": "node tests/spec/partialDefaultTemplate/test.js"
},
Expand Down
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const config = {
onPrepareConfig: (apiConfig) => apiConfig,
onCreateRequestParams: (rawType) => {},
onCreateRouteName: () => {},
onFormatTypeName: (typeName, rawTypeName) => {},
},
defaultResponseType: TS_KEYWORDS.VOID,
singleHttpClient: false,
Expand All @@ -69,6 +70,8 @@ const config = {
templatesToRender: {},
toJS: false,
silent: false,
typePrefix: "",
typeSuffix: "",
};

/** needs to use data everywhere in project */
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ module.exports = {
disableStrictSSL = config.disableStrictSSL,
cleanOutput,
silent = config.silent,
typePrefix = config.typePrefix,
typeSuffix = config.typeSuffix,
}) =>
new Promise((resolve, reject) => {
addToConfig({
Expand All @@ -71,6 +73,8 @@ module.exports = {
constants,
silent,
toJS: translateToJavaScript,
typePrefix,
typeSuffix,
});
(spec ? convertSwaggerObject(spec) : getSwaggerObject(input, url, disableStrictSSL))
.then(({ usageSchema, originalSchema }) => {
Expand Down
49 changes: 30 additions & 19 deletions src/modelNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,7 @@ const isValidName = (name) => /^([A-Za-z$_]{1,})$/g.test(name);

const formattedModelNamesMap = new Map();

const checkAndRenameModelName = (name) => {
if (typeof name !== "string") {
if (!config.silent) console.warn("🔨 wrong name of the model name", name, config.silent);

return name;
}

if (formattedModelNamesMap.has(name)) {
return formattedModelNamesMap.get(name);
}

if (/^([A-Z_]{1,})$/g.test(name)) {
return name;
}

const fixModelName = (name) => {
if (!isValidName(name)) {
if (!/^[a-zA-Z_$]/g.test(name)) {
name = `Type ${name}`;
Expand All @@ -37,14 +23,39 @@ const checkAndRenameModelName = (name) => {
if (name.includes("-")) name = _.startCase(name).replace(/ /g, "");
}

const formattedModelName = _.replace(_.startCase(name), /\s/g, "");
return name;
};

const formatModelName = (name) => {
if (typeof name !== "string") {
if (!config.silent) console.warn("🔨 wrong name of the model name", name);

return name;
}

if (/^([A-Z_]{1,})$/g.test(name)) {
return name;
}

if (formattedModelNamesMap.has(name)) {
return formattedModelNamesMap.get(name);
}

const fixedModelName = fixModelName(name);

const formattedModelName = _.replace(
_.startCase(`${config.typePrefix}_${fixedModelName}_${config.typeSuffix}`),
/\s/g,
"",
);
const modelName = config.hooks.onFormatTypeName(formattedModelName, name) || formattedModelName;

formattedModelNamesMap.set(name, formattedModelName);
formattedModelNamesMap.set(name, modelName);

return formattedModelName;
return modelName;
};

module.exports = {
checkAndRenameModelName,
formatModelName: formatModelName,
isValidName,
};
4 changes: 2 additions & 2 deletions src/modelTypes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { formatters } = require("./typeFormatters");
const { checkAndRenameModelName } = require("./modelNames");
const { formatModelName } = require("./modelNames");
const { config } = require("./config");
const { getTypeData } = require("./components");

Expand All @@ -12,7 +12,7 @@ const prepareModelType = (typeInfo) => {
}

const resultContent = formatters[type] ? formatters[type](content) : content;
const name = checkAndRenameModelName(originalName);
const name = formatModelName(originalName);

return {
typeIdentifier,
Expand Down
10 changes: 5 additions & 5 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
getInlineParseContent,
checkAndAddNull,
} = require("./schema");
const { checkAndRenameModelName } = require("./modelNames");
const { formatModelName } = require("./modelNames");
const {
DEFAULT_BODY_ARG_NAME,
SUCCESS_RESPONSE_STATUS_RANGE,
Expand Down Expand Up @@ -49,15 +49,15 @@ const getTypeFromRequestInfo = ({ requestInfo, parsedSchemas, operationId, defau
const content = getInlineParseContent(schema, "none");
const foundedSchemaByName = _.find(
parsedSchemas,
(parsedSchema) => checkAndRenameModelName(parsedSchema.name) === content,
(parsedSchema) => formatModelName(parsedSchema.name) === content,
);
const foundSchemaByContent = _.find(parsedSchemas, (parsedSchema) =>
_.isEqual(parsedSchema.content, content),
);

const foundSchema = foundedSchemaByName || foundSchemaByContent;

return foundSchema ? checkAndRenameModelName(foundSchema.name) : content;
return foundSchema ? formatModelName(foundSchema.name) : content;
}

if (refTypeInfo) {
Expand All @@ -67,12 +67,12 @@ const getTypeFromRequestInfo = ({ requestInfo, parsedSchemas, operationId, defau
// TODO:HACK fix problem of swagger2opeanpi
const typeNameWithoutOpId = _.replace(refTypeInfo.typeName, operationId, "");
if (_.find(parsedSchemas, (schema) => schema.name === typeNameWithoutOpId)) {
return checkAndRenameModelName(typeNameWithoutOpId);
return formatModelName(typeNameWithoutOpId);
}

switch (refTypeInfo.componentName) {
case "schemas":
return checkAndRenameModelName(refTypeInfo.typeName);
return formatModelName(refTypeInfo.typeName);
case "responses":
case "requestBodies":
return getInlineParseContent(getSchemaFromRequestType(refTypeInfo.rawTypeData), "none");
Expand Down
11 changes: 5 additions & 6 deletions src/schema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _ = require("lodash");
const { inlineExtraFormatters } = require("./typeFormatters");
const { isValidName, checkAndRenameModelName } = require("./modelNames");
const { isValidName, formatModelName } = require("./modelNames");
const { formatDescription, internalCase } = require("./common");
const { JS_PRIMITIVE_TYPES, JS_EMPTY_TYPES, TS_KEYWORDS, SCHEMA_TYPES } = require("./constants");
const { config } = require("./config");
Expand Down Expand Up @@ -95,7 +95,7 @@ const getType = (schema) => {
const refTypeInfo = getRefType(schema);

if (refTypeInfo) {
return checkAndAddNull(schema, checkAndRenameModelName(refTypeInfo.typeName));
return checkAndAddNull(schema, formatModelName(refTypeInfo.typeName));
}

const primitiveType = getTypeAlias(schema);
Expand Down Expand Up @@ -230,15 +230,14 @@ const schemaParsers = {

if (enumNamesAsValues && _.size(enumNames)) {
content = _.map(enumNames, (enumName, index) => ({
key: checkAndRenameModelName(enumName),
key: formatModelName(enumName),
type: TS_KEYWORDS.STRING,
value: `"${enumName}"`,
}));
} else if (_.size(enumNames) > _.size(schema.enum)) {
content = _.map(enumNames, (enumName, index) => ({
key:
(enumNames && enumNames[index]) ||
(isIntegerEnum ? enumName : checkAndRenameModelName(enumName)),
(enumNames && enumNames[index]) || (isIntegerEnum ? enumName : formatModelName(enumName)),
type: keyType,
value: enumName === null ? enumName : isIntegerEnum ? `${enumName}` : `"${enumName}"`,
}));
Expand All @@ -247,7 +246,7 @@ const schemaParsers = {
const enumName = enumNames && enumNames[index];

return {
key: enumName || (isIntegerEnum ? key : checkAndRenameModelName(key)),
key: enumName || (isIntegerEnum ? key : formatModelName(key)),
type: keyType,
value: key === null ? key : isIntegerEnum ? `${key}` : `"${key}"`,
};
Expand Down
Loading

0 comments on commit 7d38f20

Please sign in to comment.