Skip to content

Commit 85362a4

Browse files
author
rcatoio
committed
feat: add a parameter to sort types and types properties
1 parent f5d45e9 commit 85362a4

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ const {
104104
defaultResponse,
105105
unwrapResponseData,
106106
disableThrowOnError,
107+
sortTypes,
107108
singleHttpClient,
108109
axios,
109110
silent,
@@ -121,6 +122,7 @@ generateApi({
121122
defaultResponseType: defaultResponse,
122123
unwrapResponseData: unwrapResponseData,
123124
disableThrowOnError: disableThrowOnError,
125+
sortTypes: sortTypes,
124126
generateUnionEnums: unionEnums,
125127
generateResponses: responses,
126128
extractRequestParams: !!extractRequestParams,

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const config = {
6060
httpClientType: HTTP_CLIENT.FETCH,
6161
unwrapResponseData: false,
6262
disableThrowOnError: false,
63+
sortTypes: false,
6364
templatePaths: {
6465
/** `templates/base` */
6566
base: "",

src/index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module.exports = {
4646
defaultResponseType = config.defaultResponseType,
4747
unwrapResponseData = config.unwrapResponseData,
4848
disableThrowOnError = config.disableThrowOnError,
49+
sortTypes = config.sortTypes,
4950
singleHttpClient = config.singleHttpClient,
5051
prettier: prettierOptions = constants.PRETTIER_OPTIONS,
5152
hooks: rawHooks,
@@ -81,6 +82,7 @@ module.exports = {
8182
defaultResponseType,
8283
unwrapResponseData,
8384
disableThrowOnError,
85+
sortTypes,
8486
singleHttpClient,
8587
constants,
8688
silent,
@@ -140,11 +142,41 @@ module.exports = {
140142
const hasFormDataRoutes = routes.some((route) => route.hasFormDataParams);
141143

142144
const usageComponentSchemas = filterComponentsMap(componentsMap, "schemas");
145+
const sortByProperty = (o1, o2, propertyName) => {
146+
if(o1[propertyName] > o2[propertyName]) {
147+
return 1;
148+
}
149+
if(o1[propertyName] < o2[propertyName]) {
150+
return -1;
151+
}
152+
return 0;
153+
}
154+
const sortByTypeName = (o1, o2) => sortByProperty(o1, o2, 'typeName');
155+
156+
const sortByName = (o1, o2) => sortByProperty(o1, o2, 'name');
157+
158+
const sortSchemas = (schemas) => {
159+
if(config.sortTypes) {
160+
return schemas.sort(sortByTypeName).map((schema) => {
161+
if(schema.rawTypeData?.properties) {
162+
return {
163+
...schema,
164+
rawTypeData: {
165+
...schema.rawTypeData,
166+
'$parsed': {...schema.rawTypeData['$parsed'], content: schema.rawTypeData['$parsed'].content.sort(sortByName)}
167+
}
168+
}
169+
}
170+
return schema;
171+
});
172+
}
173+
return schemas;
174+
};
143175

144176
const rawConfiguration = {
145177
apiConfig: createApiConfig(usageSchema),
146178
config,
147-
modelTypes: _.map(usageComponentSchemas, prepareModelType),
179+
modelTypes: _.map(sortSchemas(usageComponentSchemas), prepareModelType),
148180
rawModelTypes: usageComponentSchemas,
149181
hasFormDataRoutes,
150182
hasSecurityRoutes,

0 commit comments

Comments
 (0)