-
Notifications
You must be signed in to change notification settings - Fork 17
/
client.ts
41 lines (35 loc) · 1.27 KB
/
client.ts
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
import axios from "axios";
import {HaberdasherClientJSON, HaberdasherClientProtobuf} from "./generated/service.twirp";
import {NodeHttpRPC} from "../src/twirp";
interface Rpc {
request(
service: string,
method: string,
contentType: "application/json" | "application/protobuf",
data: object | Uint8Array
): Promise<object | Uint8Array>;
}
const client = axios.create({
baseURL: "http://localhost:8000/twirp",
})
export const axiosImplementation: Rpc = {
request(service, method, contentType, data) {
return client.post(`${service}/${method}`, data, {
responseType: contentType === "application/protobuf" ? 'arraybuffer' : "json",
headers: {
"content-type": contentType,
}
}).then(response => {
return response.data
});
}
}
export const jsonClient = new HaberdasherClientJSON(axiosImplementation);
// export const protobufClient = new HaberdasherClientProtobuf(axiosImplementation);
// Standard implementation
// export const jsonClient = new HaberdasherClientJSON(NodeHttpRPC({
// baseUrl: "http://localhost:8000/twirp",
// }));
export const protobufClient = new HaberdasherClientProtobuf(NodeHttpRPC({
baseUrl: "http://localhost:8000/twirp",
}));