Skip to content

Commit

Permalink
fix: Use the right Metadata for grpc-web. Fixes #270. (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenh authored Apr 7, 2021
1 parent 51e5386 commit 640e645
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
3 changes: 1 addition & 2 deletions integration/grpc-web-no-streaming-observable/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { grpc } from '@improbable-eng/grpc-web';
import { Observable } from 'rxjs';
import { BrowserHeaders } from 'browser-headers';
import { take } from 'rxjs/operators';
import { Metadata } from 'grpc';

export const protobufPackage = 'rpx';

Expand Down Expand Up @@ -355,7 +354,7 @@ export const Empty = {
* but with the streaming method removed.
*/
export interface DashState {
UserSettings(request: DeepPartial<Empty>, metadata?: Metadata): Observable<DashUserSettingsState>;
UserSettings(request: DeepPartial<Empty>, metadata?: grpc.Metadata): Observable<DashUserSettingsState>;
}

export class DashStateClientImpl implements DashState {
Expand Down
3 changes: 1 addition & 2 deletions integration/grpc-web-no-streaming/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { util, configure, Writer, Reader } from 'protobufjs/minimal';
import * as Long from 'long';
import { grpc } from '@improbable-eng/grpc-web';
import { BrowserHeaders } from 'browser-headers';
import { Metadata } from 'grpc';

export const protobufPackage = 'rpx';

Expand Down Expand Up @@ -353,7 +352,7 @@ export const Empty = {
* but with the streaming method removed.
*/
export interface DashState {
UserSettings(request: DeepPartial<Empty>, metadata?: Metadata): Promise<DashUserSettingsState>;
UserSettings(request: DeepPartial<Empty>, metadata?: grpc.Metadata): Promise<DashUserSettingsState>;
}

export class DashStateClientImpl implements DashState {
Expand Down
14 changes: 14 additions & 0 deletions integration/grpc-web/example-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DashStateClientImpl } from './example';

describe('grpc-web', () => {
it('at least compiles', () => {
// TODO move the hand-run `client-ts` integration into here, but for now
// at least check that things compile
const rpc = {
unary: jest.fn(),
invoke: jest.fn(),
};
const client = new DashStateClientImpl(rpc);
client.UserSettings({});
});
});
11 changes: 5 additions & 6 deletions integration/grpc-web/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { grpc } from '@improbable-eng/grpc-web';
import { Observable } from 'rxjs';
import { BrowserHeaders } from 'browser-headers';
import { share } from 'rxjs/operators';
import { Metadata } from 'grpc';

export const protobufPackage = 'rpx';

Expand Down Expand Up @@ -731,8 +730,8 @@ export const Empty = {
};

export interface DashState {
UserSettings(request: DeepPartial<Empty>, metadata?: Metadata): Promise<DashUserSettingsState>;
ActiveUserSettingsStream(request: DeepPartial<Empty>, metadata?: Metadata): Observable<DashUserSettingsState>;
UserSettings(request: DeepPartial<Empty>, metadata?: grpc.Metadata): Promise<DashUserSettingsState>;
ActiveUserSettingsStream(request: DeepPartial<Empty>, metadata?: grpc.Metadata): Observable<DashUserSettingsState>;
}

export class DashStateClientImpl implements DashState {
Expand Down Expand Up @@ -805,9 +804,9 @@ export const DashStateActiveUserSettingsStreamDesc: UnaryMethodDefinitionish = {
* ----------------------
*/
export interface DashAPICreds {
Create(request: DeepPartial<DashAPICredsCreateReq>, metadata?: Metadata): Promise<DashCred>;
Update(request: DeepPartial<DashAPICredsUpdateReq>, metadata?: Metadata): Promise<DashCred>;
Delete(request: DeepPartial<DashAPICredsDeleteReq>, metadata?: Metadata): Promise<DashCred>;
Create(request: DeepPartial<DashAPICredsCreateReq>, metadata?: grpc.Metadata): Promise<DashCred>;
Update(request: DeepPartial<DashAPICredsUpdateReq>, metadata?: grpc.Metadata): Promise<DashCred>;
Delete(request: DeepPartial<DashAPICredsDeleteReq>, metadata?: grpc.Metadata): Promise<DashCred>;
}

export class DashAPICredsClientImpl implements DashAPICreds {
Expand Down
5 changes: 3 additions & 2 deletions src/generate-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { Context } from './context';
const hash = imp('hash*object-hash');
const dataloader = imp('DataLoader*dataloader');
const Reader = imp('Reader@protobufjs/minimal');
const Metadata = imp('Metadata@grpc');

/**
* Generates an interface for `serviceDesc`.
Expand Down Expand Up @@ -67,8 +66,10 @@ export function generateService(

// Use metadata as last argument for interface only configuration
if (options.outputClientImpl === 'grpc-web') {
params.push(code`metadata?: ${Metadata}`);
// We have to use grpc.Metadata where grpc will come from @improbable-eng
params.push(code`metadata?: grpc.Metadata`);
} else if (options.addGrpcMetadata) {
const Metadata = imp('Metadata@grpc');
const q = options.addNestjsRestParameter ? '' : '?';
params.push(code`metadata${q}: ${Metadata}`);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ describe('options', () => {
}
`);
});

it('can set outputJsonMethods with nestJs=true', () => {
const options = optionsFromParameter('outputClientImpl=grpc-web,addGrpcMetadata=false');
expect(options).toMatchObject({
outputClientImpl: 'grpc-web',
addGrpcMetadata: false,
});
});
});

0 comments on commit 640e645

Please sign in to comment.