Skip to content

fix incorrect working --route-types with --modular option #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "test:--templates"]
},
{
"name": "Debug test-all",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "test-all"]
}
]
}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# next release

BREAKING_CHANGES:
- format `namespace` name in `--route-types` as camelCase with upper first capitalized letter
`foo_bar` -> `FooBar`

Fixes:
- Incorrect working the `--route-types` option with `--modular` option (route types should be splitted on files)

# 6.4.2

Fixes:
Expand Down
8 changes: 7 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ const config = {
custom: "",
},
/** Record<templateName, templateContent> */
templatesToRender: {},
templatesToRender: {
api: "",
dataContracts: "",
httpClient: "",
routeTypes: "",
routeName: "",
},
toJS: false,
silent: false,
typePrefix: "",
Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ const { createComponentsMap, filterComponentsMap } = require("./components");
const { createFile, pathIsExist, pathIsDir, createDir, cleanDir } = require("./files");
const { addToConfig, config } = require("./config");
const { getTemplates, getTemplatePaths } = require("./templates");
const { formatModelName } = require("./modelNames");
const constants = require("./constants");
const { generateOutputFiles } = require("./output");

const { SCHEMA_TYPES } = constants;

module.exports = {
constants: constants,
generateApi: ({
input,
output,
Expand Down Expand Up @@ -116,10 +120,13 @@ module.exports = {
const hasQueryRoutes = routes.some((route) => route.hasQuery);
const hasFormDataRoutes = routes.some((route) => route.hasFormDataParams);

const componentSchemas = filterComponentsMap(componentsMap, "schemas");

const rawConfiguration = {
apiConfig: createApiConfig(usageSchema, hasSecurityRoutes),
config,
modelTypes: _.map(filterComponentsMap(componentsMap, "schemas"), prepareModelType),
modelTypes: _.map(componentSchemas, prepareModelType),
rawModelTypes: componentSchemas,
hasFormDataRoutes,
hasSecurityRoutes,
hasQueryRoutes,
Expand Down
4 changes: 0 additions & 4 deletions src/modelTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ const prepareModelType = (typeInfo) => {
const typeData = getTypeData(typeInfo);
let { typeIdentifier, name: originalName, content, type, description } = typeData;

if (config.generateUnionEnums && typeIdentifier === "enum") {
typeIdentifier = "type";
}

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

Expand Down
74 changes: 49 additions & 25 deletions src/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,61 @@ const createMultipleFileInfos = (templatesToRender, configuration) => {
const { fileNames, generateRouteTypes, generateClient } = configuration.config;
const modularApiFileInfos = [];

if (generateClient && routes.$outOfModule) {
const outOfModuleApiContent = renderTemplate(templatesToRender.api, {
...configuration,
route: configuration.routes.$outOfModule,
});

modularApiFileInfos.push(
createFileInfo(configuration, fileNames.outOfModuleApi, outOfModuleApiContent),
);
if (routes.$outOfModule) {
if (generateRouteTypes) {
const outOfModuleRouteContent = renderTemplate(templatesToRender.routeTypes, {
...configuration,
route: configuration.routes.$outOfModule,
});

modularApiFileInfos.push(
createFileInfo(configuration, fileNames.outOfModuleApi, outOfModuleRouteContent),
);
}
if (generateClient) {
const outOfModuleApiContent = renderTemplate(templatesToRender.api, {
...configuration,
route: configuration.routes.$outOfModule,
});

modularApiFileInfos.push(
createFileInfo(configuration, fileNames.outOfModuleApi, outOfModuleApiContent),
);
}
}

if (generateClient && routes.combined) {
if (routes.combined) {
modularApiFileInfos.push(
..._.reduce(
routes.combined,
(apiFileInfos, route) => {
const apiModuleContent = renderTemplate(templatesToRender.api, {
...configuration,
route,
});

return [
...apiFileInfos,
createFileInfo(configuration, classNameCase(route.moduleName), apiModuleContent),
];
if (generateRouteTypes) {
const routeModuleContent = renderTemplate(templatesToRender.routeTypes, {
...configuration,
route,
});

apiFileInfos.push(
createFileInfo(
configuration,
classNameCase(`${route.moduleName}_Route`),
routeModuleContent,
),
);
}

if (generateClient) {
const apiModuleContent = renderTemplate(templatesToRender.api, {
...configuration,
route,
});

apiFileInfos.push(
createFileInfo(configuration, classNameCase(route.moduleName), apiModuleContent),
);
}

return apiFileInfos;
},
[],
),
Expand All @@ -84,12 +114,6 @@ const createMultipleFileInfos = (templatesToRender, configuration) => {
fileNames.dataContracts,
renderTemplate(templatesToRender.dataContracts, configuration),
),
generateRouteTypes &&
createFileInfo(
configuration,
fileNames.routeTypes,
renderTemplate(templatesToRender.routeTypes, configuration),
),
generateClient &&
createFileInfo(
configuration,
Expand Down
2 changes: 2 additions & 0 deletions src/render/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require("path");
const { classNameCase, formatDescription, internalCase } = require("../../common");
const { getComponentByRef } = require("../../components");
const { config } = require("../../config");
const { formatModelName } = require("../../modelNames");
const { getInlineParseContent, getParseContent, parseSchema } = require("../../schema");
const { formatters, inlineExtraFormatters } = require("../../typeFormatters");

Expand All @@ -16,6 +17,7 @@ module.exports = {
parseSchema,
formatters,
inlineExtraFormatters,
formatModelName,
fmtToJSDocLine: require("./fmtToJSDocLine"),
_: _,
require: (packageOrPath) => {
Expand Down
23 changes: 15 additions & 8 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ const getTypeAlias = (rawSchema) => {
};

const getEnumNames = (schema) => {
return schema["x-enumNames"] || schema["xEnumNames"] || schema["x-enumnames"];
return (
schema["x-enumNames"] ||
schema["xEnumNames"] ||
schema["x-enumnames"] ||
schema["x-enum-varnames"]
);
};

const getInternalSchemaType = (schema) => {
Expand Down Expand Up @@ -126,6 +131,8 @@ const getObjectTypeContent = (schema) => {
const required = isRequired(property, name, requiredProperties);
const rawTypeData = _.get(getRefType(property), "rawTypeData", {});
const nullable = !!(rawTypeData.nullable || property.nullable);
const fieldName = isValidName(name) ? name : `"${name}"`;
const fieldValue = getInlineParseContent(property);

return {
$$raw: property,
Expand All @@ -146,12 +153,9 @@ const getObjectTypeContent = (schema) => {
]).join("\n"),
isRequired: required,
isNullable: nullable,
field: _.compact([
isValidName(name) ? name : `"${name}"`,
!required && "?",
": ",
getInlineParseContent(property),
]).join(""),
name: fieldName,
value: fieldValue,
field: _.compact([fieldName, !required && "?", ": ", fieldValue]).join(""),
};
});

Expand Down Expand Up @@ -272,7 +276,10 @@ const schemaParsers = {
schemaType: SCHEMA_TYPES.ENUM,
type: SCHEMA_TYPES.ENUM,
keyType: keyType,
typeIdentifier: !enumNames && isIntegerEnum ? TS_KEYWORDS.TYPE : TS_KEYWORDS.ENUM,
typeIdentifier:
config.generateUnionEnums || (!enumNames && isIntegerEnum)
? TS_KEYWORDS.TYPE
: TS_KEYWORDS.ENUM,
name: typeName,
description: formatDescription(schema.description),
content,
Expand Down
4 changes: 2 additions & 2 deletions templates/default/api.eta
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ export class Api<SecurityDataType extends unknown><% if (!config.singleHttpClien

<% routes.outOfModule && routes.outOfModule.forEach((route) => { %>

<%~ includeFile('./procedure-call.eta', { route, utils, config }) %>
<%~ includeFile('./procedure-call.eta', { ...it, route }) %>

<% }) %>

<% routes.combined && routes.combined.forEach(({ routes = [], moduleName }) => { %>
<%~ moduleName %> = {
<% routes.forEach((route) => { %>

<%~ includeFile('./procedure-call.eta', { route, utils, config }) %>
<%~ includeFile('./procedure-call.eta', { ...it, route }) %>

<% }) %>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%
const { utils, config, routes, modelTypes } = it;
const { _ } = utils;
const { _, classNameCase } = utils;
const dataContracts = config.modular ? _.map(modelTypes, "name") : [];
%>

Expand All @@ -14,14 +14,14 @@ import { <%~ dataContracts.join(", ") %> } from "./<%~ config.fileNames.dataCont

<% routes.outOfModule && routes.outOfModule.forEach(({ routes = [] }) => { %>
<% routes.forEach((route) => { %>
<%~ includeFile('@base/route-type.eta', { route, utils, config }) %>
<%~ includeFile('@base/route-type.eta', { ...it, route }) %>
<% }) %>
<% }) %>

<% routes.combined && routes.combined.forEach(({ routes = [], moduleName }) => { %>
export namespace <%~ moduleName %> {
export namespace <%~ classNameCase(moduleName) %> {
<% routes.forEach((route) => { %>
<%~ includeFile('@base/route-type.eta', { route, utils, config }) %>
<%~ includeFile('@base/route-type.eta', { ...it, route }) %>
<% }) %>
}

Expand Down
2 changes: 1 addition & 1 deletion templates/modular/api.eta
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export class <%= apiClassName %><SecurityDataType = unknown><% if (!config.singl
<% } %>

<% routes.forEach((route) => { %>
<%~ includeFile('./procedure-call.eta', { route, utils, config }) %>
<%~ includeFile('./procedure-call.eta', { ...it, route }) %>
<% }) %>
}
18 changes: 18 additions & 0 deletions templates/modular/route-types.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%
const { utils, config, route, modelTypes } = it;
const { _, classNameCase } = utils;
const { routes, moduleName } = route;
const dataContracts = config.modular ? _.map(modelTypes, "name") : [];

%>
<% if (dataContracts.length) { %>
import { <%~ dataContracts.join(", ") %> } from "./<%~ config.fileNames.dataContracts %>"
<% } %>

export namespace <%~ classNameCase(moduleName) %> {
<% _.forEach(routes, (route) => { %>

<%~ includeFile('@base/route-type.eta', { ...it, route }) %>

<% }) %>
}
Loading