Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Enable noImplicitAny #105

Merged
merged 2 commits into from
Sep 14, 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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import * as extend from 'extend';
import {teenyRequest} from 'teeny-request';
import {Zone} from './zone';
import {Response} from 'request';
export {Record, RecordMetadata} from './record';

export interface GetZonesRequest {
autoPaginate?: boolean;
Expand Down
31 changes: 23 additions & 8 deletions src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ import {ChangeCallback} from './change';
import {Zone} from './zone';
const format = require('string-format-obj');

export interface RecordObject {
rrdatas?: Array<{}>;
rrdata?: {};
data?: {};
type?: string;
}

export interface RecordMetadata {
name: string;
data: string|string[];
ttl: number;
type?: string;
signatureRrdatas?: string[];
}

/**
* Create a Resource Record object.
*
Expand All @@ -49,13 +64,13 @@ const format = require('string-format-obj');
* data: '1.2.3.4'
* });
*/
export class Record {
export class Record implements RecordObject {
zone_: Zone;
type: string;
metadata;
rrdatas;
data;
constructor(zone: Zone, type: string, metadata) {
metadata: RecordMetadata;
rrdatas?: Array<{}>;
data?: {};
constructor(zone: Zone, type: string, metadata: RecordMetadata) {
this.zone_ = zone;
/**
* @name Record#type
Expand Down Expand Up @@ -137,7 +152,7 @@ export class Record {
* @returns {object}
*/
toJSON() {
const recordObject = extend({}, this.metadata, {
const recordObject: RecordObject = extend({}, this.metadata, {
type: this.type.toUpperCase(),
});
if (recordObject.data) {
Expand Down Expand Up @@ -171,7 +186,7 @@ export class Record {
* based on the type of record.
* @returns {Record}
*/
static fromZoneRecord_(zone: Zone, type: string, bindData) {
static fromZoneRecord_(zone: Zone, type: string, bindData: RecordMetadata) {
const typeToZoneFormat = {
a: '{ip}',
aaaa: '{ip}',
Expand All @@ -182,7 +197,7 @@ export class Record {
spf: '{data}',
srv: '{priority} {weight} {port} {target}',
txt: '{txt}',
};
} as {[index: string]: string};
const metadata = {
data: format(typeToZoneFormat[type.toLowerCase()], bindData),
name: bindData.name,
Expand Down
48 changes: 34 additions & 14 deletions src/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {teenyRequest} from 'teeny-request';
const zonefile = require('dns-zonefile');

import {Change, ChangeCallback, CreateChangeRequest} from './change';
import {Record} from './record';
import {Record, RecordMetadata, RecordObject} from './record';
import {DNS} from '.';
import {Response} from 'request';

Expand All @@ -52,6 +52,20 @@ export interface GetRecordsRequest {
filterByTypes_?: {[index: string]: boolean};
}

export interface GetChangesRequest {
autoPaginate?: boolean;
maxApiCalls?: number;
maxResults?: number;
pageToken?: string;
sort?: string;
sortOrder?: string;
}

export interface GetChangesCallback {
(err: Error|null, changes?: Change[]|null, nextQuery?: {}|null,
apiResponse?: Response): void;
}

/**
* A Zone object is used to interact with your project's managed zone. It will
* help you add or delete records, delete your zone, and many other convenience
Expand Down Expand Up @@ -349,7 +363,7 @@ class Zone extends ServiceObject {
if (!config || (!config.add && !config.delete)) {
throw new Error('Cannot create a change with no additions or deletions.');
}
const groupByType = changes => {
const groupByType = (changes: RecordObject[]) => {
changes = groupBy(changes, 'type');
const changesArray: Array<{}> = [];
// tslint:disable-next-line:forin
Expand All @@ -361,7 +375,8 @@ class Zone extends ServiceObject {
const templateRecord = extend({}, records[0]);
if (records.length > 1) {
// Combine the `rrdatas` values from all records of the same type.
templateRecord.rrdatas = flatten(records.map(x => x.rrdatas));
templateRecord.rrdatas =
flatten(records.map((x: RecordObject) => x.rrdatas));
}
changesArray.push(templateRecord);
}
Expand Down Expand Up @@ -643,7 +658,7 @@ class Zone extends ServiceObject {
* //-
* zone.export(zoneFilename).then(() => {});
*/
export(localPath: string, callback) {
export(localPath: string, callback: (err: Error|null) => void) {
this.getRecords((err, records) => {
if (err) {
callback(err);
Expand Down Expand Up @@ -718,8 +733,13 @@ class Zone extends ServiceObject {
* const changes = data[0];
* });
*/
getChanges(query, callback?) {
if (is.fn(query)) {
getChanges(callback: GetChangesCallback): void;
getChanges(query: GetChangesRequest, callback: GetChangesCallback): void;
getChanges(
queryOrCallback: GetChangesRequest|GetChangesCallback,
callback?: GetChangesCallback) {
let query = queryOrCallback as GetChangesRequest;
if (typeof query === 'function') {
callback = query;
query = {};
}
Expand All @@ -734,10 +754,10 @@ class Zone extends ServiceObject {
},
(err, resp) => {
if (err) {
callback(err, null, null, resp);
callback!(err, null, null, resp);
return;
}
const changes = (resp.changes || []).map(change => {
const changes = (resp.changes || []).map((change: Change) => {
const changeInstance = this.change(change.id);
changeInstance.metadata = change;
return changeInstance;
Expand All @@ -748,7 +768,7 @@ class Zone extends ServiceObject {
pageToken: resp.nextPageToken,
});
}
callback(null, changes, nextQuery, resp);
callback!(null, changes, nextQuery, resp);
});
}
/**
Expand Down Expand Up @@ -879,12 +899,12 @@ class Zone extends ServiceObject {
callback!(err, null, null, resp);
return;
}
let records = (resp.rrsets || []).map(record => {
return this.record(record.type, record);
let records = (resp.rrsets || []).map((record: RecordMetadata) => {
return this.record(record.type!, record);
});
if ((query as GetRecordsRequest).filterByTypes_) {
records = records.filter(record => {
return (query as GetRecordsRequest).filterByTypes_![record.type];
records = records.filter((record: RecordMetadata) => {
return (query as GetRecordsRequest).filterByTypes_![record.type!];
});
}
let nextQuery: {}|null = null;
Expand Down Expand Up @@ -1009,7 +1029,7 @@ class Zone extends ServiceObject {
* delete: oldARecord
* }, (err, change, apiResponse) => {});
*/
record(type: string, metadata: {}) {
record(type: string, metadata: RecordMetadata) {
return new Record(this, type, metadata);
}
/**
Expand Down
30 changes: 17 additions & 13 deletions system-test/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const tmp = require('tmp');
import * as uuid from 'uuid';
import {DNS} from '../src';
import {ChangeCallback} from '../src/change';
import {Record} from '../src';
import {Response} from 'request';

const dns = new DNS();
const DNS_DOMAIN = process.env.GCLOUD_TESTS_DNS_DOMAIN || 'gitnpm.com.';
Expand Down Expand Up @@ -175,7 +177,7 @@ describe('dns', () => {
});

tmp.setGracefulCleanup();
tmp.file((err, tmpFilePath) => {
tmp.file((err: Error, tmpFilePath: string) => {
assert.ifError(err);
fs.writeFileSync(tmpFilePath, zoneFileTemplate, 'utf-8');
ZONE.empty(err => {
Expand All @@ -191,15 +193,15 @@ describe('dns', () => {
})[0];

assert.strictEqual(
spfRecord.toJSON().rrdatas[0],
spfRecord.toJSON().rrdatas![0],
'"v=spf1" "mx:' + DNS_DOMAIN + '" "-all"');

const txtRecord = records!.filter(record => {
return record.type === 'TXT';
})[0];

assert.strictEqual(
txtRecord.toJSON().rrdatas[0],
txtRecord.toJSON().rrdatas![0],
'"google-site-verification=xxxxxxxxxxxxYYYYYYXXX"');

done();
Expand All @@ -211,7 +213,7 @@ describe('dns', () => {

it('should export records to a zone file', done => {
tmp.setGracefulCleanup();
tmp.file((err, tmpFilename) => {
tmp.file((err: Error, tmpFilename: string) => {
assert.ifError(err);
async.series(
[
Expand Down Expand Up @@ -245,15 +247,15 @@ describe('dns', () => {
it('should get a list of changes', done => {
ZONE.getChanges((err, changes) => {
assert.ifError(err);
assert(changes.length >= 0);
assert(changes!.length >= 0);
done();
});
});

it('should get metadata', done => {
ZONE.getChanges((err, changes) => {
assert.ifError(err);
const change = changes[0];
const change = changes![0];
const expectedMetadata = change.metadata;
change.getMetadata((err, metadata) => {
assert.ifError(err);
Expand Down Expand Up @@ -292,13 +294,15 @@ describe('dns', () => {

ZONE.replaceRecords('cname', newRecords, err => {
assert.ifError(err);
const onRecordsReceived = (err, records, nextQuery) => {
if (nextQuery) {
ZONE.getRecords(nextQuery, onRecordsReceived);
return;
}
ZONE.deleteRecords(newRecords, done);
};
const onRecordsReceived =
(err?: Error|null, records?: Record[]|null, nextQuery?: {}|null,
apiResponse?: Response) => {
if (nextQuery) {
ZONE.getRecords(nextQuery, onRecordsReceived);
return;
}
ZONE.deleteRecords(newRecords, done);
};
ZONE.getRecords(
{
type: 'cname',
Expand Down
49 changes: 26 additions & 23 deletions test/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import * as extend from 'extend';
import * as proxyquire from 'proxyquire';
import {ServiceObject, ServiceObjectConfig} from '@google-cloud/common';
import * as promisify from '@google-cloud/promisify';
import {Response} from 'request';
import {Change} from '../src/change';

let promisified = false;
const fakePromisify = extend({}, promisify, {
// tslint:disable-next-line:variable-name
promisifyAll(Class) {
if (Class.name === 'Change') {
promisifyAll(esClass: Function) {
if (esClass.name === 'Change') {
promisified = true;
}
},
Expand All @@ -41,9 +42,10 @@ class FakeServiceObject extends ServiceObject {
}

describe('Change', () => {
// tslint:disable-next-line:variable-name
let Change;
let change;
// tslint:disable-next-line:variable-name no-any
let Change: any;
// tslint:disable-next-line: no-any
let change: any;

const ZONE = {
name: 'zone-name',
Expand Down Expand Up @@ -90,7 +92,7 @@ describe('Change', () => {
it('should call the parent change method', done => {
const config = {};

change.parent.createChange = config_ => {
change.parent.createChange = (config_: {}) => {
assert.strictEqual(config, config_);
done();
};
Expand All @@ -103,19 +105,19 @@ describe('Change', () => {
const apiResponse = {};

beforeEach(() => {
change.parent.createChange = (config, callback) => {
change.parent.createChange = (config: {}, callback: Function) => {
callback(error, null, apiResponse);
};
});

it('should execute callback with error & apiResponse', done => {
change.create({}, (err, change, apiResponse_) => {
assert.strictEqual(err, error);
assert.strictEqual(change, null);
assert.strictEqual(apiResponse_, apiResponse);

done();
});
change.create(
{}, (err: Error, change: Change, apiResponse_: Response) => {
assert.strictEqual(err, error);
assert.strictEqual(change, null);
assert.strictEqual(apiResponse_, apiResponse);
done();
});
});
});

Expand All @@ -127,22 +129,23 @@ describe('Change', () => {
const apiResponse = {};

beforeEach(() => {
change.parent.createChange = (config, callback) => {
change.parent.createChange = (config: {}, callback: Function) => {
callback(null, changeInstance, apiResponse);
};
});

it('should execute callback with self & API response', done => {
change.create({}, (err, change_, apiResponse_) => {
assert.ifError(err);
assert.strictEqual(change_, change);
assert.strictEqual(apiResponse_, apiResponse);
done();
});
change.create(
{}, (err: Error, change_: Change, apiResponse_: Response) => {
assert.ifError(err);
assert.strictEqual(change_, change);
assert.strictEqual(apiResponse_, apiResponse);
done();
});
});

it('should assign the ID and metadata from the change', done => {
change.create({}, (err, change_) => {
change.create({}, (err: Error, change_: Change) => {
assert.ifError(err);
assert.strictEqual(change_.id, changeInstance.id);
assert.strictEqual(change_.metadata, changeInstance.metadata);
Expand Down
Loading