Skip to content

Commit

Permalink
feat: move micro-gen common interface to gax (#646)
Browse files Browse the repository at this point in the history
* add common interface

* export operation

* lint

* export operations.d.ts

* fix
  • Loading branch information
xiaozhenliu-gg5 authored and alexander-fenster committed Nov 9, 2019
1 parent 235744e commit b93f9c1
Show file tree
Hide file tree
Showing 9 changed files with 17,072 additions and 33 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"files": [
"build/src",
"build/tools/compileProtos.js",
"protos"
"protos",
"build/protos/operations.d.ts"
],
"bin": {
"compileProtos": "./build/tools/compileProtos.js"
Expand Down Expand Up @@ -94,10 +95,10 @@
"test": "nyc mocha build/test/unit",
"lint": "gts check && eslint samples/*.js samples/**/*.js",
"clean": "gts clean",
"compile": "tsc -p . && cp src/*.json build/src && cp src/*.js build/src && cp -r test/fixtures build/test/",
"compile-protos": "pbjs -t json google/longrunning/operations.proto -p ./protos > protos/operations.json && pbjs -t json google/rpc/status.proto google/rpc/error_details.proto google/protobuf/any.proto -p ./protos > protos/status.json",
"compile": "tsc -p . && cp src/*.json build/src && cp src/*.js build/src && cp -r test/fixtures build/test",
"compile-protos": "pbjs -t json google/longrunning/operations.proto -p ./protos > protos/operations.json && pbjs -t json google/rpc/status.proto google/rpc/error_details.proto google/protobuf/any.proto -p ./protos > protos/status.json && pbjs -t static-module google/longrunning/operations.proto -p ./protos > protos/operations.js && pbts protos/operations.js -o protos/operations.d.ts",
"fix": "gts fix && eslint --fix samples/*.js samples/**/*.js",
"prepare": "npm run compile && node ./build/tools/prepublish.js",
"prepare": "npm run compile && node ./build/tools/prepublish.js && mkdir -p build/protos && cp protos/operations.d.ts build/protos/",
"posttest": "npm run lint",
"system-test": "nyc mocha build/test/system-test --timeout 120000",
"samples-test": "echo no sample tests 😱",
Expand Down
4,751 changes: 4,751 additions & 0 deletions protos/operations.d.ts

Large diffs are not rendered by default.

12,246 changes: 12,246 additions & 0 deletions protos/operations.js

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export interface Descriptor {
getApiCaller(settings: CallSettings): APICaller;
}

export {
LongRunningDescriptor as LongrunningDescriptor,
} from './longRunningCalls/longRunningDescriptor';
export {LongRunningDescriptor as LongrunningDescriptor} from './longRunningCalls/longRunningDescriptor';
export {PageDescriptor} from './paginationCalls/pageDescriptor';
export {StreamDescriptor} from './streamingCalls/streamDescriptor';
export {BundleDescriptor} from './bundlingCalls/bundleDescriptor';
48 changes: 47 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import {GrpcClient, GrpcClientOptions} from './grpc';
import {GrpcClient, GrpcClientOptions, ClientStubOptions} from './grpc';
import {GoogleAuthOptions} from 'google-auth-library';
import {
LongrunningDescriptor,
PageDescriptor,
StreamDescriptor,
} from './descriptor';
import * as longrunning from './longRunningCalls/longrunning';
import * as operationProtos from '../protos/operations';
import * as operationsClient from './operationsClient';
import * as routingHeader from './routingHeader';
import * as gax from './gax';

export {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
export {CancellablePromise, OngoingCall} from './call';
Expand Down Expand Up @@ -94,3 +103,40 @@ export {
GaxCall,
CancellableStream,
} from './apitypes';

export interface ClientOptions
extends GrpcClientOptions,
GoogleAuthOptions,
ClientStubOptions {
libName?: string;
libVersion?: string;
clientConfig?: gax.ClientConfig;
fallback?: boolean;
apiEndpoint?: string;
}

export interface Descriptors {
page: {[name: string]: PageDescriptor};
stream: {[name: string]: StreamDescriptor};
longrunning: {[name: string]: LongrunningDescriptor};
}

export interface Callback<
ResponseObject,
NextRequestObject,
RawResponseObject
> {
(
err: Error | null | undefined,
value?: ResponseObject | null,
nextRequest?: NextRequestObject,
rawResponse?: RawResponseObject
): void;
}

export interface LROperation<ResultType, MetadataType>
extends longrunning.Operation {
promise(): Promise<
[ResultType, MetadataType, operationProtos.google.longrunning.Operation]
>;
}
5 changes: 3 additions & 2 deletions src/longRunningCalls/longRunningApiCaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ import {
} from '../gax';
import {GoogleError} from '../googleError';

import {Operation, LROOperation} from './longrunning';
import {Operation} from './longrunning';
import {LongRunningDescriptor} from './longRunningDescriptor';
import * as operationProtos from '../../protos/operations';

export class LongrunningApiCaller implements APICaller {
longrunningDescriptor: LongRunningDescriptor;
Expand Down Expand Up @@ -105,7 +106,7 @@ export class LongrunningApiCaller implements APICaller {
}

const operation = new Operation(
rawResponse as LROOperation,
rawResponse as operationProtos.google.longrunning.Operation,
longrunningDescriptor,
backoffSettings!,
settings
Expand Down
32 changes: 12 additions & 20 deletions src/longRunningCalls/longrunning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {BackoffSettings, CallOptions} from '../gax';
import {GoogleError} from '../googleError';
import {Metadata} from '../grpc';
import {LongRunningDescriptor} from './longRunningDescriptor';
import * as operationProtos from '../../protos/operations';

/**
* @callback GetOperationCallback
Expand All @@ -55,16 +56,7 @@ export interface GetOperationCallback {
): void;
}

// This interface is essentially the TypeScript representation of google.longrunning.Operation.
// TODO: we'll replace it with the proper type generated by `pbts`.
export interface LROOperation {
name: string;
metadata: {value: Uint8Array};
done: boolean;
result: string;
error: GoogleError;
response: {value: Uint8Array};
}
type LROOperation = operationProtos.google.longrunning.Operation;

export class Operation extends EventEmitter {
completeListeners: number;
Expand Down Expand Up @@ -106,7 +98,7 @@ export class Operation extends EventEmitter {
this.latestResponse = grpcOp;
this.name = this.latestResponse.name;
this.done = this.latestResponse.done;
this.error = this.latestResponse.error;
this.error = (this.latestResponse.error as unknown) as GoogleError;
this.longrunningDescriptor = longrunningDescriptor;
this.result = null;
this.metadata = null;
Expand Down Expand Up @@ -189,8 +181,8 @@ export class Operation extends EventEmitter {
const PromiseCtor = self._callOptions!.promise!;
return new PromiseCtor((resolve, reject) => {
if (self.latestResponse.error) {
const error = new GoogleError(self.latestResponse.error.message);
error.code = self.latestResponse.error.code;
const error = new GoogleError(self.latestResponse.error.message!);
error.code = self.latestResponse.error.code!;
reject(error);
} else {
resolve([self.result, self.metadata, self.latestResponse]);
Expand Down Expand Up @@ -229,8 +221,8 @@ export class Operation extends EventEmitter {

if (op.done) {
if (op.result === 'error') {
const error = new GoogleError(op.error!.message);
error.code = op.error!.code;
const error = new GoogleError(op.error!.message!);
error.code = op.error!.code!;
this.error = error;
if (callback) {
callback(error);
Expand All @@ -240,14 +232,14 @@ export class Operation extends EventEmitter {

if (responseDecoder && op.response) {
this.response = op.response;
response = responseDecoder(op.response.value);
response = responseDecoder(op.response.value!);
this.result = response;
this.done = true;
}
}

if (metadataDecoder && op.metadata) {
metadata = (metadataDecoder(op.metadata.value) as unknown) as Metadata;
metadata = (metadataDecoder(op.metadata.value!) as unknown) as Metadata;
this.metadata = metadata;
}
if (callback) {
Expand Down Expand Up @@ -277,7 +269,7 @@ export class Operation extends EventEmitter {
}
let previousMetadataBytes: Uint8Array;
if (this.latestResponse.metadata) {
previousMetadataBytes = this.latestResponse.metadata.value;
previousMetadataBytes = this.latestResponse.metadata.value!;
}

// tslint:disable-next-line no-any
Expand Down Expand Up @@ -322,12 +314,12 @@ export class Operation extends EventEmitter {
(!previousMetadataBytes ||
(rawResponse &&
!arrayEquals(
rawResponse.metadata.value,
rawResponse.metadata.value!,
previousMetadataBytes
)))
) {
setImmediate(emit, 'progress', metadata, rawResponse);
previousMetadataBytes = rawResponse!.metadata!.value;
previousMetadataBytes = rawResponse!.metadata!.value!;
}
// special case: some APIs fail to set either result or error
// but set done = true (e.g. speech with silent file).
Expand Down
5 changes: 4 additions & 1 deletion test/unit/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ describe('Task', () => {
expected: [data],
},
{
data: [[data, data], [data, data, data]],
data: [
[data, data],
[data, data, data],
],
message: 'a single message added',
expected: [data, data, data, data, data],
},
Expand Down
5 changes: 3 additions & 2 deletions test/unit/longrunning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {status} from '@grpc/grpc-js';
import * as sinon from 'sinon';

import {LongrunningDescriptor} from '../../src';
import * as operationProtos from '../../protos/operations';
import {GaxCallPromise} from '../../src/apitypes';
import * as gax from '../../src/gax';
import {GoogleError} from '../../src/googleError';
Expand Down Expand Up @@ -205,7 +206,7 @@ describe('longrunning', () => {
totalTimeoutMillis
);
const operation = longrunning.operation(
(SUCCESSFUL_OP as {}) as longrunning.LROOperation,
(SUCCESSFUL_OP as {}) as operationProtos.google.longrunning.Operation,
desc,
backoff
);
Expand Down Expand Up @@ -505,7 +506,7 @@ describe('longrunning', () => {
return promise;
}
const operation = longrunning.operation(
(SUCCESSFUL_OP as {}) as longrunning.LROOperation,
(SUCCESSFUL_OP as {}) as operationProtos.google.longrunning.Operation,
desc,
backoff,
{
Expand Down

0 comments on commit b93f9c1

Please sign in to comment.