github.com/vcilabs/webrpc@v0.5.2-0.20201116131534-162e27b1b33b/gen/typescript/templates/server.ts.tmpl (about)

     1  {{define "server"}}
     2  
     3  {{- if .Services}}
     4  //
     5  // Server
     6  //
     7  export class WebRPCError extends Error {
     8      statusCode?: number
     9  
    10      constructor(msg: string = "error", statusCode?: number) {
    11          super("webrpc error: " + msg);
    12  
    13          Object.setPrototypeOf(this, WebRPCError.prototype);
    14  
    15          this.statusCode = statusCode;
    16      }
    17  }
    18  
    19  import express from 'express'
    20  
    21      {{- range .Services}}
    22          {{$name := .Name}}
    23          {{$serviceName := .Name | serviceInterfaceName}}
    24  
    25          export type {{$serviceName}}Service = {
    26              {{range .Methods}}
    27                  {{.Name}}: (args: {{.Name}}Args) => {{.Name}}Return | Promise<{{.Name}}Return>
    28              {{end}}
    29          }
    30  
    31          export const create{{$serviceName}}App = (serviceImplementation: {{$serviceName}}Service) => {
    32              const app = express();
    33  
    34              app.use(express.json())
    35  
    36              app.post('/*', async (req, res) => {
    37                  const requestPath = req.baseUrl + req.path
    38  
    39                  if (!req.body) {
    40                      res.status(400).send("webrpc error: missing body");
    41  
    42                      return
    43                  }
    44  
    45                  switch(requestPath) {
    46                      {{range .Methods}}
    47  
    48                      case "/rpc/{{$name}}/{{.Name}}": {                        
    49                          try {
    50                              {{ range .Inputs }}
    51                                  {{- if not .Optional}}
    52                                      if (!("{{ .Name }}" in req.body)) {
    53                                          throw new WebRPCError("Missing Argument `{{ .Name }}`")
    54                                      }
    55                                  {{end -}}
    56  
    57                                  if ("{{ .Name }}" in req.body && !validateType(req.body["{{ .Name }}"], "{{ .Type | jsFieldType }}")) {
    58                                      throw new WebRPCError("Invalid Argument: {{ .Name }}")
    59                                  }
    60                              {{end}}
    61  
    62                              const response = await serviceImplementation["{{.Name}}"](req.body);
    63  
    64                              {{ range .Outputs}}
    65                                  if (!("{{ .Name }}" in response)) {
    66                                      throw new WebRPCError("internal", 500);
    67                                  }
    68                              {{end}}
    69  
    70                              res.status(200).json(response);
    71                          } catch (err) {
    72                              if (err instanceof WebRPCError) {
    73                                  const statusCode = err.statusCode || 400
    74                                  const message = err.message
    75  
    76                                  res.status(statusCode).json({
    77                                      msg: message,
    78                                      status: statusCode,
    79                                      code: ""
    80                                  });
    81  
    82                                  return
    83                              }
    84  
    85                              if (err.message) {
    86                                  res.status(400).send(err.message);
    87  
    88                                  return;
    89                              }
    90  
    91                              res.status(400).end();
    92                          }
    93                      }
    94  
    95                      return;
    96                      {{end}}
    97  
    98                      default: {
    99                          res.status(404).end()
   100                      }
   101                  }
   102              });
   103  
   104              return app;
   105          };
   106      {{- end}}
   107  {{end -}}
   108  {{end}}