-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
108 lines (95 loc) · 3.56 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package client
import (
"fmt"
"github.com/specgen-io/specgen/v2/goven/typescript/writer"
"strings"
"github.com/specgen-io/specgen/v2/goven/generator"
"github.com/specgen-io/specgen/v2/goven/spec"
"github.com/specgen-io/specgen/v2/goven/typescript/types"
"github.com/specgen-io/specgen/v2/goven/typescript/validations"
)
func getUrl(endpoint spec.Endpoint) string {
url := endpoint.Url
for _, param := range endpoint.UrlParams {
url = strings.Replace(url, spec.UrlParamStr(¶m), "${stringify(parameters."+param.Name.CamelCase()+")}", -1)
}
return url
}
func operationSignature(operation *spec.NamedOperation) string {
return fmt.Sprintf(`%s: async (%s): Promise<%s>`, operation.Name.CamelCase(), createOperationParams(operation), responseType(operation))
}
func createParams(params []spec.NamedParam, required bool) []string {
tsParams := []string{}
for _, param := range params {
isRequired := param.Default == nil && !param.Type.Definition.IsNullable()
if isRequired == required {
requiredSign := ""
if !isRequired {
requiredSign = "?"
}
paramType := ¶m.Type.Definition
if !isRequired && !paramType.IsNullable() {
paramType = spec.Nullable(paramType)
}
tsParams = append(tsParams, param.Name.CamelCase()+requiredSign+": "+types.TsType(paramType))
}
}
return tsParams
}
func createOperationParams(operation *spec.NamedOperation) string {
operationParams := []string{}
operationParams = append(operationParams, createParams(operation.HeaderParams, true)...)
if operation.BodyIs(spec.RequestBodyString) || operation.BodyIs(spec.RequestBodyJson) {
operationParams = append(operationParams, "body: "+types.TsType(&operation.Body.Type.Definition))
}
operationParams = append(operationParams, createParams(operation.Endpoint.UrlParams, true)...)
operationParams = append(operationParams, createParams(operation.QueryParams, true)...)
operationParams = append(operationParams, createParams(operation.HeaderParams, false)...)
operationParams = append(operationParams, createParams(operation.QueryParams, false)...)
if len(operationParams) == 0 {
return ""
}
return fmt.Sprintf("parameters: {%s}", strings.Join(operationParams, ", "))
}
type CommonGenerator struct {
Modules *Modules
validation validations.Validation
}
func (g *CommonGenerator) ParamsBuilder() *generator.CodeFile {
w := writer.New(g.Modules.Params)
w.Lines(`
export function stringify(value: ScalarParam): string {
if (value instanceof Date) {
return value.toISOString()
}
return String(value)
}
export function stringifyX(value: ScalarParam | ScalarParam[]): string | string[] {
if (Array.isArray(value)) {
return value.map(stringify)
} else {
return stringify(value)
}
}
type ScalarParam = string | number | boolean | Date
type ParamType = undefined | ScalarParam | ScalarParam[]
type ParamItem = [string, string]
export function strParamsItems(params: Record<string, ParamType>): ParamItem[] {
return Object.entries(params)
.filter(([key, value]) => value != undefined)
.map(([key, value]): ParamItem[] => {
if (Array.isArray(value)) {
return value.map(item => [key, stringify(item)])
} else {
return [[key, stringify(value!)]]
}
})
.flat()
}
export function strParamsObject(params: Record<string, ParamType>): Record<string, string | string[]> {
return Object.keys(params)
.filter(paramName => params[paramName] != undefined)
.reduce((obj, paramName) => ({...obj, [paramName]: stringifyX(params[paramName]!)}), {} as Record<string, string | string[]>)
}`)
return w.ToCodeFile()
}