|
|
@ -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,9 +119,10 @@ 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 {
|
|
|
|
|
|
|
|
for _, route := range group.Routes {
|
|
|
|
handler, ok := apiutil.GetAnnotationValue(route.Annotations, "server", "handler")
|
|
|
|
handler, ok := apiutil.GetAnnotationValue(route.Annotations, "server", "handler")
|
|
|
|
if !ok {
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("missing handler annotation for route %q", route.Path)
|
|
|
|
return "", fmt.Errorf("missing handler annotation for route %q", route.Path)
|
|
|
@ -143,9 +144,10 @@ func genApi(api *spec.ApiSpec, localTypes []spec.Type, caller string, prefixForT
|
|
|
|
responseGeneric = fmt.Sprintf("<%s>", val)
|
|
|
|
responseGeneric = fmt.Sprintf("<%s>", val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&builder, `return %s.%s%s(%s)`, caller, strings.ToLower(route.Method),
|
|
|
|
fmt.Fprintf(&builder, `return %s.%s%s(%s)`, caller, strings.ToLower(route.Method),
|
|
|
|
util.Title(responseGeneric), callParamsForRoute(route))
|
|
|
|
util.Title(responseGeneric), callParamsForRoute(route, group))
|
|
|
|
builder.WriteString("\n}\n\n")
|
|
|
|
builder.WriteString("\n}\n\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
apis := builder.String()
|
|
|
|
apis := builder.String()
|
|
|
|
return apis, nil
|
|
|
|
return apis, nil
|
|
|
@ -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 {
|
|
|
|
|
|
|
|
value, ok := apiutil.GetAnnotationValue(group.Annotations, "server", pathPrefix)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return "\"" + route.Path + "\""
|
|
|
|
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 {
|
|
|
|