forked from horizon-games/protoc-gen-twirp_ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twirp.go
47 lines (37 loc) · 1.04 KB
/
twirp.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
package main
var twirpFileName = "twirp.ts"
// based on https://github.com/larrymyers/protoc-gen-twirp_typescript/blob/master/example/ts_client/twirp.ts
var twirpSource = `
/* tslint:disable */
// This file has been generated by https://github.com/horizon-games/protoc-gen-twirp_ts.
// Do not edit.
export interface TwirpErrorJSON {
code: string
msg: string
meta: {
[index: string]: string
}
}
export class TwirpError extends Error {
code: string
meta: {
[index: string]: string
}
constructor(te: TwirpErrorJSON) {
super(te.msg)
this.code = te.code
this.meta = te.meta
}
}
export const throwTwirpError = (resp: Response) => {
return resp.json().then((err: TwirpErrorJSON) => { throw new TwirpError(err) })
}
export const createTwirpRequest = (body: object = {}, headers: object = {}): object => {
return {
method: 'POST',
headers: { ...headers, 'Content-Type': 'application/json' },
body: JSON.stringify(body || {})
}
}
export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>
`