apify-client 3.0.0-beta.62 → 3.0.0-beta.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Readable } from 'node:stream';
|
|
1
2
|
import type { STORAGE_GENERAL_ACCESS } from '@apify/consts';
|
|
2
3
|
import type { ApiClientSubResourceOptions } from '../base/api_client.js';
|
|
3
4
|
import { ResourceClient } from '../base/resource_client.js';
|
|
@@ -167,6 +168,34 @@ export declare class DatasetClient<Data extends Record<string | number, any> = R
|
|
|
167
168
|
* ```
|
|
168
169
|
*/
|
|
169
170
|
downloadItems(format: `${DownloadItemsFormat}`, options?: DatasetClientDownloadItemsOptions): Promise<Buffer>;
|
|
171
|
+
/**
|
|
172
|
+
* Downloads dataset items in a specific format as a Readable stream. Only works in Node.js.
|
|
173
|
+
*
|
|
174
|
+
* Takes the same arguments as {@link downloadItems}, but resolves to the response body as a stream, so a large
|
|
175
|
+
* export is processed in chunks without holding all of it in memory.
|
|
176
|
+
*
|
|
177
|
+
* The stream holds its connection open until you consume it to the end or destroy it. Iterating it with
|
|
178
|
+
* `for await` or passing it to `pipeline()` from `node:stream/promises` does either one for you.
|
|
179
|
+
*
|
|
180
|
+
* @param format - Output format: `'json'`, `'jsonl'`, `'csv'`, `'xlsx'`, `'xml'`, `'rss'`, or `'html'`
|
|
181
|
+
* @param options - Download and formatting options, the same as for {@link downloadItems}
|
|
182
|
+
* @param options.timeoutSecs - Timeout for the API to start responding. Default is `'long'`. Once the stream is
|
|
183
|
+
* returned, it only limits inactivity: the stream errors if no data arrives for that long. To stop the download
|
|
184
|
+
* early or cap its total duration, destroy the stream or abort `signal`.
|
|
185
|
+
* @returns Readable stream of the items serialized to the specified format
|
|
186
|
+
* @see https://docs.apify.com/api/v2/dataset-items-get
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```javascript
|
|
190
|
+
* const { createWriteStream } = require('node:fs');
|
|
191
|
+
* const { pipeline } = require('node:stream/promises');
|
|
192
|
+
*
|
|
193
|
+
* // Write a CSV export straight to a file
|
|
194
|
+
* const stream = await client.dataset('my-dataset').streamItems('csv', { bom: true });
|
|
195
|
+
* await pipeline(stream, createWriteStream('output.csv'));
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
streamItems(format: `${DownloadItemsFormat}`, options?: DatasetClientDownloadItemsOptions): Promise<Readable>;
|
|
170
199
|
/**
|
|
171
200
|
* Stores one or more items into the dataset.
|
|
172
201
|
*
|
|
@@ -3,6 +3,7 @@ import { createStorageContentSignatureAsync } from '@apify/utilities';
|
|
|
3
3
|
import { ResourceClient } from '../base/resource_client.js';
|
|
4
4
|
import * as schemas from '../schemas.js';
|
|
5
5
|
import { timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
|
|
6
|
+
import { runtime } from '#runtime';
|
|
6
7
|
import { anyObjectSchema, applyQueryParamsToUrl, cast, isNonArrayObject, paginationOptionsShape, parseArgument, parseResponse, SCANNED_COUNT, } from '../utils.js';
|
|
7
8
|
/**
|
|
8
9
|
* Supported formats for downloading dataset items.
|
|
@@ -269,6 +270,52 @@ export class DatasetClient extends ResourceClient {
|
|
|
269
270
|
});
|
|
270
271
|
return cast(data);
|
|
271
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Downloads dataset items in a specific format as a Readable stream. Only works in Node.js.
|
|
275
|
+
*
|
|
276
|
+
* Takes the same arguments as {@link downloadItems}, but resolves to the response body as a stream, so a large
|
|
277
|
+
* export is processed in chunks without holding all of it in memory.
|
|
278
|
+
*
|
|
279
|
+
* The stream holds its connection open until you consume it to the end or destroy it. Iterating it with
|
|
280
|
+
* `for await` or passing it to `pipeline()` from `node:stream/promises` does either one for you.
|
|
281
|
+
*
|
|
282
|
+
* @param format - Output format: `'json'`, `'jsonl'`, `'csv'`, `'xlsx'`, `'xml'`, `'rss'`, or `'html'`
|
|
283
|
+
* @param options - Download and formatting options, the same as for {@link downloadItems}
|
|
284
|
+
* @param options.timeoutSecs - Timeout for the API to start responding. Default is `'long'`. Once the stream is
|
|
285
|
+
* returned, it only limits inactivity: the stream errors if no data arrives for that long. To stop the download
|
|
286
|
+
* early or cap its total duration, destroy the stream or abort `signal`.
|
|
287
|
+
* @returns Readable stream of the items serialized to the specified format
|
|
288
|
+
* @see https://docs.apify.com/api/v2/dataset-items-get
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* ```javascript
|
|
292
|
+
* const { createWriteStream } = require('node:fs');
|
|
293
|
+
* const { pipeline } = require('node:stream/promises');
|
|
294
|
+
*
|
|
295
|
+
* // Write a CSV export straight to a file
|
|
296
|
+
* const stream = await client.dataset('my-dataset').streamItems('csv', { bom: true });
|
|
297
|
+
* await pipeline(stream, createWriteStream('output.csv'));
|
|
298
|
+
* ```
|
|
299
|
+
*/
|
|
300
|
+
async streamItems(format, options = {}) {
|
|
301
|
+
parseArgument(format, itemFormatSchema);
|
|
302
|
+
const { timeoutSecs = 'long', signal, ...query } = parseArgument(options, downloadItemsOptionsSchema, 'DatasetClientDownloadItemsOptions');
|
|
303
|
+
if (!runtime.isNode) {
|
|
304
|
+
throw new Error('The streamItems() method can only be used in Node.js environment.');
|
|
305
|
+
}
|
|
306
|
+
const { data } = await this.httpClient.call({
|
|
307
|
+
url: this.buildUrl('items'),
|
|
308
|
+
method: 'GET',
|
|
309
|
+
params: this.buildParams({
|
|
310
|
+
format,
|
|
311
|
+
...query,
|
|
312
|
+
}),
|
|
313
|
+
responseType: 'stream',
|
|
314
|
+
timeoutSecs,
|
|
315
|
+
signal,
|
|
316
|
+
});
|
|
317
|
+
return cast(data);
|
|
318
|
+
}
|
|
272
319
|
/**
|
|
273
320
|
* Stores one or more items into the dataset.
|
|
274
321
|
*
|