|
|
@ -85,7 +85,7 @@ func genHandler(dir, webApi, caller string, api *spec.ApiSpec, unwrapApi bool) e
|
|
|
|
imports += fmt.Sprintf(`import * as components from "%s"`, "./"+outputFile)
|
|
|
|
imports += fmt.Sprintf(`import * as components from "%s"`, "./"+outputFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
apis, err := genApi(api, localTypes, caller, prefixForType)
|
|
|
|
apis, err := genApi(api, caller, prefixForType)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -119,32 +119,34 @@ func genTypes(localTypes []spec.Type, inlineType func(string) (*spec.Type, error
|
|
|
|
return types, nil
|
|
|
|
return types, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func genApi(api *spec.ApiSpec, localTypes []spec.Type, caller string, prefixForType func(string) string) (string, error) {
|
|
|
|
func genApi(api *spec.ApiSpec, caller string, prefixForType func(string) string) (string, error) {
|
|
|
|
var builder strings.Builder
|
|
|
|
var builder strings.Builder
|
|
|
|
for _, route := range api.Service.Routes() {
|
|
|
|
for _, group := range api.Service.Groups {
|
|
|
|
handler, ok := apiutil.GetAnnotationValue(route.Annotations, "server", "handler")
|
|
|
|
for _, route := range group.Routes {
|
|
|
|
if !ok {
|
|
|
|
handler, ok := apiutil.GetAnnotationValue(route.Annotations, "server", "handler")
|
|
|
|
return "", fmt.Errorf("missing handler annotation for route %q", route.Path)
|
|
|
|
if !ok {
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("missing handler annotation for route %q", route.Path)
|
|
|
|
handler = util.Untitle(handler)
|
|
|
|
|
|
|
|
handler = strings.Replace(handler, "Handler", "", 1)
|
|
|
|
|
|
|
|
comment := commentForRoute(route)
|
|
|
|
|
|
|
|
if len(comment) > 0 {
|
|
|
|
|
|
|
|
fmt.Fprintf(&builder, "%s\n", comment)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(&builder, "export function %s(%s) {\n", handler, paramsForRoute(route, prefixForType))
|
|
|
|
|
|
|
|
writeIndent(&builder, 1)
|
|
|
|
|
|
|
|
responseGeneric := "<null>"
|
|
|
|
|
|
|
|
if len(route.ResponseType.Name) > 0 {
|
|
|
|
|
|
|
|
val, err := goTypeToTs(route.ResponseType.Name, prefixForType)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
responseGeneric = fmt.Sprintf("<%s>", val)
|
|
|
|
handler = util.Untitle(handler)
|
|
|
|
|
|
|
|
handler = strings.Replace(handler, "Handler", "", 1)
|
|
|
|
|
|
|
|
comment := commentForRoute(route)
|
|
|
|
|
|
|
|
if len(comment) > 0 {
|
|
|
|
|
|
|
|
fmt.Fprintf(&builder, "%s\n", comment)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(&builder, "export function %s(%s) {\n", handler, paramsForRoute(route, prefixForType))
|
|
|
|
|
|
|
|
writeIndent(&builder, 1)
|
|
|
|
|
|
|
|
responseGeneric := "<null>"
|
|
|
|
|
|
|
|
if len(route.ResponseType.Name) > 0 {
|
|
|
|
|
|
|
|
val, err := goTypeToTs(route.ResponseType.Name, prefixForType)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
responseGeneric = fmt.Sprintf("<%s>", val)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(&builder, `return %s.%s%s(%s)`, caller, strings.ToLower(route.Method),
|
|
|
|
|
|
|
|
util.Title(responseGeneric), callParamsForRoute(route, group))
|
|
|
|
|
|
|
|
builder.WriteString("\n}\n\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&builder, `return %s.%s%s(%s)`, caller, strings.ToLower(route.Method),
|
|
|
|
|
|
|
|
util.Title(responseGeneric), callParamsForRoute(route))
|
|
|
|
|
|
|
|
builder.WriteString("\n}\n\n")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
apis := builder.String()
|
|
|
|
apis := builder.String()
|
|
|
@ -188,21 +190,28 @@ func commentForRoute(route spec.Route) string {
|
|
|
|
return builder.String()
|
|
|
|
return builder.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func callParamsForRoute(route spec.Route) string {
|
|
|
|
func callParamsForRoute(route spec.Route, group spec.Group) string {
|
|
|
|
hasParams := pathHasParams(route)
|
|
|
|
hasParams := pathHasParams(route)
|
|
|
|
hasBody := hasRequestBody(route)
|
|
|
|
hasBody := hasRequestBody(route)
|
|
|
|
if hasParams && hasBody {
|
|
|
|
if hasParams && hasBody {
|
|
|
|
return fmt.Sprintf("%s, %s, %s", pathForRoute(route), "params", "req")
|
|
|
|
return fmt.Sprintf("%s, %s, %s", pathForRoute(route, group), "params", "req")
|
|
|
|
} else if hasParams {
|
|
|
|
} else if hasParams {
|
|
|
|
return fmt.Sprintf("%s, %s", pathForRoute(route), "params")
|
|
|
|
return fmt.Sprintf("%s, %s", pathForRoute(route, group), "params")
|
|
|
|
} else if hasBody {
|
|
|
|
} else if hasBody {
|
|
|
|
return fmt.Sprintf("%s, %s", pathForRoute(route), "req")
|
|
|
|
return fmt.Sprintf("%s, %s", pathForRoute(route, group), "req")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pathForRoute(route)
|
|
|
|
return pathForRoute(route, group)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func pathForRoute(route spec.Route) string {
|
|
|
|
func pathForRoute(route spec.Route, group spec.Group) string {
|
|
|
|
return "\"" + route.Path + "\""
|
|
|
|
value, ok := apiutil.GetAnnotationValue(group.Annotations, "server", pathPrefix)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
return "\"" + route.Path + "\""
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
value = strings.TrimPrefix(value, `"`)
|
|
|
|
|
|
|
|
value = strings.TrimSuffix(value, `"`)
|
|
|
|
|
|
|
|
return fmt.Sprintf(`"%s/%s"`, value, strings.TrimPrefix(route.Path, "/"))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func pathHasParams(route spec.Route) bool {
|
|
|
|
func pathHasParams(route spec.Route) bool {
|
|
|
|