Skip to content

Commit

Permalink
Add ability to use Buf Schema Registry as a schema source for gRPC re…
Browse files Browse the repository at this point in the history
…quests (Kong#6975)

* Add support for Buf Reflection Api

* Add test; Change tooltips to links

* style

* Remove label class

* request tests

* Update copy

* Rename prop; Fix alignment, input

* Add user agent header

* use onBlur and simplify

* fix lint

---------

Co-authored-by: jackkav <jackkav@gmail.com>
  • Loading branch information
srikrsna-buf and jackkav committed Mar 13, 2024
1 parent f70e7ab commit 1f4ed9f
Show file tree
Hide file tree
Showing 9 changed files with 441 additions and 23 deletions.
50 changes: 50 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/insomnia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
"dependencies": {
"@apideck/better-ajv-errors": "^0.3.6",
"@apidevtools/swagger-parser": "10.1.0",
"@bufbuild/protobuf": "^1.4.1",
"@connectrpc/connect": "^1.1.3",
"@connectrpc/connect-node": "^1.1.3",
"@getinsomnia/node-libcurl": "^2.4.1-9",
"@grpc/grpc-js": "^1.8.17",
"@grpc/proto-loader": "^0.7.7",
Expand Down
4 changes: 4 additions & 0 deletions packages/insomnia/src/__jest__/setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { TextDecoder, TextEncoder } from 'util';

Object.assign(globalThis, { TextDecoder, TextEncoder });

globalThis.__DEV__ = false;

globalThis.requestAnimationFrame = (callback: FrameRequestCallback) => {
Expand Down
89 changes: 86 additions & 3 deletions packages/insomnia/src/main/ipc/__tests__/grpc.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import {
AnyMessage,
MethodInfo,
PartialMessage,
ServiceType,
} from '@bufbuild/protobuf';
import { UnaryResponse } from '@connectrpc/connect';
import { createConnectTransport } from '@connectrpc/connect-node';
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import * as grpcReflection from 'grpc-reflection-js';
import protobuf from 'protobufjs';
Expand All @@ -6,6 +14,7 @@ import { globalBeforeEach } from '../../../__jest__/before-each';
import { loadMethodsFromReflection } from '../grpc';

jest.mock('grpc-reflection-js');
jest.mock('@connectrpc/connect-node');

describe('loadMethodsFromReflection', () => {
beforeEach(globalBeforeEach);
Expand Down Expand Up @@ -37,7 +46,11 @@ describe('loadMethodsFromReflection', () => {
});

it('parses methods', async () => {
const methods = await loadMethodsFromReflection({ url: 'foo.com', metadata: [] });
const methods = await loadMethodsFromReflection({
url: 'foo.com',
metadata: [],
reflectionApi: { enabled: false, apiKey: '', url: '', module: '' },
});
expect(methods).toStrictEqual([{
type: 'unary',
fullPath: '/FooService/Foo',
Expand Down Expand Up @@ -75,7 +88,11 @@ describe('loadMethodsFromReflection', () => {
});

it('parses methods', async () => {
const methods = await loadMethodsFromReflection({ url: 'foo.com', metadata: [] });
const methods = await loadMethodsFromReflection({
url: 'foo.com',
metadata: [],
reflectionApi: { enabled: false, apiKey: '', url: '', module: '' },
});
expect(methods).toStrictEqual([{
type: 'unary',
fullPath: '/FooService/format',
Expand Down Expand Up @@ -125,7 +142,11 @@ describe('loadMethodsFromReflection', () => {
});

it('parses methods', async () => {
const methods = await loadMethodsFromReflection({ url: 'foo-bar.com', metadata: [] });
const methods = await loadMethodsFromReflection({
url: 'foo-bar.com',
metadata: [],
reflectionApi: { enabled: false, apiKey: '', url: '', module: '' },
});
expect(methods).toStrictEqual([{
type: 'unary',
fullPath: '/FooService/Foo',
Expand All @@ -142,4 +163,66 @@ describe('loadMethodsFromReflection', () => {
});
});

describe('buf reflection api', () => {
it('loads module', async () => {
(createConnectTransport as unknown as jest.Mock).mockImplementation(
options => {
expect(options.baseUrl).toStrictEqual('https://buf.build');
return {
async unary(
service: ServiceType,
method: MethodInfo,
_: AbortSignal | undefined,
__: number | undefined,
header: HeadersInit | undefined,
input: PartialMessage<AnyMessage>
): Promise<UnaryResponse> {
expect(new Headers(header).get('Authorization')).toStrictEqual('Bearer TEST_KEY');
expect(input).toStrictEqual({ module: 'buf.build/connectrpc/eliza' });
return {
service: service,
method: method,
header: new Headers(),
trailer: new Headers(),
stream: false,
// Output of running `buf curl https://buf.build/buf.reflect.v1beta1.FileDescriptorSetService/GetFileDescriptorSet --data '{"module": "buf.build/connectrpc/eliza"}' --schema buf.build/bufbuild/reflect -H 'Authorization: Bearer buf-token'`
message: method.O.fromJsonString(
'{"fileDescriptorSet":{"file":[{"name":"connectrpc/eliza/v1/eliza.proto","package":"connectrpc.eliza.v1","messageType":[{"name":"SayRequest","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"SayResponse","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"ConverseRequest","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"ConverseResponse","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]},{"name":"IntroduceRequest","field":[{"name":"name","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"name"}]},{"name":"IntroduceResponse","field":[{"name":"sentence","number":1,"label":"LABEL_OPTIONAL","type":"TYPE_STRING","jsonName":"sentence"}]}],"service":[{"name":"ElizaService","method":[{"name":"Say","inputType":".connectrpc.eliza.v1.SayRequest","outputType":".connectrpc.eliza.v1.SayResponse","options":{"idempotencyLevel":"NO_SIDE_EFFECTS"}},{"name":"Converse","inputType":".connectrpc.eliza.v1.ConverseRequest","outputType":".connectrpc.eliza.v1.ConverseResponse","options":{},"clientStreaming":true,"serverStreaming":true},{"name":"Introduce","inputType":".connectrpc.eliza.v1.IntroduceRequest","outputType":".connectrpc.eliza.v1.IntroduceResponse","options":{},"serverStreaming":true}]}],"syntax":"proto3"}]},"version":"233fca715f49425581ec0a1b660be886"}'
),
};
},
};
}
);
const methods = await loadMethodsFromReflection({
url: 'foo.com',
metadata: [],
reflectionApi: {
enabled: true,
apiKey: 'TEST_KEY',
url: 'https://buf.build',
module: 'buf.build/connectrpc/eliza',
},
});
expect(methods).toStrictEqual(
[
{
example: undefined,
fullPath: '/connectrpc.eliza.v1.ElizaService/Say',
type: 'unary',
},
{
example: undefined,
fullPath: '/connectrpc.eliza.v1.ElizaService/Converse',
type: 'bidi',
},
{
example: undefined,
fullPath: '/connectrpc.eliza.v1.ElizaService/Introduce',
type: 'server',
},
]
);
});
});
});
Loading

0 comments on commit 1f4ed9f

Please sign in to comment.