Skip to content

Commit 4627926

Browse files
RoCatrcatoio
and
rcatoio
authored
feat: add a parameter to sort types and types properties (#299)
Co-authored-by: rcatoio <[email protected]>
1 parent 2d090e7 commit 4627926

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
@@ -90,6 +90,7 @@ const {
9090
cleanOutput,
9191
defaultResponse,
9292
unwrapResponseData,
93+
sortTypes,
9394
singleHttpClient,
9495
axios,
9596
silent,
@@ -107,6 +108,7 @@ generateApi({
107108
defaultResponseAsSuccess: defaultAsSuccess,
108109
defaultResponseType: defaultResponse,
109110
unwrapResponseData: unwrapResponseData,
111+
sortTypes: sortTypes,
110112
generateUnionEnums: unionEnums,
111113
generateResponses: responses,
112114
extractRequestParams: !!extractRequestParams,

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const config = {
5959
singleHttpClient: false,
6060
httpClientType: HTTP_CLIENT.FETCH,
6161
unwrapResponseData: false,
62+
sortTypes: false,
6263
templatePaths: {
6364
/** `templates/base` */
6465
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
extractRequestBody = config.extractRequestBody,
4747
defaultResponseType = config.defaultResponseType,
4848
unwrapResponseData = config.unwrapResponseData,
49+
sortTypes = config.sortTypes,
4950
singleHttpClient = config.singleHttpClient,
5051
prettier: prettierOptions = getPrettierOptions(),
5152
hooks: rawHooks,
@@ -81,6 +82,7 @@ module.exports = {
8182
cleanOutput,
8283
defaultResponseType,
8384
unwrapResponseData,
85+
sortTypes,
8486
singleHttpClient,
8587
constants,
8688
silent,
@@ -141,11 +143,41 @@ module.exports = {
141143
const hasFormDataRoutes = routes.some((route) => route.hasFormDataParams);
142144

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

145177
const rawConfiguration = {
146178
apiConfig: createApiConfig(usageSchema),
147179
config,
148-
modelTypes: _.map(usageComponentSchemas, prepareModelType),
180+
modelTypes: _.map(sortSchemas(usageComponentSchemas), prepareModelType),
149181
rawModelTypes: usageComponentSchemas,
150182
hasFormDataRoutes,
151183
hasSecurityRoutes,

0 commit comments

Comments
 (0)