Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ts): add type packages for many things #406

Merged
merged 1 commit into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,21 @@
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^2.3.0",
"@types/arrify": "^1.0.4",
"@types/async": "^2.0.50",
"@types/concat-stream": "^1.6.0",
"@types/extend": "^3.0.0",
"@types/is": "0.0.20",
"@types/lodash.snakecase": "^4.1.4",
"@types/merge-stream": "^1.1.2",
"@types/mocha": "^5.2.5",
"@types/p-queue": "^2.3.1",
"@types/p-retry": "^2.0.0",
"@types/proxyquire": "^1.3.28",
"@types/stack-trace": "0.0.29",
"@types/through2": "^2.0.34",
"@types/time-span": "^2.0.0",
"@types/uuid": "^3.4.4",
"async": "^2.6.1",
"binary-search-bounds": "^2.0.4",
"codecov": "^3.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/batch-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
'use strict';

import {promisifyAll} from '@google-cloud/promisify';
const extend = require('extend');
const is = require('is');
import * as extend from 'extend';
import * as is from 'is';

const codec = require('./codec');
const Transaction = require('./transaction');
Expand Down
14 changes: 7 additions & 7 deletions src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

const codec = module.exports;

const arrify = require('arrify');
const {Service} = require('@google-cloud/common-grpc');
const extend = require('extend');
import * as arrify from 'arrify';
import {Service} from '@google-cloud/common-grpc';
import * as extend from 'extend';
import * as is from 'is';

