-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
123 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v18.15.0 | ||
v18.18.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,50 @@ | ||
# Protoplugin Example | ||
|
||
This directory contains an example plugin, which shows how to work with the | ||
plugin framework. It also contains a separate webpage which shows the generated files working with a remote server. | ||
This example shows how to write a custom plugin. We generate [Twirp](https://twitchtv.github.io/twirp/docs/spec_v7.html) | ||
clients from service definitions in Protobuf files. | ||
|
||
The code generation logic for the actual plugin is located in [`protoc-gen-twirp-es.ts`](src/protoc-gen-twirp-es.ts). | ||
|
||
The sample plugin generates a [Twirp](https://twitchtv.github.io/twirp/docs/spec_v7.html) client from service | ||
definitions in Protobuf files. The Twirp client uses base types generated from | ||
[`@bufbuild/protobuf-es`](https://www.npmjs.com/package/@bufbuild/protoc-gen-es). | ||
## Run the example | ||
|
||
From the project root, first install and build all required packages: | ||
You will need [Node](https://nodejs.org/en/download/) in version 18.17.0 or later installed. | ||
Download the example project and install its dependencies: | ||
|
||
```shell | ||
npm install -w packages/protoplugin-example | ||
npm run -w packages/protobuf build | ||
npm run -w packages/protoplugin build | ||
npm run -w packages/protoc-gen-es build | ||
curl -L https://github.com/bufbuild/protobuf-es/archive/refs/heads/main.zip > protobuf-es-main.zip | ||
unzip protobuf-es-main.zip 'protobuf-es-main/packages/protoplugin-example/*' | ||
|
||
cd protobuf-es-main/packages/protoplugin-example | ||
npm install | ||
``` | ||
|
||
To see the client in action: | ||
|
||
```shell | ||
npm start | ||
``` | ||
|
||
Open http://127.0.0.1:3000/ in your browser. | ||
|
||
|
||
To re-generate code: | ||
|
||
```shell | ||
npx buf generate buf.build/connectrpc/eliza | ||
``` | ||
|
||
Next, `cd` into the example directory and build: | ||
This will generate the [Eliza module](https://buf.build/connectrpc/eliza) from the Buf Schema Registry (BSR). | ||
You can change this path to generate additional files locally or from the BSR. | ||
|
||
Test the generated code: | ||
|
||
```shell | ||
cd packages/protoplugin-example | ||
npm run build | ||
npm test | ||
``` | ||
|
||
To run the plugin (i.e. generate files), use the following command. This will generate files based on the | ||
[Eliza module](https://buf.build/connectrpc/eliza) in the Buf Schema Registry (BSR). You can change this path to generate | ||
additional files locally or from the BSR. | ||
## About this example | ||
|
||
`npx buf generate buf.build/connectrpc/eliza` | ||
This example is a starting point - we encourage you to try it out and experiment. | ||
|
||
To run the example webpage and see the generated code in action: | ||
Take a look at the code generation logic in [protoc-gen-twirp-es.ts](./src/protoc-gen-twirp-es.ts), | ||
and at [buf.gen.yaml](./buf.gen.yaml) for how it is invoked. | ||
|
||
`npm run start` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/protoplugin-example/src/gen/connectrpc/eliza/v1/eliza_pb.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/protoplugin-example/src/gen/connectrpc/eliza/v1/eliza_twirp.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2021-2023 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import * as assert from "node:assert/strict"; | ||
import { describe, it, mock } from "node:test"; | ||
import { ElizaServiceClient } from "../src/gen/connectrpc/eliza/v1/eliza_twirp"; | ||
import { SayRequest } from "../src/gen/connectrpc/eliza/v1/eliza_pb"; | ||
|
||
describe("custom plugin", async () => { | ||
it("should generate client class", () => { | ||
assert.equal(typeof ElizaServiceClient, "function"); | ||
const client = new ElizaServiceClient("https://example.com"); | ||
assert.ok(client !== undefined); | ||
}); | ||
describe("generated client", () => { | ||
it("should should take argument in constructor", () => { | ||
const client = new ElizaServiceClient("https://example.com"); | ||
assert.ok(client !== undefined); | ||
assert.equal( | ||
(client as unknown as Record<string, unknown>).baseUrl, | ||
"https://example.com", | ||
); | ||
}); | ||
it("should have method for unary RPC", () => { | ||
const client = new ElizaServiceClient("https://example.com"); | ||
assert.equal(typeof client.say, "function"); | ||
}); | ||
it("should use fetch", async (t) => { | ||
let fetch = mock.fn<typeof globalThis.fetch>(globalThis.fetch); | ||
globalThis.fetch = fetch; | ||
t.after(() => fetch.mock.restore()); | ||
fetch.mock.mockImplementationOnce( | ||
async () => | ||
new Response('{"sentence":"ho"}', { | ||
status: 200, | ||
headers: { "Content-Type": "application/json" }, | ||
}), | ||
); | ||
const client = new ElizaServiceClient("https://example.com"); | ||
const res = await client.say(new SayRequest({ sentence: "hi" })); | ||
assert.equal(res.sentence, "ho"); | ||
assert.equal(fetch.mock.callCount(), 1); | ||
const [argInput, argInit] = fetch.mock.calls[0].arguments; | ||
assert.strictEqual( | ||
argInput, | ||
"https://example.com/connectrpc.eliza.v1.ElizaService/Say", | ||
); | ||
assert.equal(argInit?.method, "POST"); | ||
assert.equal( | ||
new Headers(argInit?.headers).get("Content-Type"), | ||
"application/json", | ||
); | ||
assert.equal(argInit?.body, '{"sentence":"hi"}'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters