-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
274 lines (245 loc) · 8.71 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*!
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*!
* @module common/paginator
*/
import arrify = require('arrify');
import * as extend from 'extend';
import {TransformOptions} from 'stream';
import {ResourceStream} from './resource-stream';
export interface ParsedArguments extends TransformOptions {
/**
* Query object. This is most commonly an object, but to make the API more
* simple, it can also be a string in some places.
*/
query?: ParsedArguments;
/**
* Callback function.
*/
callback?: Function;
/**
* Auto-pagination enabled.
*/
autoPaginate?: boolean;
/**
* Maximum API calls to make.
*/
maxApiCalls?: number;
/**
* Maximum results to return.
*/
maxResults?: number;
pageSize?: number;
streamOptions?: ParsedArguments;
}
/*! Developer Documentation
*
* paginator is used to auto-paginate `nextQuery` methods as well as
* streamifying them.
*
* Before:
*
* search.query('done=true', function(err, results, nextQuery) {
* search.query(nextQuery, function(err, results, nextQuery) {});
* });
*
* After:
*
* search.query('done=true', function(err, results) {});
*
* Methods to extend should be written to accept callbacks and return a
* `nextQuery`.
*/
export class Paginator {
/**
* Cache the original method, then overwrite it on the Class's prototype.
*
* @param {function} Class - The parent class of the methods to extend.
* @param {string|string[]} methodNames - Name(s) of the methods to extend.
*/
// tslint:disable-next-line:variable-name
extend(Class: Function, methodNames: string | string[]) {
methodNames = arrify(methodNames);
methodNames.forEach(methodName => {
const originalMethod = Class.prototype[methodName];
// map the original method to a private member
Class.prototype[methodName + '_'] = originalMethod;
// overwrite the original to auto-paginate
/* eslint-disable @typescript-eslint/no-explicit-any */
Class.prototype[methodName] = function (...args: any[]) {
const parsedArguments = paginator.parseArguments_(args);
return paginator.run_(parsedArguments, originalMethod.bind(this));
};
});
}
/**
* Wraps paginated API calls in a readable object stream.
*
* This method simply calls the nextQuery recursively, emitting results to a
* stream. The stream ends when `nextQuery` is null.
*
* `maxResults` will act as a cap for how many results are fetched and emitted
* to the stream.
*
* @param {string} methodName - Name of the method to streamify.
* @return {function} - Wrapped function.
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
streamify<T = any>(methodName: string) {
return function (
// tslint:disable-next-line:no-any
this: {[index: string]: Function},
/* eslint-disable @typescript-eslint/no-explicit-any */
...args: any[]
): ResourceStream<T> {
const parsedArguments = paginator.parseArguments_(args);
const originalMethod = this[methodName + '_'] || this[methodName];
return paginator.runAsStream_<T>(
parsedArguments,
originalMethod.bind(this)
);
};
}
/**
* Parse a pseudo-array `arguments` for a query and callback.
*
* @param {array} args - The original `arguments` pseduo-array that the original
* method received.
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
parseArguments_(args: any[]) {
let query: string | ParsedArguments | undefined;
let autoPaginate = true;
let maxApiCalls = -1;
let maxResults = -1;
let callback: Function | undefined;
const firstArgument = args[0];
const lastArgument = args[args.length - 1];
if (typeof firstArgument === 'function') {
callback = firstArgument;
} else {
query = firstArgument;
}
if (typeof lastArgument === 'function') {
callback = lastArgument;
}
if (typeof query === 'object') {
query = extend<{}, ParsedArguments>(true, {}, query) as ParsedArguments;
// Check if the user only asked for a certain amount of results.
if (query.maxResults && typeof query.maxResults === 'number') {
// `maxResults` is used API-wide.
maxResults = query.maxResults;
} else if (typeof query.pageSize === 'number') {
// `pageSize` is Pub/Sub's `maxResults`.
maxResults = query.pageSize!;
}
if (query.maxApiCalls && typeof query.maxApiCalls === 'number') {
maxApiCalls = query.maxApiCalls;
delete query.maxApiCalls;
}
// maxResults is the user specified limit.
if (maxResults !== -1 || query.autoPaginate === false) {
autoPaginate = false;
}
}
const parsedArguments = {
query: query || {},
autoPaginate,
maxApiCalls,
maxResults,
callback,
} as ParsedArguments;
parsedArguments.streamOptions = extend<{}, ParsedArguments>(
true,
{},
parsedArguments.query as ParsedArguments
);
delete parsedArguments.streamOptions.autoPaginate;
delete parsedArguments.streamOptions.maxResults;
delete parsedArguments.streamOptions.pageSize;
return parsedArguments;
}
/**
* This simply checks to see if `autoPaginate` is set or not, if it's true
* then we buffer all results, otherwise simply call the original method.
*
* @param {array} parsedArguments - Parsed arguments from the original method
* call.
* @param {object=|string=} parsedArguments.query - Query object. This is most
* commonly an object, but to make the API more simple, it can also be a
* string in some places.
* @param {function=} parsedArguments.callback - Callback function.
* @param {boolean} parsedArguments.autoPaginate - Auto-pagination enabled.
* @param {boolean} parsedArguments.maxApiCalls - Maximum API calls to make.
* @param {number} parsedArguments.maxResults - Maximum results to return.
* @param {function} originalMethod - The cached method that accepts a callback
* and returns `nextQuery` to receive more results.
*/
run_(parsedArguments: ParsedArguments, originalMethod: Function) {
const query = parsedArguments.query;
const callback = parsedArguments.callback!;
if (!parsedArguments.autoPaginate) {
return originalMethod(query, callback);
}
const results = new Array<{}>();
let otherArgs: unknown[] = [];
const promise = new Promise((resolve, reject) => {
const stream = paginator.runAsStream_(parsedArguments, originalMethod);
stream
.on('error', reject)
.on('data', (data: {}) => results.push(data))
.on('end', () => {
otherArgs = stream._otherArgs || [];
resolve(results);
});
});
if (!callback) {
return promise.then(results => [results, query, ...otherArgs]);
}
promise.then(
results => callback(null, results, query, ...otherArgs),
(err: Error) => callback(err)
);
}
/**
* This method simply calls the nextQuery recursively, emitting results to a
* stream. The stream ends when `nextQuery` is null.
*
* `maxResults` will act as a cap for how many results are fetched and emitted
* to the stream.
*
* @param {object=|string=} parsedArguments.query - Query object. This is most
* commonly an object, but to make the API more simple, it can also be a
* string in some places.
* @param {function=} parsedArguments.callback - Callback function.
* @param {boolean} parsedArguments.autoPaginate - Auto-pagination enabled.
* @param {boolean} parsedArguments.maxApiCalls - Maximum API calls to make.
* @param {number} parsedArguments.maxResults - Maximum results to return.
* @param {function} originalMethod - The cached method that accepts a callback
* and returns `nextQuery` to receive more results.
* @return {stream} - Readable object stream.
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
runAsStream_<T = any>(
parsedArguments: ParsedArguments,
originalMethod: Function
): ResourceStream<T> {
return new ResourceStream<T>(parsedArguments, originalMethod);
}
}
const paginator = new Paginator();
export {paginator};
export {ResourceStream};