diff --git a/tools/goctl/api/cmd.go b/tools/goctl/api/cmd.go index d513ffdd862a..f4e82f646d5b 100644 --- a/tools/goctl/api/cmd.go +++ b/tools/goctl/api/cmd.go @@ -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") diff --git a/tools/goctl/api/tsgen/gen.go b/tools/goctl/api/tsgen/gen.go index 34732b67622d..3714658f1907 100644 --- a/tools/goctl/api/tsgen/gen.go +++ b/tools/goctl/api/tsgen/gen.go @@ -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 diff --git a/tools/goctl/api/tsgen/genpacket.go b/tools/goctl/api/tsgen/genpacket.go index 152c15c2f769..f809813d392b 100644 --- a/tools/goctl/api/tsgen/genpacket.go +++ b/tools/goctl/api/tsgen/genpacket.go @@ -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 = "" + } + fmt.Fprintf(&builder, "export function %s%s(%s) {\n", handler, genericsType, paramsForRoute(route)) writeIndent(&builder, 1) responseGeneric := "" if len(route.ResponseTypeName()) > 0 { @@ -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) @@ -141,6 +148,10 @@ func paramsForRoute(route spec.Route) string { } } } + + if VarBoolCustomBody { + params = append(params, "body?: T") + } return strings.Join(params, ", ") } @@ -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") @@ -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 { diff --git a/tools/goctl/api/tsgen/request.ts b/tools/goctl/api/tsgen/request.ts index 287f18297e9b..1f39967f39bb 100644 --- a/tools/goctl/api/tsgen/request.ts +++ b/tools/goctl/api/tsgen/request.ts @@ -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(