diff --git a/index.js b/index.js index 32c9e7a1..3cd8c484 100755 --- a/index.js +++ b/index.js @@ -90,6 +90,7 @@ const { cleanOutput, defaultResponse, unwrapResponseData, + sortTypes, singleHttpClient, axios, silent, @@ -107,6 +108,7 @@ generateApi({ defaultResponseAsSuccess: defaultAsSuccess, defaultResponseType: defaultResponse, unwrapResponseData: unwrapResponseData, + sortTypes: sortTypes, generateUnionEnums: unionEnums, generateResponses: responses, extractRequestParams: !!extractRequestParams, diff --git a/src/config.js b/src/config.js index 1c452cbe..73938fe9 100644 --- a/src/config.js +++ b/src/config.js @@ -59,6 +59,7 @@ const config = { singleHttpClient: false, httpClientType: HTTP_CLIENT.FETCH, unwrapResponseData: false, + sortTypes: false, templatePaths: { /** `templates/base` */ base: "", diff --git a/src/index.js b/src/index.js index 753a2a82..c149b8ca 100644 --- a/src/index.js +++ b/src/index.js @@ -46,6 +46,7 @@ module.exports = { extractRequestBody = config.extractRequestBody, defaultResponseType = config.defaultResponseType, unwrapResponseData = config.unwrapResponseData, + sortTypes = config.sortTypes, singleHttpClient = config.singleHttpClient, prettier: prettierOptions = getPrettierOptions(), hooks: rawHooks, @@ -81,6 +82,7 @@ module.exports = { cleanOutput, defaultResponseType, unwrapResponseData, + sortTypes, singleHttpClient, constants, silent, @@ -141,11 +143,41 @@ module.exports = { const hasFormDataRoutes = routes.some((route) => route.hasFormDataParams); const usageComponentSchemas = filterComponentsMap(componentsMap, "schemas"); + const sortByProperty = (o1, o2, propertyName) => { + if(o1[propertyName] > o2[propertyName]) { + return 1; + } + if(o1[propertyName] < o2[propertyName]) { + return -1; + } + return 0; + } + const sortByTypeName = (o1, o2) => sortByProperty(o1, o2, 'typeName'); + + const sortByName = (o1, o2) => sortByProperty(o1, o2, 'name'); + + const sortSchemas = (schemas) => { + if(config.sortTypes) { + return schemas.sort(sortByTypeName).map((schema) => { + if(schema.rawTypeData?.properties) { + return { + ...schema, + rawTypeData: { + ...schema.rawTypeData, + '$parsed': {...schema.rawTypeData['$parsed'], content: schema.rawTypeData['$parsed'].content.sort(sortByName)} + } + } + } + return schema; + }); + } + return schemas; + }; const rawConfiguration = { apiConfig: createApiConfig(usageSchema), config, - modelTypes: _.map(usageComponentSchemas, prepareModelType), + modelTypes: _.map(sortSchemas(usageComponentSchemas), prepareModelType), rawModelTypes: usageComponentSchemas, hasFormDataRoutes, hasSecurityRoutes,