diff --git a/src/longRunningCalls/longRunningApiCaller.ts b/src/longRunningCalls/longRunningApiCaller.ts index 32d2fa612..f41bfb4ca 100644 --- a/src/longRunningCalls/longRunningApiCaller.ts +++ b/src/longRunningCalls/longRunningApiCaller.ts @@ -41,7 +41,7 @@ import { } from '../gax'; import {GoogleError} from '../googleError'; -import {Operation} from './longrunning'; +import {Operation, LROOperation} from './longrunning'; import {LongRunningDescriptor} from './longRunningDescriptor'; export class LongrunningApiCaller implements APICaller { @@ -105,7 +105,7 @@ export class LongrunningApiCaller implements APICaller { } const operation = new Operation( - rawResponse as Operation, + rawResponse as LROOperation, longrunningDescriptor, backoffSettings!, settings diff --git a/src/longRunningCalls/longRunningDescriptor.ts b/src/longRunningCalls/longRunningDescriptor.ts index e93543256..285ff59ce 100644 --- a/src/longRunningCalls/longRunningDescriptor.ts +++ b/src/longRunningCalls/longRunningDescriptor.ts @@ -29,18 +29,18 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import * as protobuf from 'protobufjs'; import {Descriptor} from '../descriptor'; import {CallSettings} from '../gax'; import {Metadata} from '../grpc'; import {OperationsClient} from '../operationsClient'; - import {LongrunningApiCaller} from './longRunningApiCaller'; /** * A callback to upack a google.protobuf.Any message. */ export interface AnyDecoder { - (message: {}): Metadata; + (reader: protobuf.Reader | Uint8Array, length?: number): protobuf.Message<{}>; } /** diff --git a/src/longRunningCalls/longrunning.ts b/src/longRunningCalls/longrunning.ts index 6ab0feb5f..3069601a6 100644 --- a/src/longRunningCalls/longrunning.ts +++ b/src/longRunningCalls/longrunning.ts @@ -36,8 +36,7 @@ import {GaxCallPromise, ResultTuple} from '../apitypes'; import {CancellablePromise} from '../call'; import {BackoffSettings, CallOptions} from '../gax'; import {GoogleError} from '../googleError'; -import {Metadata, MetadataValue} from '../grpc'; - +import {Metadata} from '../grpc'; import {LongRunningDescriptor} from './longRunningDescriptor'; /** @@ -52,14 +51,25 @@ export interface GetOperationCallback { err?: Error | null, result?: {}, metadata?: {}, - rawResponse?: Operation + rawResponse?: LROOperation ): 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}; +} + export class Operation extends EventEmitter { completeListeners: number; hasActiveListeners: boolean; - latestResponse: Operation; + latestResponse: LROOperation; longrunningDescriptor: LongRunningDescriptor; result: {} | null; metadata: Metadata | null; @@ -69,7 +79,7 @@ export class Operation extends EventEmitter { name?: string; done?: boolean; error?: GoogleError; - response?: {value: {}}; + response?: {}; /** * Wrapper for a google.longrunnung.Operation. @@ -85,7 +95,7 @@ export class Operation extends EventEmitter { * requests. */ constructor( - grpcOp: Operation, + grpcOp: LROOperation, longrunningDescriptor: LongRunningDescriptor, backoffSettings: BackoffSettings, callOptions?: CallOptions @@ -201,8 +211,8 @@ export class Operation extends EventEmitter { ); const noCallbackPromise = this.currentCallPromise_!.then(responses => { - self.latestResponse = responses[0] as Operation; - self._unpackResponse(responses[0] as Operation, callback); + self.latestResponse = responses[0] as LROOperation; + self._unpackResponse(responses[0] as LROOperation, callback); return promisifyResponse()!; }); @@ -211,7 +221,7 @@ export class Operation extends EventEmitter { } } - _unpackResponse(op: Operation, callback?: GetOperationCallback) { + _unpackResponse(op: LROOperation, callback?: GetOperationCallback) { const responseDecoder = this.longrunningDescriptor.responseDecoder; const metadataDecoder = this.longrunningDescriptor.metadataDecoder; let response: {}; @@ -237,7 +247,7 @@ export class Operation extends EventEmitter { } if (metadataDecoder && op.metadata) { - metadata = metadataDecoder(op.metadata.value); + metadata = (metadataDecoder(op.metadata.value) as unknown) as Metadata; this.metadata = metadata; } if (callback) { @@ -265,7 +275,7 @@ export class Operation extends EventEmitter { if (this.backoffSettings.totalTimeoutMillis) { deadline = now.getTime() + this.backoffSettings.totalTimeoutMillis; } - let previousMetadataBytes: MetadataValue; + let previousMetadataBytes: Uint8Array; if (this.latestResponse.metadata) { previousMetadataBytes = this.latestResponse.metadata.value; } @@ -378,7 +388,7 @@ export class Operation extends EventEmitter { * requests. */ export function operation( - op: Operation, + op: LROOperation, longrunningDescriptor: LongRunningDescriptor, backoffSettings: BackoffSettings, callOptions?: CallOptions diff --git a/test/unit/longrunning.ts b/test/unit/longrunning.ts index 87f3a86bd..18c100287 100644 --- a/test/unit/longrunning.ts +++ b/test/unit/longrunning.ts @@ -205,7 +205,7 @@ describe('longrunning', () => { totalTimeoutMillis ); const operation = longrunning.operation( - (SUCCESSFUL_OP as {}) as longrunning.Operation, + (SUCCESSFUL_OP as {}) as longrunning.LROOperation, desc, backoff ); @@ -488,7 +488,7 @@ describe('longrunning', () => { return promise; } const operation = longrunning.operation( - (SUCCESSFUL_OP as {}) as longrunning.Operation, + (SUCCESSFUL_OP as {}) as longrunning.LROOperation, desc, backoff, {