File

src/service-object.ts

Extends

ServiceObject

Index

Properties
Methods

Constructor

constructor(config: ServiceObjectConfig)

GrpcServiceObject is a base class, meant to be inherited from by a service object that uses the gRPC protobuf API.

Parameters :
Name Type Optional Description
config ServiceObjectConfig No
  • Configuration object.

Properties

parent
Type : GrpcServiceObject

Methods

delete
delete()

Delete the object.

Returns : Promise<>
delete
delete(callback?: RequestCallback)
Parameters :
Name Type Optional
callback RequestCallback Yes
Returns : void | Promise
delete
delete(callback: RequestCallback)
Parameters :
Name Type Optional
callback RequestCallback No
Returns : void
getMetadata
getMetadata()

Get the metadata of this object.

Returns : Promise<Metadata>
getMetadata
getMetadata(callback: MetadataCallback)
Parameters :
Name Type Optional
callback MetadataCallback No
Returns : void
getMetadata
getMetadata(callback?: MetadataCallback)
Parameters :
Name Type Optional
callback MetadataCallback Yes
Returns : void | Promise
request
request(...args: Array)

Patch a request to the GrpcService object.

Parameters :
Name Type Optional
args Array<literal type> No
Returns : any
requestStream
requestStream(...args: Array)

Patch a streaming request to the GrpcService object.

Parameters :
Name Type Optional
args Array<literal type> No
Returns : any
requestWritableStream
requestWritableStream(...args: Array)

Patch a writable streaming request to the GrpcService object.

Parameters :
Name Type Optional
args Array<literal type> No
Returns : any
setMetadata
setMetadata(metadata: Metadata, callback?: ResponseCallback)
Parameters :
Name Type Optional
metadata Metadata No
callback ResponseCallback Yes
Returns : void | Promise
setMetadata
setMetadata(metadata: Metadata, callback: ResponseCallback)
Parameters :
Name Type Optional
metadata Metadata No
callback ResponseCallback No
Returns : void
setMetadata
setMetadata(metadata: Metadata)

Set the metadata for this object.

Parameters :
Name Type Optional Description
metadata Metadata No
  • The metadata to set on this object.
Returns : Promise<SetMetadataResponse>
import {
  Metadata,
  MetadataCallback,
  ResponseCallback,
  ServiceObject,
  ServiceObjectConfig,
  SetMetadataResponse,
  util,
} from '@google-cloud/common';
import {promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import {CoreOptions, RequestCallback, Response} from 'teeny-request';

export class GrpcServiceObject extends ServiceObject {
  parent!: GrpcServiceObject;

  /**
   * GrpcServiceObject is a base class, meant to be inherited from by a service
   * object that uses the gRPC protobuf API.
   *
   * @constructor
   * @alias module:common/grpc-service-object
   *
   * @private
   *
   * @param {object} config - Configuration object.
   */
  constructor(config: ServiceObjectConfig) {
    super(config);
  }

  /**
   * Delete the object.
   *
   * @param {function=} callback - The callback function.
   * @param {?error} callback.err - An error returned while making this request.
   */
  delete(): Promise<[Response]>;
  delete(callback: RequestCallback): void;
  delete(callback?: RequestCallback): void | Promise<[Response]> {
    // tslint:disable-next-line:no-any
    const protoOpts = (this.methods.delete as any).protoOpts;
    const reqOpts = this.getOpts(this.methods.delete);
    this.request(protoOpts, reqOpts, callback || util.noop);
  }

  /**
   * Get the metadata of this object.
   *
   * @param {function} callback - The callback function.
   * @param {?error} callback.err - An error returned while making this request.
   * @param {object} callback.metadata - The metadata for this object.
   */
  getMetadata(): Promise<Metadata>;
  getMetadata(callback: MetadataCallback): void;
  getMetadata(callback?: MetadataCallback): void | Promise<Metadata> {
    // tslint:disable-next-line:no-any
    const protoOpts = (this.methods.getMetadata as any).protoOpts;
    const reqOpts = this.getOpts(this.methods.getMetadata);
    this.request(protoOpts, reqOpts, (err: Error, resp: Response) => {
      if (err) {
        callback!(err, null, resp);
        return;
      }
      this.metadata = resp;
      callback!(null, this.metadata, resp);
    });
  }

  /**
   * Set the metadata for this object.
   *
   * @param {object} metadata - The metadata to set on this object.
   * @param {function=} callback - The callback function.
   * @param {?error} callback.err - An error returned while making this request.
   */
  setMetadata(metadata: Metadata): Promise<SetMetadataResponse>;
  setMetadata(metadata: Metadata, callback: ResponseCallback): void;
  setMetadata(
    metadata: Metadata,
    callback?: ResponseCallback
  ): void | Promise<SetMetadataResponse> {
    // tslint:disable-next-line:no-any
    const protoOpts = (this.methods.setMetadata as any).protoOpts;
    const reqOpts = extend(
      true,
      {},
      this.getOpts(this.methods.setMetadata),
      metadata
    );
    this.request(protoOpts, reqOpts, callback || util.noop);
  }

  /**
   * Patch a request to the GrpcService object.
   *
   * @private
   */
  request(...args: Array<{}>) {
    return this.parent.request.apply(this.parent, args);
  }

  /**
   * Patch a streaming request to the GrpcService object.
   *
   * @private
   */
  requestStream(...args: Array<{}>) {
    return this.parent.requestStream.apply(this.parent, args);
  }

  /**
   * Patch a writable streaming request to the GrpcService object.
   *
   * @private
   */
  requestWritableStream(...args: Array<{}>) {
    // tslint:disable-next-line:no-any
    return (this.parent as any).requestWritableStream.apply(this.parent, args);
  }

  private getOpts(metadata: boolean | {reqOpts?: CoreOptions}) {
    return typeof metadata === 'boolean' ? {} : metadata.reqOpts || {};
  }
}

/*! Developer Documentation
 *
 * All async methods (except for streams) will return a Promise in the event
 * that a callback is omitted.
 */
promisifyAll(GrpcServiceObject);

result-matching ""

    No results matching ""