apify-client 3.0.0-beta.64 → 3.0.0-beta.65
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.
|
@@ -133,6 +133,11 @@ export declare class DatasetClient<Data extends Record<string | number, any> = R
|
|
|
133
133
|
* dataset items, this method returns the items serialized to the provided format
|
|
134
134
|
* (JSON, CSV, Excel, etc.) as a Buffer. Useful for exporting data for further processing.
|
|
135
135
|
*
|
|
136
|
+
* In Node.js you can use the `stream` option to get a Readable stream, so a large export is processed in chunks
|
|
137
|
+
* without holding all of it in memory. The stream holds its connection open until you consume it to the end or
|
|
138
|
+
* destroy it. Iterating it with `for await` or passing it to `pipeline()` from `node:stream/promises` does either
|
|
139
|
+
* one for you.
|
|
140
|
+
*
|
|
136
141
|
* @param format - Output format: `'json'`, `'jsonl'`, `'csv'`, `'xlsx'`, `'xml'`, `'rss'`, or `'html'`
|
|
137
142
|
* @param options - Download and formatting options (extends all options from {@link listItems})
|
|
138
143
|
* @param options.attachment - If `true`, the response will have `Content-Disposition: attachment` header.
|
|
@@ -143,8 +148,12 @@ export declare class DatasetClient<Data extends Record<string | number, any> = R
|
|
|
143
148
|
* @param options.xmlRow - Name of the XML element for each item. Default is `'item'`.
|
|
144
149
|
* @param options.fields - Array of field names to include in the export.
|
|
145
150
|
* @param options.omit - Array of field names to exclude from the export.
|
|
146
|
-
* @param options.
|
|
147
|
-
* @
|
|
151
|
+
* @param options.stream - If `true`, the items are returned as a Readable stream. Node.js only.
|
|
152
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`. With `stream`, it bounds the
|
|
153
|
+
* wait for the API to start responding; once the stream is returned, it only limits inactivity: the stream errors
|
|
154
|
+
* if no data arrives for that long. To stop the download early or cap its total duration, destroy the stream or
|
|
155
|
+
* abort `signal`.
|
|
156
|
+
* @returns Buffer containing the serialized data in the specified format, or a Readable stream of it with `stream`
|
|
148
157
|
* @see https://docs.apify.com/api/v2/dataset-items-get
|
|
149
158
|
*
|
|
150
159
|
* @example
|
|
@@ -165,37 +174,21 @@ export declare class DatasetClient<Data extends Record<string | number, any> = R
|
|
|
165
174
|
* xmlRoot: 'products',
|
|
166
175
|
* xmlRow: 'product'
|
|
167
176
|
* });
|
|
168
|
-
* ```
|
|
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
177
|
*
|
|
188
|
-
*
|
|
189
|
-
* ```javascript
|
|
178
|
+
* // Stream a CSV export straight to a file
|
|
190
179
|
* const { createWriteStream } = require('node:fs');
|
|
191
180
|
* 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 });
|
|
181
|
+
* const stream = await client.dataset('my-dataset').downloadItems('csv', { stream: true });
|
|
195
182
|
* await pipeline(stream, createWriteStream('output.csv'));
|
|
196
183
|
* ```
|
|
197
184
|
*/
|
|
198
|
-
|
|
185
|
+
downloadItems(format: `${DownloadItemsFormat}`, options?: DatasetClientDownloadItemsOptions & {
|
|
186
|
+
stream?: false;
|
|
187
|
+
}): Promise<Buffer>;
|
|
188
|
+
downloadItems(format: `${DownloadItemsFormat}`, options: DatasetClientDownloadItemsOptions & {
|
|
189
|
+
stream: true;
|
|
190
|
+
}): Promise<Readable>;
|
|
191
|
+
downloadItems(format: `${DownloadItemsFormat}`, options?: DatasetClientDownloadItemsOptions): Promise<Buffer | Readable>;
|
|
199
192
|
/**
|
|
200
193
|
* Stores one or more items into the dataset.
|
|
201
194
|
*
|
|
@@ -347,4 +340,9 @@ export interface DatasetClientDownloadItemsOptions extends Omit<DatasetClientLis
|
|
|
347
340
|
skipHeaderRow?: boolean;
|
|
348
341
|
xmlRoot?: string;
|
|
349
342
|
xmlRow?: string;
|
|
343
|
+
/**
|
|
344
|
+
* If `true`, the items are returned as a Readable stream. Node.js only.
|
|
345
|
+
* @default false
|
|
346
|
+
*/
|
|
347
|
+
stream?: boolean;
|
|
350
348
|
}
|
|
@@ -56,6 +56,7 @@ const downloadItemsOptionsSchema = z.strictObject({
|
|
|
56
56
|
xmlRoot: z.string().optional(),
|
|
57
57
|
xmlRow: z.string().optional(),
|
|
58
58
|
signature: z.string().optional(),
|
|
59
|
+
stream: z.boolean().optional(),
|
|
59
60
|
...timeoutOptionsShape,
|
|
60
61
|
});
|
|
61
62
|
const pushItemsSchema = z.union([itemSchema, z.string(), z.array(itemSchema)]);
|
|
@@ -213,95 +214,11 @@ export class DatasetClient extends ResourceClient {
|
|
|
213
214
|
};
|
|
214
215
|
return this.listPaginatedFromCallback(fetchItems, listOptions);
|
|
215
216
|
}
|
|
216
|
-
/**
|
|
217
|
-
* Downloads dataset items in a specific format.
|
|
218
|
-
*
|
|
219
|
-
* Unlike {@link listItems} which returns a {@link PaginatedList} with an array of individual
|
|
220
|
-
* dataset items, this method returns the items serialized to the provided format
|
|
221
|
-
* (JSON, CSV, Excel, etc.) as a Buffer. Useful for exporting data for further processing.
|
|
222
|
-
*
|
|
223
|
-
* @param format - Output format: `'json'`, `'jsonl'`, `'csv'`, `'xlsx'`, `'xml'`, `'rss'`, or `'html'`
|
|
224
|
-
* @param options - Download and formatting options (extends all options from {@link listItems})
|
|
225
|
-
* @param options.attachment - If `true`, the response will have `Content-Disposition: attachment` header.
|
|
226
|
-
* @param options.bom - If `true`, adds UTF-8 BOM to the beginning of the file (useful for Excel compatibility).
|
|
227
|
-
* @param options.delimiter - CSV delimiter character. Default is `,` (comma).
|
|
228
|
-
* @param options.skipHeaderRow - If `true`, CSV export will not include the header row with field names.
|
|
229
|
-
* @param options.xmlRoot - Name of the root XML element. Default is `'items'`.
|
|
230
|
-
* @param options.xmlRow - Name of the XML element for each item. Default is `'item'`.
|
|
231
|
-
* @param options.fields - Array of field names to include in the export.
|
|
232
|
-
* @param options.omit - Array of field names to exclude from the export.
|
|
233
|
-
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
234
|
-
* @returns Buffer containing the serialized data in the specified format
|
|
235
|
-
* @see https://docs.apify.com/api/v2/dataset-items-get
|
|
236
|
-
*
|
|
237
|
-
* @example
|
|
238
|
-
* ```javascript
|
|
239
|
-
* // Download as CSV with BOM for Excel compatibility
|
|
240
|
-
* const csvBuffer = await client.dataset('my-dataset').downloadItems('csv', { bom: true });
|
|
241
|
-
* require('fs').writeFileSync('output.csv', csvBuffer);
|
|
242
|
-
*
|
|
243
|
-
* // Download as Excel with custom options
|
|
244
|
-
* const xlsxBuffer = await client.dataset('my-dataset').downloadItems('xlsx', {
|
|
245
|
-
* fields: ['url', 'title', 'price'],
|
|
246
|
-
* skipEmpty: true,
|
|
247
|
-
* limit: 1000
|
|
248
|
-
* });
|
|
249
|
-
*
|
|
250
|
-
* // Download as XML with custom element names
|
|
251
|
-
* const xmlBuffer = await client.dataset('my-dataset').downloadItems('xml', {
|
|
252
|
-
* xmlRoot: 'products',
|
|
253
|
-
* xmlRow: 'product'
|
|
254
|
-
* });
|
|
255
|
-
* ```
|
|
256
|
-
*/
|
|
257
217
|
async downloadItems(format, options = {}) {
|
|
258
218
|
parseArgument(format, itemFormatSchema);
|
|
259
|
-
const { timeoutSecs = 'long', signal, ...query } = parseArgument(options, downloadItemsOptionsSchema, 'DatasetClientDownloadItemsOptions');
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
method: 'GET',
|
|
263
|
-
params: this.buildParams({
|
|
264
|
-
format,
|
|
265
|
-
...query,
|
|
266
|
-
}),
|
|
267
|
-
responseType: 'buffer',
|
|
268
|
-
timeoutSecs,
|
|
269
|
-
signal,
|
|
270
|
-
});
|
|
271
|
-
return cast(data);
|
|
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.');
|
|
219
|
+
const { timeoutSecs = 'long', signal, stream, ...query } = parseArgument(options, downloadItemsOptionsSchema, 'DatasetClientDownloadItemsOptions');
|
|
220
|
+
if (stream && !runtime.isNode) {
|
|
221
|
+
throw new Error('The stream option can only be used in Node.js environment.');
|
|
305
222
|
}
|
|
306
223
|
const { data } = await this.httpClient.call({
|
|
307
224
|
url: this.buildUrl('items'),
|
|
@@ -310,7 +227,7 @@ export class DatasetClient extends ResourceClient {
|
|
|
310
227
|
format,
|
|
311
228
|
...query,
|
|
312
229
|
}),
|
|
313
|
-
responseType: 'stream',
|
|
230
|
+
responseType: stream ? 'stream' : 'buffer',
|
|
314
231
|
timeoutSecs,
|
|
315
232
|
signal,
|
|
316
233
|
});
|