apify-client 3.0.0-beta.1 → 3.0.0-beta.100

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.
Files changed (131) hide show
  1. package/README.md +21 -5
  2. package/dist/apify_api_error.d.ts +64 -22
  3. package/dist/apify_api_error.js +100 -30
  4. package/dist/apify_client.d.ts +150 -35
  5. package/dist/apify_client.js +198 -118
  6. package/dist/apify_client_error.d.ts +10 -0
  7. package/dist/apify_client_error.js +10 -0
  8. package/dist/base/api_client.d.ts +9 -9
  9. package/dist/base/api_client.js +41 -45
  10. package/dist/base/resource_client.d.ts +24 -10
  11. package/dist/base/resource_client.js +78 -48
  12. package/dist/base/resource_collection_client.d.ts +15 -6
  13. package/dist/base/resource_collection_client.js +40 -21
  14. package/dist/body_parser.d.ts +3 -4
  15. package/dist/body_parser.js +20 -32
  16. package/dist/bundle.js +47 -52
  17. package/dist/bundle.js.map +1 -1
  18. package/dist/generated/api.d.ts +3363 -1185
  19. package/dist/generated/api.js +1 -2
  20. package/dist/generated/schemas.d.ts +8146 -0
  21. package/dist/generated/schemas.js +1866 -0
  22. package/dist/http_clients/axios.d.ts +61 -0
  23. package/dist/http_clients/axios.js +119 -0
  24. package/dist/http_clients/base.d.ts +391 -0
  25. package/dist/http_clients/base.js +650 -0
  26. package/dist/http_clients/index.d.ts +4 -0
  27. package/dist/http_clients/index.js +2 -0
  28. package/dist/http_compressors/base.d.ts +40 -0
  29. package/dist/http_compressors/base.js +9 -0
  30. package/dist/http_compressors/brotli.d.ts +33 -0
  31. package/dist/http_compressors/brotli.js +39 -0
  32. package/dist/http_compressors/gzip.d.ts +33 -0
  33. package/dist/http_compressors/gzip.js +39 -0
  34. package/dist/http_compressors/index.d.ts +8 -0
  35. package/dist/http_compressors/index.js +3 -0
  36. package/dist/http_compressors/resolve.d.ts +24 -0
  37. package/dist/http_compressors/resolve.js +30 -0
  38. package/dist/http_compressors/zstd.d.ts +34 -0
  39. package/dist/http_compressors/zstd.js +43 -0
  40. package/dist/index.d.ts +38 -32
  41. package/dist/index.js +36 -36
  42. package/dist/invalid_response_body_error.d.ts +14 -0
  43. package/dist/invalid_response_body_error.js +18 -0
  44. package/dist/lazy_schema.d.ts +8 -0
  45. package/dist/lazy_schema.js +11 -0
  46. package/dist/models.d.ts +196 -278
  47. package/dist/models.js +14 -15
  48. package/dist/resource_clients/actor.d.ts +115 -45
  49. package/dist/resource_clients/actor.js +187 -116
  50. package/dist/resource_clients/actor_collection.d.ts +21 -17
  51. package/dist/resource_clients/actor_collection.js +26 -21
  52. package/dist/resource_clients/actor_env_var.d.ts +13 -6
  53. package/dist/resource_clients/actor_env_var.js +21 -14
  54. package/dist/resource_clients/actor_env_var_collection.d.ts +18 -22
  55. package/dist/resource_clients/actor_env_var_collection.js +23 -20
  56. package/dist/resource_clients/actor_server.d.ts +149 -0
  57. package/dist/resource_clients/actor_server.js +199 -0
  58. package/dist/resource_clients/actor_version.d.ts +30 -10
  59. package/dist/resource_clients/actor_version.js +29 -23
  60. package/dist/resource_clients/actor_version_collection.d.ts +20 -21
  61. package/dist/resource_clients/actor_version_collection.js +25 -20
  62. package/dist/resource_clients/build.d.ts +24 -11
  63. package/dist/resource_clients/build.js +43 -26
  64. package/dist/resource_clients/build_collection.d.ts +10 -8
  65. package/dist/resource_clients/build_collection.js +13 -13
  66. package/dist/resource_clients/dataset.d.ts +87 -40
  67. package/dist/resource_clients/dataset.js +159 -175
  68. package/dist/resource_clients/dataset_collection.d.ts +13 -9
  69. package/dist/resource_clients/dataset_collection.js +29 -21
  70. package/dist/resource_clients/key_value_store.d.ts +110 -28
  71. package/dist/resource_clients/key_value_store.js +177 -99
  72. package/dist/resource_clients/key_value_store_collection.d.ts +13 -9
  73. package/dist/resource_clients/key_value_store_collection.js +29 -21
  74. package/dist/resource_clients/log.d.ts +42 -23
  75. package/dist/resource_clients/log.js +156 -85
  76. package/dist/resource_clients/request_queue.d.ts +77 -38
  77. package/dist/resource_clients/request_queue.js +251 -172
  78. package/dist/resource_clients/request_queue_collection.d.ts +12 -8
  79. package/dist/resource_clients/request_queue_collection.js +23 -20
  80. package/dist/resource_clients/run.d.ts +134 -34
  81. package/dist/resource_clients/run.js +225 -84
  82. package/dist/resource_clients/run_collection.d.ts +8 -6
  83. package/dist/resource_clients/run_collection.js +18 -18
  84. package/dist/resource_clients/schedule.d.ts +20 -11
  85. package/dist/resource_clients/schedule.js +36 -30
  86. package/dist/resource_clients/schedule_collection.d.ts +10 -6
  87. package/dist/resource_clients/schedule_collection.js +20 -17
  88. package/dist/resource_clients/store_collection.d.ts +9 -7
  89. package/dist/resource_clients/store_collection.js +18 -18
  90. package/dist/resource_clients/task.d.ts +44 -21
  91. package/dist/resource_clients/task.js +116 -86
  92. package/dist/resource_clients/task_collection.d.ts +13 -9
  93. package/dist/resource_clients/task_collection.js +19 -16
  94. package/dist/resource_clients/user.d.ts +22 -13
  95. package/dist/resource_clients/user.js +46 -47
  96. package/dist/resource_clients/webhook.d.ts +21 -12
  97. package/dist/resource_clients/webhook.js +35 -30
  98. package/dist/resource_clients/webhook_collection.d.ts +11 -7
  99. package/dist/resource_clients/webhook_collection.js +20 -17
  100. package/dist/resource_clients/webhook_dispatch.d.ts +9 -6
  101. package/dist/resource_clients/webhook_dispatch.js +11 -10
  102. package/dist/resource_clients/webhook_dispatch_collection.d.ts +7 -5
  103. package/dist/resource_clients/webhook_dispatch_collection.js +13 -13
  104. package/dist/response_validation_error.d.ts +27 -0
  105. package/dist/response_validation_error.js +38 -0
  106. package/dist/runtime/node.d.ts +6 -0
  107. package/dist/runtime/node.js +87 -0
  108. package/dist/runtime/types.d.ts +60 -0
  109. package/dist/runtime/types.js +1 -0
  110. package/dist/runtime/web.d.ts +5 -0
  111. package/dist/runtime/web.js +21 -0
  112. package/dist/schemas.d.ts +381 -0
  113. package/dist/schemas.js +37 -0
  114. package/dist/statistics.d.ts +1 -6
  115. package/dist/statistics.js +7 -11
  116. package/dist/timeouts.d.ts +84 -0
  117. package/dist/timeouts.js +37 -0
  118. package/dist/timezones.d.ts +1 -1
  119. package/dist/timezones.js +1 -2
  120. package/dist/utils.d.ts +99 -70
  121. package/dist/utils.js +251 -229
  122. package/dist/wait_for_resources.d.ts +8 -0
  123. package/dist/wait_for_resources.js +39 -0
  124. package/package.json +48 -47
  125. package/dist/argument_validation_error.d.ts +0 -17
  126. package/dist/argument_validation_error.js +0 -157
  127. package/dist/http_client.d.ts +0 -88
  128. package/dist/http_client.js +0 -260
  129. package/dist/index.mjs +0 -42
  130. package/dist/interceptors.d.ts +0 -19
  131. package/dist/interceptors.js +0 -110
