11{{define " server" }}
22
3+ {{- $services := .Services -}}
34{{- $typeMap := .TypeMap -}}
45
5- {{- if .Services }}
6+ {{- if $services }}
67//
78// Server
89//
@@ -17,11 +18,9 @@ class WebRPCError extends Error {
1718
1819import express from 'express'
1920
20- {{- range .Services }}
21- {{$name := .Name }}
22- {{$serviceName := .Name }}
21+ {{- range $_ , $service := $services }}
2322
24- export const create{{$serviceName }}App = (serviceImplementation) = > {
23+ export const create{{$service .Name }}App = (serviceImplementation) = > {
2524 const app = express();
2625
2726 app.use (express.json ())
@@ -36,26 +35,26 @@ import express from 'express'
3635 }
3736
3837 switch(requestPath) {
39- {{range .Methods}}
38+ {{range $_, $method := $service .Methods}}
4039
41- case "/rpc/{{$name }}/{{.Name}}": {
40+ case "/rpc/{{$service.Name }}/{{$method .Name}}": {
4241 try {
43- {{ range .Inputs }}
44- {{- if not .Optional}}
45- if (!("{{ .Name }}" in req.body)) {
46- throw new WebRPCError("Missing Argument `{{ .Name }}`")
42+ {{ range $_, $input := $method .Inputs }}
43+ {{- if not $input .Optional}}
44+ if (!("{{ $input .Name }}" in req.body)) {
45+ throw new WebRPCError("Missing Argument `{{ $input .Name }}`")
4746 }
4847 {{end -}}
4948
50- if (typeof req.body["{{.Name}}"] !== "{{template "type" dict "Type" .Type "TypeMap" $typeMap}}") {
51- throw new WebRPCError("Invalid arg: {{ .Name }}, got type " + typeof req.body["{{ .Name }}"] + " expected " + "{{template "type" dict "Type" .Type "TypeMap" $typeMap}}", 400);
49+ if (typeof req.body["{{$input .Name}}"] !== "{{template "type" dict "Type" $input .Type "TypeMap" $typeMap}}") {
50+ throw new WebRPCError("Invalid arg: {{ $input .Name }}, got type " + typeof req.body["{{ $input .Name }}"] + " expected " + "{{template "type" dict "Type" $input .Type "TypeMap" $typeMap}}", 400);
5251 }
5352 {{end}}
5453
55- const response = await serviceImplementation["{{.Name}}"](req.body);
54+ const response = await serviceImplementation["{{$method .Name}}"](req.body);
5655
57- {{ range .Outputs}}
58- if (!("{{ .Name }}" in response)) {
56+ {{ range $_, $output := .Outputs}}
57+ if (!("{{ $output .Name }}" in response)) {
5958 throw new WebRPCError("internal", 500);
6059 }
6160 {{end}}
0 commit comments