-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
51 lines (42 loc) · 1.38 KB
/
params.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
package service
import (
"fmt"
"github.com/specgen-io/specgen/v2/goven/typescript/writer"
"github.com/specgen-io/specgen/v2/goven/generator"
"github.com/specgen-io/specgen/v2/goven/spec"
)
func paramsTypeName(operation *spec.NamedOperation, namePostfix string) string {
return fmt.Sprintf("%s%s", operation.Name.PascalCase(), namePostfix)
}
func urlParamsType(operation *spec.NamedOperation) string {
return paramsTypeName(operation, "UrlParams")
}
func headersType(operation *spec.NamedOperation) string {
return paramsTypeName(operation, "HeaderParams")
}
func queryType(operation *spec.NamedOperation) string {
return paramsTypeName(operation, "QueryParams")
}
func (g *Generator) ParamsStaticCode() *generator.CodeFile {
w := writer.New(g.Modules.Params)
w.Lines(`
export function zipHeaders(headers: string[]): Record<string, string | string[]> {
const result: Record<string, string | string[]> = {}
for (let i = 0; i < headers.length / 2; i++) {
const key: string = headers[i*2]
const value: string = headers[i*2+1]
if (key in result) {
const existingValue = result[key]
if (Array.isArray(existingValue)) {
existingValue.push(value)
} else {
result[key] = [existingValue, value]
}
} else {
result[key] = value
}
}
return result
}`)
return w.ToCodeFile()
}