apify-client 3.0.0-beta.9 → 3.0.0-rc.0

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 (116) hide show
  1. package/README.md +10 -2
  2. package/dist/apify_api_error.d.ts +11 -26
  3. package/dist/apify_api_error.js +31 -22
  4. package/dist/apify_client.d.ts +125 -10
  5. package/dist/apify_client.js +133 -48
  6. package/dist/base/api_client.d.ts +7 -7
  7. package/dist/base/api_client.js +36 -24
  8. package/dist/base/resource_client.d.ts +21 -8
  9. package/dist/base/resource_client.js +67 -33
  10. package/dist/base/resource_collection_client.d.ts +12 -4
  11. package/dist/base/resource_collection_client.js +34 -11
  12. package/dist/body_parser.d.ts +3 -4
  13. package/dist/body_parser.js +19 -26
  14. package/dist/bundle.js +27 -22
  15. package/dist/bundle.js.map +1 -1
  16. package/dist/generated/api.d.ts +785 -290
  17. package/dist/generated/schemas.d.ts +720 -747
  18. package/dist/generated/schemas.js +166 -162
  19. package/dist/http_clients/axios.d.ts +60 -0
  20. package/dist/http_clients/axios.js +117 -0
  21. package/dist/http_clients/base.d.ts +331 -0
  22. package/dist/http_clients/base.js +563 -0
  23. package/dist/http_clients/index.d.ts +4 -0
  24. package/dist/http_clients/index.js +2 -0
  25. package/dist/http_compressors/base.d.ts +40 -0
  26. package/dist/http_compressors/base.js +9 -0
  27. package/dist/http_compressors/brotli.d.ts +33 -0
  28. package/dist/http_compressors/brotli.js +39 -0
  29. package/dist/http_compressors/gzip.d.ts +33 -0
  30. package/dist/http_compressors/gzip.js +39 -0
  31. package/dist/http_compressors/index.d.ts +6 -0
  32. package/dist/http_compressors/index.js +2 -0
  33. package/dist/http_compressors/resolve.d.ts +23 -0
  34. package/dist/http_compressors/resolve.js +27 -0
  35. package/dist/index.d.ts +4 -1
  36. package/dist/index.js +3 -1
  37. package/dist/invalid_response_body_error.d.ts +13 -0
  38. package/dist/invalid_response_body_error.js +17 -0
  39. package/dist/models.d.ts +162 -194
  40. package/dist/models.js +6 -4
  41. package/dist/resource_clients/actor.d.ts +74 -34
  42. package/dist/resource_clients/actor.js +87 -56
  43. package/dist/resource_clients/actor_collection.d.ts +18 -12
  44. package/dist/resource_clients/actor_collection.js +11 -3
  45. package/dist/resource_clients/actor_env_var.d.ts +11 -4
  46. package/dist/resource_clients/actor_env_var.js +16 -6
  47. package/dist/resource_clients/actor_env_var_collection.d.ts +9 -4
  48. package/dist/resource_clients/actor_env_var_collection.js +11 -4
  49. package/dist/resource_clients/actor_version.d.ts +20 -5
  50. package/dist/resource_clients/actor_version.js +19 -9
  51. package/dist/resource_clients/actor_version_collection.d.ts +11 -4
  52. package/dist/resource_clients/actor_version_collection.js +13 -4
  53. package/dist/resource_clients/build.d.ts +20 -7
  54. package/dist/resource_clients/build.js +35 -15
  55. package/dist/resource_clients/build_collection.d.ts +7 -5
  56. package/dist/resource_clients/build_collection.js +4 -1
  57. package/dist/resource_clients/dataset.d.ts +53 -35
  58. package/dist/resource_clients/dataset.js +105 -80
  59. package/dist/resource_clients/dataset_collection.d.ts +10 -6
  60. package/dist/resource_clients/dataset_collection.js +13 -2
  61. package/dist/resource_clients/key_value_store.d.ts +62 -20
  62. package/dist/resource_clients/key_value_store.js +78 -40
  63. package/dist/resource_clients/key_value_store_collection.d.ts +10 -6
  64. package/dist/resource_clients/key_value_store_collection.js +13 -2
  65. package/dist/resource_clients/log.d.ts +13 -20
  66. package/dist/resource_clients/log.js +141 -59
  67. package/dist/resource_clients/request_queue.d.ts +68 -31
  68. package/dist/resource_clients/request_queue.js +173 -91
  69. package/dist/resource_clients/request_queue_collection.d.ts +9 -5
  70. package/dist/resource_clients/request_queue_collection.js +9 -3
  71. package/dist/resource_clients/run.d.ts +119 -28
  72. package/dist/resource_clients/run.js +190 -49
  73. package/dist/resource_clients/run_collection.d.ts +5 -3
  74. package/dist/resource_clients/run_collection.js +4 -1
  75. package/dist/resource_clients/schedule.d.ts +16 -7
  76. package/dist/resource_clients/schedule.js +30 -22
  77. package/dist/resource_clients/schedule_collection.d.ts +6 -2
  78. package/dist/resource_clients/schedule_collection.js +9 -3
  79. package/dist/resource_clients/store_collection.d.ts +6 -4
  80. package/dist/resource_clients/store_collection.js +4 -1
  81. package/dist/resource_clients/task.d.ts +36 -13
  82. package/dist/resource_clients/task.js +79 -46
  83. package/dist/resource_clients/task_collection.d.ts +9 -5
  84. package/dist/resource_clients/task_collection.js +9 -3
  85. package/dist/resource_clients/user.d.ts +19 -10
  86. package/dist/resource_clients/user.js +42 -38
  87. package/dist/resource_clients/webhook.d.ts +17 -8
  88. package/dist/resource_clients/webhook.js +30 -22
  89. package/dist/resource_clients/webhook_collection.d.ts +8 -4
  90. package/dist/resource_clients/webhook_collection.js +9 -3
  91. package/dist/resource_clients/webhook_dispatch.d.ts +4 -1
  92. package/dist/resource_clients/webhook_dispatch.js +7 -2
  93. package/dist/resource_clients/webhook_dispatch_collection.d.ts +3 -1
  94. package/dist/resource_clients/webhook_dispatch_collection.js +4 -1
  95. package/dist/runtime/node.d.ts +6 -0
  96. package/dist/runtime/node.js +49 -0
  97. package/dist/runtime/types.d.ts +52 -0
  98. package/dist/runtime/types.js +1 -0
  99. package/dist/runtime/web.d.ts +5 -0
  100. package/dist/runtime/web.js +18 -0
  101. package/dist/schemas.d.ts +1 -1
  102. package/dist/schemas.js +1 -1
  103. package/dist/statistics.d.ts +1 -6
  104. package/dist/statistics.js +2 -2
  105. package/dist/timeouts.d.ts +84 -0
  106. package/dist/timeouts.js +37 -0
  107. package/dist/timezones.d.ts +1 -1
  108. package/dist/utils.d.ts +82 -45
  109. package/dist/utils.js +197 -146
  110. package/dist/wait_for_resources.d.ts +8 -0
  111. package/dist/wait_for_resources.js +39 -0
  112. package/package.json +32 -29
  113. package/dist/http_client.d.ts +0 -88
  114. package/dist/http_client.js +0 -255
  115. package/dist/interceptors.d.ts +0 -19
  116. package/dist/interceptors.js +0 -105
