src/paginationCalls/pageDescriptor.ts
A descriptor for methods that support pagination.
Properties |
Methods |
constructor(requestPageTokenField: string, responsePageTokenField: string, resourceField: string)
|
Defined in src/paginationCalls/pageDescriptor.ts:50
|
Optional requestPageSizeField |
Type : string
|
Defined in src/paginationCalls/pageDescriptor.ts:49
|
requestPageTokenField |
Type : string
|
Defined in src/paginationCalls/pageDescriptor.ts:47
|
resourceField |
Type : string
|
Defined in src/paginationCalls/pageDescriptor.ts:50
|
responsePageTokenField |
Type : string
|
Defined in src/paginationCalls/pageDescriptor.ts:48
|
createStream | ||||||||||||
createStream(apiCall: GaxCall, request: literal type, options: CallSettings)
|
||||||||||||
Defined in src/paginationCalls/pageDescriptor.ts:65
|
||||||||||||
Creates a new object Stream which emits the resource on 'data' event.
Parameters :
Returns :
Transform
|
getApiCaller | ||||||
getApiCaller(settings: CallSettings)
|
||||||
Defined in src/paginationCalls/pageDescriptor.ts:121
|
||||||
Parameters :
Returns :
APICaller
|
import * as ended from 'is-stream-ended';
import {PassThrough, Transform} from 'stream';
import {APICaller} from '../apiCaller';
import {GaxCall} from '../apitypes';
import {Descriptor} from '../descriptor';
import {CallSettings} from '../gax';
import {NormalApiCaller} from '../normalCalls/normalApiCaller';
import {PagedApiCaller} from './pagedApiCaller';
/**
* A descriptor for methods that support pagination.
*/
export class PageDescriptor implements Descriptor {
requestPageTokenField: string;
responsePageTokenField: string;
requestPageSizeField?: string;
resourceField: string;
constructor(
requestPageTokenField: string,
responsePageTokenField: string,
resourceField: string
) {
this.requestPageTokenField = requestPageTokenField;
this.responsePageTokenField = responsePageTokenField;
this.resourceField = resourceField;
}
/**
* Creates a new object Stream which emits the resource on 'data' event.
*/
createStream(
apiCall: GaxCall,
request: {},
options: CallSettings
): Transform {
const stream = new PassThrough({objectMode: true});
options = Object.assign({}, options, {autoPaginate: false});
const maxResults = 'maxResults' in options ? options.maxResults : -1;
let pushCount = 0;
let started = false;
function callback(err: Error | null, resources, next) {
if (err) {
stream.emit('error', err);
return;
}
for (let i = 0; i < resources.length; ++i) {
if (ended(stream)) {
return;
}
if (resources[i] === null) {
continue;
}
stream.push(resources[i]);
pushCount++;
if (pushCount === maxResults) {
stream.end();
}
}
if (ended(stream)) {
return;
}
if (!next) {
stream.end();
return;
}
// When pageToken is specified in the original options, it will overwrite
// the page token field in the next request. Therefore it must be cleared.
if ('pageToken' in options) {
delete options.pageToken;
}
if (stream.isPaused()) {
request = next;
started = false;
} else {
setImmediate(apiCall, next, options, callback);
}
}
stream.on('resume', () => {
if (!started) {
started = true;
apiCall(request, options, callback);
}
});
return stream;
}
getApiCaller(settings: CallSettings): APICaller {
if (!settings.autoPaginate) {
return new NormalApiCaller();
}
return new PagedApiCaller(this);
}
}