You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

swagger-typescript-api

Package Overview
Dependencies
Maintainers
1
Versions
154
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swagger-typescript-api - npm Package Compare versions

Comparing version

to
11.0.0--beta-2

src/commands/generate-templates.js

101

index.js

@@ -205,51 +205,56 @@ #!/usr/bin/env node

switch (command) {
case null: {
await generateApi({
name: options.name,
url: options.path,
generateRouteTypes: options.routeTypes,
generateClient: !!(options.axios || options.client),
httpClientType: options.axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
defaultResponseAsSuccess: options.defaultAsSuccess,
defaultResponseType: options.defaultResponse,
unwrapResponseData: options.unwrapResponseData,
disableThrowOnError: options.disableThrowOnError,
sortTypes: options.sortTypes,
generateUnionEnums: options.unionEnums,
addReadonly: options.addReadonly,
generateResponses: options.responses,
extractRequestParams: !!options.extractRequestParams,
extractRequestBody: !!options.extractRequestBody,
extractResponseBody: !!options.extractResponseBody,
extractResponseError: !!options.extractResponseError,
input: resolve(process.cwd(), options.path),
output: resolve(process.cwd(), options.output || "."),
templates: options.templates,
modular: !!options.modular,
toJS: !!options.js,
enumNamesAsValues: options.enumNamesAsValues,
moduleNameIndex: +(options.moduleNameIndex || 0),
moduleNameFirstTag: options.moduleNameFirstTag,
disableStrictSSL: !!options.disableStrictSSL,
disableProxy: !!options.disableProxy,
singleHttpClient: !!options.singleHttpClient,
cleanOutput: !!options.cleanOutput,
silent: !!options.silent,
typePrefix: options.typePrefix,
typeSuffix: options.typeSuffix,
patch: !!options.patch,
apiClassName: options.apiClassName,
debug: options.debug,
anotherArrayType: options.anotherArrayType,
});
break;
try {
switch (command) {
case null: {
await generateApi({
name: options.name,
url: options.path,
generateRouteTypes: options.routeTypes,
generateClient: !!(options.axios || options.client),
httpClientType: options.axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
defaultResponseAsSuccess: options.defaultAsSuccess,
defaultResponseType: options.defaultResponse,
unwrapResponseData: options.unwrapResponseData,
disableThrowOnError: options.disableThrowOnError,
sortTypes: options.sortTypes,
generateUnionEnums: options.unionEnums,
addReadonly: options.addReadonly,
generateResponses: options.responses,
extractRequestParams: !!options.extractRequestParams,
extractRequestBody: !!options.extractRequestBody,
extractResponseBody: !!options.extractResponseBody,
extractResponseError: !!options.extractResponseError,
input: resolve(process.cwd(), options.path),
output: resolve(process.cwd(), options.output || "."),
templates: options.templates,
modular: !!options.modular,
toJS: !!options.js,
enumNamesAsValues: options.enumNamesAsValues,
moduleNameIndex: +(options.moduleNameIndex || 0),
moduleNameFirstTag: options.moduleNameFirstTag,
disableStrictSSL: !!options.disableStrictSSL,
disableProxy: !!options.disableProxy,
singleHttpClient: !!options.singleHttpClient,
cleanOutput: !!options.cleanOutput,
silent: !!options.silent,
typePrefix: options.typePrefix,
typeSuffix: options.typeSuffix,
patch: !!options.patch,
apiClassName: options.apiClassName,
debug: options.debug,
anotherArrayType: options.anotherArrayType,
});
break;
}
case "generate-templates": {
console.info("todo");
break;
}
default: {
break;
}
}
case "generate-templates": {
console.info("todo");
break;
}
default: {
break;
}
} catch (e) {
console.error(e);
process.exit(1);
}

@@ -256,0 +261,0 @@ };

{
"name": "swagger-typescript-api",
"version": "11.0.0--beta-1",
"version": "11.0.0--beta-2",
"description": "Generate typescript/javascript api from swagger schema",

@@ -5,0 +5,0 @@ "scripts": {

@@ -532,9 +532,4 @@ const _ = require("lodash");

extractResponseBodyIfItNeeded = (routeInfo, responseBodyInfo, routeParams, rawRouteInfo, routeName) => {
if (
this.config.extractResponseBody &&
responseBodyInfo.responses.length &&
responseBodyInfo.success &&
responseBodyInfo.success.schema
) {
extractResponseBodyIfItNeeded = (routeInfo, responseBodyInfo, routeName) => {
if (responseBodyInfo.responses.length && responseBodyInfo.success && responseBodyInfo.success.schema) {
const typeName = this.config.componentTypeNameResolver.resolve([

@@ -556,3 +551,3 @@ pascalCase(`${routeName.usage} Data`),

if (idx > -1) {
responseBodyInfo.responses[idx] = successResponse.schema;
_.assign(responseBodyInfo.responses[idx], successResponse.schema);
}

@@ -563,9 +558,4 @@ }

extractResponseErrorIfItNeeded = (routeInfo, responseBodyInfo, routeParams, rawRouteInfo, routeName) => {
if (
this.config.extractResponseError &&
responseBodyInfo.responses.length &&
responseBodyInfo.error.schemas &&
responseBodyInfo.error.schemas.length
) {
extractResponseErrorIfItNeeded = (routeInfo, responseBodyInfo, routeName) => {
if (responseBodyInfo.responses.length && responseBodyInfo.error.schemas && responseBodyInfo.error.schemas.length) {
const typeName = this.config.componentTypeNameResolver.resolve([

@@ -601,4 +591,4 @@ pascalCase(`${routeName.usage} Error`),

getRouteName = (routeInfo) => {
const { moduleName } = routeInfo;
getRouteName = (rawRouteInfo) => {
const { moduleName } = rawRouteInfo;
const { routeNameDuplicatesMap, templatesToRender } = this.config;

@@ -608,6 +598,6 @@ const routeNameTemplate = templatesToRender.routeName;

const routeNameFromTemplate = this.templates.renderTemplate(routeNameTemplate, {
routeInfo: routeInfo,
routeInfo: rawRouteInfo,
});
const routeName = this.config.hooks.onFormatRouteName(routeInfo, routeNameFromTemplate) || routeNameFromTemplate;
const routeName = this.config.hooks.onFormatRouteName(rawRouteInfo, routeNameFromTemplate) || routeNameFromTemplate;

@@ -637,3 +627,3 @@ const duplicateIdentifier = `${moduleName}|${routeName}`;

return this.config.hooks.onCreateRouteName(routeNameInfo, routeInfo) || routeNameInfo;
return this.config.hooks.onCreateRouteName(routeNameInfo, rawRouteInfo) || routeNameInfo;
};

@@ -684,2 +674,3 @@

const rawRouteInfo = {
...otherInfo,
pathArgs,

@@ -698,3 +689,2 @@ operationId,

consumes,
...otherInfo,
};

@@ -718,4 +708,8 @@

this.extractResponseBodyIfItNeeded(routeInfo, responseBodyInfo, routeParams, rawRouteInfo, routeName);
this.extractResponseErrorIfItNeeded(routeInfo, responseBodyInfo, routeParams, rawRouteInfo, routeName);
if (this.config.extractResponseBody) {
this.extractResponseBodyIfItNeeded(routeInfo, responseBodyInfo, routeName);
}
if (this.config.extractResponseError) {
this.extractResponseErrorIfItNeeded(routeInfo, responseBodyInfo, routeName);
}

@@ -759,25 +753,2 @@ const queryType = routeParams.query.length ? this.schemaParser.getInlineParseContent(queryObjectSchema) : null;

let routeArgs = _.compact([...pathArgs, specificArgs.query, specificArgs.body]);
if (routeArgs.some((pathArg) => pathArg.optional)) {
const { optionalArgs, requiredArgs } = _.reduce(
[...routeArgs],
(acc, pathArg) => {
if (pathArg.optional) {
acc.optionalArgs.push(pathArg);
} else {
acc.requiredArgs.push(pathArg);
}
return acc;
},
{
optionalArgs: [],
requiredArgs: [],
},
);
routeArgs = [...requiredArgs, ...optionalArgs];
}
return {

@@ -832,7 +803,5 @@ id: routeId,

const parsedRouteInfo = this.parseRouteInfo(rawRouteName, routeInfo, method, usageSchema, parsedSchemas);
const processedRouteInfo = this.config.hooks.onCreateRoute(parsedRouteInfo);
const route = processedRouteInfo || parsedRouteInfo;
const usageRouteData = this.config.hooks.onCreateRoute(parsedRouteInfo);
const route = usageRouteData || parsedRouteInfo;
if (!this.hasSecurityRoutes && route.security) {

@@ -848,3 +817,3 @@ this.hasSecurityRoutes = route.security;

this.routes.push(usageRouteData || parsedRouteInfo);
this.routes.push(route);
});

@@ -855,24 +824,26 @@ });

getGroupedRoutes = () => {
return _.reduce(
this.routes.reduce(
(modules, route) => {
if (route.namespace) {
if (!modules[route.namespace]) {
modules[route.namespace] = [];
}
modules[route.namespace].push(route);
} else {
modules.$outOfModule.push(route);
const groupedRoutes = this.routes.reduce(
(modules, route) => {
if (route.namespace) {
if (!modules[route.namespace]) {
modules[route.namespace] = [];
}
return modules;
},
{
$outOfModule: [],
},
),
(acc, packRoutes, moduleName) => {
modules[route.namespace].push(route);
} else {
modules.$outOfModule.push(route);
}
return modules;
},
{
$outOfModule: [],
},
);
return _.reduce(
groupedRoutes,
(acc, routesGroup, moduleName) => {
if (moduleName === "$outOfModule") {
acc.outOfModule = packRoutes;
acc.outOfModule = routesGroup;
} else {

@@ -883,3 +854,3 @@ if (!acc.combined) acc.combined = [];

moduleName,
routes: _.map(packRoutes, (route) => {
routes: _.map(routesGroup, (route) => {
const { original: originalName, usage: usageName } = route.routeName;

@@ -890,5 +861,5 @@

if (
packRoutes.length > 1 &&
routesGroup.length > 1 &&
usageName !== originalName &&
!_.some(packRoutes, ({ routeName, id }) => id !== route.id && originalName === routeName.original)
!_.some(routesGroup, ({ routeName, id }) => id !== route.id && originalName === routeName.original)
) {

@@ -895,0 +866,0 @@ return {

@@ -122,15 +122,15 @@ const { resolve } = require("path");

findTemplateWithExt = (path) => {
const raw = this.cropExtension(path);
const pathVariants = this.config.templateExtensions.map((extension) => `${raw}${extension}`);
return pathVariants.find((variant) => this.fileSystem.pathIsExist(variant));
};
getTemplateContent = (path) => {
const foundTemplatePathKey = _.keys(this.config.templatePaths).find((key) => _.startsWith(path, `@${key}`));
const findPathWithExt = (path) => {
const raw = this.cropExtension(path);
const pathVariants = this.config.templateExtensions.map((extension) => `${raw}${extension}`);
return pathVariants.find((variant) => this.fileSystem.pathIsExist(variant));
};
const rawPath = resolve(
_.replace(path, `@${foundTemplatePathKey}`, this.config.templatePaths[foundTemplatePathKey]),
);
const fixedPath = findPathWithExt(rawPath);
const fixedPath = this.findTemplateWithExt(rawPath);

@@ -141,3 +141,3 @@ if (fixedPath) {

const customPath = findPathWithExt(resolve(this.config.templatePaths.custom, path));
const customPath = this.findTemplateWithExt(resolve(this.config.templatePaths.custom, path));

@@ -148,3 +148,3 @@ if (customPath) {

const originalPath = findPathWithExt(resolve(this.config.templatePaths.original, path));
const originalPath = this.findTemplateWithExt(resolve(this.config.templatePaths.original, path));

@@ -151,0 +151,0 @@ if (originalPath) {