Skip to content

Commit

Permalink
refactor(ts): enable lint and fix (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Dec 14, 2018
1 parent dcb7f65 commit 717b818
Show file tree
Hide file tree
Showing 16 changed files with 1,303 additions and 1,410 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -31,14 +31,14 @@
"scripts": {
"docs": "jsdoc -c .jsdoc.js",
"generate-scaffolding": "repo-tools generate all && repo-tools generate lib_samples_readme -l samples/ --config ../.cloud-repo-tools.json",
"lint": "eslint 'samples/*.js' 'samples/**/*.js'",
"lint": "gts check && eslint '**/*.js'",
"cover": "nyc --reporter=lcov mocha build/test && nyc report",
"samples-test": "cd samples/ && npm link ../ && npm test && cd ../",
"test-no-cover": "mocha build/test",
"test": "npm run cover",
"presystem-test": "npm run compile",
"system-test": "mocha build/system-test --timeout 600000",
"fix": "eslint 'samples/*.js' 'samples/**/*.js' --fix",
"fix": "gts fix && eslint '**/*.js' --fix",
"clean": "gts clean",
"compile": "tsc -p . && cp -r src/v1/ build/src/v1/ && cp -r protos build/ && cp test/*.js build/test",
"prepare": "npm run compile",
Expand Down
4 changes: 4 additions & 0 deletions src/.eslintrc.yml
@@ -0,0 +1,4 @@
---
rules:
node/no-unpublished-require: off
node/no-missing-require: off
48 changes: 23 additions & 25 deletions src/entity.ts
Expand Up @@ -184,7 +184,6 @@ entity.isDsGeoPoint = isDsGeoPoint;
* });
*/
class Key {

namespace: string;
id?: string;
name?: string;
Expand Down Expand Up @@ -294,7 +293,7 @@ function decodeValueProto(valueProto) {
}

case 'integerValue': {
return parseInt(value, 10);
return Number(value);
}

case 'entityValue': {
Expand All @@ -306,13 +305,11 @@ function decodeValueProto(valueProto) {
}

case 'timestampValue': {
const milliseconds = parseInt(value.nanos, 10) / 1e6;
return new Date(parseInt(value.seconds, 10) * 1000 + milliseconds);
const milliseconds = Number(value.nanos) / 1e6;
return new Date(Number(value.seconds) * 1000 + milliseconds);
}

default: {
return value;
}
default: { return value; }
}
}

