From f7b625f8f6af200c86105ffd61889226bd001e18 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Fri, 21 Sep 2018 11:22:47 -0700 Subject: [PATCH] fix: Improve typescript types (#109) --- packages/google-cloud-dns/package.json | 2 +- packages/google-cloud-dns/src/change.ts | 25 ++++++++++++++----------- packages/google-cloud-dns/src/zone.ts | 20 ++++++++++---------- packages/google-cloud-dns/test/zone.ts | 3 ++- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/packages/google-cloud-dns/package.json b/packages/google-cloud-dns/package.json index efb6f786819..d59824d7389 100644 --- a/packages/google-cloud-dns/package.json +++ b/packages/google-cloud-dns/package.json @@ -51,7 +51,7 @@ "system-test": "mocha build/system-test --timeout 600000", "clean": "gts clean", "compile": "tsc -p .", - "fix": "gts fix && prettier --write samples/*.js samples/*/*.js", + "fix": "eslint --fix 'samples/**/*.js' && prettier --write samples/*.js samples/*/*.js && gts fix", "prepare": "npm run compile", "pretest": "npm run compile" }, diff --git a/packages/google-cloud-dns/src/change.ts b/packages/google-cloud-dns/src/change.ts index de5a91fde53..8a8ef0a8bdc 100644 --- a/packages/google-cloud-dns/src/change.ts +++ b/packages/google-cloud-dns/src/change.ts @@ -17,19 +17,21 @@ 'use strict'; import {teenyRequest} from 'teeny-request'; -import {ServiceObject} from '@google-cloud/common'; +import {ServiceObject, CreateOptions} from '@google-cloud/common'; import {promisifyAll} from '@google-cloud/promisify'; import {Zone} from './zone'; import {Record} from './record'; -import {Response} from 'request'; +import * as r from 'request'; -export interface CreateChangeRequest { +export interface CreateChangeRequest extends CreateOptions { add?: Record|Record[]; delete?: Record|Record[]; } +export type CreateChangeResponse = [Change, r.Response]; + export interface ChangeCallback { - (err?: Error|null, change?: Change|null, apiResponse?: Response): void; + (err: Error|null, change?: Change|null, apiResponse?: r.Response): void; } /** @@ -45,6 +47,7 @@ export interface ChangeCallback { * const change = zone.change('change-id'); */ export class Change extends ServiceObject { + parent!: Zone; constructor(zone: Zone, id?: string) { const methods = { /** @@ -192,9 +195,8 @@ export class Change extends ServiceObject { */ id, methods, - requestModule: teenyRequest, - // tslint:disable-next-line:no-any - } as any); + requestModule: teenyRequest as typeof r, + }); } /** * Create a change. @@ -230,15 +232,16 @@ export class Change extends ServiceObject { * const apiResponse = data[1]; * }); */ - create(callback: ChangeCallback): void; + create(config?: CreateChangeRequest): Promise; create(config: CreateChangeRequest, callback: ChangeCallback): void; + create(callback: ChangeCallback): void; create( - configOrCallback: CreateChangeRequest|ChangeCallback, - callback?: ChangeCallback) { + configOrCallback?: CreateChangeRequest|ChangeCallback, + callback?: ChangeCallback): void|Promise { const config = typeof configOrCallback === 'object' ? configOrCallback : {}; callback = typeof configOrCallback === 'function' ? configOrCallback! : callback; - (this.parent as Zone).createChange(config, (err, change, apiResponse) => { + this.parent.createChange(config, (err, change, apiResponse) => { if (err) { callback!(err, null, apiResponse); return; diff --git a/packages/google-cloud-dns/src/zone.ts b/packages/google-cloud-dns/src/zone.ts index 2998098614a..6c653823e66 100644 --- a/packages/google-cloud-dns/src/zone.ts +++ b/packages/google-cloud-dns/src/zone.ts @@ -31,7 +31,7 @@ const zonefile = require('dns-zonefile'); import {Change, ChangeCallback, CreateChangeRequest} from './change'; import {Record, RecordMetadata, RecordObject} from './record'; import {DNS} from '.'; -import {Response} from 'request'; +import * as r from 'request'; export interface DeleteZoneConfig { force?: boolean; @@ -39,7 +39,7 @@ export interface DeleteZoneConfig { export interface GetRecordsCallback { (err: Error|null, records?: Record[]|null, nextQuery?: {}|null, - apiResponse?: Response): void; + apiResponse?: r.Response): void; } export interface GetRecordsRequest { @@ -63,7 +63,7 @@ export interface GetChangesRequest { export interface GetChangesCallback { (err: Error|null, changes?: Change[]|null, nextQuery?: {}|null, - apiResponse?: Response): void; + apiResponse?: r.Response): void; } /** @@ -245,8 +245,7 @@ class Zone extends ServiceObject { id: name, createMethod: dns.createZone.bind(dns), methods, - // tslint:disable-next-line:no-any - requestModule: teenyRequest as any, + requestModule: teenyRequest as typeof r, }); /** * @name Zone#name @@ -460,11 +459,12 @@ class Zone extends ServiceObject { * const apiResponse = data[0]; * }); */ + delete(options?: DeleteZoneConfig): Promise<[r.Response]>; delete(callback: DeleteCallback): void; delete(options: DeleteZoneConfig, callback: DeleteCallback): void; delete( - optionsOrCallback: DeleteZoneConfig|DeleteCallback, - callback?: DeleteCallback): void { + optionsOrCallback?: DeleteZoneConfig|DeleteCallback, + callback?: DeleteCallback): void|Promise<[r.Response]> { const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {}; callback = @@ -473,7 +473,7 @@ class Zone extends ServiceObject { this.empty(this.delete.bind(this, callback)); return; } - super.delete(callback); + super.delete(callback!); } /** * @typedef {array} ZoneDeleteRecordsResponse @@ -616,7 +616,7 @@ class Zone extends ServiceObject { return record.type !== 'NS' && record.type !== 'SOA'; }); if (recordsToDelete.length === 0) { - callback(); + callback(null); } else { this.deleteRecords(recordsToDelete, callback); } @@ -1134,7 +1134,7 @@ class Zone extends ServiceObject { return; } if (records!.length === 0) { - callback(); + callback(null); return; } this.deleteRecords(records!, callback); diff --git a/packages/google-cloud-dns/test/zone.ts b/packages/google-cloud-dns/test/zone.ts index c22db228f61..811abea898a 100644 --- a/packages/google-cloud-dns/test/zone.ts +++ b/packages/google-cloud-dns/test/zone.ts @@ -353,7 +353,8 @@ describe('Zone', () => { }); it('should try to delete again after emptying', done => { - FakeServiceObject.prototype.delete = () => { + // tslint:disable-next-line:no-any + (FakeServiceObject.prototype as any).delete = () => { done(); };