@@ -1,63 +1,83 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DownloadItemsFormat = exports.DatasetClient = void 0;
4
- const zod_1 = require("zod");
5
- const utilities_1 = require("@apify/utilities");
6
- const resource_client_1 = require("../base/resource_client");
7
- const utils_1 = require("../utils");
1
+ import { z } from 'zod';
2
+ import { createStorageContentSignatureAsync } from '@apify/utilities';
3
+ import { ResourceClient } from '../base/resource_client.js';
4
+ import * as schemas from '../schemas.js';
5
+ import { timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
6
+ import { runtime } from '#runtime';
7
+ import { anyObjectSchema, applyQueryParamsToUrl, cast, isNonArrayObject, paginationOptionsShape, parseArgument, parseResponse, } from '../utils.js';
8
+ /**
9
+ * Supported formats for downloading dataset items.
10
+ */
11
+ export var DownloadItemsFormat;
12
+ (function (DownloadItemsFormat) {
13
+ DownloadItemsFormat["JSON"] = "json";
14
+ DownloadItemsFormat["JSONL"] = "jsonl";
15
+ DownloadItemsFormat["XML"] = "xml";
16
+ DownloadItemsFormat["HTML"] = "html";
17
+ DownloadItemsFormat["CSV"] = "csv";
18
+ DownloadItemsFormat["XLSX"] = "xlsx";
19
+ DownloadItemsFormat["RSS"] = "rss";
20
+ })(DownloadItemsFormat || (DownloadItemsFormat = {}));
21
+ const itemFormatSchema = z.enum(DownloadItemsFormat);
8
22
  // A predicate, not a `z.object()` arm, which would walk and copy every key of every pushed item.
9
- const itemSchema = zod_1.z.custom(utils_1.isNonArrayObject, 'Expected an object');
10
- const listItemsOptionsSchema = zod_1.z.strictObject({
11
- clean: zod_1.z.boolean().optional(),
12
- desc: zod_1.z.boolean().optional(),
13
- flatten: zod_1.z.array(zod_1.z.string()).optional(),
14
- fields: zod_1.z.array(zod_1.z.string()).optional(),
15
- omit: zod_1.z.array(zod_1.z.string()).optional(),
16
- ...utils_1.paginationOptionsShape,
17
- skipEmpty: zod_1.z.boolean().optional(),
18
- skipHidden: zod_1.z.boolean().optional(),
19
- unwind: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]).optional(),
20
- view: zod_1.z.string().optional(),
21
- signature: zod_1.z.string().optional(),
23
+ const itemSchema = z.custom(isNonArrayObject, 'Expected an object');
24
+ const listItemsOptionsSchema = z.strictObject({
25
+ clean: z.boolean().optional(),
26
+ desc: z.boolean().optional(),
27
+ flatten: z.array(z.string()).optional(),
28
+ fields: z.array(z.string()).optional(),
29
+ omit: z.array(z.string()).optional(),
30
+ ...paginationOptionsShape,
31
+ skipEmpty: z.boolean().optional(),
32
+ skipHidden: z.boolean().optional(),
33
+ unwind: z.array(z.string()).optional(),
34
+ view: z.string().optional(),
35
+ signature: z.string().optional(),
36
+ ...timeoutOptionsShape,
22
37
  });
23
38
  // Spells out `limit` / `offset` instead of spreading `paginationOptionsShape`: a download is one
24
39
  // request, so `chunkSize` has nothing to size. The options type omits it to match.
25
- const downloadItemsOptionsSchema = zod_1.z.strictObject({
26
- attachment: zod_1.z.boolean().optional(),
27
- bom: zod_1.z.boolean().optional(),
28
- clean: zod_1.z.boolean().optional(),
29
- delimiter: zod_1.z.string().optional(),
30
- desc: zod_1.z.boolean().optional(),
31
- flatten: zod_1.z.array(zod_1.z.string()).optional(),
32
- fields: zod_1.z.array(zod_1.z.string()).optional(),
33
- omit: zod_1.z.array(zod_1.z.string()).optional(),
34
- limit: zod_1.z.number().min(0).optional(),
35
- offset: zod_1.z.number().min(0).optional(),
36
- skipEmpty: zod_1.z.boolean().optional(),
37
- skipHeaderRow: zod_1.z.boolean().optional(),
38
- skipHidden: zod_1.z.boolean().optional(),
39
- unwind: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]).optional(),
40
- view: zod_1.z.string().optional(),
41
- xmlRoot: zod_1.z.string().optional(),
42
- xmlRow: zod_1.z.string().optional(),
43
- signature: zod_1.z.string().optional(),
40
+ const downloadItemsOptionsSchema = z.strictObject({
41
+ attachment: z.boolean().optional(),
42
+ bom: z.boolean().optional(),
43
+ clean: z.boolean().optional(),
44
+ delimiter: z.string().optional(),
45
+ desc: z.boolean().optional(),
46
+ flatten: z.array(z.string()).optional(),
47
+ fields: z.array(z.string()).optional(),
48
+ omit: z.array(z.string()).optional(),
49
+ limit: z.number().min(0).optional(),
50
+ offset: z.number().min(0).optional(),
51
+ skipEmpty: z.boolean().optional(),
52
+ skipHeaderRow: z.boolean().optional(),
53
+ skipHidden: z.boolean().optional(),
54
+ unwind: z.array(z.string()).optional(),
55
+ view: z.string().optional(),
56
+ xmlRoot: z.string().optional(),
57
+ xmlRow: z.string().optional(),
58
+ signature: z.string().optional(),
59
+ stream: z.boolean().optional(),
60
+ ...timeoutOptionsShape,
44
61
  });
45
- const pushItemsSchema = zod_1.z.union([itemSchema, zod_1.z.string(), zod_1.z.array(zod_1.z.union([itemSchema, zod_1.z.string()]))]);
46
- // Every option becomes a query parameter of the generated URL, so `chunkSize` (client-side only) and
47
- // `signature` (which this method produces) are left out. The options type omits both to match.
48
- const createItemsPublicUrlOptionsSchema = zod_1.z.strictObject({
49
- clean: zod_1.z.boolean().optional(),
50
- desc: zod_1.z.boolean().optional(),
51
- flatten: zod_1.z.array(zod_1.z.string()).optional(),
52
- fields: zod_1.z.array(zod_1.z.string()).optional(),
53
- omit: zod_1.z.array(zod_1.z.string()).optional(),
54
- limit: zod_1.z.number().min(0).optional(),
55
- offset: zod_1.z.number().min(0).optional(),
56
- skipEmpty: zod_1.z.boolean().optional(),
57
- skipHidden: zod_1.z.boolean().optional(),
58
- unwind: zod_1.z.union([zod_1.z.string(), zod_1.z.array(zod_1.z.string())]).optional(),
59
- view: zod_1.z.string().optional(),
60
- expiresInSecs: zod_1.z.number().optional(),
62
+ const pushItemsSchema = z.union([itemSchema, z.string(), z.array(itemSchema)]);
63
+ // Apart from `timeoutSecs`, `signal` and `expiresInSecs`, every option becomes a query parameter of the generated
64
+ // URL, so `chunkSize` (client-side only) and `signature` (which this method produces) are left out. The options type
65
+ // omits both to match.
66
+ const createItemsPublicUrlOptionsSchema = z.strictObject({
67
+ clean: z.boolean().optional(),
68
+ desc: z.boolean().optional(),
69
+ flatten: z.array(z.string()).optional(),
70
+ fields: z.array(z.string()).optional(),
71
+ format: itemFormatSchema.optional(),
72
+ omit: z.array(z.string()).optional(),
73
+ limit: z.number().min(0).optional(),
74
+ offset: z.number().min(0).optional(),
75
+ skipEmpty: z.boolean().optional(),
76
+ skipHidden: z.boolean().optional(),
77
+ unwind: z.array(z.string()).optional(),
78
+ view: z.string().optional(),
79
+ expiresInSecs: z.number().positive().optional(),
80
+ ...timeoutOptionsShape,
61
81
  });
62
82
  /**
63
83
  * Client for managing a specific Dataset.
@@ -88,7 +108,7 @@ const createItemsPublicUrlOptionsSchema = zod_1.z.strictObject({
88
108
  *
89
109
  * @see https://docs.apify.com/platform/storage/dataset
90
110
  */
91
- class DatasetClient extends resource_client_1.ResourceClient {
111
+ export class DatasetClient extends ResourceClient {
92
112
  /**
93
113
  * @hidden
94
114
  */
@@ -101,30 +121,39 @@ class DatasetClient extends resource_client_1.ResourceClient {
101
121
  /**
102
122
  * Gets the dataset object from the Apify API.
103
123
  *
124
+ * @param options - Request options
125
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
104
126
  * @returns The Dataset object, or `undefined` if it does not exist
105
127
  * @see https://docs.apify.com/api/v2/dataset-get
106
128
  */
107
- async get() {
108
- return this._get({}, resource_client_1.SMALL_TIMEOUT_MILLIS);
129
+ async get(options = {}) {
130
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
131
+ return this.getResource(schemas.DatasetResource(), {}, timeoutSecs, signal);
109
132
  }
110
133
  /**
111
134
  * Updates the dataset with specified fields.
112
135
  *
113
136
  * @param newFields - Fields to update in the dataset
137
+ * @param options - Request options
138
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
114
139
  * @returns The updated Dataset object
115
140
  * @see https://docs.apify.com/api/v2/dataset-put
116
141
  */
117
- async update(newFields) {
118
- (0, utils_1.parseArgument)(newFields, utils_1.anyObjectSchema);
119
- return this._update(newFields, resource_client_1.SMALL_TIMEOUT_MILLIS);
142
+ async update(newFields, options = {}) {
143
+ parseArgument(newFields, anyObjectSchema);
144
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
145
+ return this.updateResource(schemas.DatasetResource(), newFields, timeoutSecs, signal);
120
146
  }
121
147
  /**
122
148
  * Deletes the dataset.
123
149
  *
150
+ * @param options - Request options
151
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
124
152
  * @see https://docs.apify.com/api/v2/dataset-delete
125
153
  */
126
- async delete() {
127
- return this._delete(resource_client_1.SMALL_TIMEOUT_MILLIS);
154
+ async delete(options = {}) {
155
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
156
+ return this.deleteResource(timeoutSecs, signal);
128
157
  }
129
158
  /**
130
159
  * Lists items in the dataset.
@@ -136,6 +165,7 @@ class DatasetClient extends resource_client_1.ResourceClient {
136
165
  * @param options - Options for listing items
137
166
  * @param options.limit - Maximum number of items to return. Default is all items.
138
167
  * @param options.chunkSize - Maximum number of items returned in one API response. Relevant in the context of asyncIterator.
168
+ * Without it, iteration loads the whole dataset, or all `limit` items, in one response.
139
169
  * @param options.offset - Number of items to skip from the beginning. Default is 0.
140
170
  * @param options.desc - If `true`, items are returned in descending order (newest first). Default is `false`.
141
171
  * @param options.fields - Array of field names to include in the results. Omits all other fields.
@@ -144,8 +174,9 @@ class DatasetClient extends resource_client_1.ResourceClient {
144
174
  * @param options.skipEmpty - If `true`, skips empty items. Default is `false`.
145
175
  * @param options.skipHidden - If `true`, skips hidden fields (fields starting with `#`). Default is `false`.
146
176
  * @param options.flatten - Array of field names to flatten. Nested objects are converted to dot notation (e.g., `obj.field`).
147
- * @param options.unwind - Field name or array of field names to unwind. Each array value creates a separate item.
177
+ * @param options.unwind - Array of field names to unwind. Each array value creates a separate item.
148
178
  * @param options.view - Name of a predefined view to use for field selection.
179
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'long'`.
149
180
  * @returns A paginated list with `items`, `total` count, `offset`, `count`, and `limit`
150
181
  * @see https://docs.apify.com/api/v2/dataset-items-get
151
182
  *
@@ -171,83 +202,51 @@ class DatasetClient extends resource_client_1.ResourceClient {
171
202
  * ```
172
203
  */
173
204
  listItems(options = {}) {
174
- const parsed = (0, utils_1.parseArgument)(options, listItemsOptionsSchema, 'DatasetClientListItemOptions');
205
+ const { timeoutSecs = 'long', signal, ...listOptions } = parseArgument(options, listItemsOptionsSchema, 'DatasetClientListItemOptions');
175
206
  const fetchItems = async (datasetListOptions = {}) => {
176
207
  const response = await this.httpClient.call({
177
- url: this._url('items'),
208
+ url: this.buildUrl('items'),
178
209
  method: 'GET',
179
- params: this._params(datasetListOptions),
180
- timeout: resource_client_1.DEFAULT_TIMEOUT_MILLIS,
210
+ params: this.buildParams(datasetListOptions),
211
+ timeoutSecs,
212
+ signal,
181
213
  });
182
- return this._createPaginationList(response, datasetListOptions.desc ?? false);
214
+ return this.#createPaginationList(response, datasetListOptions.desc ?? false);
183
215
  };
184
- return this._listPaginatedFromCallback(fetchItems, parsed);
216
+ return this.listPaginatedFromCallback(fetchItems, listOptions);
185
217
  }
186
- /**
187
- * Downloads dataset items in a specific format.
188
- *
189
- * Unlike {@link listItems} which returns a {@link PaginatedList} with an array of individual
190
- * dataset items, this method returns the items serialized to the provided format
191
- * (JSON, CSV, Excel, etc.) as a Buffer. Useful for exporting data for further processing.
192
- *
193
- * @param format - Output format: `'json'`, `'jsonl'`, `'csv'`, `'xlsx'`, `'xml'`, `'rss'`, or `'html'`
194
- * @param options - Download and formatting options (extends all options from {@link listItems})
195
- * @param options.attachment - If `true`, the response will have `Content-Disposition: attachment` header.
196
- * @param options.bom - If `true`, adds UTF-8 BOM to the beginning of the file (useful for Excel compatibility).
197
- * @param options.delimiter - CSV delimiter character. Default is `,` (comma).
198
- * @param options.skipHeaderRow - If `true`, CSV export will not include the header row with field names.
199
- * @param options.xmlRoot - Name of the root XML element. Default is `'items'`.
200
- * @param options.xmlRow - Name of the XML element for each item. Default is `'item'`.
201
- * @param options.fields - Array of field names to include in the export.
202
- * @param options.omit - Array of field names to exclude from the export.
203
- * @returns Buffer containing the serialized data in the specified format
204
- * @see https://docs.apify.com/api/v2/dataset-items-get
205
- *
206
- * @example
207
- * ```javascript
208
- * // Download as CSV with BOM for Excel compatibility
209
- * const csvBuffer = await client.dataset('my-dataset').downloadItems('csv', { bom: true });
210
- * require('fs').writeFileSync('output.csv', csvBuffer);
211
- *
212
- * // Download as Excel with custom options
213
- * const xlsxBuffer = await client.dataset('my-dataset').downloadItems('xlsx', {
214
- * fields: ['url', 'title', 'price'],
215
- * skipEmpty: true,
216
- * limit: 1000
217
- * });
218
- *
219
- * // Download as XML with custom element names
220
- * const xmlBuffer = await client.dataset('my-dataset').downloadItems('xml', {
221
- * xmlRoot: 'products',
222
- * xmlRow: 'product'
223
- * });
224
- * ```
225
- */
226
218
  async downloadItems(format, options = {}) {
227
- (0, utils_1.parseArgument)(format, itemFormatSchema);
228
- const parsed = (0, utils_1.parseArgument)(options, downloadItemsOptionsSchema, 'DatasetClientDownloadItemsOptions');
219
+ parseArgument(format, itemFormatSchema);
220
+ const { timeoutSecs = 'long', signal, stream, ...query } = parseArgument(options, downloadItemsOptionsSchema, 'DatasetClientDownloadItemsOptions');
221
+ if (stream && !runtime.isNode) {
222
+ throw new Error('The stream option can only be used in Node.js environment.');
223
+ }
229
224
  const { data } = await this.httpClient.call({
230
- url: this._url('items'),
225
+ url: this.buildUrl('items'),
231
226
  method: 'GET',
232
- params: this._params({
227
+ params: this.buildParams({
233
228
  format,
234
- ...parsed,
229
+ ...query,
235
230
  }),
236
- forceBuffer: true,
237
- timeout: resource_client_1.DEFAULT_TIMEOUT_MILLIS,
231
+ responseType: stream ? 'stream' : 'buffer',
232
+ timeoutSecs,
233
+ signal,
238
234
  });
239
- return (0, utils_1.cast)(data);
235
+ return cast(data);
240
236
  }
241
237
  /**
242
238
  * Stores one or more items into the dataset.
243
239
  *
244
- * Items can be objects, strings, or arrays thereof. Each item will be stored as a separate
245
- * record in the dataset. Objects are automatically serialized to JSON. If you provide an array,
246
- * all items will be stored in order. This method is idempotent - calling it multiple times
247
- * with the same data will not create duplicates, but will append items each time.
240
+ * Each item will be stored as a separate record in the dataset. Objects are automatically
241
+ * serialized to JSON. If you provide an array, all items will be stored in order. This method
242
+ * is idempotent - calling it multiple times with the same data will not create duplicates, but
243
+ * will append items each time.
248
244
  *
249
- * @param items - A single item (object or string) or an array of items to store.
250
- * Objects are automatically stringified to JSON. Strings are stored as-is.
245
+ * @param items - A single item, an array of items, or a string that is the JSON serialization of
246
+ * either - the API only accepts an object or an array of objects, so a plain string
247
+ * is not a valid item on its own.
248
+ * @param options - Request options
249
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
251
250
  * @see https://docs.apify.com/api/v2/dataset-items-post
252
251
  *
253
252
  * @example
@@ -265,23 +264,22 @@ class DatasetClient extends resource_client_1.ResourceClient {
265
264
  * { url: 'https://test.com', title: 'Test' },
266
265
  * { url: 'https://demo.com', title: 'Demo' }
267
266
  * ]);
268
- *
269
- * // Store string items
270
- * await client.dataset('my-dataset').pushItems(['item1', 'item2', 'item3']);
271
267
  * ```
272
268
  */
273
- async pushItems(items) {
274
- (0, utils_1.parseArgument)(items, pushItemsSchema);
269
+ async pushItems(items, options = {}) {
270
+ parseArgument(items, pushItemsSchema);
271
+ const { timeoutSecs = 'medium', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
275
272
  await this.httpClient.call({
276
- url: this._url('items'),
273
+ url: this.buildUrl('items'),
277
274
  method: 'POST',
278
275
  headers: {
279
276
  'content-type': 'application/json; charset=utf-8',
280
277
  },
281
278
  data: items,
282
- params: this._params(),
283
- doNotRetryTimeouts: true, // see timeout handling in http-client
284
- timeout: resource_client_1.MEDIUM_TIMEOUT_MILLIS,
279
+ params: this.buildParams(),
280
+ doNotRetryTimeouts: true,
281
+ timeoutSecs,
282
+ signal,
285
283
  });
286
284
  }
287
285
  /**
@@ -290,25 +288,22 @@ class DatasetClient extends resource_client_1.ResourceClient {
290
288
  * Returns statistics for each field in the dataset, including information about
291
289
  * data types, null counts, and value ranges.
292
290
  *
293
- * @returns Dataset statistics, or `undefined` if not available
291
+ * @param options - Request options
292
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
293
+ * @returns Dataset statistics
294
294
  * @see https://docs.apify.com/api/v2/dataset-statistics-get
295
295
  * @since Added in 2.11.2
296
296
  */
297
- async getStatistics() {
298
- const requestOpts = {
299
- url: this._url('statistics'),
297
+ async getStatistics(options = {}) {
298
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
299
+ const response = await this.httpClient.call({
300
+ url: this.buildUrl('statistics'),
300
301
  method: 'GET',
301
- params: this._params(),
302
- timeout: resource_client_1.SMALL_TIMEOUT_MILLIS,
303
- };
304
- try {
305
- const response = await this.httpClient.call(requestOpts);
306
- return (0, utils_1.cast)((0, utils_1.pluckData)(response.data));
307
- }
308
- catch (err) {
309
- (0, utils_1.catchNotFoundOrThrow)(err);
310
- }
311
- return undefined;
302
+ params: this.buildParams(),
303
+ timeoutSecs,
304
+ signal,
305
+ });
306
+ return parseResponse(response, schemas.DatasetStatistics());
312
307
  }
313
308
  /**
314
309
  * Generates a public URL for accessing dataset items.
@@ -318,10 +313,13 @@ class DatasetClient extends resource_client_1.ResourceClient {
318
313
  * This is useful for sharing dataset results with external services or users.
319
314
  *
320
315
  * @param options - URL generation options (extends all options from {@link listItems})
321
- * @param options.expiresInSecs - Number of seconds until the signed URL expires. If omitted, the URL never expires.
316
+ * @param options.expiresInSecs - Number of seconds until the signed URL expires. Must be positive. If omitted, the URL
317
+ * never expires.
322
318
  * @param options.fields - Array of field names to include in the response.
319
+ * @param options.format - Format of the items served by the URL. Defaults to `json`.
323
320
  * @param options.limit - Maximum number of items to return.
324
321
  * @param options.offset - Number of items to skip.
322
+ * @param options.timeoutSecs - Timeout for the API request that fetches the dataset. Default is `'long'`.
325
323
  * @returns A public URL string for accessing the dataset items
326
324
  *
327
325
  * @example
@@ -343,47 +341,33 @@ class DatasetClient extends resource_client_1.ResourceClient {
343
341
  * @since Added in 2.13.0
344
342
  */
345
343
  async createItemsPublicUrl(options = {}) {
346
- const parsed = (0, utils_1.parseArgument)(options, createItemsPublicUrlOptionsSchema, 'DatasetClientCreateItemsUrlOptions');
347
- const dataset = await this.get();
348
- const { expiresInSecs, ...queryOptions } = parsed;
349
- let createdItemsPublicUrl = new URL(this._publicUrl('items'));
344
+ const { timeoutSecs = 'long', signal, expiresInSecs, ...queryOptions } = parseArgument(options, createItemsPublicUrlOptionsSchema, 'DatasetClientCreateItemsUrlOptions');
345
+ const dataset = await this.get({ timeoutSecs, signal });
346
+ let createdItemsPublicUrl = new URL(this.buildPublicUrl('items'));
350
347
  if (dataset?.urlSigningSecretKey) {
351
- const signature = await (0, utilities_1.createStorageContentSignatureAsync)({
348
+ const signature = await createStorageContentSignatureAsync({
352
349
  resourceId: dataset.id,
353
350
  urlSigningSecretKey: dataset.urlSigningSecretKey,
354
351
  expiresInMillis: expiresInSecs ? expiresInSecs * 1000 : undefined,
355
352
  });
356
353
  createdItemsPublicUrl.searchParams.set('signature', signature);
357
354
  }
358
- createdItemsPublicUrl = (0, utils_1.applyQueryParamsToUrl)(createdItemsPublicUrl, queryOptions);
355
+ createdItemsPublicUrl = applyQueryParamsToUrl(createdItemsPublicUrl, queryOptions);
359
356
  return createdItemsPublicUrl.toString();
360
357
  }
361
- _createPaginationList(response, userProvidedDesc) {
358
+ #createPaginationList(response, userProvidedDesc) {
359
+ const descHeader = response.headers['x-apify-pagination-desc'];
362
360
  return {
363
361
  items: response.data,
364
362
  total: Number(response.headers['x-apify-pagination-total']),
365
363
  offset: Number(response.headers['x-apify-pagination-offset']),
366
- count: response.data.length, // because x-apify-pagination-count returns invalid values when hidden/empty items are skipped
364
+ // `x-apify-pagination-count` reports the rows scanned, which filters and `unwind` can leave above or below
365
+ // the items returned. It is derived from the dataset's item count, which lags a fresh push, so it can also
366
+ // fall below the items returned on an unfiltered page.
367
+ count: Math.max(Number(response.headers['x-apify-pagination-count'] ?? 0), response.data.length),
367
368
  limit: Number(response.headers['x-apify-pagination-limit']), // API returns 999999999999 when no limit is used
368
369
  // TODO: Replace this once https://github.com/apify/apify-core/issues/3503 is solved
369
- desc: JSON.parse(response.headers['x-apify-pagination-desc'] ?? userProvidedDesc),
370
+ desc: typeof descHeader === 'string' ? JSON.parse(descHeader) : userProvidedDesc,
370
371
  };
371
372
  }
372
373
  }
373
- exports.DatasetClient = DatasetClient;
374
- /**
375
- * Supported formats for downloading dataset items.
376
- */
377
- var DownloadItemsFormat;
378
- (function (DownloadItemsFormat) {
379
- DownloadItemsFormat["JSON"] = "json";
380
- DownloadItemsFormat["JSONL"] = "jsonl";
381
- DownloadItemsFormat["XML"] = "xml";
382
- DownloadItemsFormat["HTML"] = "html";
383
- DownloadItemsFormat["CSV"] = "csv";
384
- DownloadItemsFormat["XLSX"] = "xlsx";
385
- DownloadItemsFormat["RSS"] = "rss";
386
- })(DownloadItemsFormat || (exports.DownloadItemsFormat = DownloadItemsFormat = {}));
387
- const validItemFormats = [...new Set(Object.values(DownloadItemsFormat).map((item) => item.toLowerCase()))];
388
- // Declared below `validItemFormats` because it references it at module load time.
389
- const itemFormatSchema = zod_1.z.enum(validItemFormats);
@@ -1,8 +1,9 @@
1
1
  import { STORAGE_OWNERSHIP_FILTER } from '@apify/consts';
2
- import type { ApiClientSubResourceOptions } from '../base/api_client';
3
- import { ResourceCollectionClient } from '../base/resource_collection_client';
4
- import type { PaginatedList, PaginationOptions } from '../utils';
5
- import type { Dataset } from './dataset';
2
+ import type { ApiClientSubResourceOptions } from '../base/api_client.js';
3
+ import { ResourceCollectionClient } from '../base/resource_collection_client.js';
4
+ import type { TimeoutOptions } from '../timeouts.js';
5
+ import type { PaginatedList, PaginationOptions } from '../utils.js';
6
+ import type { DatasetResource } from './dataset.js';
6
7
  /**
7
8
  * Client for managing the collection of datasets in your account.
8
9
  *
@@ -45,21 +46,24 @@ export declare class DatasetCollectionClient extends ResourceCollectionClient {
45
46
  * ```
46
47
  *
47
48
  * @param options - Pagination options.
49
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
48
50
  * @returns A paginated iterator of Datasets.
49
51
  * @see https://docs.apify.com/api/v2/datasets-get
50
52
  */
51
- list(options?: DatasetCollectionClientListOptions): Promise<DatasetCollectionClientListResult> & AsyncIterable<Dataset>;
53
+ list(options?: DatasetCollectionClientListOptions): Promise<DatasetCollectionClientListResult> & AsyncIterable<DatasetResource>;
52
54
  /**
53
55
  * Gets or creates a dataset with the specified name.
54
56
  *
55
57
  * @param name - Name of the dataset. If not provided, a default dataset is used.
56
58
  * @param options - Additional options like schema.
59
+ * @param options.schema - Schema of the dataset.
60
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
57
61
  * @returns The dataset object.
58
62
  * @see https://docs.apify.com/api/v2/datasets-post
59
63
  */
60
- getOrCreate(name?: string, options?: DatasetCollectionClientGetOrCreateOptions): Promise<Dataset>;
64
+ getOrCreate(name?: string, options?: DatasetCollectionClientGetOrCreateOptions): Promise<DatasetResource>;
61
65
  }
62
- export interface DatasetCollectionClientListOptions extends PaginationOptions {
66
+ export interface DatasetCollectionClientListOptions extends PaginationOptions, TimeoutOptions {
63
67
  unnamed?: boolean;
64
68
  desc?: boolean;
65
69
  /**
@@ -71,9 +75,9 @@ export interface DatasetCollectionClientListOptions extends PaginationOptions {
71
75
  /**
72
76
  * @since Added in 2.3.0
73
77
  */
74
- export interface DatasetCollectionClientGetOrCreateOptions {
78
+ export interface DatasetCollectionClientGetOrCreateOptions extends TimeoutOptions {
75
79
  schema?: Record<string, unknown>;
76
80
  }
77
- export type DatasetCollectionClientListResult = PaginatedList<Dataset> & {
81
+ export type DatasetCollectionClientListResult = PaginatedList<DatasetResource> & {
78
82
  unnamed: boolean;
79
83
  };
@@ -1,18 +1,18 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DatasetCollectionClient = void 0;
4
- const zod_1 = require("zod");
5
- const consts_1 = require("@apify/consts");
6
- const resource_collection_client_1 = require("../base/resource_collection_client");
7
- const utils_1 = require("../utils");
8
- const listOptionsSchema = zod_1.z.strictObject({
9
- unnamed: zod_1.z.boolean().optional(),
10
- ...utils_1.paginationOptionsShape,
11
- desc: zod_1.z.boolean().optional(),
12
- ownership: zod_1.z.enum(consts_1.STORAGE_OWNERSHIP_FILTER).optional(),
1
+ import { z } from 'zod';
2
+ import { STORAGE_OWNERSHIP_FILTER } from '@apify/consts';
3
+ import { ResourceCollectionClient } from '../base/resource_collection_client.js';
4
+ import * as schemas from '../schemas.js';
5
+ import { optionalSignalSchema, optionalTimeoutSchema, timeoutOptionsShape } from '../timeouts.js';
6
+ import { anyObjectSchema, paginationOptionsShape, parseArgument } from '../utils.js';
7
+ const listOptionsSchema = z.strictObject({
8
+ unnamed: z.boolean().optional(),
9
+ ...paginationOptionsShape,
10
+ desc: z.boolean().optional(),
11
+ ownership: z.enum(STORAGE_OWNERSHIP_FILTER).optional(),
12
+ ...timeoutOptionsShape,
13
13
  });
14
- const nameSchema = zod_1.z.string().optional();
15
- const schemaSchema = utils_1.anyObjectSchema.optional();
14
+ const nameSchema = z.string().optional();
15
+ const schemaSchema = anyObjectSchema.optional();
16
16
  /**
17
17
  * Client for managing the collection of datasets in your account.
18
18
  *
@@ -33,7 +33,7 @@ const schemaSchema = utils_1.anyObjectSchema.optional();
33
33
  *
34
34
  * @see https://docs.apify.com/platform/storage/dataset
35
35
  */
36
- class DatasetCollectionClient extends resource_collection_client_1.ResourceCollectionClient {
36
+ export class DatasetCollectionClient extends ResourceCollectionClient {
37
37
  /**
38
38
  * @hidden
39
39
  */
@@ -60,25 +60,33 @@ class DatasetCollectionClient extends resource_collection_client_1.ResourceColle
60
60
  * ```
61
61
  *
62
62
  * @param options - Pagination options.
63
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
63
64
  * @returns A paginated iterator of Datasets.
64
65
  * @see https://docs.apify.com/api/v2/datasets-get
65
66
  */
66
67
  list(options = {}) {
67
- const parsed = (0, utils_1.parseArgument)(options, listOptionsSchema, 'DatasetCollectionClientListOptions');
68
- return this._listPaginated(parsed);
68
+ const parsed = parseArgument(options, listOptionsSchema, 'DatasetCollectionClientListOptions');
69
+ return this.listResourcesPaginated(schemas.ListOfDatasets(), parsed, 'medium');
69
70
  }
70
71
  /**
71
72
  * Gets or creates a dataset with the specified name.
72
73
  *
73
74
  * @param name - Name of the dataset. If not provided, a default dataset is used.
74
75
  * @param options - Additional options like schema.
76
+ * @param options.schema - Schema of the dataset.
77
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
75
78
  * @returns The dataset object.
76
79
  * @see https://docs.apify.com/api/v2/datasets-post
77
80
  */
78
81
  async getOrCreate(name, options) {
79
- (0, utils_1.parseArgument)(name, nameSchema);
80
- (0, utils_1.parseArgument)(options?.schema, schemaSchema); // TODO: Add schema validation
81
- return this._getOrCreate(name, options);
82
+ parseArgument(name, nameSchema);
83
+ parseArgument(options?.schema, schemaSchema); // TODO: Add schema validation
84
+ parseArgument(options?.timeoutSecs, optionalTimeoutSchema);
85
+ parseArgument(options?.signal, optionalSignalSchema);
86
+ // `timeoutSecs` and `signal` are not part of the resource, so the body carries only the rest, and stays
87
+ // absent when there is nothing else to send.
88
+ const { timeoutSecs = 'short', signal, ...resource } = options ?? {};
89
+ const hasResource = Object.keys(resource).length > 0;
90
+ return this.getOrCreateResource(schemas.DatasetResource(), name, hasResource ? resource : undefined, timeoutSecs, signal);
82
91
  }
83
92
  }
84
- exports.DatasetCollectionClient = DatasetCollectionClient;