Skip to content

Commit

Permalink
chore: split the usage of common (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Aug 10, 2018
1 parent 8545ccf commit 3754ddf
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 94 deletions.
2 changes: 2 additions & 0 deletions packages/google-cloud-dns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
},
"dependencies": {
"@google-cloud/common": "^0.20.0",
"@google-cloud/paginator": "^0.1.0",
"@google-cloud/promisify": "^0.3.0",
"arrify": "^1.0.1",
"dns-zonefile": "0.2.2",
"extend": "^3.0.0",
Expand Down
9 changes: 5 additions & 4 deletions packages/google-cloud-dns/src/change.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

'use strict';

const common = require('@google-cloud/common');
const {ServiceObject} = require('@google-cloud/common');
const {promisifyAll} = require('@google-cloud/promisify');
const util = require('util');

/**
Expand Down Expand Up @@ -167,7 +168,7 @@ function Change(zone, id) {
* @name Change#metadata
* @type {object}
*/
common.ServiceObject.call(this, {
ServiceObject.call(this, {
parent: zone,

/**
Expand All @@ -186,7 +187,7 @@ function Change(zone, id) {
});
}

util.inherits(Change, common.ServiceObject);
util.inherits(Change, ServiceObject);

/**
* Create a change.
Expand Down Expand Up @@ -243,6 +244,6 @@ Change.prototype.create = function(config, callback) {
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(Change);
promisifyAll(Change);

module.exports = Change;
16 changes: 9 additions & 7 deletions packages/google-cloud-dns/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
'use strict';

const arrify = require('arrify');
const common = require('@google-cloud/common');
const {Service} = require('@google-cloud/common');
const {paginator} = require('@google-cloud/paginator');
const {promisifyAll} = require('@google-cloud/promisify');
const extend = require('extend');
const is = require('is');
const util = require('util');
Expand Down Expand Up @@ -83,7 +85,7 @@ function DNS(options) {
return new DNS(options);
}

options = common.util.normalizeArguments(this, options);
options = options || {};

const config = {
baseUrl: 'https://www.googleapis.com/dns/v1',
Expand All @@ -94,10 +96,10 @@ function DNS(options) {
packageJson: require('../package.json'),
};

common.Service.call(this, config, options);
Service.call(this, config, options);
}

util.inherits(DNS, common.Service);
util.inherits(DNS, Service);

/**
* Config to set for the zone.
Expand Down Expand Up @@ -304,7 +306,7 @@ DNS.prototype.getZones = function(query, callback) {
* this.end();
* });
*/
DNS.prototype.getZonesStream = common.paginator.streamify('getZones');
DNS.prototype.getZonesStream = paginator.streamify('getZones');

/**
* Get a reference to a Zone.
Expand Down Expand Up @@ -333,14 +335,14 @@ DNS.prototype.zone = function(name) {
*
* These methods can be auto-paginated.
*/
common.paginator.extend(DNS, 'getZones');
paginator.extend(DNS, 'getZones');

/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(DNS, {
promisifyAll(DNS, {
exclude: ['zone'],
});

Expand Down
4 changes: 2 additions & 2 deletions packages/google-cloud-dns/src/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'use strict';

const arrify = require('arrify');
const common = require('@google-cloud/common');
const {promisifyAll} = require('@google-cloud/promisify');
const extend = require('extend');
const format = require('string-format-obj');

Expand Down Expand Up @@ -201,7 +201,7 @@ Record.prototype.toString = function() {
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(Record, {
promisifyAll(Record, {
exclude: ['toJSON', 'toString'],
});

Expand Down
18 changes: 10 additions & 8 deletions packages/google-cloud-dns/src/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
'use strict';

const arrify = require('arrify');
const common = require('@google-cloud/common');
const {ServiceObject} = require('@google-cloud/common');
const {paginator} = require('@google-cloud/paginator');
const {promisifyAll} = require('@google-cloud/promisify');
const exec = require('methmeth');
const extend = require('extend');
const flatten = require('lodash.flatten');
Expand Down Expand Up @@ -198,7 +200,7 @@ function Zone(dns, name) {
* @name Zone#metadata
* @type {object}
*/
common.ServiceObject.call(this, {
ServiceObject.call(this, {
parent: dns,

/**
Expand All @@ -224,7 +226,7 @@ function Zone(dns, name) {
this.name = name;
}

util.inherits(Zone, common.ServiceObject);
util.inherits(Zone, ServiceObject);

/**
* @typedef {array} ZoneAddRecordsResponse
Expand Down Expand Up @@ -463,7 +465,7 @@ Zone.prototype.delete = function(options, callback) {
return;
}

common.ServiceObject.prototype.delete.call(this, callback);
ServiceObject.prototype.delete.call(this, callback);
};

/**
Expand Down Expand Up @@ -798,7 +800,7 @@ Zone.prototype.getChanges = function(query, callback) {
* this.end();
* });
*/
Zone.prototype.getChangesStream = common.paginator.streamify('getChanges');
Zone.prototype.getChangesStream = paginator.streamify('getChanges');

/**
* Query object for listing records.
Expand Down Expand Up @@ -980,7 +982,7 @@ Zone.prototype.getRecords = function(query, callback) {
* this.end();
* });
*/
Zone.prototype.getRecordsStream = common.paginator.streamify('getRecords');
Zone.prototype.getRecordsStream = paginator.streamify('getRecords');

/**
* @typedef {array} ZoneImportResponse
Expand Down Expand Up @@ -1229,14 +1231,14 @@ Zone.prototype.deleteRecordsByType_ = function(recordTypes, callback) {
*
* These methods can be auto-paginated.
*/
common.paginator.extend(Zone, ['getChanges', 'getRecords']);
paginator.extend(Zone, ['getChanges', 'getRecords']);

/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(Zone, {
promisifyAll(Zone, {
exclude: ['change', 'record'],
});

Expand Down
12 changes: 6 additions & 6 deletions packages/google-cloud-dns/test/change.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const assert = require('assert');
const extend = require('extend');
const nodeutil = require('util');
const proxyquire = require('proxyquire');
const ServiceObject = require('@google-cloud/common').ServiceObject;
const util = require('@google-cloud/common').util;
const {ServiceObject} = require('@google-cloud/common');
const promisify = require('@google-cloud/promisify');

let promisified = false;
const fakeUtil = extend({}, util, {
const fakePromisify = extend({}, promisify, {
promisifyAll: function(Class) {
if (Class.name === 'Change') {
promisified = true;
Expand All @@ -45,17 +45,17 @@ describe('Change', function() {

const ZONE = {
name: 'zone-name',
createChange: util.noop,
createChange: function() {},
};

const CHANGE_ID = 'change-id';

before(function() {
Change = proxyquire('../src/change.js', {
Change = proxyquire('../src/change', {
'@google-cloud/common': {
ServiceObject: FakeServiceObject,
util: fakeUtil,
},
'@google-cloud/promisify': fakePromisify,
});
});

Expand Down
61 changes: 26 additions & 35 deletions packages/google-cloud-dns/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@ const assert = require('assert');
const extend = require('extend');
const nodeutil = require('util');
const proxyquire = require('proxyquire');
const Service = require('@google-cloud/common').Service;
const util = require('@google-cloud/common').util;
const {Service} = require('@google-cloud/common');
const {util} = require('@google-cloud/common');
const promisify = require('@google-cloud/promisify');

let extended = false;
const fakePaginator = {
extend: function(Class, methods) {
if (Class.name !== 'DNS') {
return;
}

extended = true;
methods = arrify(methods);
assert.strictEqual(Class.name, 'DNS');
assert.deepStrictEqual(methods, ['getZones']);
},
streamify: function(methodName) {
return methodName;
paginator: {
extend: function(Class, methods) {
if (Class.name !== 'DNS') {
return;
}

extended = true;
methods = arrify(methods);
assert.strictEqual(Class.name, 'DNS');
assert.deepStrictEqual(methods, ['getZones']);
},
streamify: function(methodName) {
return methodName;
},
},
};

Expand All @@ -48,19 +51,21 @@ function FakeService() {

nodeutil.inherits(FakeService, Service);

let promisified = false;
const fakeUtil = extend({}, util, {
makeAuthenticatedRequestFactory: util.noop,
makeAuthenticatedRequestFactory: function() {},
});
const originalFakeUtil = extend(true, {}, fakeUtil);

let promisified = false;
const fakePromisify = extend({}, promisify, {
promisifyAll: function(Class, options) {
if (Class.name !== 'DNS') {
return;
}

promisified = true;
assert.deepStrictEqual(options.exclude, ['zone']);
},
});
const originalFakeUtil = extend(true, {}, fakeUtil);

function FakeZone() {
this.calledWith_ = arguments;
Expand All @@ -76,10 +81,10 @@ describe('DNS', function() {
DNS = proxyquire('../', {
'@google-cloud/common': {
Service: FakeService,
paginator: fakePaginator,
util: fakeUtil,
},
'./zone.js': FakeZone,
'@google-cloud/paginator': fakePaginator,
'@google-cloud/promisify': fakePromisify,
'./zone': FakeZone,
});
});

Expand Down Expand Up @@ -109,20 +114,6 @@ describe('DNS', function() {
});
});

it('should normalize the arguments', function() {
let normalizeArgumentsCalled = false;
const options = {};

fakeUtil.normalizeArguments = function(context, options_) {
normalizeArgumentsCalled = true;
assert.strictEqual(options_, options);
return options_;
};

new DNS(options);
assert.strictEqual(normalizeArgumentsCalled, true);
});

it('should inherit from Service', function() {
assert(dns instanceof Service);

Expand Down
13 changes: 5 additions & 8 deletions packages/google-cloud-dns/test/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
const assert = require('assert');
const extend = require('extend');
const proxyquire = require('proxyquire');
const util = require('@google-cloud/common').util;
const promisify = require('@google-cloud/promisify');

let promisified = false;
const fakeUtil = extend({}, util, {
const fakePromisify = extend({}, promisify, {
promisifyAll: function(Class, options) {
if (Class.name !== 'Record') {
return;
}

promisified = true;
assert.deepStrictEqual(options.exclude, ['toJSON', 'toString']);
},
Expand All @@ -38,7 +37,7 @@ describe('Record', function() {
let record;

const ZONE = {
deleteRecords: util.noop,
deleteRecords: function() {},
};
const TYPE = 'A';
const METADATA = {
Expand All @@ -48,10 +47,8 @@ describe('Record', function() {
};

before(function() {
Record = proxyquire('../src/record.js', {
'@google-cloud/common': {
util: fakeUtil,
},
Record = proxyquire('../src/record', {
'@google-cloud/promisify': fakePromisify,
});
});

Expand Down
Loading

0 comments on commit 3754ddf

Please sign in to comment.