Skip to content

Commit

Permalink
ts gen request custom url prefix and body.
Browse files Browse the repository at this point in the history
  • Loading branch information
chaosannals authored and kevwan committed Dec 28, 2024
1 parent 805e568 commit 57eb003
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions tools/goctl/api/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func init() {
tsCmdFlags.StringVar(&tsgen.VarStringAPI, "api")
tsCmdFlags.StringVar(&tsgen.VarStringCaller, "caller")
tsCmdFlags.BoolVar(&tsgen.VarBoolUnWrap, "unwrap")
tsCmdFlags.StringVar(&tsgen.VarStringUrlPrefix, "url")
tsCmdFlags.BoolVar(&tsgen.VarBoolCustomBody, "body")

validateCmdFlags.StringVar(&validate.VarStringAPI, "api")

Expand Down
4 changes: 4 additions & 0 deletions tools/goctl/api/tsgen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ var (
VarStringCaller string
// VarBoolUnWrap describes whether wrap or not.
VarBoolUnWrap bool
// VarStringUrlPrefix request url prefix
VarStringUrlPrefix string
// VarBoolCustomBody request custom body
VarBoolCustomBody bool
)

// TsCommand provides the entry to generate typescript codes
Expand Down
25 changes: 21 additions & 4 deletions tools/goctl/api/tsgen/genpacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ func genAPI(api *spec.ApiSpec, caller string) (string, error) {
if len(comment) > 0 {
fmt.Fprintf(&builder, "%s\n", comment)
}
fmt.Fprintf(&builder, "export function %s(%s) {\n", handler, paramsForRoute(route))
genericsType := ""
if VarBoolCustomBody {
genericsType = "<T>"
}
fmt.Fprintf(&builder, "export function %s%s(%s) {\n", handler, genericsType, paramsForRoute(route))
writeIndent(&builder, 1)
responseGeneric := "<null>"
if len(route.ResponseTypeName()) > 0 {
Expand All @@ -101,6 +105,9 @@ func genAPI(api *spec.ApiSpec, caller string) (string, error) {

func paramsForRoute(route spec.Route) string {
if route.RequestType == nil {
if VarBoolCustomBody {
return "body?: T"
}
return ""
}
hasParams := pathHasParams(route)
Expand Down Expand Up @@ -141,6 +148,10 @@ func paramsForRoute(route spec.Route) string {
}
}
}

if VarBoolCustomBody {
params = append(params, "body?: T")
}
return strings.Join(params, ", ")
}

Expand Down Expand Up @@ -180,7 +191,13 @@ func callParamsForRoute(route spec.Route, group spec.Group) string {
configParams := []string{}

if hasBody {
configParams = append(configParams, "body: JSON.stringify(req)")
if VarBoolCustomBody {
configParams = append(configParams, "body: JSON.stringify(body ?? req)")
} else {
configParams = append(configParams, "body: JSON.stringify(req)")
}
} else if VarBoolCustomBody {
configParams = append(configParams, "body: body ? JSON.stringify(body): null")
}
if hasHeader {
configParams = append(configParams, "headers: headers")
Expand All @@ -205,12 +222,12 @@ func pathForRoute(route spec.Route, group spec.Group) string {
routePath = strings.Join(pathSlice, "/")
}
if len(prefix) == 0 {
return "`" + routePath + "`"
return "`" + VarStringUrlPrefix + routePath + "`"
}

prefix = strings.TrimPrefix(prefix, `"`)
prefix = strings.TrimSuffix(prefix, `"`)
return fmt.Sprintf("`%s/%s`", prefix, strings.TrimPrefix(routePath, "/"))
return fmt.Sprintf("`%s%s/%s`", VarStringUrlPrefix, prefix, strings.TrimPrefix(routePath, "/"))
}

func pathHasParams(route spec.Route) bool {
Expand Down
6 changes: 5 additions & 1 deletion tools/goctl/api/tsgen/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export async function request(
},
});

return response.json();
if (response.headers.get('Content-Type') == 'application/json') {
return response.json();
} else {
return response.text();
}
}

function api<T>(
Expand Down

0 comments on commit 57eb003

Please sign in to comment.