class SpannerDate {
Expand Down Expand Up @@ -284,7 +284,7 @@ codec.decode = decode;
function encode(value) {
function preEncode(value) {
const numberShouldBeStringified =
(!(value instanceof Float) && is.int(value)) ||
(!(value instanceof Float) && is.integer(value)) ||
value instanceof Int ||
is.infinite(value) ||
Number.isNaN(value);
Expand Down Expand Up @@ -318,7 +318,7 @@ function encode(value) {
return value;
}

return Service.encodeValue_(preEncode(value));
return (Service as any).encodeValue_(preEncode(value));
}

codec.encode = encode;
Expand All @@ -336,7 +336,7 @@ codec.encode = encode;
* // 'float64'
*/
function getType(field) {
if (is.bool(field)) {
if (is.boolean(field)) {
return 'bool';
}

Expand Down Expand Up @@ -387,7 +387,7 @@ function getType(field) {
for (let i = 0; i < field.length; i++) {
child = field[i];

if (!is.nil(child)) {
if (!is.null(child)) {
break;
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

'use strict';

const arrify = require('arrify');
import * as arrify from 'arrify';
import {promisifyAll} from '@google-cloud/promisify';
const {ServiceObject} = require('@google-cloud/common-grpc');
const extend = require('extend');
const is = require('is');
const retry = require('p-retry');
const streamEvents = require('stream-events');
const through = require('through2');
import * as extend from 'extend';
import * as is from 'is';
import * as retry from 'p-retry';
import * as streamEvents from 'stream-events';
import * as through from 'through2';
import { Abortable } from '@google-cloud/common';

const BatchTransaction = require('./batch-transaction');
const codec = require('./codec');
Expand Down Expand Up @@ -967,7 +968,7 @@ class Database extends ServiceObject {
let requestStream;
let session;
const waitForSessionStream = streamEvents(through.obj());
waitForSessionStream.abort = function() {
(waitForSessionStream as any).abort = function() {
releaseSession();
if (requestStream) {
requestStream.cancel();
Expand Down
141 changes: 75 additions & 66 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

'use strict';

const {Service, Operation} = require('@google-cloud/common-grpc');
import {Service, Operation} from '@google-cloud/common-grpc';
import {paginator} from '@google-cloud/paginator';
const {replaceProjectIdToken} = require('@google-cloud/projectify');
const {promisifyAll} = require('@google-cloud/promisify');
const extend = require('extend');
import {GoogleAuth} from 'google-auth-library';
const is = require('is');
const path = require('path');
const streamEvents = require('stream-events');
const through = require('through2');
import {replaceProjectIdToken} from '@google-cloud/projectify';
import {promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
import * as is from 'is';
import * as path from 'path';
import * as streamEvents from 'stream-events';
import * as through from 'through2';
import { GrpcServiceConfig } from '@google-cloud/common-grpc/build/src/service';
const codec = require('./codec');
const Database = require('./database');
const Instance = require('./instance');
Expand Down Expand Up @@ -156,6 +157,21 @@ const gapic = Object.freeze({
* @param {ClientConfig} [options] Configuration options.
*/
class Spanner extends Service {
options: GoogleAuthOptions;
auth: GoogleAuth;
clients_: Map<string, {}>;
instances_: Map<string, {}>;
getInstancesStream: Function;

/**
* Placeholder used to auto populate a column with the commit timestamp.
* This can only be used for timestamp columns that have set the option
* "(allow_commit_timestamp=true)" in the schema.
*
* @type {string}
*/
static COMMIT_TIMESTAMP = 'spanner.commit_timestamp()';

constructor(options) {
const scopes: {}[] = [];
const clientClasses = [
Expand Down Expand Up @@ -190,12 +206,50 @@ class Spanner extends Service {
},
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
packageJson: require('../../package.json'),
};
} as {} as GrpcServiceConfig;
super(config, options);
this.options = options;
this.auth = new GoogleAuth(this.options);
this.clients_ = new Map();
this.instances_ = new Map();


/**
* Get a list of {@link Instance} objects as a readable object stream.
*
* Wrapper around {@link v1.InstanceAdminClient#listInstances}.
*
* @see {@link v1.InstanceAdminClient#listInstances}
* @see [ListInstances API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.ListInstances)
*
* @method Spanner#getInstancesStream
* @param {GetInstancesRequest} [query] Query object for listing instances.
* @returns {ReadableStream} A readable stream that emits {@link Instance}
* instances.
*
* @example
* const {Spanner} = require('@google-cloud/spanner');
* const spanner = new Spanner();
*
* spanner.getInstancesStream()
* .on('error', console.error)
* .on('data', function(instance) {
* // `instance` is an `Instance` object.
* })
* .on('end', function() {
* // All instances retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* spanner.getInstancesStream()
* .on('data', function(instance) {
* this.end();
* });
*/
this.getInstancesStream = paginator.streamify('getInstances');
}

/**
Expand Down Expand Up @@ -419,7 +473,7 @@ class Spanner extends Service {
if (instances) {
arguments[1] = instances.map(instance => {
const instanceInstance = self.instance(instance.name);
instanceInstance.metadata = instance;
(instanceInstance as any).metadata = instance;
return instanceInstance;
});
}
Expand Down Expand Up @@ -628,9 +682,9 @@ class Spanner extends Service {
if (!this.clients_.has(clientName)) {
this.clients_.set(clientName, new gapic.v1[clientName](this.options));
}
const gaxClient = this.clients_.get(clientName);
const gaxClient = this.clients_.get(clientName)!;
let reqOpts = extend(true, {}, config.reqOpts);
reqOpts = replaceProjectIdToken(reqOpts, projectId);
reqOpts = replaceProjectIdToken(reqOpts, projectId!);
const requestFn = gaxClient[config.method].bind(
gaxClient,
reqOpts,
Expand All @@ -650,7 +704,7 @@ class Spanner extends Service {
* @param {function} [callback] Callback function.
* @returns {Promise}
*/
request(config, callback) {
request(config, callback): void|Promise<any> {
if (is.fn(callback)) {
this.prepareGapicRequest_(config, (err, requestFn) => {
if (err) {
Expand Down Expand Up @@ -770,51 +824,6 @@ class Spanner extends Service {
}
}

/**
* Placeholder used to auto populate a column with the commit timestamp.
* This can only be used for timestamp columns that have set the option
* "(allow_commit_timestamp=true)" in the schema.
*
* @type {string}
*/
Spanner.COMMIT_TIMESTAMP = 'spanner.commit_timestamp()';

/**
* Get a list of {@link Instance} objects as a readable object stream.
*
* Wrapper around {@link v1.InstanceAdminClient#listInstances}.
*
* @see {@link v1.InstanceAdminClient#listInstances}
* @see [ListInstances API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.ListInstances)
*
* @method Spanner#getInstancesStream
* @param {GetInstancesRequest} [query] Query object for listing instances.
* @returns {ReadableStream} A readable stream that emits {@link Instance}
* instances.
*
* @example
* const {Spanner} = require('@google-cloud/spanner');
* const spanner = new Spanner();
*
* spanner.getInstancesStream()
* .on('error', console.error)
* .on('data', function(instance) {
* // `instance` is an `Instance` object.
* })
* .on('end', function() {
* // All instances retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* spanner.getInstancesStream()
* .on('data', function(instance) {
* this.end();
* });
*/
Spanner.prototype.getInstancesStream = paginator.streamify('getInstances');

/*! Developer Documentation
*
Expand Down Expand Up @@ -870,7 +879,7 @@ export {Spanner};
* @see Instance
* @type {Constructor}
*/
Spanner.Instance = Instance;
export {Instance};

/**
* {@link Database} class.
Expand All @@ -879,7 +888,7 @@ Spanner.Instance = Instance;
* @see Database
* @type {Constructor}
*/
Spanner.Database = Database;
export {Database};

/**
* {@link Session} class.
Expand All @@ -888,7 +897,7 @@ Spanner.Database = Database;
* @see Session
* @type {Constructor}
*/
Spanner.Session = Session;
export {Session};

/**
* {@link SessionPool} class.
Expand All @@ -897,7 +906,7 @@ Spanner.Session = Session;
* @see SessionPool
* @type {Constructor}
*/
Spanner.SessionPool = SessionPool;
export {SessionPool};

/**
* {@link Table} class.
Expand All @@ -906,7 +915,7 @@ Spanner.SessionPool = SessionPool;
* @see Table
* @type {Constructor}
*/
Spanner.Table = Table;
export {Table};

/**
* {@link Transaction} class.
Expand All @@ -915,7 +924,7 @@ Spanner.Table = Table;
* @see Transaction
* @type {Constructor}
*/
Spanner.Transaction = Transaction;
export {Transaction};

/**
* {@link TransactionRequest} class.
Expand All @@ -924,7 +933,7 @@ Spanner.Transaction = Transaction;
* @see TransactionRequest
* @type {Constructor}
*/
Spanner.TransactionRequest = TransactionRequest;
export {TransactionRequest};

/**
* @type {object}
Expand Down
8 changes: 4 additions & 4 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

'use strict';

const arrify = require('arrify');
import * as arrify from 'arrify';
const common = require('@google-cloud/common-grpc');
import { ServiceObjectConfig } from '@google-cloud/common';
import {paginator} from '@google-cloud/paginator';
import {promisifyAll} from '@google-cloud/promisify';
const extend = require('extend');
const is = require('is');
const snakeCase = require('lodash.snakecase');
import * as extend from 'extend';
import * as is from 'is';
import snakeCase =require('lodash.snakecase');

const Database = require('./database');

Expand Down
Loading