genpacket.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package tsgen
  2. import (
  3. "fmt"
  4. "path"
  5. "strings"
  6. "text/template"
  7. "github.com/zeromicro/go-zero/tools/goctl/api/spec"
  8. apiutil "github.com/zeromicro/go-zero/tools/goctl/api/util"
  9. "github.com/zeromicro/go-zero/tools/goctl/util"
  10. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  11. )
  12. const (
  13. handlerTemplate = `{{.imports}}
  14. {{.apis}}
  15. `
  16. )
  17. func genHandler(dir, webAPI, caller string, api *spec.ApiSpec, unwrapAPI bool) error {
  18. filename := strings.Replace(api.Service.Name, "-api", "", 1) + ".ts"
  19. if err := pathx.RemoveIfExist(path.Join(dir, filename)); err != nil {
  20. return err
  21. }
  22. fp, created, err := apiutil.MaybeCreateFile(dir, "", filename)
  23. if err != nil {
  24. return err
  25. }
  26. if !created {
  27. return nil
  28. }
  29. defer fp.Close()
  30. imports := ""
  31. if len(caller) == 0 {
  32. caller = "webapi"
  33. }
  34. importCaller := caller
  35. if unwrapAPI {
  36. importCaller = "{ " + importCaller + " }"
  37. }
  38. if len(webAPI) > 0 {
  39. imports += `import ` + importCaller + ` from ` + "\"" + webAPI + "\""
  40. }
  41. if len(api.Types) != 0 {
  42. if len(imports) > 0 {
  43. imports += pathx.NL
  44. }
  45. outputFile := apiutil.ComponentName(api)
  46. imports += fmt.Sprintf(`import * as components from "%s"`, "./"+outputFile)
  47. imports += fmt.Sprintf(`%sexport * from "%s"`, pathx.NL, "./"+outputFile)
  48. }
  49. apis, err := genAPI(api, caller)
  50. if err != nil {
  51. return err
  52. }
  53. t := template.Must(template.New("handlerTemplate").Parse(handlerTemplate))
  54. return t.Execute(fp, map[string]string{
  55. "imports": imports,
  56. "apis": strings.TrimSpace(apis),
  57. })
  58. }
  59. func genAPI(api *spec.ApiSpec, caller string) (string, error) {
  60. var builder strings.Builder
  61. for _, group := range api.Service.Groups {
  62. for _, route := range group.Routes {
  63. handler := route.Handler
  64. if len(handler) == 0 {
  65. return "", fmt.Errorf("missing handler annotation for route %q", route.Path)
  66. }
  67. handler = util.Untitle(handler)
  68. handler = strings.Replace(handler, "Handler", "", 1)
  69. comment := commentForRoute(route)
  70. if len(comment) > 0 {
  71. fmt.Fprintf(&builder, "%s\n", comment)
  72. }
  73. fmt.Fprintf(&builder, "export function %s(%s) {\n", handler, paramsForRoute(route))
  74. writeIndent(&builder, 1)
  75. responseGeneric := "<null>"
  76. if len(route.ResponseTypeName()) > 0 {
  77. val, err := goTypeToTs(route.ResponseType, true)
  78. if err != nil {
  79. return "", err
  80. }
  81. responseGeneric = fmt.Sprintf("<%s>", val)
  82. }
  83. fmt.Fprintf(&builder, `return %s.%s%s(%s)`, caller, strings.ToLower(route.Method),
  84. util.Title(responseGeneric), callParamsForRoute(route, group))
  85. builder.WriteString("\n}\n\n")
  86. }
  87. }
  88. apis := builder.String()
  89. return apis, nil
  90. }
  91. func paramsForRoute(route spec.Route) string {
  92. if route.RequestType == nil {
  93. return ""
  94. }
  95. hasParams := pathHasParams(route)
  96. hasBody := hasRequestBody(route)
  97. rt, err := goTypeToTs(route.RequestType, true)
  98. if err != nil {
  99. fmt.Println(err.Error())
  100. return ""
  101. }
  102. if hasParams && hasBody {
  103. return fmt.Sprintf("params: %s, req: %s", rt+"Params", rt)
  104. } else if hasParams {
  105. return fmt.Sprintf("params: %s", rt+"Params")
  106. } else if hasBody {
  107. return fmt.Sprintf("req: %s", rt)
  108. }
  109. return ""
  110. }
  111. func commentForRoute(route spec.Route) string {
  112. var builder strings.Builder
  113. comment := route.JoinedDoc()
  114. builder.WriteString("/**")
  115. builder.WriteString("\n * @description " + comment)
  116. hasParams := pathHasParams(route)
  117. hasBody := hasRequestBody(route)
  118. if hasParams && hasBody {
  119. builder.WriteString("\n * @param params")
  120. builder.WriteString("\n * @param req")
  121. } else if hasParams {
  122. builder.WriteString("\n * @param params")
  123. } else if hasBody {
  124. builder.WriteString("\n * @param req")
  125. }
  126. builder.WriteString("\n */")
  127. return builder.String()
  128. }
  129. func callParamsForRoute(route spec.Route, group spec.Group) string {
  130. hasParams := pathHasParams(route)
  131. hasBody := hasRequestBody(route)
  132. if hasParams && hasBody {
  133. return fmt.Sprintf("%s, %s, %s", pathForRoute(route, group), "params", "req")
  134. } else if hasParams {
  135. return fmt.Sprintf("%s, %s", pathForRoute(route, group), "params")
  136. } else if hasBody {
  137. return fmt.Sprintf("%s, %s", pathForRoute(route, group), "req")
  138. }
  139. return pathForRoute(route, group)
  140. }
  141. func pathForRoute(route spec.Route, group spec.Group) string {
  142. prefix := group.GetAnnotation(pathPrefix)
  143. if len(prefix) == 0 {
  144. return "\"" + route.Path + "\""
  145. }
  146. prefix = strings.TrimPrefix(prefix, `"`)
  147. prefix = strings.TrimSuffix(prefix, `"`)
  148. return fmt.Sprintf(`"%s/%s"`, prefix, strings.TrimPrefix(route.Path, "/"))
  149. }
  150. func pathHasParams(route spec.Route) bool {
  151. ds, ok := route.RequestType.(spec.DefineStruct)
  152. if !ok {
  153. return false
  154. }
  155. return len(ds.Members) != len(ds.GetBodyMembers())
  156. }
  157. func hasRequestBody(route spec.Route) bool {
  158. ds, ok := route.RequestType.(spec.DefineStruct)
  159. if !ok {
  160. return false
  161. }
  162. return len(route.RequestTypeName()) > 0 && len(ds.GetBodyMembers()) > 0
  163. }