Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
feat: add a parameter to sort types and types properties (acacode#299)
Browse files Browse the repository at this point in the history
Co-authored-by: rcatoio <rcatoio@doubletrade.com>
  • Loading branch information
2 people authored and grandsilence committed May 16, 2022
1 parent d31c336 commit 423323a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const {
cleanOutput,
defaultResponse,
unwrapResponseData,
sortTypes,
singleHttpClient,
axios,
silent,
Expand All @@ -107,6 +108,7 @@ generateApi({
defaultResponseAsSuccess: defaultAsSuccess,
defaultResponseType: defaultResponse,
unwrapResponseData: unwrapResponseData,
sortTypes: sortTypes,
generateUnionEnums: unionEnums,
generateResponses: responses,
extractRequestParams: !!extractRequestParams,
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const config = {
singleHttpClient: false,
httpClientType: HTTP_CLIENT.FETCH,
unwrapResponseData: false,
sortTypes: false,
templatePaths: {
/** `templates/base` */
base: "",
Expand Down
34 changes: 33 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -81,6 +82,7 @@ module.exports = {
cleanOutput,
defaultResponseType,
unwrapResponseData,
sortTypes,
singleHttpClient,
constants,
silent,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 423323a

Please sign in to comment.