diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..7d6cf97 --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +Language: JavaScript +BasedOnStyle: Google +ColumnLimit: 80 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 6b002b9..7af0320 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ system-test/secrets.js system-test/*key.json *.lock package-lock.json +build/ diff --git a/package.json b/package.json index a3ed41b..caf7420 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,10 @@ "node": ">=6.0.0" }, "repository": "googleapis/nodejs-dns", - "main": "./src/index.js", + "main": "./build/src/index.js", + "types": "./build/src/index.d.ts", "files": [ - "src", + "build/src", "AUTHORS", "CONTRIBUTORS", "LICENSE" @@ -41,19 +42,21 @@ "greenkeeper[bot] " ], "scripts": { - "cover": "nyc --reporter=lcov mocha test/*.js && nyc report", "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 src/ samples/ system-test/ test/", - "prettier": "prettier --write src/*.js src/*/*.js samples/*.js samples/*/*.js test/*.js test/*/*.js system-test/*.js system-test/*/*.js", - "publish-module": "node ../../scripts/publish.js dns", - "test-no-cover": "mocha test/*.js", - "test": "npm run cover", + "lint": "echo lint not enabled 👻 # gts check && eslint samples/", + "test": "nyc mocha build/test", "samples-test": "cd samples/ && npm link ../ && npm test && cd ../", - "system-test": "mocha system-test/*.js --timeout 600000" + "presystem-test": "npm run compile", + "system-test": "mocha build/system-test --timeout 600000", + "clean": "gts clean", + "compile": "tsc -p .", + "fix": "gts fix && prettier --write samples/*.js samples/*/*.js", + "prepare": "npm run compile", + "pretest": "npm run compile" }, "dependencies": { - "@google-cloud/common": "^0.23.0", + "@google-cloud/common": "^0.24.0", "@google-cloud/paginator": "^0.1.0", "@google-cloud/promisify": "^0.3.0", "arrify": "^1.0.1", @@ -69,12 +72,23 @@ }, "devDependencies": { "@google-cloud/nodejs-repo-tools": "^2.3.0", + "@types/arrify": "^1.0.4", + "@types/async": "^2.0.49", + "@types/extend": "^3.0.0", + "@types/is": "0.0.20", + "@types/lodash.flatten": "^4.4.4", + "@types/lodash.groupby": "^4.6.4", + "@types/mocha": "^5.2.5", + "@types/node": "^10.9.4", + "@types/proxyquire": "^1.3.28", + "@types/uuid": "^3.4.4", "async": "^2.6.1", "codecov": "^3.0.2", "eslint": "^5.0.0", "eslint-config-prettier": "^3.0.0", "eslint-plugin-node": "^7.0.0", "eslint-plugin-prettier": "^2.6.0", + "gts": "^0.8.0", "ink-docstrap": "^1.3.2", "intelli-espower-loader": "^1.0.1", "jsdoc": "^3.5.5", @@ -84,6 +98,12 @@ "prettier": "^1.13.5", "proxyquire": "^2.0.1", "tmp": "^0.0.33", + "typescript": "~2.8.0", "uuid": "^3.2.1" + }, + "nyc": { + "exclude": [ + "build/test" + ] } } diff --git a/samples/system-test/quickstart.test.js b/samples/system-test/quickstart.test.js index 2a9d797..2684529 100644 --- a/samples/system-test/quickstart.test.js +++ b/samples/system-test/quickstart.test.js @@ -20,7 +20,7 @@ const sinon = require(`sinon`); const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); -const DNS = proxyquire(`@google-cloud/dns`, {}); +const {DNS} = proxyquire(`@google-cloud/dns`, {}); const dns = new DNS(); const uuid = require(`uuid`); diff --git a/samples/system-test/zones.test.js b/samples/system-test/zones.test.js index 1972c60..fa4c428 100644 --- a/samples/system-test/zones.test.js +++ b/samples/system-test/zones.test.js @@ -15,7 +15,7 @@ 'use strict'; -const DNS = require(`@google-cloud/dns`); +const {DNS} = require(`@google-cloud/dns`); const path = require(`path`); const test = require(`ava`); const tools = require(`@google-cloud/nodejs-repo-tools`); diff --git a/samples/zones.js b/samples/zones.js index 9fcb1e5..b34f97f 100644 --- a/samples/zones.js +++ b/samples/zones.js @@ -18,7 +18,7 @@ function listZones() { // [START dns_list_zones] // Imports the Google Cloud client library - const DNS = require('@google-cloud/dns'); + const {DNS} = require('@google-cloud/dns'); // Creates a client const dns = new DNS(); diff --git a/src/change.js b/src/change.ts similarity index 94% rename from src/change.js rename to src/change.ts index 3e82df6..ed76a23 100644 --- a/src/change.js +++ b/src/change.ts @@ -16,9 +16,9 @@ 'use strict'; -const teenyRequest = require('teeny-request').teenyRequest; -const {ServiceObject} = require('@google-cloud/common'); -const {promisifyAll} = require('@google-cloud/promisify'); +import {teenyRequest} from 'teeny-request'; +import {ServiceObject} from '@google-cloud/common'; +import {promisifyAll} from '@google-cloud/promisify'; /** * @class @@ -32,7 +32,7 @@ const {promisifyAll} = require('@google-cloud/promisify'); * const zone = dns.zone('zone-id'); * const change = zone.change('change-id'); */ -class Change extends ServiceObject { +export class Change extends ServiceObject { constructor(zone, id) { const methods = { /** @@ -178,9 +178,9 @@ class Change extends ServiceObject { * @name Change#id * @type {string} */ - id: id, - methods: methods, - requestModule: teenyRequest, + id, + methods, + requestModule: teenyRequest as any, }); } /** @@ -217,8 +217,8 @@ class Change extends ServiceObject { * const apiResponse = data[1]; * }); */ - create(config, callback) { - this.parent.createChange(config, (err, change, apiResponse) => { + create(config, callback?) { + (this.parent as any).createChange(config, (err, change, apiResponse) => { if (err) { callback(err, null, apiResponse); return; @@ -236,5 +236,3 @@ class Change extends ServiceObject { * that a callback is omitted. */ promisifyAll(Change); - -module.exports = Change; diff --git a/src/index.js b/src/index.ts similarity index 86% rename from src/index.js rename to src/index.ts index 0811754..33b7d4b 100644 --- a/src/index.js +++ b/src/index.ts @@ -16,15 +16,15 @@ 'use strict'; -const arrify = require('arrify'); -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 teenyRequest = require('teeny-request').teenyRequest; +import * as arrify from 'arrify'; +import { Service } from '@google-cloud/common'; +import { paginator } from '@google-cloud/paginator'; +import { promisifyAll } from '@google-cloud/promisify'; +import * as extend from 'extend'; +import * as is from 'is'; +import { teenyRequest } from 'teeny-request'; -const Zone = require('./zone'); +import { Zone } from './zone'; /** * @typedef {object} ClientConfig @@ -80,6 +80,7 @@ const Zone = require('./zone'); * Full quickstart example: */ class DNS extends Service { + getZonesStream; constructor(options) { options = options || {}; const config = { @@ -88,10 +89,42 @@ class DNS extends Service { 'https://www.googleapis.com/auth/ndev.clouddns.readwrite', 'https://www.googleapis.com/auth/cloud-platform', ], - packageJson: require('../package.json'), - requestModule: teenyRequest, + packageJson: require('../../package.json'), + requestModule: teenyRequest as any, }; super(config, options); + + /** + * Get {@link Zone} objects for all of the zones in your project as + * a readable object stream. + * + * @method DNS#getZonesStream + * @param {GetZonesRequest} [query] Query object for listing zones. + * @returns {ReadableStream} A readable stream that emits {@link Zone} instances. + * + * @example + * const DNS = require('@google-cloud/dns'); + * const dns = new DNS(); + * + * dns.getZonesStream() + * .on('error', console.error) + * .on('data', function(zone) { + * // zone is a Zone object. + * }) + * .on('end', function() { + * // All zones retrieved. + * }); + * + * //- + * // If you anticipate many results, you can end a stream early to prevent + * // unnecessary processing and API requests. + * //- + * dns.getZonesStream() + * .on('data', function(zone) { + * this.end(); + * }); + */ + this.getZonesStream = paginator.streamify('getZones'); } /** * Config to set for the zone. @@ -166,7 +199,7 @@ class DNS extends Service { uri: '/managedZones', json: config, }, - function(err, resp) { + function (err, resp) { if (err) { callback(err, null, resp); return; @@ -233,12 +266,12 @@ class DNS extends Service { uri: '/managedZones', qs: query, }, - function(err, resp) { + function (err, resp) { if (err) { callback(err, null, null, resp); return; } - const zones = arrify(resp.managedZones).map(function(zone) { + const zones = arrify(resp.managedZones).map(function (zone) { const zoneInstance = self.zone(zone.name); zoneInstance.metadata = zone; return zoneInstance; @@ -276,38 +309,6 @@ class DNS extends Service { } } -/** - * Get {@link Zone} objects for all of the zones in your project as - * a readable object stream. - * - * @method DNS#getZonesStream - * @param {GetZonesRequest} [query] Query object for listing zones. - * @returns {ReadableStream} A readable stream that emits {@link Zone} instances. - * - * @example - * const DNS = require('@google-cloud/dns'); - * const dns = new DNS(); - * - * dns.getZonesStream() - * .on('error', console.error) - * .on('data', function(zone) { - * // zone is a Zone object. - * }) - * .on('end', function() { - * // All zones retrieved. - * }); - * - * //- - * // If you anticipate many results, you can end a stream early to prevent - * // unnecessary processing and API requests. - * //- - * dns.getZonesStream() - * .on('data', function(zone) { - * this.end(); - * }); - */ -DNS.prototype.getZonesStream = paginator.streamify('getZones'); - /*! Developer Documentation * * These methods can be auto-paginated. @@ -330,7 +331,7 @@ promisifyAll(DNS, { * @see Zone * @type {Constructor} */ -DNS.Zone = Zone; +export { Zone }; /** * The default export of the `@google-cloud/dns` package is the {@link DNS} @@ -361,4 +362,4 @@ DNS.Zone = Zone; * region_tag:dns_quickstart * Full quickstart example: */ -module.exports = DNS; +export { DNS }; diff --git a/src/record.js b/src/record.ts similarity index 96% rename from src/record.js rename to src/record.ts index fc8800a..c39ea00 100644 --- a/src/record.js +++ b/src/record.ts @@ -16,9 +16,9 @@ 'use strict'; -const arrify = require('arrify'); -const {promisifyAll} = require('@google-cloud/promisify'); -const extend = require('extend'); +import * as arrify from 'arrify'; +import {promisifyAll} from '@google-cloud/promisify'; +import * as extend from 'extend'; const format = require('string-format-obj'); /** @@ -47,7 +47,12 @@ const format = require('string-format-obj'); * data: '1.2.3.4' * }); */ -class Record { +export class Record { + zone_; + type; + metadata; + rrdatas; + data; constructor(zone, type, metadata) { this.zone_ = zone; /** @@ -193,5 +198,3 @@ class Record { promisifyAll(Record, { exclude: ['toJSON', 'toString'], }); - -module.exports = Record; diff --git a/src/zone.js b/src/zone.ts similarity index 97% rename from src/zone.js rename to src/zone.ts index 3bbbf31..3e9457d 100644 --- a/src/zone.js +++ b/src/zone.ts @@ -16,22 +16,21 @@ 'use strict'; -const arrify = require('arrify'); -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'); +import * as arrify from 'arrify'; +import {ServiceObject} from '@google-cloud/common'; +import {paginator} from '@google-cloud/paginator'; +import {promisifyAll} from '@google-cloud/promisify'; +import * as extend from 'extend'; const flatten = require('lodash.flatten'); -const fs = require('fs'); +import * as fs from 'fs'; const groupBy = require('lodash.groupby'); -const is = require('is'); +import * as is from 'is'; const prop = require('propprop'); -const teenyRequest = require('teeny-request').teenyRequest; +import {teenyRequest} from 'teeny-request'; const zonefile = require('dns-zonefile'); -const Change = require('./change'); -const Record = require('./record'); +import {Change} from './change'; +import {Record} from './record'; /** * A Zone object is used to interact with your project's managed zone. It will @@ -211,8 +210,8 @@ class Zone extends ServiceObject { */ id: name, createMethod: dns.createZone.bind(dns), - methods: methods, - requestModule: teenyRequest, + methods, + requestModule: teenyRequest as any, }); /** * @name Zone#name @@ -337,8 +336,8 @@ class Zone extends ServiceObject { } const body = extend( { - additions: groupByType(arrify(config.add).map(exec('toJSON'))), - deletions: groupByType(arrify(config.delete).map(exec('toJSON'))), + additions: groupByType(arrify(config.add).map(x => x.toJSON())), + deletions: groupByType(arrify(config.delete).map(x => x.toJSON())), }, config ); @@ -346,7 +345,7 @@ class Zone extends ServiceObject { delete body.delete; function groupByType(changes) { changes = groupBy(changes, 'type'); - const changesArray = []; + const changesArray: Array<{}> = []; for (const recordType in changes) { const recordsByName = groupBy(changes[recordType], 'name'); for (const recordName in recordsByName) { @@ -431,7 +430,7 @@ class Zone extends ServiceObject { * const apiResponse = data[0]; * }); */ - delete(options, callback) { + delete(options, callback?) { if (is.fn(options)) { callback = options; options = {}; @@ -624,7 +623,7 @@ class Zone extends ServiceObject { callback(err); return; } - const stringRecords = records.map(exec('toString')).join('\n'); + const stringRecords = records.map(x => x.toString()).join('\n'); fs.writeFile(localPath, stringRecords, 'utf-8', function(err) { callback(err || null); }); @@ -820,7 +819,7 @@ class Zone extends ServiceObject { * const records = data[0]; * }); */ - getRecords(query, callback) { + getRecords(query, callback?) { const self = this; if (is.fn(query)) { callback = query; @@ -833,7 +832,7 @@ class Zone extends ServiceObject { filterByTypes_[type.toUpperCase()] = true; }); query = { - filterByTypes_: filterByTypes_, + filterByTypes_, }; } const requestQuery = extend({}, query); @@ -917,7 +916,7 @@ class Zone extends ServiceObject { const defaultTTL = parsedZonefile.$ttl; delete parsedZonefile.$ttl; const recordTypes = Object.keys(parsedZonefile); - const recordsToCreate = []; + const recordsToCreate: Array<{}> = []; recordTypes.forEach(function(recordType) { const recordTypeSet = arrify(parsedZonefile[recordType]); recordTypeSet.forEach(function(record) { @@ -1182,4 +1181,4 @@ promisifyAll(Zone, { * @name module:@google-cloud/dns.Zone * @see Zone */ -module.exports = Zone; +export {Zone}; diff --git a/system-test/dns.js b/system-test/dns.ts similarity index 88% rename from system-test/dns.js rename to system-test/dns.ts index a0e5112..5618633 100644 --- a/system-test/dns.js +++ b/system-test/dns.ts @@ -16,15 +16,15 @@ 'use strict'; -const assert = require('assert'); -const async = require('async'); +import * as assert from 'assert'; +import * as async from 'async'; const exec = require('methmeth'); const format = require('string-format-obj'); -const fs = require('fs'); +import * as fs from 'fs'; const tmp = require('tmp'); -const uuid = require('uuid'); +import * as uuid from 'uuid'; -const DNS = require('../'); +const {DNS} = require('../src'); const dns = new DNS(); const DNS_DOMAIN = process.env.GCLOUD_TESTS_DNS_DOMAIN || 'gitnpm.com.'; @@ -167,10 +167,10 @@ describe('dns', function() { }); it('should import records from a zone file', function(done) { - const zoneFilename = require.resolve('./data/zonefile.zone'); + const zoneFilename = require.resolve('../../system-test/data/zonefile.zone'); let zoneFileTemplate = fs.readFileSync(zoneFilename, 'utf-8'); zoneFileTemplate = format(zoneFileTemplate, { - DNS_DOMAIN: DNS_DOMAIN, + DNS_DOMAIN, }); tmp.setGracefulCleanup(); @@ -331,37 +331,23 @@ describe('dns', function() { }); }); - it('should replace records', function(done) { + it('should replace records', async () => { const name = 'test-zone-' + uuid.v4().substr(0, 18); // Do this in a new zone so no existing records are affected. - dns.createZone(name, {dnsName: DNS_DOMAIN}, function(err, zone) { - assert.ifError(err); - - zone.getRecords('ns', function(err, originalRecords) { - assert.ifError(err); - - const originalData = originalRecords[0].data; - - const newRecord = zone.record('ns', { - ttl: 3600, - name: DNS_DOMAIN, - data: ['ns1.nameserver.net.', 'ns2.nameserver.net.'], - }); - - zone.replaceRecords('ns', newRecord, function(err, change) { - assert.ifError(err); - - const deleted = change.metadata.deletions[0].rrdatas; - const added = change.metadata.additions[0].rrdatas; - - assert.deepStrictEqual(deleted, originalData); - assert.deepStrictEqual(added, newRecord.data); - - done(); - }); - }); + const [zone] = await dns.createZone(name, {dnsName: DNS_DOMAIN}); + const [originalRecords] = await zone.getRecords('ns'); + const originalData = originalRecords[0].data; + const newRecord = zone.record('ns', { + ttl: 3600, + name: DNS_DOMAIN, + data: ['ns1.nameserver.net.', 'ns2.nameserver.net.'], }); + const [change] = await zone.replaceRecords('ns', newRecord); + const deleted = change.metadata.deletions[0].rrdatas; + const added = change.metadata.additions[0].rrdatas; + assert.deepStrictEqual(deleted, originalData); + assert.deepStrictEqual(added, newRecord.data); }); }); }); diff --git a/test/change.js b/test/change.ts similarity index 92% rename from test/change.js rename to test/change.ts index b98b3a0..74d8d97 100644 --- a/test/change.js +++ b/test/change.ts @@ -16,16 +16,16 @@ 'use strict'; -const assert = require('assert'); -const extend = require('extend'); -const nodeutil = require('util'); -const proxyquire = require('proxyquire'); -const {ServiceObject} = require('@google-cloud/common'); -const promisify = require('@google-cloud/promisify'); +import * as assert from 'assert'; +import * as extend from 'extend'; +import * as nodeutil from 'util'; +import * as proxyquire from 'proxyquire'; +import {ServiceObject} from '@google-cloud/common'; +import * as promisify from '@google-cloud/promisify'; let promisified = false; const fakePromisify = extend({}, promisify, { - promisifyAll: function(Class) { + promisifyAll(Class) { if (Class.name === 'Change') { promisified = true; } @@ -45,7 +45,7 @@ describe('Change', function() { const ZONE = { name: 'zone-name', - createChange: function() {}, + createChange() {}, }; const CHANGE_ID = 'change-id'; @@ -56,7 +56,7 @@ describe('Change', function() { ServiceObject: FakeServiceObject, }, '@google-cloud/promisify': fakePromisify, - }); + }).Change; }); beforeEach(function() { diff --git a/test/index.js b/test/index.ts similarity index 94% rename from test/index.js rename to test/index.ts index ce581ee..4d265fd 100644 --- a/test/index.js +++ b/test/index.ts @@ -16,29 +16,28 @@ 'use strict'; -const arrify = require('arrify'); -const assert = require('assert'); -const extend = require('extend'); -const nodeutil = require('util'); -const proxyquire = require('proxyquire'); -const {Service} = require('@google-cloud/common'); +import * as arrify from 'arrify'; +import * as assert from 'assert'; +import * as extend from 'extend'; +import * as nodeutil from 'util'; +import * as proxyquire from 'proxyquire'; +import {Service} from '@google-cloud/common'; const {util} = require('@google-cloud/common'); -const promisify = require('@google-cloud/promisify'); +import * as promisify from '@google-cloud/promisify'; let extended = false; const fakePaginator = { paginator: { - extend: function(Class, methods) { + extend(Class, methods) { if (Class.name !== 'DNS') { return; } - extended = true; methods = arrify(methods); assert.strictEqual(Class.name, 'DNS'); assert.deepStrictEqual(methods, ['getZones']); }, - streamify: function(methodName) { + streamify(methodName) { return methodName; }, }, @@ -52,13 +51,13 @@ function FakeService() { nodeutil.inherits(FakeService, Service); const fakeUtil = extend({}, util, { - makeAuthenticatedRequestFactory: function() {}, + makeAuthenticatedRequestFactory() {}, }); const originalFakeUtil = extend(true, {}, fakeUtil); let promisified = false; const fakePromisify = extend({}, promisify, { - promisifyAll: function(Class, options) { + promisifyAll(Class, options) { if (Class.name !== 'DNS') { return; } @@ -78,14 +77,16 @@ describe('DNS', function() { const PROJECT_ID = 'project-id'; before(function() { - DNS = proxyquire('../', { + DNS = proxyquire('../src', { '@google-cloud/common': { Service: FakeService, }, '@google-cloud/paginator': fakePaginator, '@google-cloud/promisify': fakePromisify, - './zone': FakeZone, - }); + './zone': { + Zone: FakeZone, + } + }).DNS; }); beforeEach(function() { @@ -121,7 +122,7 @@ describe('DNS', function() { ]); assert.deepStrictEqual( calledWith.packageJson, - require('../package.json') + require('../../package.json') ); }); }); @@ -376,7 +377,6 @@ describe('DNS', function() { it('should return a Zone', function() { const newZoneName = 'new-zone-name'; const newZone = dns.zone(newZoneName); - assert(newZone instanceof FakeZone); assert.strictEqual(newZone.calledWith_[0], dns); assert.strictEqual(newZone.calledWith_[1], newZoneName); diff --git a/test/record.js b/test/record.ts similarity index 97% rename from test/record.js rename to test/record.ts index 40d4218..ad56663 100644 --- a/test/record.js +++ b/test/record.ts @@ -16,14 +16,14 @@ 'use strict'; -const assert = require('assert'); -const extend = require('extend'); -const proxyquire = require('proxyquire'); -const promisify = require('@google-cloud/promisify'); +import * as assert from 'assert'; +import * as extend from 'extend'; +import * as proxyquire from 'proxyquire'; +import * as promisify from '@google-cloud/promisify'; let promisified = false; const fakePromisify = extend({}, promisify, { - promisifyAll: function(Class, options) { + promisifyAll(Class, options) { if (Class.name !== 'Record') { return; } @@ -37,7 +37,7 @@ describe('Record', function() { let record; const ZONE = { - deleteRecords: function() {}, + deleteRecords() {}, }; const TYPE = 'A'; const METADATA = { @@ -49,7 +49,7 @@ describe('Record', function() { before(function() { Record = proxyquire('../src/record', { '@google-cloud/promisify': fakePromisify, - }); + }).Record; }); beforeEach(function() { diff --git a/test/zone.js b/test/zone.ts similarity index 95% rename from test/zone.js rename to test/zone.ts index 636b3f2..1f8ea2a 100644 --- a/test/zone.js +++ b/test/zone.ts @@ -16,21 +16,21 @@ 'use strict'; -const arrify = require('arrify'); -const assert = require('assert'); +import * as arrify from 'arrify'; +import * as assert from 'assert'; const exec = require('methmeth'); -const extend = require('extend'); +import * as extend from 'extend'; const flatten = require('lodash.flatten'); -const nodeutil = require('util'); +import * as nodeutil from 'util'; const prop = require('propprop'); -const proxyquire = require('proxyquire'); -const {ServiceObject} = require('@google-cloud/common'); -const promisify = require('@google-cloud/promisify'); -const uuid = require('uuid'); +import * as proxyquire from 'proxyquire'; +import {ServiceObject} from '@google-cloud/common'; +import * as promisify from '@google-cloud/promisify'; +import * as uuid from 'uuid'; let promisified = false; const fakePromisify = extend({}, promisify, { - promisifyAll: function(Class, options) { + promisifyAll(Class, options) { if (Class.name !== 'Zone') { return; } @@ -41,7 +41,7 @@ const fakePromisify = extend({}, promisify, { let parseOverride; const fakeDnsZonefile = { - parse: function() { + parse() { return (parseOverride || function() {}).apply(null, arguments); }, }; @@ -49,10 +49,10 @@ const fakeDnsZonefile = { let writeFileOverride; let readFileOverride; const fakeFs = { - readFile: function() { + readFile() { return (readFileOverride || function() {}).apply(null, arguments); }, - writeFile: function() { + writeFile() { return (writeFileOverride || function() {}).apply(null, arguments); }, }; @@ -61,14 +61,17 @@ function FakeChange() { this.calledWith_ = arguments; } -function FakeRecord() { - this.calledWith_ = arguments; +class FakeRecord { + calledWith_; + constructor() { + this.calledWith_ = arguments; + } + static fromZoneRecord_() { + const record = new FakeRecord(); + record.calledWith_ = arguments; + return record; + }; } -FakeRecord.fromZoneRecord_ = function() { - const record = new FakeRecord(); - record.calledWith_ = arguments; - return record; -}; function FakeServiceObject() { this.calledWith_ = arguments; @@ -80,7 +83,7 @@ nodeutil.inherits(FakeServiceObject, ServiceObject); let extended = false; const fakePaginator = { paginator: { - extend: function(Class, methods) { + extend(Class, methods) { if (Class.name !== 'Zone') { return; } @@ -90,7 +93,7 @@ const fakePaginator = { assert.strictEqual(Class.name, 'Zone'); assert.deepStrictEqual(methods, ['getChanges', 'getRecords']); }, - streamify: function(methodName) { + streamify(methodName) { return methodName; }, }, @@ -101,7 +104,7 @@ describe('Zone', function() { let zone; const DNS = { - createZone: function() {}, + createZone() {}, }; const ZONE_NAME = 'zone-name'; @@ -114,9 +117,13 @@ describe('Zone', function() { }, '@google-cloud/promisify': fakePromisify, '@google-cloud/paginator': fakePaginator, - './change': FakeChange, - './record': FakeRecord, - }); + './change': { + Change: FakeChange, + }, + './record': { + Record: FakeRecord + } + }).Zone; }); beforeEach(function() { @@ -147,7 +154,7 @@ describe('Zone', function() { it('should inherit from ServiceObject', function(done) { const dnsInstance = extend({}, DNS, { createZone: { - bind: function(context) { + bind(context) { assert.strictEqual(context, dnsInstance); done(); }, @@ -197,7 +204,7 @@ describe('Zone', function() { }); describe('createChange', function() { - function generateRecord(recordJson) { + function generateRecord(recordJson?) { recordJson = extend( { name: uuid.v1(), @@ -208,7 +215,7 @@ describe('Zone', function() { ); return { - toJSON: function() { + toJSON() { return recordJson; }, }; @@ -466,22 +473,22 @@ describe('Zone', function() { const records = [ { - toString: function() { + toString() { return 'a'; }, }, { - toString: function() { + toString() { return 'a'; }, }, { - toString: function() { + toString() { return 'a'; }, }, { - toString: function() { + toString() { return 'a'; }, }, @@ -638,7 +645,7 @@ describe('Zone', function() { it('should build a nextQuery if necessary', function(done) { const nextPageToken = 'next-page-token'; const apiResponseWithNextPageToken = extend({}, apiResponse, { - nextPageToken: nextPageToken, + nextPageToken, }); const expectedNextQuery = { pageToken: nextPageToken, @@ -720,7 +727,7 @@ describe('Zone', function() { it('should execute callback with nextQuery if necessary', function(done) { const nextPageToken = 'next-page-token'; const apiResponseWithNextPageToken = extend({}, apiResponse, { - nextPageToken: nextPageToken, + nextPageToken, }); const expectedNextQuery = {pageToken: nextPageToken}; @@ -830,7 +837,7 @@ describe('Zone', function() { describe('success', function() { const recordType = 'ns'; - let parsedZonefile = {}; + let parsedZonefile: any = {}; beforeEach(function() { parsedZonefile = { @@ -849,39 +856,29 @@ describe('Zone', function() { it('should add records', function(done) { zone.addRecords = function(recordsToCreate, callback) { assert.strictEqual(recordsToCreate.length, 1); - const recordToCreate = recordsToCreate[0]; - assert(recordToCreate instanceof FakeRecord); - const args = recordToCreate.calledWith_; assert.strictEqual(args[0], zone); assert.strictEqual(args[1], recordType); assert.strictEqual(args[2], parsedZonefile[recordType]); - callback(); }; - zone.import(path, done); }); it('should use the default ttl', function(done) { const defaultTTL = '90'; - parsedZonefile.$ttl = defaultTTL; parsedZonefile[recordType] = {}; parsedZonefile.mx = {ttl: '180'}; - zone.addRecords = function(recordsToCreate) { const record1 = recordsToCreate[0].calledWith_[2]; assert.strictEqual(record1.ttl, defaultTTL); - const record2 = recordsToCreate[1].calledWith_[2]; assert.strictEqual(record2.ttl, '180'); - done(); }; - zone.import(path, done); }); }); @@ -891,11 +888,8 @@ describe('Zone', function() { it('should return a Record object', function() { const type = 'a'; const metadata = {a: 'b', c: 'd'}; - const record = zone.record(type, metadata); - assert(record instanceof FakeRecord); - const args = record.calledWith_; assert.strictEqual(args[0], zone); assert.strictEqual(args[1], type); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..1942ae0 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "./node_modules/gts/tsconfig-google.json", + "compilerOptions": { + "rootDir": ".", + "outDir": "build", + "noImplicitAny": false, + "noImplicitThis": false + }, + "include": [ + "src/*.ts", + "test/*.ts", + "system-test/*.ts" + ] +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..617dc97 --- /dev/null +++ b/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "gts/tslint.json" +}