apify-client 3.0.0-beta.5 → 3.0.0-beta.50

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 (115) hide show
  1. package/README.md +13 -3
  2. package/dist/apify_api_error.d.ts +62 -21
  3. package/dist/apify_api_error.js +96 -23
  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 +54 -26
  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 +610 -112
  17. package/dist/generated/schemas.d.ts +726 -703
  18. package/dist/generated/schemas.js +92 -85
  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 +333 -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 +19 -22
  40. package/dist/resource_clients/actor.d.ts +72 -32
  41. package/dist/resource_clients/actor.js +87 -56
  42. package/dist/resource_clients/actor_collection.d.ts +9 -5
  43. package/dist/resource_clients/actor_collection.js +11 -3
  44. package/dist/resource_clients/actor_env_var.d.ts +10 -3
  45. package/dist/resource_clients/actor_env_var.js +16 -6
  46. package/dist/resource_clients/actor_env_var_collection.d.ts +14 -18
  47. package/dist/resource_clients/actor_env_var_collection.js +17 -11
  48. package/dist/resource_clients/actor_version.d.ts +18 -3
  49. package/dist/resource_clients/actor_version.js +19 -9
  50. package/dist/resource_clients/actor_version_collection.d.ts +16 -17
  51. package/dist/resource_clients/actor_version_collection.js +19 -11
  52. package/dist/resource_clients/build.d.ts +18 -6
  53. package/dist/resource_clients/build.js +34 -15
  54. package/dist/resource_clients/build_collection.d.ts +3 -1
  55. package/dist/resource_clients/build_collection.js +4 -1
  56. package/dist/resource_clients/dataset.d.ts +51 -33
  57. package/dist/resource_clients/dataset.js +105 -80
  58. package/dist/resource_clients/dataset_collection.d.ts +6 -2
  59. package/dist/resource_clients/dataset_collection.js +13 -2
  60. package/dist/resource_clients/key_value_store.d.ts +58 -16
  61. package/dist/resource_clients/key_value_store.js +78 -40
  62. package/dist/resource_clients/key_value_store_collection.d.ts +6 -2
  63. package/dist/resource_clients/key_value_store_collection.js +13 -2
  64. package/dist/resource_clients/log.d.ts +11 -19
  65. package/dist/resource_clients/log.js +64 -46
  66. package/dist/resource_clients/request_queue.d.ts +56 -26
  67. package/dist/resource_clients/request_queue.js +176 -103
  68. package/dist/resource_clients/request_queue_collection.d.ts +6 -2
  69. package/dist/resource_clients/request_queue_collection.js +9 -3
  70. package/dist/resource_clients/run.d.ts +108 -18
  71. package/dist/resource_clients/run.js +183 -43
  72. package/dist/resource_clients/run_collection.d.ts +3 -1
  73. package/dist/resource_clients/run_collection.js +4 -1
  74. package/dist/resource_clients/schedule.d.ts +14 -5
  75. package/dist/resource_clients/schedule.js +29 -21
  76. package/dist/resource_clients/schedule_collection.d.ts +6 -2
  77. package/dist/resource_clients/schedule_collection.js +9 -3
  78. package/dist/resource_clients/store_collection.d.ts +3 -1
  79. package/dist/resource_clients/store_collection.js +4 -1
  80. package/dist/resource_clients/task.d.ts +33 -10
  81. package/dist/resource_clients/task.js +79 -46
  82. package/dist/resource_clients/task_collection.d.ts +6 -2
  83. package/dist/resource_clients/task_collection.js +9 -3
  84. package/dist/resource_clients/user.d.ts +17 -8
  85. package/dist/resource_clients/user.js +42 -38
  86. package/dist/resource_clients/webhook.d.ts +14 -5
  87. package/dist/resource_clients/webhook.js +30 -22
  88. package/dist/resource_clients/webhook_collection.d.ts +6 -2
  89. package/dist/resource_clients/webhook_collection.js +9 -3
  90. package/dist/resource_clients/webhook_dispatch.d.ts +4 -1
  91. package/dist/resource_clients/webhook_dispatch.js +7 -2
  92. package/dist/resource_clients/webhook_dispatch_collection.d.ts +3 -1
  93. package/dist/resource_clients/webhook_dispatch_collection.js +4 -1
  94. package/dist/runtime/node.d.ts +6 -0
  95. package/dist/runtime/node.js +49 -0
  96. package/dist/runtime/types.d.ts +52 -0
  97. package/dist/runtime/types.js +1 -0
  98. package/dist/runtime/web.d.ts +5 -0
  99. package/dist/runtime/web.js +18 -0
  100. package/dist/schemas.d.ts +1 -1
  101. package/dist/schemas.js +1 -1
  102. package/dist/statistics.d.ts +1 -6
  103. package/dist/statistics.js +2 -2
  104. package/dist/timeouts.d.ts +84 -0
  105. package/dist/timeouts.js +37 -0
  106. package/dist/timezones.d.ts +1 -1
  107. package/dist/utils.d.ts +81 -52
  108. package/dist/utils.js +201 -170
  109. package/dist/wait_for_resources.d.ts +8 -0
  110. package/dist/wait_for_resources.js +39 -0
  111. package/package.json +32 -29
  112. package/dist/http_client.d.ts +0 -88
  113. package/dist/http_client.js +0 -255
  114. package/dist/interceptors.d.ts +0 -19
  115. 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,6 +85,8 @@ 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).
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.
70
90
  * @returns The ActorRun object, or `undefined` if it does not exist
