-
Notifications
You must be signed in to change notification settings - Fork 0
/
implementation.go
46 lines (41 loc) · 1.59 KB
/
implementation.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
package service
import (
"fmt"
"github.com/specgen-io/specgen/v2/goven/typescript/types"
"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/writer"
)
func (g *Generator) ServicesImpls(specification *spec.Spec) []generator.CodeFile {
files := []generator.CodeFile{}
for _, version := range specification.Versions {
for _, api := range version.Http.Apis {
files = append(files, *g.serviceImpl(&api))
}
}
return files
}
func (g *Generator) serviceImpl(api *spec.Api) *generator.CodeFile {
w := writer.New(g.Modules.ServiceImpl(api))
w.Imports.Star(g.Modules.ServiceApi(api), `service`)
w.Imports.Star(g.Modules.Models(api.InHttp.InVersion), types.ModelsPackage)
w.Imports.Star(g.Modules.ErrorsModels, types.ErrorsPackage)
w.EmptyLine()
w.Line("export const %sService = (): service.%s => {", api.Name.CamelCase(), serviceInterfaceName(api))
operations := []string{}
for _, operation := range api.Operations {
operations = append(operations, operation.Name.CamelCase())
params := ""
if operation.BodyIs(spec.RequestBodyString) || operation.BodyIs(spec.RequestBodyJson) || operation.HasParams() {
params = fmt.Sprintf(`params: service.%s`, operationParamsTypeName(&operation))
}
w.Line(" const %s = async (%s): Promise<%s> => {", operation.Name.CamelCase(), params, ResponseType(&operation, "service"))
w.Line(" throw new Error('Not Implemented')")
w.Line(" }")
w.EmptyLine()
}
w.Line(" return {%s}", strings.Join(operations, ", "))
w.Line("}")
return w.ToCodeFile()
}