Skip to content

Commit 61c8122

Browse files
refactor(ts): add more types (#246)
1 parent c2180e6 commit 61c8122

4 files changed

Lines changed: 207 additions & 111 deletions

File tree

handwritten/bigquery/src/index.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,24 @@ import {teenyRequest} from 'teeny-request';
3030

3131
import {Dataset, DataSetOptions} from './dataset';
3232
import {Job, JobOptions} from './job';
33-
import {Table, TempResponse} from './table';
33+
import {Table, TableField} from './table';
3434
import {GoogleErrorBody} from '@google-cloud/common/build/src/util';
3535

36+
export interface GetDatasetsOptions {
37+
all?: boolean;
38+
filter?: string;
39+
autoPaginate?: boolean;
40+
maxApiCalls?: number;
41+
maxResults?: number;
42+
pageToken?: string;
43+
}
44+
45+
export type DatasetsResponse = [Dataset[], GetDatasetsOptions, r.Response];
46+
export interface DatasetsCallback {
47+
(err: Error|null, datasets?: Dataset[]|null,
48+
nextQuery?: GetDatasetsOptions|null, apiResponse?: r.Response): void;
49+
}
50+
3651
export type CreateQueryJobResponse = [Job, r.Response];
3752
export interface CreateQueryJobCallback {
3853
(err: Error|null, job?: Job, apiResponse?: r.Response): void;
@@ -235,7 +250,7 @@ export class BigQuery extends common.Service {
235250
});
236251
}
237252

238-
function convert(schemaField, value) {
253+
function convert(schemaField: TableField, value) {
239254
if (is.null(value)) {
240255
return value;
241256
}
@@ -1085,18 +1100,16 @@ export class BigQuery extends common.Service {
10851100
* //-
10861101
* bigquery.getDatasets().then(function(datasets) {});
10871102
*/
1088-
getDatasets(options?): Promise<TempResponse>;
1089-
getDatasets(options, callback): void;
1090-
getDatasets(callback): void;
1091-
getDatasets(options?, callback?): void|Promise<TempResponse> {
1092-
const that = this;
1093-
1094-
if (is.fn(options)) {
1095-
callback = options;
1096-
options = {};
1097-
}
1098-
1099-
options = options || {};
1103+
getDatasets(options?: GetDatasetsOptions): Promise<DatasetsResponse>;
1104+
getDatasets(options: GetDatasetsOptions, callback: DatasetsCallback): void;
1105+
getDatasets(callback: DatasetsCallback): void;
1106+
getDatasets(
1107+
optionsOrCallback?: GetDatasetsOptions|DatasetsCallback,
1108+
cb?: DatasetsCallback): void|Promise<DatasetsResponse> {
1109+
const options =
1110+
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
1111+
const callback =
1112+
typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
11001113

11011114
this.request(
11021115
{
@@ -1105,11 +1118,11 @@ export class BigQuery extends common.Service {
11051118
},
11061119
(err, resp) => {
11071120
if (err) {
1108-
callback(err, null, null, resp);
1121+
callback!(err, null, null, resp);
11091122
return;
11101123
}
11111124

1112-
let nextQuery = null;
1125+
let nextQuery: GetDatasetsOptions|null = null;
11131126

11141127
if (resp.nextPageToken) {
11151128
nextQuery = extend({}, options, {
@@ -1118,15 +1131,15 @@ export class BigQuery extends common.Service {
11181131
}
11191132

11201133
const datasets = (resp.datasets || []).map((dataset) => {
1121-
const ds = that.dataset(dataset.datasetReference.datasetId, {
1134+
const ds = this.dataset(dataset.datasetReference.datasetId, {
11221135
location: dataset.location,
11231136
});
11241137

11251138
ds.metadata = dataset;
11261139
return ds;
11271140
});
11281141

1129-
callback(null, datasets, nextQuery, resp);
1142+
callback!(null, datasets, nextQuery, resp);
11301143
});
11311144
}
11321145

0 commit comments

Comments
 (0)