71
91
  * @see https://docs.apify.com/api/v2/actor-run-get
72
92
  *
@@ -81,14 +101,15 @@ 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).
112
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
92
113
  * @returns The updated ActorRun object with `ABORTING` or `ABORTED` status
93
114
  * @see https://docs.apify.com/api/v2/actor-run-abort-post
94
115
  *
@@ -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,10 +153,11 @@ 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.
160
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
134
161
  * @returns The metamorphed ActorRun object (same ID, but now running the target Actor)
135
162
  * @see https://docs.apify.com/api/v2/actor-run-metamorph-post
136
163
  *
@@ -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,6 +204,8 @@ 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
  *
207
+ * @param options - Request options
208
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
182
209
  * @returns The updated ActorRun object
183
210
  * @see https://docs.apify.com/api/v2/actor-run-reboot-post
184
211
  *
@@ -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,6 +233,8 @@ 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')
236
+ * @param options - Request options
237
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
206
238
  * @returns The updated ActorRun object
207
239
  *
208
240
  * @example
@@ -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,10 +260,11 @@ 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.
267
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
234
268
  * @returns The new (resurrected) ActorRun object
235
269
  * @see https://docs.apify.com/api/v2/post-resurrect-run
236
270
  *
@@ -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;
@@ -293,7 +333,8 @@ export class RunClient extends ResourceClient {
293
333
  *
294
334
  * @param options - Wait options
295
335
  * @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)
336
+ * @param options.timeoutSecs - Timeout for each polling API request. Default is `'noTimeout'`.
337
+ * @returns The ActorRun object (finished or still running if the timeout was reached)
297
338
  *
298
339
  * @example
299
340
  * ```javascript
@@ -310,11 +351,13 @@ export class RunClient extends ResourceClient {
310
351
  */
311
352
  async waitForFinish(options = {}) {
312
353
  const parsed = parseArgument(options, waitForFinishOptionsSchema, 'RunWaitForFinishOptions');
313
- return this._waitForFinish(schemas.Run(), parsed);
354
+ return this.waitForJobFinish(schemas.Run(), parsed);
314
355
  }
315
356
  /**
316
357
  * Returns a client for the default dataset of this Actor run.
317
358
  *
359
+ * A 404 from this client throws an `ApifyApiError`, since the run itself may be what is missing.
360
+ *
318
361
  * @returns A client for accessing the run's default dataset
319
362
  * @see https://docs.apify.com/api/v2/actor-run-get
320
363
  *
@@ -325,13 +368,16 @@ export class RunClient extends ResourceClient {
325
368
  * ```
326
369
  */