Expand Down Expand Up @@ -455,6 +452,7 @@ function entityFromEntityProto(entityProto) {

const properties = entityProto.properties || {};

// tslint:disable-next-line forin
for (const property in properties) {
const value = properties[property];
entityObject[property] = entity.decodeValueProto(value);
Expand Down Expand Up @@ -502,10 +500,13 @@ function entityToEntityProto(entityObject) {
const entityProto = {
key: null,

properties: Object.keys(properties).reduce((encoded, key) => {
encoded[key] = entity.encodeValue(properties[key]);
return encoded;
}, {}),
properties: Object.keys(properties)
.reduce(
(encoded, key) => {
encoded[key] = entity.encodeValue(properties[key]);
return encoded;
},
{}),
};

if (excludeFromIndexes && excludeFromIndexes.length > 0) {
Expand All @@ -526,11 +527,9 @@ function entityToEntityProto(entityObject) {
if (!hasArrayPath && !hasEntityPath) {
// This is the path end node. Traversal ends here in either case.
if (entity.properties) {
if (
entity.properties[path] &&
// array properties should be excluded with [] syntax:
!entity.properties[path].arrayValue
) {
if (entity.properties[path] &&
// array properties should be excluded with [] syntax:
!entity.properties[path].arrayValue) {
// This is the property to exclude!
entity.properties[path].excludeFromIndexes = true;
}
Expand Down Expand Up @@ -561,27 +560,25 @@ function entityToEntityProto(entityObject) {
return;
}

if (
firstPathPartIsArray &&
// check also if the property in question is actually an array value.
entity.properties[firstPathPart].arrayValue
) {
if (firstPathPartIsArray &&
// check also if the property in question is actually an array value.
entity.properties[firstPathPart].arrayValue) {
const array = entity.properties[firstPathPart].arrayValue;
array.values.forEach(value => {
if (remainderPath === '') {
// We want to exclude *this* array property, which is
// equivalent with excluding all its values
// (including entity values at their roots):
excludePathFromEntity(
value,
remainderPath // === ''
value,
remainderPath // === ''
);
} else {
// Path traversal continues at value.entityValue,
// if it is an entity, or must end at value.
excludePathFromEntity(
value.entityValue || value,
remainderPath // !== ''
value.entityValue || value,
remainderPath // !== ''
);
}
});
Expand Down Expand Up @@ -758,6 +755,7 @@ function keyToKeyProto(key) {
}

keyProto.path.unshift(pathElement);
// tslint:disable-next-line no-conditional-assignment
} while ((key = key.parent) && ++numKeysWalked);

return keyProto;
Expand Down
106 changes: 54 additions & 52 deletions src/index.ts
Expand Up @@ -37,13 +37,13 @@
*/

import * as arrify from 'arrify';
import {GrpcClient, GrpcClientOptions} from 'google-gax';
import {GoogleAuth} from 'google-auth-library';
import {GrpcClient, GrpcClientOptions} from 'google-gax';
import * as is from 'is';

import {DatastoreRequest} from './request';
import {entity} from './entity';
import {Query} from './query';
import {DatastoreRequest} from './request';
import {Transaction} from './transaction';

const {grpc} = new GrpcClient({} as GrpcClientOptions);
Expand Down Expand Up @@ -88,7 +88,7 @@ const gapic = Object.freeze({
*
* Additionally, `DATASTORE_PROJECT_ID` is recognized. If you have this set,
* you don't need to provide a `projectId`.
*-
*
*
* @class
* @extends {DatastoreRequest}
Expand All @@ -104,13 +104,14 @@ const gapic = Object.freeze({
* @example <caption>Import the client library</caption>
* const {Datastore} = require('@google-cloud/datastore');
*
* @example <caption>Create a client that uses <a href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application Default Credentials (ADC)</a>:</caption>
* const datastore = new Datastore();
* @example <caption>Create a client that uses <a
* href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application
* Default Credentials (ADC)</a>:</caption> const datastore = new Datastore();
*
* @example <caption>Create a client with <a href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit credentials</a>:</caption>
* const datastore = new Datastore({
* projectId: 'your-project-id',
* keyFilename: '/path/to/keyfile.json'
* @example <caption>Create a client with <a
* href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit
* credentials</a>:</caption> const datastore = new Datastore({ projectId:
* 'your-project-id', keyFilename: '/path/to/keyfile.json'
* });
*
* @example <caption>Retrieving Records</caption>
Expand Down Expand Up @@ -312,9 +313,11 @@ const gapic = Object.freeze({
* const customerId2 = 4993882;
* const customerKey1 = datastore.key(['Customer', customerId1]);
* const customerKey2 = datastore.key(['Customer', customerId2]);
* const cookieKey1 = datastore.key(['Customer', customerId1, 'Cookie', 'cookie28839']); // child entity
* const cookieKey2 = datastore.key(['Customer', customerId1, 'Cookie', 'cookie78984']); // child entity
* const cookieKey3 = datastore.key(['Customer', customerId2, 'Cookie', 'cookie93911']); // child entity
* const cookieKey1 = datastore.key(['Customer', customerId1, 'Cookie',
* 'cookie28839']); // child entity const cookieKey2 =
* datastore.key(['Customer', customerId1, 'Cookie', 'cookie78984']); // child
* entity const cookieKey3 = datastore.key(['Customer', customerId2, 'Cookie',
* 'cookie93911']); // child entity
*
* const entities = [];
*
Expand Down Expand Up @@ -400,7 +403,7 @@ class Datastore extends DatastoreRequest {
this.namespace = options.namespace;

const userProvidedProjectId =
options.projectId || process.env.DATASTORE_PROJECT_ID;
options.projectId || process.env.DATASTORE_PROJECT_ID;
const defaultProjectId = '{{projectId}}';

/**
Expand All @@ -413,16 +416,15 @@ class Datastore extends DatastoreRequest {
this.determineBaseUrl_(options.apiEndpoint);

this.options = Object.assign(
{
libName: 'gccl',
libVersion: require('../../package.json').version,
scopes: gapic.v1.DatastoreClient.scopes,
servicePath: this.baseUrl_,
port: is.number(this.port_) ? this.port_ : 443,
projectId: userProvidedProjectId,
},
options
);
{
libName: 'gccl',
libVersion: require('../../package.json').version,
scopes: gapic.v1.DatastoreClient.scopes,
servicePath: this.baseUrl_,
port: is.number(this.port_) ? this.port_ : 443,
projectId: userProvidedProjectId,
},
options);
if (this.customEndpoint_) {
this.options.sslCreds = grpc.credentials.createInsecure();
}
Expand Down Expand Up @@ -647,7 +649,8 @@ class Datastore extends DatastoreRequest {
}

/**
* Helper to create a Key object, scoped to the instance's namespace by default.
* Helper to create a Key object, scoped to the instance's namespace by
* default.
*
* You may also specify a configuration object to define a namespace and path.
*
Expand All @@ -664,15 +667,15 @@ class Datastore extends DatastoreRequest {
* const key = datastore.key('Company');
*
* @example
* <caption>Create a complete key with a kind value of `Company` and id `123`.</caption>
* const {Datastore} = require('@google-cloud/datastore');
* <caption>Create a complete key with a kind value of `Company` and id
* `123`.</caption> const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const key = datastore.key(['Company', 123]);
*
* @example
* <caption>If the ID integer is outside the bounds of a JavaScript Number object, create an Int.</caption>
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* <caption>If the ID integer is outside the bounds of a JavaScript Number
* object, create an Int.</caption> const {Datastore} =
* require('@google-cloud/datastore'); const datastore = new Datastore();
* const key = datastore.key([
* 'Company',
* datastore.int('100000000000001234')
Expand All @@ -686,21 +689,19 @@ class Datastore extends DatastoreRequest {
* const key = datastore.key(['Company', 'Google']);
*
* @example
* <caption>Create a complete key from a provided namespace and path.</caption>
* const {Datastore} = require('@google-cloud/datastore');
* <caption>Create a complete key from a provided namespace and
* path.</caption> const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const key = datastore.key({
* namespace: 'My-NS',
* path: ['Company', 123]
* });
*/
key(options) {
options = is.object(options)
? options
: {
namespace: this.namespace,
path: arrify(options),
};
options = is.object(options) ? options : {
namespace: this.namespace,
path: arrify(options),
};
return new entity.Key(options);
}

Expand Down Expand Up @@ -742,9 +743,9 @@ class Datastore extends DatastoreRequest {
}

/**
* Determine the appropriate endpoint to use for API requests. If not explicitly
* defined, check for the "DATASTORE_EMULATOR_HOST" environment variable, used
* to connect to a local Datastore server.
* Determine the appropriate endpoint to use for API requests. If not
* explicitly defined, check for the "DATASTORE_EMULATOR_HOST" environment
* variable, used to connect to a local Datastore server.
*
* @private
*
Expand All @@ -768,10 +769,9 @@ class Datastore extends DatastoreRequest {
this.port_ = Number(baseUrl.match(port)![1]);
}

this.baseUrl_ = baseUrl
.replace(leadingProtocol, '')
.replace(port, '')
.replace(trailingSlashes, '');
this.baseUrl_ = baseUrl.replace(leadingProtocol, '')
.replace(port, '')
.replace(trailingSlashes, '');
}

/**
Expand Down Expand Up @@ -813,19 +813,21 @@ export {Datastore};
* @module {Datastore} @google-cloud/datastore
* @alias nodejs-datastore
*
* @example <caption>Install the client library with <a href="https://www.npmjs.com/">npm</a>:</caption>
* npm install --save @google-cloud/datastore
* @example <caption>Install the client library with <a
* href="https://www.npmjs.com/">npm</a>:</caption> npm install --save
* @google-cloud/datastore
*
* @example <caption>Import the client library</caption>
* const {Datastore} = require('@google-cloud/datastore');
*
* @example <caption>Create a client that uses <a href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application Default Credentials (ADC)</a>:</caption>
* const datastore = new Datastore();
* @example <caption>Create a client that uses <a
* href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application
* Default Credentials (ADC)</a>:</caption> const datastore = new Datastore();
*
* @example <caption>Create a client with <a href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit credentials</a>:</caption>
* const datastore = new Datastore({
* projectId: 'your-project-id',
* keyFilename: '/path/to/keyfile.json'
* @example <caption>Create a client with <a
* href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit
* credentials</a>:</caption> const datastore = new Datastore({ projectId:
* 'your-project-id', keyFilename: '/path/to/keyfile.json'
* });
*
* @example <caption>include:samples/quickstart.js</caption>
Expand Down
3 changes: 2 additions & 1 deletion src/query.ts
Expand Up @@ -384,7 +384,8 @@ class Query {
* });
*
* //-
* // A keys-only query returns just the keys of the result entities instead of
* // A keys-only query returns just the keys of the result entities instead
* of
* // the entities themselves, at lower latency and cost.
* //-
* query.select('__key__');
Expand Down

0 comments on commit 717b818

Please sign in to comment.