This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
/
ts_template.go
86 lines (64 loc) · 3.96 KB
/
ts_template.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
package main
const tsIndexTemplate = `{{ range $service := .services }}import * as {{ $service.ImportName }} from './{{ $service.Name }}';
{{ end }}
export class Client {
constructor(token: string) {
{{ range $service := .services }}
this.{{ $service.Name}} = new {{ $service.ImportName }}.{{ title $service.Name}}Service(token);{{end}}
}
{{ range $service := .services }}
{{ $service.Name}}: {{ $service.ImportName }}.{{ title $service.Name}}Service;{{end}}
}
export default (token = process.env.M3O_API_TOKEN as string) => {
return {
{{ range $service := .services }}
{{ $service.Name}}: new {{ $service.ImportName }}.{{ title $service.Name}}Service(token),{{end}}
}
}
`
const tsServiceTemplate = `import * as m3o from '@m3o/m3o-node';
{{ $service := .service }}
export class {{ title $service.Name }}Service{
private client: m3o.Client;
constructor(token: string) {
this.client = new m3o.Client({token: token})
}
{{ range $key, $req := $service.Spec.Components.RequestBodies }}{{ $reqType := requestType $key }}{{ $endpointName := requestTypeToEndpointName $key}}{{ if endpointComment $endpointName $service.Spec.Components.Schemas }}{{ endpointComment $endpointName $service.Spec.Components.Schemas }}{{ end }}{{ untitle $endpointName}}(request: {{ requestType $key }}): {{ if isStream $service.Spec $service.Name $reqType }}Promise<m3o.Stream<{{ $reqType }}, {{ requestTypeToResponseType $key }}>>{{ end }}{{ if isNotStream $service.Spec $service.Name $reqType }}Promise<{{ requestTypeToResponseType $key }}>{{ end }} {
{{ if isStream $service.Spec $service.Name $reqType }}return this.client.stream("{{ $service.Name }}", "{{ requestTypeToEndpointPath $key}}", request);{{ end }}{{ if isNotStream $service.Spec $service.Name $reqType }}return this.client.call("{{ $service.Name }}", "{{ requestTypeToEndpointPath $key}}", request) as Promise<{{ requestTypeToResponseType $key }}>;{{ end }}
};
{{ end }}
}
{{ range $typeName, $schema := $service.Spec.Components.Schemas }}
export interface {{ title $typeName }}{{ "{" }}
{{ recursiveTypeDefinition "typescript" $service.Name $typeName $service.Spec.Components.Schemas }}{{ "}" }}
{{end}}
`
const tsExampleTemplate = `{{ $service := .service }}const m3o = require('m3o')(process.env.M3O_API_TOKEN)
async function main() {
let rsp = await m3o.{{ $service.Name }}.{{ .endpoint }}({{ tsExampleRequest $service.Name .endpoint $service.Spec.Components.Schemas .example.Request }})
{{ $reqType := requestType .endpoint }}{{ if isNotStream $service.Spec $service.Name $reqType }}console.log(rsp)
{{ end }}{{ if isStream $service.Spec $service.Name $reqType }}rsp.onMessage(msg => {
console.log(msg)
}){{ end}}
}
main()`
const tsReadmeTopTemplate = `{{ $service := .service }}# {{ title $service.Name }}
An [m3o.com](https://m3o.com) API. For example usage see [m3o.com/{{ title $service.Name }}/api](https://m3o.com/{{ title $service.Name }}/api).
Endpoints:
`
const tsReadmeBottomTemplate = `{{ $service := .service }}## {{ title .endpoint}}
{{ endpointDescription .endpoint $service.Spec.Components.Schemas }}
[https://m3o.com/{{ $service.Name }}/api#{{ title .endpoint}}](https://m3o.com/{{ $service.Name }}/api#{{ title .endpoint}})
` + "```" + `js
const { {{ title $service.Name }}Service } = require('m3o/{{ $service.Name }}');
const {{ $service.Name }}Service = new {{ title $service.Name }}Service(process.env.M3O_API_TOKEN)
{{ if endpointComment .endpoint $service.Spec.Components.Schemas }}{{ endpointComment .endpoint $service.Spec.Components.Schemas }}{{ end }}async function {{ untitle .funcName }}() {
const rsp = await {{ $service.Name }}Service.{{ .endpoint }}({{ tsExampleRequest $service.Name .endpoint $service.Spec.Components.Schemas .example.Request }})
{{ $reqType := requestType .endpoint }}{{ if isNotStream $service.Spec $service.Name $reqType }}console.log(rsp)
{{ end }}{{ if isStream $service.Spec $service.Name $reqType }}rsp.onMessage(msg => {
console.log(msg)
}){{ end}}
}
{{ untitle .funcName }}()
` + "```" + `
`