Skip to content

Commit

Permalink
fix: Improve typescript types (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Sep 21, 2018
1 parent 0ee5b3f commit f7b625f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/google-cloud-dns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
25 changes: 14 additions & 11 deletions packages/google-cloud-dns/src/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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 = {
/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -230,15 +232,16 @@ export class Change extends ServiceObject {
* const apiResponse = data[1];
* });
*/
create(callback: ChangeCallback): void;
create(config?: CreateChangeRequest): Promise<CreateChangeResponse>;
create(config: CreateChangeRequest, callback: ChangeCallback): void;
create(callback: ChangeCallback): void;
create(
configOrCallback: CreateChangeRequest|ChangeCallback,
callback?: ChangeCallback) {
configOrCallback?: CreateChangeRequest|ChangeCallback,
callback?: ChangeCallback): void|Promise<CreateChangeResponse> {
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;
Expand Down
20 changes: 10 additions & 10 deletions packages/google-cloud-dns/src/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ 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;
}

export interface GetRecordsCallback {
(err: Error|null, records?: Record[]|null, nextQuery?: {}|null,
apiResponse?: Response): void;
apiResponse?: r.Response): void;
}

export interface GetRecordsRequest {
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -1134,7 +1134,7 @@ class Zone extends ServiceObject {
return;
}
if (records!.length === 0) {
callback();
callback(null);
return;
}
this.deleteRecords(records!, callback);
Expand Down
3 changes: 2 additions & 1 deletion packages/google-cloud-dns/test/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down

0 comments on commit f7b625f

Please sign in to comment.