Skip to content

Commit f86c124

Browse files
committed
feat: add sort-route-params flags
1 parent 0d8aded commit f86c124

File tree

9 files changed

+737
-233
lines changed

9 files changed

+737
-233
lines changed

index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ const generateCommand = defineCommand({
258258
description: "sort routes in alphabetical order",
259259
default: codeGenBaseConfig.sortRoutes,
260260
},
261+
"sort-route-params": {
262+
type: "boolean",
263+
description: "sort route params from path order",
264+
default: codeGenBaseConfig.sortRouteParams,
265+
},
261266
"sort-types": {
262267
type: "boolean",
263268
description: "sort fields and types",
@@ -324,6 +329,7 @@ const generateCommand = defineCommand({
324329
silent: args.silent,
325330
singleHttpClient: args["single-http-client"],
326331
sortRoutes: args["sort-routes"],
332+
sortRouteParams: args["sort-route-params"],
327333
sortTypes: args["sort-types"],
328334
templates: args.templates,
329335
toJS: args.js,

src/configuration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export class CodeGenConfig {
117117
disableThrowOnError = false;
118118
sortTypes = false;
119119
sortRoutes = false;
120+
sortRouteParams = false;
120121
templatePaths = {
121122
/** `templates/base` */
122123
base: "",

templates/default/procedure-call.ejs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,27 @@ const rawWrapperArgs = config.extractRequestParams ?
3939
requestConfigParam,
4040
])
4141
42-
const wrapperArgs = _
43-
// Sort by optionality
44-
.sortBy(rawWrapperArgs, [o => o.optional])
45-
.map(argToTmpl)
46-
.join(', ')
42+
43+
const requiredArgs = rawWrapperArgs.filter((o) => !o.optional)
44+
const optionalArgs = rawWrapperArgs.filter((o) => o.optional)
45+
46+
// sort by params index of params in path
47+
if (config.sortRouteParams) {
48+
requiredArgs.sort(({name}) => {
49+
const idx = path.indexOf(`{${name}}`)
50+
if (idx === -1) {
51+
return Infinity
52+
}
53+
return idx
54+
})
55+
}
56+
57+
const sortedRawWrapperArgs = [
58+
...requiredArgs,
59+
...optionalArgs
60+
]
61+
62+
const wrapperArgs = sortedRawWrapperArgs.map(argToTmpl).join(', ')
4763
4864
// RequestParams["type"]
4965
const requestContentKind = {

0 commit comments

Comments
 (0)