Skip to content

Commit

Permalink
Improved example
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos committed Jun 14, 2021
1 parent 0c0e4dc commit 213ebb2
Show file tree
Hide file tree
Showing 4 changed files with 1,029 additions and 25 deletions.
16 changes: 13 additions & 3 deletions example/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios";
import {HaberdasherClientJSON, HaberdasherClientProtobuf} from "./generated/haberdasher.twirp";
import {NodeHttpRPC} from "twirp-ts";

interface Rpc {
request(
Expand All @@ -14,7 +15,7 @@ const client = axios.create({
baseURL: "http://localhost:8000/twirp",
})

export const httpImplementation: Rpc = {
export const axiosImplementation: Rpc = {
request(service, method, contentType, data) {
return client.post(`${service}/${method}`, data, {
responseType: contentType === "application/protobuf" ? 'arraybuffer' : "json",
Expand All @@ -27,5 +28,14 @@ export const httpImplementation: Rpc = {
}
}

export const jsonClient = new HaberdasherClientJSON(httpImplementation);
export const protobufClient = new HaberdasherClientProtobuf(httpImplementation);
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",
// }));
35 changes: 22 additions & 13 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as http from "http";
import express from 'express';
import {createHaberdasherServer} from "./generated/haberdasher.twirp";
import {TwirpContext} from "twirp-ts";
import {Hat, Size} from "./generated/service";
Expand All @@ -7,23 +8,31 @@ import {jsonClient, protobufClient} from "./client";
const server = createHaberdasherServer({
async MakeHat(ctx: TwirpContext, request: Size): Promise<Hat> {
return Hat.fromPartial({
name: "wooow",
name: "cup",
inches: 3,
color: "blue",
});
},
});

http
.createServer(server.httpHandler())
.listen(8080, async () => {
const jsonResp = await jsonClient.MakeHat({
inches: 1,
});
const app = express();

console.log("response from JSON client", jsonResp);
app.use("/twirp", server.httpHandler({
prefix: false,
}));

http.createServer(app).listen(8000, async () => {
const jsonResp = await jsonClient.MakeHat({
inches: 1,
});

console.log("response from JSON client", jsonResp);

const protobufResp = await protobufClient.MakeHat({
inches: 1,
});

console.log("response from Protobuf client", protobufResp);
});

const protobufResp = await protobufClient.MakeHat({
inches: 1,
});

console.log("response from Protobuf client", protobufResp);
});
Loading

0 comments on commit 213ebb2

Please sign in to comment.