327
370
  dataset() {
328
- return new DatasetClient(this._subResourceOptions({
371
+ return new DatasetClient(this.subResourceOptions({
329
372
  resourcePath: 'dataset',
330
373
  }));
331
374
  }
332
375
  /**
333
376
  * Returns a client for the default key-value store of this Actor run.
334
377
  *
378
+ * `get()` and `delete()` throw an `ApifyApiError` on a 404, since the run itself may be what is missing. Record
379
+ * lookups such as `getRecord()` read a 404 as a missing record.
380
+ *
335
381
  * @returns A client for accessing the run's default key-value store
336
382
  * @see https://docs.apify.com/api/v2/actor-run-get
337
383
  *
@@ -342,13 +388,16 @@ export class RunClient extends ResourceClient {
342
388
  * ```
343
389
  */
344
390
  keyValueStore() {
345
- return new KeyValueStoreClient(this._subResourceOptions({
391
+ return new KeyValueStoreClient(this.subResourceOptions({
346
392
  resourcePath: 'key-value-store',
347
393
  }));
348
394
  }
349
395
  /**
350
396
  * Returns a client for the default Request queue of this Actor run.
351
397
  *
398
+ * `get()` and `delete()` throw an `ApifyApiError` on a 404, since the run itself may be what is missing.
399
+ * `getRequest()` reads a 404 as a missing request.
400
+ *
352
401
  * @returns A client for accessing the run's default Request queue
353
402
  * @see https://docs.apify.com/api/v2/actor-run-get
354
403
  *
@@ -359,13 +408,15 @@ export class RunClient extends ResourceClient {
359
408
  * ```
360
409
  */
361
410
  requestQueue() {
362
- return new RequestQueueClient(this._subResourceOptions({
411
+ return new RequestQueueClient(this.subResourceOptions({
363
412
  resourcePath: 'request-queue',
364
413
  }));
365
414
  }
366
415
  /**
367
416
  * Returns a client for accessing the log of this Actor run.
368
417
  *
418
+ * A 404 from this client throws an `ApifyApiError`, since the run itself may be what is missing.
419
+ *
369
420
  * @returns A client for accessing the run's log
370
421
  * @see https://docs.apify.com/api/v2/actor-run-get
371
422
  *
@@ -377,32 +428,121 @@ export class RunClient extends ResourceClient {
377
428
  * ```
378
429
  */
379
430
  log() {
380
- return new LogClient(this._subResourceOptions({
431
+ return new LogClient(this.subResourceOptions({
381
432
  resourcePath: 'log',
382
433
  }));
383
434
  }
384
435
  /**
385
436
  * Get StreamedLog for convenient streaming of the run log and their redirection.
437
+ *
438
+ * @param options - Streaming options
439
+ * @param options.toLog - Log instance to redirect the run log to. Use `'default'` for a preconfigured one, or `null` to disable the redirection.
440
+ * @param options.fromStart - If `true`, the log is streamed from its beginning. Default is `true`.
441
+ * @param options.timeoutSecs - Timeout for the API requests that fetch the run and its Actor. Default is `'long'`.
386
442
  * @since Added in 2.20.0
387
443
  */
388
444
  async getStreamedLog(options = {}) {
389
- const { fromStart = true } = options;
445
+ parseArgument(options.timeoutSecs, optionalTimeoutSchema);
446
+ parseArgument(options.signal, optionalSignalSchema);
447
+ const { fromStart = true, timeoutSecs = 'long', signal } = options;
390
448
  let { toLog } = options;
391
- if (toLog === null || !isNode()) {
449
+ if (toLog === null || !runtime.isNode) {
392
450
  // Explicitly no logging or not in Node.js
393
451
  return undefined;
394
452
  }
395
453
  if (toLog === undefined || toLog === 'default') {
396
454
  // Create default StreamedLog
397
455
  // Get actor name and run id
398
- const runData = await this.get();
456
+ const runData = await this.get({ timeoutSecs, signal });
399
457
  const runId = runData?.id ?? '';
400
458
  const actorId = runData?.actId ?? '';
401
- const actorData = (await this.apifyClient.actor(actorId).get()) || { name: '' };
459
+ // `apifyClient.actor()` rejects an empty ID, which is what a run that could not be read leaves here.
460
+ const actorData = actorId ? await this.apifyClient.actor(actorId).get({ timeoutSecs, signal }) : undefined;
402
461
  const actorName = actorData?.name ?? '';
403
462
  const name = [actorName, `runId:${runId}`].filter(Boolean).join(' ');
404
463
  toLog = new Log({ level: LEVELS.DEBUG, prefix: `${name} -> `, logger: new LoggerActorRedirect() });
405
464
  }
406
- return new StreamedLog({ logClient: this.log(), toLog, fromStart });
465
+ return new StreamedLog({ logClient: this.log(), toLog, fromStart, signal });
466
+ }
467
+ /**
468
+ * Iterates over the items of the run's default dataset while the run is still producing them.
469
+ *
470
+ * While the run has not finished, each poll yields the rows below the dataset's `itemCount` and then waits up to
471
+ * `pollIntervalSecs` for the run to finish, so the last rows are read as soon as it does. Each page is requested
472
+ * with a `limit` that ends at `itemCount`, so it covers exactly the rows it asks for, whatever the filters or
473
+ * `unwind` do to the items. `itemCount` lags a few seconds behind
474
+ * the pushed items, so once the run reaches a terminal status, the rows past it are read a page at a time until
475
+ * none are left, and the iterator returns. On a `lastRun()` client, the iterator sticks to the run that its first
476
+ * request resolves to.
477
+ *
478
+ * @param options - Iteration options
479
+ * @param options.offset - Number of rows to skip from the beginning. Default is 0.
480
+ * @param options.limit - Maximum number of dataset rows to scan. Fewer items are yielded when filters drop some,
481
+ * more when `unwind` splits a row into several. Default is no limit.
482
+ * @param options.clean - If `true`, returns only non-empty items and skips hidden fields. Default is `false`.
483
+ * @param options.fields - Array of field names to include in the results. Omits all other fields.
484
+ * @param options.omit - Array of field names to exclude from the results.
485
+ * @param options.unwind - Array of field names to unwind. Each array value creates a separate item.
486
+ * @param options.skipEmpty - If `true`, skips empty items. Default is `false`.
487
+ * @param options.skipHidden - If `true`, skips hidden fields (fields starting with `#`). Default is `false`.
488
+ * @param options.chunkSize - Maximum number of dataset rows requested per API call. Default is 1000.
489
+ * @param options.pollIntervalSecs - How long to wait for the run to finish between polls, in seconds. Default is 5.
490
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'long'`.
491
+ * @returns An async iterable of the dataset items
492
+ * @see https://docs.apify.com/api/v2/dataset-items-get
493
+ *
494
+ * @example
495
+ * ```javascript
496
+ * for await (const item of client.run('run-id').iterateDatasetItems()) {
497
+ * console.log(item);
498
+ * }
499
+ * ```
500
+ */
501
+ async *iterateDatasetItems(options = {}) {
502
+ const { offset, limit, chunkSize, pollIntervalSecs = 5, timeoutSecs = 'long', signal, ...itemOptions } = parseArgument(options, iterateDatasetItemsOptionsSchema, 'RunIterateDatasetItemsOptions');
503
+ const pageSize = chunkSize || DEFAULT_ITERATE_CHUNK_SIZE;
504
+ let position = offset ?? 0;
505
+ const end = limit ? position + limit : undefined;
506
+ let run = await this.get({ timeoutSecs, signal });
507
+ // A `lastRun()` client resolves `runs/last` on each request, so a newer run would swap the dataset mid-iteration.
508
+ const runClient = run && run.id !== this.id ? this.apifyClient.run(run.id) : this;
509
+ const datasetClient = runClient.dataset();
510
+ const listPage = async (pageOffset, pageLimit) => datasetClient.listItems({ ...itemOptions, offset: pageOffset, limit: pageLimit, timeoutSecs, signal });
511
+ while (true) {
512
+ const isFinished = !run || ACT_JOB_TERMINAL_STATUSES.includes(run.status);
513
+ const dataset = await datasetClient.get({ timeoutSecs, signal });
514
+ let itemCount = dataset?.itemCount ?? 0;
515
+ if (end !== undefined)
516
+ itemCount = Math.min(itemCount, end);
517
+ while (position < itemCount) {
518
+ const pageLimit = Math.min(pageSize, itemCount - position);
519
+ const page = await listPage(position, pageLimit);
520
+ yield* page.items;
521
+ position += pageLimit;
522
+ }
523
+ if (end !== undefined && position >= end)
524
+ return;
525
+ if (isFinished)
526
+ break;
527
+ run = await runClient.waitForFinish({ waitSecs: pollIntervalSecs, timeoutSecs, signal });
528
+ }
529
+ const { clean, skipEmpty, unwind } = itemOptions;
530
+ while (true) {
531
+ const pageLimit = end !== undefined ? Math.min(pageSize, end - position) : pageSize;
532
+ const page = await listPage(position, pageLimit);
533
+ yield* page.items;
534
+ // Only an empty page marks the end, as filters can shorten a full one. A page that `clean`, `skipEmpty` or
535
+ // `unwind` emptied past a lagging `itemCount` reports no scanned rows either, so a plain read checks.
536
+ const isEmpty = page.items.length === 0 && !page[SCANNED_COUNT];
537
+ if (isEmpty &&
538
+ (!(clean || skipEmpty || unwind?.length) ||
539
+ (await datasetClient.listItems({ offset: position, limit: 1, timeoutSecs, signal })).items
540
+ .length === 0)) {
541
+ return;
542
+ }
543
+ position += pageLimit;
544
+ if (end !== undefined && position >= end)
545
+ return;
546
+ }
407
547
  }
408
548
  }
@@ -1,6 +1,7 @@
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
6
  import type { ActorRunListItem } from './actor.js';
6
7
  /**
@@ -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
54
  list(options?: RunCollectionListOptions): PaginatedIterator<ActorRunListItem>;
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,6 +1,7 @@
1
1
  import type { ApiClientSubResourceOptions } from '../base/api_client.js';
2
2
  import { ResourceClient } from '../base/resource_client.js';
3
3
  import type { Schedule, ScheduleAction, ScheduleInvoked } from '../models.js';
4
+ import type { TimeoutOptions } from '../timeouts.js';
4
5
  import type { DistributiveOptional } from '../utils.js';
5
6
  export type { Schedule, ScheduleAction, ScheduleActionRunActor, ScheduleActionRunActorTask, ScheduledActorRunInput, ScheduledActorRunOptions, ScheduleInvoked, } from '../models.js';
6
7
  export { ScheduleActions } from '../models.js';
@@ -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<ScheduleInvoked[]>;
63
72
  }
64
73
  /**
65
74
  * Data for creating or updating a Schedule.