apify-client 3.0.0-beta.9 → 3.0.0-beta.90

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