11package codegen
22
33import (
4- "net/url"
54 "strings"
65)
76
87type uiRouteSpec struct {
9- Id string `json:"id"`
10- Path string `json:"path"`
8+ Id string `json:"id"`
9+ Path string `json:"path"`
10+ QueryParams []struct {
11+ Key string `json:"key"`
12+ Type DatatypeDef `json:"type"`
13+ } `json:"query_params"`
1114}
1215
1316// need to uppercase b/c tslint complains about pascal case
@@ -16,74 +19,31 @@ func (u *uiRouteSpec) TsOptsName() string {
1619}
1720
1821func (u * uiRouteSpec ) HasOpts () bool {
19- return len (u .PathAndQueryOpts ()) > 0
20- }
21-
22- // "/accounts/{id}?token={token}" => ["id", "token"]
23- func (u * uiRouteSpec ) PathAndQueryOpts () []string {
24- return u .placeholders (u .Path )
22+ return (len (u .PathPlaceholders ()) + len (u .QueryParams )) > 0
2523}
2624
2725// "/accounts/{id}?token={token}" => ["id"]
2826func (u * uiRouteSpec ) PathPlaceholders () []string {
29- return u .placeholders (u .PathWithoutQuery ())
30- }
31-
32- // "/accounts/{id}?token={token}" => ["token"]
33- func (u * uiRouteSpec ) QueryPlaceholders () []string {
34- return u .placeholders (u .Query ())
35- }
36-
37- func (u * uiRouteSpec ) placeholders (pathOrQuery string ) []string {
38- placeholders := []string {}
39- for _ , match := range routePlaceholderParseRe .FindAllStringSubmatch (pathOrQuery , - 1 ) {
40- placeholders = append (placeholders , match [1 ])
27+ keys := []string {}
28+ for _ , match := range routePlaceholderParseRe .FindAllStringSubmatch (u .Path , - 1 ) {
29+ keys = append (keys , match [1 ])
4130 }
4231
43- return placeholders
32+ return keys
4433}
4534
4635func (u * uiRouteSpec ) PathReJavaScript () string {
4736 // "/account/{account}/import_otp_token" => "/account/([^/]+)/import_otp_token"
48- reString := routePlaceholderParseRe .ReplaceAllStringFunc (u .PathWithoutQuery () , func (_ string ) string {
37+ reString := routePlaceholderParseRe .ReplaceAllStringFunc (u .Path , func (_ string ) string {
4938 return "([^/]+)"
5039 })
5140
5241 // "/^\/account\/([^\/]+)\/import_otp_token$/"
5342 return "/^" + strings .ReplaceAll (reString , "/" , `\/` ) + "$/"
5443}
5544
56- func (u * uiRouteSpec ) PathWithoutQuery () string {
57- // FIXME: trick to make it work with hashes
58- if strings .HasPrefix (u .Path , "#" ) {
59- parsedUrl , err := url .Parse (u .Path [1 :])
60- if err != nil {
61- panic (err )
62- }
63-
64- return "#" + parsedUrl .Path
65- } else {
66- parsedUrl , err := url .Parse (u .Path )
67- if err != nil {
68- panic (err )
69- }
70-
71- return parsedUrl .Path
72- }
73- }
74-
75- func (u * uiRouteSpec ) Query () string {
76- // FIXME: trick to make it work with hashes
77- parsedUrl , err := url .Parse (strings .TrimPrefix (u .Path , "#" ))
78- if err != nil {
79- panic (err )
80- }
81-
82- return parsedUrl .RawQuery
83- }
84-
8545func (u * uiRouteSpec ) TsPath () string {
86- return routePlaceholderParseRe .ReplaceAllStringFunc (u .PathWithoutQuery () , func (match string ) string {
46+ return routePlaceholderParseRe .ReplaceAllStringFunc (u .Path , func (match string ) string {
8747 // "{id}" => "id"
8848 placeholder := removeBraces (match )
8949
0 commit comments