@@ -1,34 +1,52 @@
1
1
  import { z } from 'zod';
2
+ import { ACT_JOB_TERMINAL_STATUSES } from '@apify/consts';
2
3
  import { LEVELS, Log } from '@apify/log';
3
4
  import { ResourceClient } from '../base/resource_client.js';
4
5
  import * as schemas from '../schemas.js';
5
- import { anyObjectSchema, isNode, parseArgument, parseResponse } from '../utils.js';
6
+ import { optionalSignalSchema, optionalTimeoutSchema, timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
7
+ import { runtime } from '#runtime';
8
+ import { anyObjectSchema, paginationOptionsShape, parseArgument, parseResponse, SCANNED_COUNT } from '../utils.js';
6
9
  import { DatasetClient } from './dataset.js';
7
10
  import { KeyValueStoreClient } from './key_value_store.js';
8
11
  import { LogClient, LoggerActorRedirect, StreamedLog } from './log.js';
9
12
  import { RequestQueueClient } from './request_queue.js';
10
13
  const RUN_CHARGE_IDEMPOTENCY_HEADER = 'idempotency-key';
11
- const getOptionsSchema = z.strictObject({ waitForFinish: z.number().optional() });
12
- const abortOptionsSchema = z.strictObject({ gracefully: z.boolean().optional() });
14
+ const getOptionsSchema = z.strictObject({ waitForFinish: z.number().optional(), ...timeoutOptionsShape });
15
+ const abortOptionsSchema = z.strictObject({ gracefully: z.boolean().optional(), ...timeoutOptionsShape });
13
16
  const targetActorIdSchema = z.string();
14
17
  const metamorphOptionsSchema = z.strictObject({
15
18
  contentType: z.string().optional(),
16
19
  build: z.string().optional(),
20
+ ...timeoutOptionsShape,
17
21
  });
18
22
  const resurrectOptionsSchema = z.strictObject({
19
23
  build: z.string().optional(),
20
24
  memory: z.number().optional(),
21
- timeout: z.number().optional(),
25
+ runTimeoutSecs: z.number().optional(),
22
26
  maxItems: z.number().optional(),
23
27
  maxTotalChargeUsd: z.number().optional(),
24
28
  restartOnError: z.boolean().optional(),
29
+ ...timeoutOptionsShape,
25
30
  });
26
31
  const chargeOptionsSchema = z.strictObject({
27
32
  eventName: z.string(),
28
33
  count: z.number().default(1),
29
34
  idempotencyKey: z.string().optional(),
35
+ ...timeoutOptionsShape,
30
36
  });
31
- const waitForFinishOptionsSchema = z.strictObject({ waitSecs: z.number().optional() });
37
+ const waitForFinishOptionsSchema = z.strictObject({ waitSecs: z.number().optional(), ...timeoutOptionsShape });
38
+ const iterateDatasetItemsOptionsSchema = z.strictObject({
39
+ clean: z.boolean().optional(),
40
+ fields: z.array(z.string()).optional(),
41
+ omit: z.array(z.string()).optional(),
42
+ ...paginationOptionsShape,
43
+ skipEmpty: z.boolean().optional(),
44
+ skipHidden: z.boolean().optional(),
45
+ unwind: z.array(z.string()).optional(),
46
+ pollIntervalSecs: z.number().min(0).optional(),
47
+ ...timeoutOptionsShape,
48
+ });
49
+ const DEFAULT_ITERATE_CHUNK_SIZE = 1000;
32
50
  /**
33
51
  * Client for managing a specific Actor run.
34
52
  *
@@ -67,7 +85,9 @@ export class RunClient extends ResourceClient {
67
85
  *
68
86
  * @param options - Get options
69
87
  * @param options.waitForFinish - Maximum time to wait (in seconds, max 60s) for the run to finish on the API side before returning. Default is 0 (returns immediately).
70
- * @returns The ActorRun object, or `undefined` if it does not exist
88
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`, extended to cover `waitForFinish`
89
+ * when the API is asked to hold the response.
90
+ * @returns The Run object, or `undefined` if it does not exist
71
91
  * @see https://docs.apify.com/api/v2/actor-run-get
72
92
  *
73
93
  * @example
@@ -81,15 +101,16 @@ export class RunClient extends ResourceClient {
81
101
  * ```
82
102
  */
83
103
  async get(options = {}) {
84
- const parsed = parseArgument(options, getOptionsSchema, 'RunGetOptions');
85
- return this._get(schemas.Run(), parsed);
104
+ const { timeoutSecs, signal, ...params } = parseArgument(options, getOptionsSchema, 'RunGetOptions');
105
+ return this.getResource(schemas.Run(), params, this.timeoutForWaitForFinish(timeoutSecs, 'short', params.waitForFinish), signal);
86
106
  }
87
107
  /**
88
108
  * Aborts the Actor run.
89
109
  *
90
110
  * @param options - Abort options
91
111
  * @param options.gracefully - If `true`, the Actor run will abort gracefully - it can send status messages and perform cleanup. Default is `false` (immediate abort).
92
- * @returns The updated ActorRun object with `ABORTING` or `ABORTED` status
112
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
113
+ * @returns The updated Run object with `ABORTING` or `ABORTED` status
93
114
  * @see https://docs.apify.com/api/v2/actor-run-abort-post
94
115
  *
95
116
  * @example
@@ -102,22 +123,27 @@ export class RunClient extends ResourceClient {
102
123
  * ```
103
124
  */
104
125
  async abort(options = {}) {
105
- const parsed = parseArgument(options, abortOptionsSchema, 'RunAbortOptions');
126
+ const { timeoutSecs = 'medium', signal, ...params } = parseArgument(options, abortOptionsSchema, 'RunAbortOptions');
106
127
  const response = await this.httpClient.call({
107
- url: this._url('abort'),
128
+ url: this.buildUrl('abort'),
108
129
  method: 'POST',
109
- params: this._params(parsed),
130
+ params: this.buildParams(params),
131
+ timeoutSecs,
132
+ signal,
110
133
  });
111
134
  return parseResponse(response, schemas.Run());
112
135
  }
113
136
  /**
114
137
  * Deletes the Actor run.
115
138
  *
139
+ * @param options - Request options
140
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
116
141
  * @see https://docs.apify.com/api/v2/actor-run-delete
117
142
  * @since Added in 2.8.1
118
143
  */
119
- async delete() {
120
- return this._delete();
144
+ async delete(options = {}) {
145
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
146
+ return this.deleteResource(timeoutSecs, signal);
121
147
  }
122
148
  /**
123
149
  * Transforms the Actor run into a run of another Actor (metamorph).
@@ -127,11 +153,12 @@ export class RunClient extends ResourceClient {
127
153
  * This is useful for chaining Actor executions or implementing complex workflows.
128
154
  *
129
155
  * @param targetActorId - ID or username/name of the target Actor
130
- * @param input - Input for the target Actor. Can be any JSON-serializable value.
156
+ * @param input - Input for the target Actor: an object or array serialized to JSON, or raw bytes sent as they
157
+ * are. Omit it to metamorph without input.
131
158
  * @param options - Metamorph options
132
159
  * @param options.build - Tag or number of the target Actor's build to run. Default is the target Actor's default build.
133
- * @param options.contentType - Content type of the input. If specified, input must be a string or Buffer.
134
- * @returns The metamorphed ActorRun object (same ID, but now running the target Actor)
160
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
161
+ * @returns The metamorphed Run object (same ID, but now running the target Actor)
135
162
  * @see https://docs.apify.com/api/v2/actor-run-metamorph-post
136
163
  *
137
164
  * @example
@@ -146,23 +173,21 @@ export class RunClient extends ResourceClient {
146
173
  */
147
174
  async metamorph(targetActorId, input, options = {}) {
148
175
  parseArgument(targetActorId, targetActorIdSchema);
149
- // input can be anything, pointless to validate
150
176
  const parsed = parseArgument(options, metamorphOptionsSchema, 'RunMetamorphOptions');
151
- const safeTargetActorId = this._toSafeId(targetActorId);
177
+ const safeTargetActorId = this.toSafeId(targetActorId);
152
178
  const params = {
153
179
  targetActorId: safeTargetActorId,
154
180
  build: parsed.build,
155
181
  };
156
182
  const request = {
157
- url: this._url('metamorph'),
183
+ url: this.buildUrl('metamorph'),
158
184
  method: 'POST',
159
185
  data: input,
160
- params: this._params(params),
161
- // Apify internal property. Tells the request serialization interceptor
162
- // to stringify functions to JSON, instead of omitting them.
163
- // TODO: remove this ts-expect-error once we have defined custom Apify axios configs
164
- // @ts-expect-error Custom Apify property
186
+ params: this.buildParams(params),
187
+ // Actor input may carry page functions, which plain JSON serialization would drop.
165
188
  stringifyFunctions: true,
189
+ timeoutSecs: parsed.timeoutSecs ?? 'medium',
190
+ signal: parsed.signal,
166
191
  };
167
192
  if (parsed.contentType) {
168
193
  request.headers = {
@@ -179,7 +204,9 @@ export class RunClient extends ResourceClient {
179
204
  * This can be useful to recover from certain errors or to force the Actor to restart
180
205
  * with a fresh environment.
181
206
  *
182
- * @returns The updated ActorRun object
207
+ * @param options - Request options
208
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
209
+ * @returns The updated Run object
183
210
  * @see https://docs.apify.com/api/v2/actor-run-reboot-post
184
211
  *
185
212
  * @example
@@ -188,10 +215,13 @@ export class RunClient extends ResourceClient {
188
215
  * ```
189
216
  * @since Added in 2.8.0
190
217
  */
191
- async reboot() {
218
+ async reboot(options = {}) {
219
+ const { timeoutSecs = 'medium', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
192
220
  const request = {
193
- url: this._url('reboot'),
221
+ url: this.buildUrl('reboot'),
194
222
  method: 'POST',
223
+ timeoutSecs,
224
+ signal,
195
225
  };
196
226
  const response = await this.httpClient.call(request);
197
227
  return parseResponse(response, schemas.Run());
@@ -203,7 +233,9 @@ export class RunClient extends ResourceClient {
203
233
  * @param newFields.statusMessage - Custom status message to display (e.g., "Processing page 10/100")
204
234
  * @param newFields.isStatusMessageTerminal - If `true`, the status message is final and won't be overwritten. Default is `false`.
205
235
  * @param newFields.generalAccess - General resource access level ('FOLLOW_USER_SETTING', 'ANYONE_WITH_ID_CAN_READ' or 'RESTRICTED')
206
- * @returns The updated ActorRun object
236
+ * @param options - Request options
237
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
238
+ * @returns The updated Run object
207
239
  *
208
240
  * @example
209
241
  * ```javascript
@@ -214,9 +246,10 @@ export class RunClient extends ResourceClient {
214
246
  * ```
215
247
  * @since Added in 2.6.0
216
248
  */
217
- async update(newFields) {
249
+ async update(newFields, options = {}) {
218
250
  parseArgument(newFields, anyObjectSchema);
219
- return this._update(schemas.Run(), newFields);
251
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
252
+ return this.updateResource(schemas.Run(), newFields, timeoutSecs, signal);
220
253
  }
221
254
  /**
222
255
  * Resurrects a finished Actor run, starting it again with the same settings.
@@ -227,11 +260,12 @@ export class RunClient extends ResourceClient {
227
260
  * @param options - Resurrection options (override original run settings)
228
261
  * @param options.build - Tag or number of the build to use. If not provided, uses the original run's build.
229
262
  * @param options.memory - Memory in megabytes. If not provided, uses the original run's memory.
230
- * @param options.timeout - Timeout in seconds. If not provided, uses the original run's timeout.
263
+ * @param options.runTimeoutSecs - Timeout for the resurrected run in seconds. If not provided, uses the original run's timeout.
231
264
  * @param options.maxItems - Maximum number of dataset items (pay-per-result Actors).
232
265
  * @param options.maxTotalChargeUsd - Maximum cost in USD (pay-per-event Actors).
233
266
  * @param options.restartOnError - Whether to restart on error.
234
- * @returns The new (resurrected) ActorRun object
267
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
268
+ * @returns The new (resurrected) Run object
235
269
  * @see https://docs.apify.com/api/v2/post-resurrect-run
236
270
  *
237
271
  * @example
@@ -242,11 +276,14 @@ export class RunClient extends ResourceClient {
242
276
  * ```
243
277
  */
244
278
  async resurrect(options = {}) {
245
- const parsed = parseArgument(options, resurrectOptionsSchema, 'RunResurrectOptions');
279
+ const { timeoutSecs = 'medium', signal, runTimeoutSecs, ...params } = parseArgument(options, resurrectOptionsSchema, 'RunResurrectOptions');
246
280
  const response = await this.httpClient.call({
247
- url: this._url('resurrect'),
281
+ url: this.buildUrl('resurrect'),
248
282
  method: 'POST',
249
- params: this._params(parsed),
283
+ // The API's `timeout` parameter bounds the run, not the request.
284
+ params: this.buildParams({ ...params, timeout: runTimeoutSecs }),
285
+ timeoutSecs,
286
+ signal,
250
287
  });
251
288
  return parseResponse(response, schemas.Run());
252
289
  }
@@ -257,17 +294,18 @@ export class RunClient extends ResourceClient {
257
294
  * @param options.eventName - **Required.** Name of the event to charge for.
258
295
  * @param options.count - Number of times to charge the event. Default is 1.
259
296
  * @param options.idempotencyKey - Optional key to ensure the charge is not duplicated. If not provided, one is auto-generated.
297
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
260
298
  * @returns Empty response object.
261
299
  * @see https://docs.apify.com/api/v2/post-charge-run
262
300
  * @since Added in 2.11.0
263
301
  */
264
302
  async charge(options) {
265
- const { eventName, count, idempotencyKey: providedIdempotencyKey, } = parseArgument(options, chargeOptionsSchema, 'RunChargeOptions');
303
+ const { eventName, count, idempotencyKey: providedIdempotencyKey, timeoutSecs = 'short', signal, } = parseArgument(options, chargeOptionsSchema, 'RunChargeOptions');
266
304
  /** To avoid duplicates during the same milisecond, doesn't need to by crypto-secure. */
267
305
  const randomSuffix = (Math.random() + 1).toString(36).slice(3, 8);
268
306
  const idempotencyKey = providedIdempotencyKey ?? `${this.id}-${eventName}-${Date.now()}-${randomSuffix}`;
269
307
  const request = {
270
- url: this._url('charge'),
308
+ url: this.buildUrl('charge'),
271
309
  method: 'POST',
272
310
  data: {
273
311
  eventName,
@@ -276,6 +314,8 @@ export class RunClient extends ResourceClient {
276
314
  headers: {
277
315
  [RUN_CHARGE_IDEMPOTENCY_HEADER]: idempotencyKey,
278
316
  },
317
+ timeoutSecs,
318
+ signal,
279
319
  };
280
320
  const response = await this.httpClient.call(request);
281
321
  return response;
@@ -290,10 +330,12 @@ export class RunClient extends ResourceClient {
290
330
  * Unlike the `waitForFinish` parameter in {@link get}, this method can wait indefinitely
291
331
  * by polling the run status. It uses the `waitForFinish` parameter internally (max 60s per call)
292
332
  * and continuously polls until the run finishes or the timeout is reached.
333
+ * Rejects with a {@link NotFoundError} if the run keeps returning 404 for 3 seconds.
293
334
  *
294
335
  * @param options - Wait options
295
336
  * @param options.waitSecs - Maximum time to wait for the run to finish, in seconds. If the limit is reached, the returned promise resolves to a run object that will have status `READY` or `RUNNING`. If omitted, waits indefinitely.
296
- * @returns The ActorRun object (finished or still running if timeout was reached)
337
+ * @param options.timeoutSecs - Timeout for each polling API request. Default is `'noTimeout'`.
338
+ * @returns The Run object (finished or still running if the timeout was reached)
297
339
  *
298
340
  * @example
299
341
  * ```javascript
@@ -310,11 +352,13 @@ export class RunClient extends ResourceClient {
310
352
  */
311
353
  async waitForFinish(options = {}) {
312
354
  const parsed = parseArgument(options, waitForFinishOptionsSchema, 'RunWaitForFinishOptions');
313
- return this._waitForFinish(schemas.Run(), parsed);
355
+ return this.waitForJobFinish(schemas.Run(), parsed);
314
356
  }
315
357
  /**
316
358
  * Returns a client for the default dataset of this Actor run.
317
359
  *
360
+ * A 404 from this client throws an `ApifyApiError`, since the run itself may be what is missing.
361
+ *
318
362
  * @returns A client for accessing the run's default dataset
319
363
  * @see https://docs.apify.com/api/v2/actor-run-get
320
364
  *
@@ -325,13 +369,16 @@ export class RunClient extends ResourceClient {
325
369
  * ```
326
370
  */
327
371
  dataset() {
328
- return new DatasetClient(this._subResourceOptions({
372
+ return new DatasetClient(this.subResourceOptions({
329
373
  resourcePath: 'dataset',
330
374
  }));
331
375
  }
332
376
  /**
333
377
  * Returns a client for the default key-value store of this Actor run.
334
378
  *
379
+ * `get()` and `delete()` throw an `ApifyApiError` on a 404, since the run itself may be what is missing. Record
380
+ * lookups such as `getRecord()` read a 404 as a missing record.
381
+ *
335
382
  * @returns A client for accessing the run's default key-value store
336
383
  * @see https://docs.apify.com/api/v2/actor-run-get
337
384
  *
@@ -342,13 +389,16 @@ export class RunClient extends ResourceClient {
342
389
  * ```
343
390
  */
344
391
  keyValueStore() {
345
- return new KeyValueStoreClient(this._subResourceOptions({
392
+ return new KeyValueStoreClient(this.subResourceOptions({
346
393
  resourcePath: 'key-value-store',
347
394
  }));
348
395
  }
349
396
  /**
350
397
  * Returns a client for the default Request queue of this Actor run.
351
398
  *
399
+ * `get()` and `delete()` throw an `ApifyApiError` on a 404, since the run itself may be what is missing.
400
+ * `getRequest()` reads a 404 as a missing request.
401
+ *
352
402
  * @returns A client for accessing the run's default Request queue
353
403
  * @see https://docs.apify.com/api/v2/actor-run-get
354
404
  *
@@ -359,13 +409,15 @@ export class RunClient extends ResourceClient {
359
409
  * ```
360
410
  */
361
411
  requestQueue() {
362
- return new RequestQueueClient(this._subResourceOptions({
412
+ return new RequestQueueClient(this.subResourceOptions({
363
413
  resourcePath: 'request-queue',
364
414
  }));
365
415
  }
366
416
  /**
367
417
  * Returns a client for accessing the log of this Actor run.
368
418
  *
419
+ * A 404 from this client throws an `ApifyApiError`, since the run itself may be what is missing.
420
+ *
369
421
  * @returns A client for accessing the run's log
370
422
  * @see https://docs.apify.com/api/v2/actor-run-get
371
423
  *
@@ -377,32 +429,121 @@ export class RunClient extends ResourceClient {
377
429
  * ```
378
430
  */
379
431
  log() {
380
- return new LogClient(this._subResourceOptions({
432
+ return new LogClient(this.subResourceOptions({
381
433
  resourcePath: 'log',
382
434
  }));
383
435
  }
384
436
  /**
385
437
  * Get StreamedLog for convenient streaming of the run log and their redirection.
438
+ *
439
+ * @param options - Streaming options
440
+ * @param options.toLog - Log instance to redirect the run log to. Use `'default'` for a preconfigured one, or `null` to disable the redirection.
441
+ * @param options.fromStart - If `true`, the log is streamed from its beginning. Default is `true`.
442
+ * @param options.timeoutSecs - Timeout for the API requests that fetch the run and its Actor. Default is `'long'`.
386
443
  * @since Added in 2.20.0
387
444
  */
388
445
  async getStreamedLog(options = {}) {
389
- const { fromStart = true } = options;
446
+ parseArgument(options.timeoutSecs, optionalTimeoutSchema);
447
+ parseArgument(options.signal, optionalSignalSchema);
448
+ const { fromStart = true, timeoutSecs = 'long', signal } = options;
390
449
  let { toLog } = options;
391
- if (toLog === null || !isNode()) {
450
+ if (toLog === null || !runtime.isNode) {
392
451
  // Explicitly no logging or not in Node.js
393
452
  return undefined;
394
453
  }
395
454
  if (toLog === undefined || toLog === 'default') {
396
455
  // Create default StreamedLog
397
456
  // Get actor name and run id
398
- const runData = await this.get();
457
+ const runData = await this.get({ timeoutSecs, signal });
399
458
  const runId = runData?.id ?? '';
400
459
  const actorId = runData?.actId ?? '';
401
- const actorData = (await this.apifyClient.actor(actorId).get()) || { name: '' };
460
+ // `apifyClient.actor()` rejects an empty ID, which is what a run that could not be read leaves here.
461
+ const actorData = actorId ? await this.apifyClient.actor(actorId).get({ timeoutSecs, signal }) : undefined;
402
462
  const actorName = actorData?.name ?? '';
403
463
  const name = [actorName, `runId:${runId}`].filter(Boolean).join(' ');
404
464
  toLog = new Log({ level: LEVELS.DEBUG, prefix: `${name} -> `, logger: new LoggerActorRedirect() });
405
465
  }
406
- return new StreamedLog({ logClient: this.log(), toLog, fromStart });
466
+ return new StreamedLog({ logClient: this.log(), toLog, fromStart, signal });
467
+ }
468
+ /**
469
+ * Iterates over the items of the run's default dataset while the run is still producing them.
470
+ *
471
+ * While the run has not finished, each poll yields the rows below the dataset's `itemCount` and then waits up to
472
+ * `pollIntervalSecs` for the run to finish, so the last rows are read as soon as it does. Each page is requested
473
+ * with a `limit` that ends at `itemCount`, so it covers exactly the rows it asks for, whatever the filters or
474
+ * `unwind` do to the items. `itemCount` lags a few seconds behind
475
+ * the pushed items, so once the run reaches a terminal status, the rows past it are read a page at a time until
476
+ * none are left, and the iterator returns. On a `lastRun()` client, the iterator sticks to the run that its first
477
+ * request resolves to.
478
+ *
479
+ * @param options - Iteration options
480
+ * @param options.offset - Number of rows to skip from the beginning. Default is 0.
481
+ * @param options.limit - Maximum number of dataset rows to scan. Fewer items are yielded when filters drop some,
482
+ * more when `unwind` splits a row into several. Default is no limit.
483
+ * @param options.clean - If `true`, returns only non-empty items and skips hidden fields. Default is `false`.
484
+ * @param options.fields - Array of field names to include in the results. Omits all other fields.
485
+ * @param options.omit - Array of field names to exclude from the results.
486
+ * @param options.unwind - Array of field names to unwind. Each array value creates a separate item.
487
+ * @param options.skipEmpty - If `true`, skips empty items. Default is `false`.
488
+ * @param options.skipHidden - If `true`, skips hidden fields (fields starting with `#`). Default is `false`.
489
+ * @param options.chunkSize - Maximum number of dataset rows requested per API call. Default is 1000.
490
+ * @param options.pollIntervalSecs - How long to wait for the run to finish between polls, in seconds. Default is 5.
491
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'long'`.
492
+ * @returns An async iterable of the dataset items
493
+ * @see https://docs.apify.com/api/v2/dataset-items-get
494
+ *
495
+ * @example
496
+ * ```javascript
497
+ * for await (const item of client.run('run-id').iterateDatasetItems()) {
498
+ * console.log(item);
499
+ * }
500
+ * ```
501
+ */
502
+ async *iterateDatasetItems(options = {}) {
503
+ const { offset, limit, chunkSize, pollIntervalSecs = 5, timeoutSecs = 'long', signal, ...itemOptions } = parseArgument(options, iterateDatasetItemsOptionsSchema, 'RunIterateDatasetItemsOptions');
504
+ const pageSize = chunkSize || DEFAULT_ITERATE_CHUNK_SIZE;
505
+ let position = offset ?? 0;
506
+ const end = limit ? position + limit : undefined;
507
+ let run = await this.get({ timeoutSecs, signal });
508
+ // A `lastRun()` client resolves `runs/last` on each request, so a newer run would swap the dataset mid-iteration.
509
+ const runClient = run && run.id !== this.id ? this.apifyClient.run(run.id) : this;
510
+ const datasetClient = runClient.dataset();
511
+ const listPage = async (pageOffset, pageLimit) => datasetClient.listItems({ ...itemOptions, offset: pageOffset, limit: pageLimit, timeoutSecs, signal });
512
+ while (true) {
513
+ const isFinished = !run || ACT_JOB_TERMINAL_STATUSES.includes(run.status);
514
+ const dataset = await datasetClient.get({ timeoutSecs, signal });
515
+ let itemCount = dataset?.itemCount ?? 0;
516
+ if (end !== undefined)
517
+ itemCount = Math.min(itemCount, end);
518
+ while (position < itemCount) {
519
+ const pageLimit = Math.min(pageSize, itemCount - position);
520
+ const page = await listPage(position, pageLimit);
521
+ yield* page.items;
522
+ position += pageLimit;
523
+ }
524
+ if (end !== undefined && position >= end)
525
+ return;
526
+ if (isFinished)
527
+ break;
528
+ run = await runClient.waitForFinish({ waitSecs: pollIntervalSecs, timeoutSecs, signal });
529
+ }
530
+ const { clean, skipEmpty, unwind } = itemOptions;
531
+ while (true) {
532
+ const pageLimit = end !== undefined ? Math.min(pageSize, end - position) : pageSize;
533
+ const page = await listPage(position, pageLimit);
534
+ yield* page.items;
535
+ // Only an empty page marks the end, as filters can shorten a full one. A page that `clean`, `skipEmpty` or
536
+ // `unwind` emptied past a lagging `itemCount` reports no scanned rows either, so a plain read checks.
537
+ const isEmpty = page.items.length === 0 && !page[SCANNED_COUNT];
538
+ if (isEmpty &&
539
+ (!(clean || skipEmpty || unwind?.length) ||
540
+ (await datasetClient.listItems({ offset: position, limit: 1, timeoutSecs, signal })).items
541
+ .length === 0)) {
542
+ return;
543
+ }
544
+ position += pageLimit;
545
+ if (end !== undefined && position >= end)
546
+ return;
547
+ }
407
548
  }
408
549
  }
@@ -1,8 +1,9 @@
1
1
  import { ACTOR_JOB_STATUSES } from '@apify/consts';
2
2
  import type { ApiClientOptionsWithOptionalResourcePath } from '../base/api_client.js';
3
3
  import { ResourceCollectionClient } from '../base/resource_collection_client.js';
4
+ import type { TimeoutOptions } from '../timeouts.js';
4
5
  import type { PaginatedIterator, PaginationOptions } from '../utils.js';
5
- import type { ActorRunListItem } from './actor.js';
6
+ import type { RunListItem } from './actor.js';
6
7
  /**
7
8
  * Client for managing the collection of Actor runs.
8
9
  *
@@ -46,12 +47,13 @@ export declare class RunCollectionClient extends ResourceCollectionClient {
46
47
  * ```
47
48
  *
48
49
  * @param options - Pagination and filtering options.
50
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
49
51
  * @returns A paginated iterator of Actor runs.
50
52
  * @see https://docs.apify.com/api/v2/actor-runs-get
51
53
  */
52
- list(options?: RunCollectionListOptions): PaginatedIterator<ActorRunListItem>;
54
+ list(options?: RunCollectionListOptions): PaginatedIterator<RunListItem>;
53
55
  }
54
- export interface RunCollectionListOptions extends PaginationOptions {
56
+ export interface RunCollectionListOptions extends PaginationOptions, TimeoutOptions {
55
57
  desc?: boolean;
56
58
  status?: (typeof ACTOR_JOB_STATUSES)[keyof typeof ACTOR_JOB_STATUSES] | (typeof ACTOR_JOB_STATUSES)[keyof typeof ACTOR_JOB_STATUSES][];
57
59
  /**
@@ -2,6 +2,7 @@ import { z } from 'zod';
2
2
  import { ACTOR_JOB_STATUSES } from '@apify/consts';
3
3
  import { ResourceCollectionClient } from '../base/resource_collection_client.js';
4
4
  import * as schemas from '../schemas.js';
5
+ import { timeoutOptionsShape } from '../timeouts.js';
5
6
  import { paginationOptionsShape, parseArgument } from '../utils.js';
6
7
  const jobStatusSchema = z.enum(ACTOR_JOB_STATUSES);
7
8
  const listOptionsSchema = z.strictObject({
@@ -10,6 +11,7 @@ const listOptionsSchema = z.strictObject({
10
11
  status: z.union([jobStatusSchema, z.array(jobStatusSchema)]).optional(),
11
12
  startedBefore: z.union([z.date(), z.string()]).optional(),
12
13
  startedAfter: z.union([z.date(), z.string()]).optional(),
14
+ ...timeoutOptionsShape,
13
15
  });
14
16
  /**
15
17
  * Client for managing the collection of Actor runs.
@@ -59,11 +61,12 @@ export class RunCollectionClient extends ResourceCollectionClient {
59
61
  * ```
60
62
  *
61
63
  * @param options - Pagination and filtering options.
64
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
62
65
  * @returns A paginated iterator of Actor runs.
63
66
  * @see https://docs.apify.com/api/v2/actor-runs-get
64
67
  */
65
68
  list(options = {}) {
66
69
  const parsed = parseArgument(options, listOptionsSchema, 'RunCollectionListOptions');
67
- return this._listPaginated(schemas.ListOfRuns(), parsed);
70
+ return this.listResourcesPaginated(schemas.ListOfRuns(), parsed, 'medium');
68
71
  }
69
72
  }
@@ -1,8 +1,9 @@
1
1
  import type { ApiClientSubResourceOptions } from '../base/api_client.js';
2
2
  import { ResourceClient } from '../base/resource_client.js';
3
- import type { Schedule, ScheduleAction, ScheduleInvoked } from '../models.js';
3
+ import type { Schedule, ScheduleAction, ScheduleLogEntry } from '../models.js';
4
+ import type { TimeoutOptions } from '../timeouts.js';
4
5
  import type { DistributiveOptional } from '../utils.js';
5
- export type { Schedule, ScheduleAction, ScheduleActionRunActor, ScheduleActionRunActorTask, ScheduledActorRunInput, ScheduledActorRunOptions, ScheduleInvoked, } from '../models.js';
6
+ export type { Schedule, ScheduleAction, ScheduleActionRunActor, ScheduleActionRunActorTask, ScheduleActionRunInput, ScheduleLogEntry, TaskOptions, } from '../models.js';
6
7
  export { ScheduleActions } from '../models.js';
7
8
  /**
8
9
  * Client for managing a specific Schedule.
@@ -35,31 +36,39 @@ export declare class ScheduleClient extends ResourceClient {
35
36
  /**
36
37
  * Retrieves the schedule.
37
38
  *
39
+ * @param options - Request options
40
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
38
41
  * @returns The schedule object, or `undefined` if it does not exist.
39
42
  * @see https://docs.apify.com/api/v2/schedule-get
40
43
  */
41
- get(): Promise<Schedule | undefined>;
44
+ get(options?: TimeoutOptions): Promise<Schedule | undefined>;
42
45
  /**
43
46
  * Updates the schedule with the specified fields.
44
47
  *
45
48
  * @param newFields - Fields to update.
49
+ * @param options - Request options
50
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
46
51
  * @returns The updated schedule object.
47
52
  * @see https://docs.apify.com/api/v2/schedule-put
48
53
  */
49
- update(newFields: ScheduleCreateOrUpdateData): Promise<Schedule>;
54
+ update(newFields: ScheduleCreateOrUpdateData, options?: TimeoutOptions): Promise<Schedule>;
50
55
  /**
51
56
  * Deletes the schedule.
52
57
  *
58
+ * @param options - Request options
59
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
53
60
  * @see https://docs.apify.com/api/v2/schedule-delete
54
61
  */
55
- delete(): Promise<void>;
62
+ delete(options?: TimeoutOptions): Promise<void>;
56
63
  /**
57
64
  * Retrieves the schedule's log.
58
65
  *
59
- * @returns The schedule log, one entry per invocation, or `undefined` if the schedule does not exist.
66
+ * @param options - Request options
67
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
68
+ * @returns The schedule log, one entry per invocation.
60
69
  * @see https://docs.apify.com/api/v2/schedule-log-get
61
70
  */
62
- getLog(): Promise<ScheduleInvoked[] | undefined>;
71
+ getLog(options?: TimeoutOptions): Promise<ScheduleLogEntry[]>;
63
72
  }
64
73
  /**
65
74
  * Data for creating or updating a Schedule.