apify-client 3.0.0-beta.27 → 3.0.0-beta.29

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 (39) hide show
  1. package/dist/apify_client.d.ts +15 -3
  2. package/dist/apify_client.js +9 -6
  3. package/dist/base/api_client.js +2 -2
  4. package/dist/base/resource_client.d.ts +3 -3
  5. package/dist/base/resource_client.js +13 -9
  6. package/dist/base/resource_collection_client.d.ts +5 -4
  7. package/dist/base/resource_collection_client.js +12 -7
  8. package/dist/bundle.js +6 -6
  9. package/dist/bundle.js.map +1 -1
  10. package/dist/http_clients/base.js +1 -21
  11. package/dist/resource_clients/actor.js +17 -13
  12. package/dist/resource_clients/actor_collection.js +2 -2
  13. package/dist/resource_clients/actor_env_var.js +6 -6
  14. package/dist/resource_clients/actor_env_var_collection.js +4 -4
  15. package/dist/resource_clients/actor_version.js +6 -6
  16. package/dist/resource_clients/actor_version_collection.js +4 -4
  17. package/dist/resource_clients/build.js +8 -6
  18. package/dist/resource_clients/dataset.js +18 -14
  19. package/dist/resource_clients/dataset_collection.js +6 -5
  20. package/dist/resource_clients/key_value_store.js +19 -14
  21. package/dist/resource_clients/key_value_store_collection.js +6 -5
  22. package/dist/resource_clients/log.d.ts +2 -0
  23. package/dist/resource_clients/log.js +10 -4
  24. package/dist/resource_clients/request_queue.js +37 -21
  25. package/dist/resource_clients/request_queue_collection.js +2 -2
  26. package/dist/resource_clients/run.js +21 -15
  27. package/dist/resource_clients/schedule.js +8 -7
  28. package/dist/resource_clients/schedule_collection.js +2 -2
  29. package/dist/resource_clients/task.js +15 -12
  30. package/dist/resource_clients/task_collection.js +2 -2
  31. package/dist/resource_clients/user.js +8 -5
  32. package/dist/resource_clients/webhook.js +8 -7
  33. package/dist/resource_clients/webhook_collection.js +2 -2
  34. package/dist/resource_clients/webhook_dispatch.js +2 -2
  35. package/dist/timeouts.d.ts +12 -0
  36. package/dist/timeouts.js +6 -0
  37. package/dist/utils.d.ts +5 -0
  38. package/dist/utils.js +21 -0
  39. package/package.json +2 -2
@@ -48,12 +48,13 @@ export class LogClient extends ResourceClient {
48
48
  * @see https://docs.apify.com/api/v2/log-get
49
49
  */
50
50
  async get(options = {}) {
51
- const { timeoutSecs = 'long', ...params } = parseArgument(options, logOptionsSchema, 'LogOptions');
51
+ const { timeoutSecs = 'long', signal, ...params } = parseArgument(options, logOptionsSchema, 'LogOptions');
52
52
  const requestOpts = {
53
53
  url: this.buildUrl(),
54
54
  method: 'GET',
55
55
  params: this.buildParams(params),
56
56
  timeoutSecs,
57
+ signal,
57
58
  };
58
59
  try {
59
60
  const response = await this.httpClient.call(requestOpts);
@@ -75,7 +76,7 @@ export class LogClient extends ResourceClient {
75
76
  * @see https://docs.apify.com/api/v2/log-get
76
77
  */
77
78
  async stream(options = {}) {
78
- const { timeoutSecs = 'long', raw } = parseArgument(options, logOptionsSchema, 'LogOptions');
79
+ const { timeoutSecs = 'long', signal, raw } = parseArgument(options, logOptionsSchema, 'LogOptions');
79
80
  const params = {
80
81
  stream: true,
81
82
  raw,
@@ -86,6 +87,7 @@ export class LogClient extends ResourceClient {
86
87
  params: this.buildParams(params),
87
88
  responseType: 'stream',
88
89
  timeoutSecs,
90
+ signal,
89
91
  };
90
92
  try {
91
93
  const response = await this.httpClient.call(requestOpts);
@@ -136,12 +138,14 @@ export class StreamedLog {
136
138
  #splitMarker = /(?:\n|^)(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)/g;
137
139
  #relevancyTimeLimit;
138
140
  #logClient;
141
+ #signal;
139
142
  #streamingTask = null;
140
143
  #stopLogging = false;
141
144
  constructor(options) {
142
- const { toLog, logClient, fromStart = true } = options;
145
+ const { toLog, logClient, fromStart = true, signal } = options;
143
146
  this.#destinationLog = toLog;
144
147
  this.#logClient = logClient;
148
+ this.#signal = signal;
145
149
  this.#relevancyTimeLimit = fromStart ? null : new Date();
146
150
  }
147
151
  /**
@@ -179,7 +183,7 @@ export class StreamedLog {
179
183
  */
180
184
  async #streamLog() {
181
185
  try {
182
- const logStream = await this.#logClient.stream({ raw: true });
186
+ const logStream = await this.#logClient.stream({ raw: true, signal: this.#signal });
183
187
  if (!logStream) {
184
188
  return;
185
189
  }
@@ -191,6 +195,8 @@ export class StreamedLog {
191
195
  }
192
196
  }
193
197
  catch (err) {
198
+ if (this.#signal?.aborted)
199
+ return;
194
200
  log.warning(`Log redirection stopped due to error`, err);
195
201
  }
196
202
  }
@@ -4,8 +4,8 @@ import log from '@apify/log';
4
4
  import { ResourceClient } from '../base/resource_client.js';
5
5
  import { ResponseValidationError } from '../response_validation_error.js';
6
6
  import * as schemas from '../schemas.js';
7
- import { optionalTimeoutSchema, timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
8
- import { anyObjectSchema, catchNotFoundOrThrow, isNonArrayObject, parseArgument, parseResponse, RequestQueuePaginationIterator, splitIntoJsonArrayBatches, utf8ByteLength, } from '../utils.js';
7
+ import { optionalSignalSchema, optionalTimeoutSchema, timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
8
+ import { anyObjectSchema, catchNotFoundOrThrow, isNonArrayObject, parseArgument, parseResponse, RequestQueuePaginationIterator, sleep, splitIntoJsonArrayBatches, utf8ByteLength, } from '../utils.js';
9
9
  const DEFAULT_PARALLEL_BATCH_ADD_REQUESTS = 5;
10
10
  const DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS = 3;
11
11
  const DEFAULT_MIN_DELAY_BETWEEN_UNPROCESSED_REQUESTS_RETRIES_MILLIS = 500;
@@ -115,8 +115,8 @@ export class RequestQueueClient extends ResourceClient {
115
115
  * @see https://docs.apify.com/api/v2/request-queue-get
116
116
  */
117
117
  async get(options = {}) {
118
- const { timeoutSecs } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
119
- return this.getResource(schemas.RequestQueue(), {}, this.#resolveTimeout(timeoutSecs, 'short'));
118
+ const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
119
+ return this.getResource(schemas.RequestQueue(), {}, this.#resolveTimeout(timeoutSecs, 'short'), signal);
120
120
  }
121
121
  /**
122
122
  * Updates the Request queue with specified fields.
@@ -129,8 +129,8 @@ export class RequestQueueClient extends ResourceClient {
129
129
  */
130
130
  async update(newFields, options = {}) {
131
131
  parseArgument(newFields, anyObjectSchema);
132
- const { timeoutSecs } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
133
- return this.updateResource(schemas.RequestQueue(), newFields, this.#resolveTimeout(timeoutSecs, 'short'));
132
+ const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
133
+ return this.updateResource(schemas.RequestQueue(), newFields, this.#resolveTimeout(timeoutSecs, 'short'), signal);
134
134
  }
135
135
  /**
136
136
  * Deletes the Request queue.
@@ -140,8 +140,8 @@ export class RequestQueueClient extends ResourceClient {
140
140
  * @see https://docs.apify.com/api/v2/request-queue-delete
141
141
  */
142
142
  async delete(options = {}) {
143
- const { timeoutSecs } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
144
- return this.deleteResource(this.#resolveTimeout(timeoutSecs, 'short'));
143
+ const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
144
+ return this.deleteResource(this.#resolveTimeout(timeoutSecs, 'short'), signal);
145
145
  }
146
146
  /**
147
147
  * Lists requests from the beginning of the queue (head).
@@ -161,6 +161,7 @@ export class RequestQueueClient extends ResourceClient {
161
161
  url: this.buildUrl('head'),
162
162
  method: 'GET',
163
163
  timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'short'),
164
+ signal: parsed.signal,
164
165
  params: this.buildParams({
165
166
  limit: parsed.limit,
166
167
  clientKey: this.#clientKey,
@@ -208,6 +209,7 @@ export class RequestQueueClient extends ResourceClient {
208
209
  url: this.buildUrl('head/lock'),
209
210
  method: 'POST',
210
211
  timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'medium'),
212
+ signal: parsed.signal,
211
213
  params: this.buildParams({
212
214
  limit: parsed.limit,
213
215
  lockSecs: parsed.lockSecs,
@@ -262,6 +264,7 @@ export class RequestQueueClient extends ResourceClient {
262
264
  url: this.buildUrl('requests'),
263
265
  method: 'POST',
264
266
  timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'short'),
267
+ signal: parsed.signal,
265
268
  data: request,
266
269
  params: this.buildParams({
267
270
  forefront: parsed.forefront,
@@ -281,6 +284,7 @@ export class RequestQueueClient extends ResourceClient {
281
284
  url: this.buildUrl('requests/batch'),
282
285
  method: 'POST',
283
286
  timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'medium'),
287
+ signal: parsed.signal,
284
288
  // The body is assembled from the requests as `batchAddRequests` serialized them; the explicit content type
285
289
  // makes the request interceptor send the string as it is instead of serializing the requests again.
286
290
  headers: { 'content-type': 'application/json' },
@@ -293,7 +297,7 @@ export class RequestQueueClient extends ResourceClient {
293
297
  return parseResponse(response, schemas.BatchAddResult());
294
298
  }
295
299
  async addRequestBatchWithRetries(requests, options = {}) {
296
- const { forefront, timeoutSecs, maxUnprocessedRequestsRetries = DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS, minDelayBetweenUnprocessedRequestsRetriesMillis = DEFAULT_MIN_DELAY_BETWEEN_UNPROCESSED_REQUESTS_RETRIES_MILLIS, } = options;
300
+ const { forefront, timeoutSecs, signal, maxUnprocessedRequestsRetries = DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS, minDelayBetweenUnprocessedRequestsRetriesMillis = DEFAULT_MIN_DELAY_BETWEEN_UNPROCESSED_REQUESTS_RETRIES_MILLIS, } = options;
297
301
  // Keep track of the requests that remain to be processed (in parameter format)
298
302
  let remainingRequests = requests;
299
303
  // Keep track of the requests that have been processed (in api format)
@@ -306,6 +310,7 @@ export class RequestQueueClient extends ResourceClient {
306
310
  const response = await this.addRequestBatch(remainingRequests, {
307
311
  forefront,
308
312
  timeoutSecs,
313
+ signal,
309
314
  });
310
315
  processedRequests.push(...response.processedRequests);
311
316
  unprocessedRequests = response.unprocessedRequests;
@@ -324,6 +329,8 @@ export class RequestQueueClient extends ResourceClient {
324
329
  }
325
330
  }
326
331
  catch (err) {
332
+ // An abort rejects the whole call, since the caller asked to stop and not for a partial result.
333
+ signal?.throwIfAborted();
327
334
  // A response the specification does not describe is reported, as everywhere else: the server may well
328
335
  // have added the batch, so calling it unprocessed would hide the mismatch behind a wrong answer.
329
336
  if (err instanceof ResponseValidationError)
@@ -339,9 +346,8 @@ export class RequestQueueClient extends ResourceClient {
339
346
  }
340
347
  // Exponential backoff
341
348
  const delayMillis = Math.floor((1 + Math.random()) * 2 ** i * minDelayBetweenUnprocessedRequestsRetriesMillis);
342
- await new Promise((resolve) => {
343
- setTimeout(resolve, delayMillis);
344
- });
349
+ await sleep(delayMillis, signal);
350
+ signal?.throwIfAborted();
345
351
  }
346
352
  return { processedRequests, unprocessedRequests };
347
353
  }
@@ -385,10 +391,11 @@ export class RequestQueueClient extends ResourceClient {
385
391
  * @since Added in 2.1.0
386
392
  */
387
393
  async batchAddRequests(requests, options = {}) {
388
- const { forefront, timeoutSecs, maxUnprocessedRequestsRetries = DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS, maxParallel = DEFAULT_PARALLEL_BATCH_ADD_REQUESTS, minDelayBetweenUnprocessedRequestsRetriesMillis = DEFAULT_MIN_DELAY_BETWEEN_UNPROCESSED_REQUESTS_RETRIES_MILLIS, } = options;
394
+ const { forefront, timeoutSecs, signal, maxUnprocessedRequestsRetries = DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS, maxParallel = DEFAULT_PARALLEL_BATCH_ADD_REQUESTS, minDelayBetweenUnprocessedRequestsRetriesMillis = DEFAULT_MIN_DELAY_BETWEEN_UNPROCESSED_REQUESTS_RETRIES_MILLIS, } = options;
389
395
  parseArgument(requests, batchAddRequestsWithRetriesSchema);
390
396
  parseArgument(forefront, optionalBooleanSchema);
391
397
  parseArgument(timeoutSecs, optionalTimeoutSchema);
398
+ parseArgument(signal, optionalSignalSchema);
392
399
  parseArgument(maxUnprocessedRequestsRetries, optionalNumberSchema);
393
400
  parseArgument(maxParallel, optionalNumberSchema);
394
401
  parseArgument(minDelayBetweenUnprocessedRequestsRetriesMillis, optionalNumberSchema);
@@ -452,11 +459,12 @@ export class RequestQueueClient extends ResourceClient {
452
459
  */
453
460
  async batchDeleteRequests(requests, options = {}) {
454
461
  parseArgument(requests, batchDeleteRequestsSchema);
455
- const { timeoutSecs } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
462
+ const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
456
463
  const response = await this.httpClient.call({
457
464
  url: this.buildUrl('requests/batch'),
458
465
  method: 'DELETE',
459
466
  timeoutSecs: this.#resolveTimeout(timeoutSecs, 'short'),
467
+ signal,
460
468
  data: requests,
461
469
  params: this.buildParams({
462
470
  clientKey: this.#clientKey,
@@ -475,11 +483,12 @@ export class RequestQueueClient extends ResourceClient {
475
483
  */
476
484
  async getRequest(id, options = {}) {
477
485
  parseArgument(id, requestIdSchema);
478
- const { timeoutSecs } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
486
+ const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
479
487
  const requestOpts = {
480
488
  url: this.buildUrl(['requests', id]),
481
489
  method: 'GET',
482
490
  timeoutSecs: this.#resolveTimeout(timeoutSecs, 'short'),
491
+ signal,
483
492
  params: this.buildParams(),
484
493
  };
485
494
  try {
@@ -508,6 +517,7 @@ export class RequestQueueClient extends ResourceClient {
508
517
  url: this.buildUrl(['requests', request.id]),
509
518
  method: 'PUT',
510
519
  timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'medium'),
520
+ signal: parsed.signal,
511
521
  data: request,
512
522
  params: this.buildParams({
513
523
  forefront: parsed.forefront,
@@ -525,11 +535,12 @@ export class RequestQueueClient extends ResourceClient {
525
535
  */
526
536
  async deleteRequest(id, options = {}) {
527
537
  parseArgument(id, requestIdSchema);
528
- const { timeoutSecs } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
538
+ const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
529
539
  await this.httpClient.call({
530
540
  url: this.buildUrl(['requests', id]),
531
541
  method: 'DELETE',
532
542
  timeoutSecs: this.#resolveTimeout(timeoutSecs, 'short'),
543
+ signal,
533
544
  params: this.buildParams({
534
545
  clientKey: this.#clientKey,
535
546
  }),
@@ -568,6 +579,7 @@ export class RequestQueueClient extends ResourceClient {
568
579
  url: this.buildUrl(['requests', id, 'lock']),
569
580
  method: 'PUT',
570
581
  timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'medium'),
582
+ signal: parsed.signal,
571
583
  params: this.buildParams({
572
584
  forefront: parsed.forefront,
573
585
  lockSecs: parsed.lockSecs,
@@ -596,6 +608,7 @@ export class RequestQueueClient extends ResourceClient {
596
608
  url: this.buildUrl(['requests', id, 'lock']),
597
609
  method: 'DELETE',
598
610
  timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'short'),
611
+ signal: parsed.signal,
599
612
  params: this.buildParams({
600
613
  forefront: parsed.forefront,
601
614
  clientKey: this.#clientKey,
@@ -615,14 +628,16 @@ export class RequestQueueClient extends ResourceClient {
615
628
  * @since Added in 2.5.1
616
629
  */
617
630
  listRequests(options = {}) {
618
- // `timeoutSecs` times every page request; it is not an API parameter, so it must not reach the query string.
619
- const { timeoutSecs, ...parsed } = parseArgument(options, listRequestsOptionsSchema, 'RequestQueueClientListRequestsOptions');
631
+ // `timeoutSecs` and `signal` apply to every page request; they are not API parameters, so they must not reach
632
+ // the query string.
633
+ const { timeoutSecs, signal, ...parsed } = parseArgument(options, listRequestsOptionsSchema, 'RequestQueueClientListRequestsOptions');
620
634
  const pageTimeout = this.#resolveTimeout(timeoutSecs, 'medium');
621
635
  const getPaginatedList = async (rqListOptions = {}) => {
622
636
  const response = await this.httpClient.call({
623
637
  url: this.buildUrl('requests'),
624
638
  method: 'GET',
625
639
  timeoutSecs: pageTimeout,
640
+ signal,
626
641
  params: this.buildParams({
627
642
  ...rqListOptions,
628
643
  filter: rqListOptions.filter ? rqListOptions.filter.join(',') : undefined,
@@ -671,11 +686,12 @@ export class RequestQueueClient extends ResourceClient {
671
686
  * @since Added in 2.12.5
672
687
  */
673
688
  async unlockRequests(options = {}) {
674
- const { timeoutSecs } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
689
+ const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
675
690
  const response = await this.httpClient.call({
676
691
  url: this.buildUrl('requests/unlock'),
677
692
  method: 'POST',
678
693
  timeoutSecs: this.#resolveTimeout(timeoutSecs, 'long'),
694
+ signal,
679
695
  params: this.buildParams({
680
696
  clientKey: this.#clientKey,
681
697
  }),
@@ -702,9 +718,9 @@ export class RequestQueueClient extends ResourceClient {
702
718
  * @since Added in 2.5.1
703
719
  */
704
720
  paginateRequests(options = {}) {
705
- const { limit, cursor, filter, maxPageLimit, timeoutSecs } = parseArgument(options, paginateRequestsOptionsSchema, 'RequestQueueClientPaginateRequestsOptions');
721
+ const { limit, cursor, filter, maxPageLimit, timeoutSecs, signal } = parseArgument(options, paginateRequestsOptionsSchema, 'RequestQueueClientPaginateRequestsOptions');
706
722
  return new RequestQueuePaginationIterator({
707
- getPage: async (pageOptions) => this.listRequests({ ...pageOptions, filter, timeoutSecs }),
723
+ getPage: async (pageOptions) => this.listRequests({ ...pageOptions, filter, timeoutSecs, signal }),
708
724
  limit,
709
725
  cursor,
710
726
  maxPageLimit,
@@ -78,7 +78,7 @@ export class RequestQueueCollectionClient extends ResourceCollectionClient {
78
78
  */
79
79
  async getOrCreate(name, options = {}) {
80
80
  parseArgument(name, nameSchema);
81
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
82
- return this.getOrCreateResource(schemas.RequestQueue(), name, undefined, timeoutSecs);
81
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
82
+ return this.getOrCreateResource(schemas.RequestQueue(), name, undefined, timeoutSecs, signal);
83
83
  }
84
84
  }
@@ -2,7 +2,7 @@ import { z } from 'zod';
2
2
  import { LEVELS, Log } from '@apify/log';
3
3
  import { ResourceClient } from '../base/resource_client.js';
4
4
  import * as schemas from '../schemas.js';
5
- import { optionalTimeoutSchema, timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
5
+ import { optionalSignalSchema, optionalTimeoutSchema, timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
6
6
  import { runtime } from '#runtime';
7
7
  import { anyObjectSchema, parseArgument, parseResponse } from '../utils.js';
8
8
  import { DatasetClient } from './dataset.js';
@@ -88,8 +88,8 @@ export class RunClient extends ResourceClient {
88
88
  * ```
89
89
  */
90
90
  async get(options = {}) {
91
- const { timeoutSecs, ...params } = parseArgument(options, getOptionsSchema, 'RunGetOptions');
92
- return this.getResource(schemas.Run(), params, this.timeoutForWaitForFinish(timeoutSecs, 'short', params.waitForFinish));
91
+ const { timeoutSecs, signal, ...params } = parseArgument(options, getOptionsSchema, 'RunGetOptions');
92
+ return this.getResource(schemas.Run(), params, this.timeoutForWaitForFinish(timeoutSecs, 'short', params.waitForFinish), signal);
93
93
  }
94
94
  /**
95
95
  * Aborts the Actor run.
@@ -110,12 +110,13 @@ export class RunClient extends ResourceClient {
110
110
  * ```
111
111
  */
112
112
  async abort(options = {}) {
113
- const { timeoutSecs = 'medium', ...params } = parseArgument(options, abortOptionsSchema, 'RunAbortOptions');
113
+ const { timeoutSecs = 'medium', signal, ...params } = parseArgument(options, abortOptionsSchema, 'RunAbortOptions');
114
114
  const response = await this.httpClient.call({
115
115
  url: this.buildUrl('abort'),
116
116
  method: 'POST',
117
117
  params: this.buildParams(params),
118
118
  timeoutSecs,
119
+ signal,
119
120
  });
120
121
  return parseResponse(response, schemas.Run());
121
122
  }
@@ -128,8 +129,8 @@ export class RunClient extends ResourceClient {
128
129
  * @since Added in 2.8.1
129
130
  */
130
131
  async delete(options = {}) {
131
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
132
- return this.deleteResource(timeoutSecs);
132
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
133
+ return this.deleteResource(timeoutSecs, signal);
133
134
  }
134
135
  /**
135
136
  * Transforms the Actor run into a run of another Actor (metamorph).
@@ -172,6 +173,7 @@ export class RunClient extends ResourceClient {
172
173
  // Actor input may carry page functions, which plain JSON serialization would drop.
173
174
  stringifyFunctions: true,
174
175
  timeoutSecs: parsed.timeoutSecs ?? 'medium',
176
+ signal: parsed.signal,
175
177
  };
176
178
  if (parsed.contentType) {
177
179
  request.headers = {
@@ -200,11 +202,12 @@ export class RunClient extends ResourceClient {
200
202
  * @since Added in 2.8.0
201
203
  */
202
204
  async reboot(options = {}) {
203
- const { timeoutSecs = 'medium' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
205
+ const { timeoutSecs = 'medium', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
204
206
  const request = {
205
207
  url: this.buildUrl('reboot'),
206
208
  method: 'POST',
207
209
  timeoutSecs,
210
+ signal,
208
211
  };
209
212
  const response = await this.httpClient.call(request);
210
213
  return parseResponse(response, schemas.Run());
@@ -231,8 +234,8 @@ export class RunClient extends ResourceClient {
231
234
  */
232
235
  async update(newFields, options = {}) {
233
236
  parseArgument(newFields, anyObjectSchema);
234
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
235
- return this.updateResource(schemas.Run(), newFields, timeoutSecs);
237
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
238
+ return this.updateResource(schemas.Run(), newFields, timeoutSecs, signal);
236
239
  }
237
240
  /**
238
241
  * Resurrects a finished Actor run, starting it again with the same settings.
@@ -259,13 +262,14 @@ export class RunClient extends ResourceClient {
259
262
  * ```
260
263
  */
261
264
  async resurrect(options = {}) {
262
- const { timeoutSecs = 'medium', runTimeoutSecs, ...params } = parseArgument(options, resurrectOptionsSchema, 'RunResurrectOptions');
265
+ const { timeoutSecs = 'medium', signal, runTimeoutSecs, ...params } = parseArgument(options, resurrectOptionsSchema, 'RunResurrectOptions');
263
266
  const response = await this.httpClient.call({
264
267
  url: this.buildUrl('resurrect'),
265
268
  method: 'POST',
266
269
  // The API's `timeout` parameter bounds the run, not the request.
267
270
  params: this.buildParams({ ...params, timeout: runTimeoutSecs }),
268
271
  timeoutSecs,
272
+ signal,
269
273
  });
270
274
  return parseResponse(response, schemas.Run());
271
275
  }
@@ -282,7 +286,7 @@ export class RunClient extends ResourceClient {
282
286
  * @since Added in 2.11.0
283
287
  */
284
288
  async charge(options) {
285
- const { eventName, count, idempotencyKey: providedIdempotencyKey, timeoutSecs = 'short', } = parseArgument(options, chargeOptionsSchema, 'RunChargeOptions');
289
+ const { eventName, count, idempotencyKey: providedIdempotencyKey, timeoutSecs = 'short', signal, } = parseArgument(options, chargeOptionsSchema, 'RunChargeOptions');
286
290
  /** To avoid duplicates during the same milisecond, doesn't need to by crypto-secure. */
287
291
  const randomSuffix = (Math.random() + 1).toString(36).slice(3, 8);
288
292
  const idempotencyKey = providedIdempotencyKey ?? `${this.id}-${eventName}-${Date.now()}-${randomSuffix}`;
@@ -297,6 +301,7 @@ export class RunClient extends ResourceClient {
297
301
  [RUN_CHARGE_IDEMPOTENCY_HEADER]: idempotencyKey,
298
302
  },
299
303
  timeoutSecs,
304
+ signal,
300
305
  };
301
306
  const response = await this.httpClient.call(request);
302
307
  return response;
@@ -424,7 +429,8 @@ export class RunClient extends ResourceClient {
424
429
  */
425
430
  async getStreamedLog(options = {}) {
426
431
  parseArgument(options.timeoutSecs, optionalTimeoutSchema);
427
- const { fromStart = true, timeoutSecs = 'long' } = options;
432
+ parseArgument(options.signal, optionalSignalSchema);
433
+ const { fromStart = true, timeoutSecs = 'long', signal } = options;
428
434
  let { toLog } = options;
429
435
  if (toLog === null || !runtime.isNode) {
430
436
  // Explicitly no logging or not in Node.js
@@ -433,15 +439,15 @@ export class RunClient extends ResourceClient {
433
439
  if (toLog === undefined || toLog === 'default') {
434
440
  // Create default StreamedLog
435
441
  // Get actor name and run id
436
- const runData = await this.get({ timeoutSecs });
442
+ const runData = await this.get({ timeoutSecs, signal });
437
443
  const runId = runData?.id ?? '';
438
444
  const actorId = runData?.actId ?? '';
439
445
  // `apifyClient.actor()` rejects an empty ID, which is what a run that could not be read leaves here.
440
- const actorData = actorId ? await this.apifyClient.actor(actorId).get({ timeoutSecs }) : undefined;
446
+ const actorData = actorId ? await this.apifyClient.actor(actorId).get({ timeoutSecs, signal }) : undefined;
441
447
  const actorName = actorData?.name ?? '';
442
448
  const name = [actorName, `runId:${runId}`].filter(Boolean).join(' ');
443
449
  toLog = new Log({ level: LEVELS.DEBUG, prefix: `${name} -> `, logger: new LoggerActorRedirect() });
444
450
  }
445
- return new StreamedLog({ logClient: this.log(), toLog, fromStart });
451
+ return new StreamedLog({ logClient: this.log(), toLog, fromStart, signal });
446
452
  }
447
453
  }
@@ -47,8 +47,8 @@ export class ScheduleClient extends ResourceClient {
47
47
  * @see https://docs.apify.com/api/v2/schedule-get
48
48
  */
49
49
  async get(options = {}) {
50
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
51
- return this.getResource(schemas.Schedule(), {}, timeoutSecs);
50
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
51
+ return this.getResource(schemas.Schedule(), {}, timeoutSecs, signal);
52
52
  }
53
53
  /**
54
54
  * Updates the schedule with the specified fields.
@@ -61,8 +61,8 @@ export class ScheduleClient extends ResourceClient {
61
61
  */
62
62
  async update(newFields, options = {}) {
63
63
  parseArgument(newFields, anyObjectSchema);
64
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
65
- return this.updateResource(schemas.Schedule(), newFields, timeoutSecs);
64
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
65
+ return this.updateResource(schemas.Schedule(), newFields, timeoutSecs, signal);
66
66
  }
67
67
  /**
68
68
  * Deletes the schedule.
@@ -72,8 +72,8 @@ export class ScheduleClient extends ResourceClient {
72
72
  * @see https://docs.apify.com/api/v2/schedule-delete
73
73
  */
74
74
  async delete(options = {}) {
75
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
76
- return this.deleteResource(timeoutSecs);
75
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
76
+ return this.deleteResource(timeoutSecs, signal);
77
77
  }
78
78
  /**
79
79
  * Retrieves the schedule's log.
@@ -84,12 +84,13 @@ export class ScheduleClient extends ResourceClient {
84
84
  * @see https://docs.apify.com/api/v2/schedule-log-get
85
85
  */
86
86
  async getLog(options = {}) {
87
- const { timeoutSecs = 'medium' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
87
+ const { timeoutSecs = 'medium', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
88
88
  const response = await this.httpClient.call({
89
89
  url: this.buildUrl('log'),
90
90
  method: 'GET',
91
91
  params: this.buildParams(),
92
92
  timeoutSecs,
93
+ signal,
93
94
  });
94
95
  return parseResponse(response, scheduleLogSchema);
95
96
  }
@@ -79,7 +79,7 @@ export class ScheduleCollectionClient extends ResourceCollectionClient {
79
79
  */
80
80
  async create(schedule, options = {}) {
81
81
  parseArgument(schedule, scheduleCreateSchema);
82
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
83
- return this.createResource(schemas.Schedule(), schedule, timeoutSecs);
82
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
83
+ return this.createResource(schemas.Schedule(), schedule, timeoutSecs, signal);
84
84
  }
85
85
  }
@@ -73,8 +73,8 @@ export class TaskClient extends ResourceClient {
73
73
  * @see https://docs.apify.com/api/v2/actor-task-get
74
74
  */
75
75
  async get(options = {}) {
76
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
77
- return this.getResource(schemas.Task(), {}, timeoutSecs);
76
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
77
+ return this.getResource(schemas.Task(), {}, timeoutSecs, signal);
78
78
  }
79
79
  /**
80
80
  * Updates the task with the specified fields.
@@ -87,8 +87,8 @@ export class TaskClient extends ResourceClient {
87
87
  */
88
88
  async update(newFields, options = {}) {
89
89
  parseArgument(newFields, anyObjectSchema);
90
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
91
- return this.updateResource(schemas.Task(), newFields, timeoutSecs);
90
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
91
+ return this.updateResource(schemas.Task(), newFields, timeoutSecs, signal);
92
92
  }
93
93
  /**
94
94
  * Publishes the task on its public landing page, by setting `isPublic` through
@@ -132,8 +132,8 @@ export class TaskClient extends ResourceClient {
132
132
  * @see https://docs.apify.com/api/v2/actor-task-delete
133
133
  */
134
134
  async delete(options = {}) {
135
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
136
- return this.deleteResource(timeoutSecs);
135
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
136
+ return this.deleteResource(timeoutSecs, signal);
137
137
  }
138
138
  /**
139
139
  * Starts an Actor task and immediately returns the Run object.
@@ -156,7 +156,7 @@ export class TaskClient extends ResourceClient {
156
156
  async start(input, options = {}) {
157
157
  parseArgument(input, inputSchema);
158
158
  const parsed = parseArgument(options, startOptionsSchema, 'TaskStartOptions');
159
- const { waitForFinish, runTimeoutSecs, memory, build, maxItems, maxTotalChargeUsd, restartOnError, timeoutSecs, } = parsed;
159
+ const { waitForFinish, runTimeoutSecs, memory, build, maxItems, maxTotalChargeUsd, restartOnError, timeoutSecs, signal, } = parsed;
160
160
  // The API's `timeout` parameter bounds the run, not the request.
161
161
  const params = {
162
162
  waitForFinish,
@@ -179,6 +179,7 @@ export class TaskClient extends ResourceClient {
179
179
  'Content-Type': 'application/json',
180
180
  },
181
181
  timeoutSecs: this.timeoutForWaitForFinish(timeoutSecs, 'medium', waitForFinish),
182
+ signal,
182
183
  };
183
184
  const response = await this.httpClient.call(request);
184
185
  return parseResponse(response, schemas.Run());
@@ -204,12 +205,12 @@ export class TaskClient extends ResourceClient {
204
205
  async call(input, options = {}) {
205
206
  parseArgument(input, inputSchema);
206
207
  const parsed = parseArgument(options, callOptionsSchema, 'TaskCallOptions');
207
- const { waitSecs, timeoutSecs = 'noTimeout', ...startOptions } = parsed;
208
- const { id } = await this.start(input, { ...startOptions, timeoutSecs });
208
+ const { waitSecs, timeoutSecs = 'noTimeout', signal, ...startOptions } = parsed;
209
+ const { id } = await this.start(input, { ...startOptions, timeoutSecs, signal });
209
210
  // Calling root client because we need access to top level API.
210
211
  // Creating a new instance of RunClient here would only allow
211
212
  // setting it up as a nested route under task API.
212
- return this.apifyClient.run(id).waitForFinish({ waitSecs, timeoutSecs });
213
+ return this.apifyClient.run(id).waitForFinish({ waitSecs, timeoutSecs, signal });
213
214
  }
214
215
  /**
215
216
  * Retrieves the Actor task's input object.
@@ -220,12 +221,13 @@ export class TaskClient extends ResourceClient {
220
221
  * @see https://docs.apify.com/api/v2/actor-task-input-get
221
222
  */
222
223
  async getInput(options = {}) {
223
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
224
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
224
225
  const response = await this.httpClient.call({
225
226
  url: this.buildUrl('input'),
226
227
  method: 'GET',
227
228
  params: this.buildParams(),
228
229
  timeoutSecs,
230
+ signal,
229
231
  });
230
232
  return cast(response.data);
231
233
  }
@@ -239,13 +241,14 @@ export class TaskClient extends ResourceClient {
239
241
  * @see https://docs.apify.com/api/v2/actor-task-input-put
240
242
  */
241
243
  async updateInput(newFields, options = {}) {
242
- const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
244
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
243
245
  const response = await this.httpClient.call({
244
246
  url: this.buildUrl('input'),
245
247
  method: 'PUT',
246
248
  params: this.buildParams(),
247
249
  data: newFields,
248
250
  timeoutSecs,
251
+ signal,
249
252
  });
250
253
  return cast(response.data);
251
254
  }
@@ -78,7 +78,7 @@ export class TaskCollectionClient extends ResourceCollectionClient {
78
78
  */
79
79
  async create(task, options = {}) {
80
80
  parseArgument(task, anyObjectSchema);
81
- const { timeoutSecs = 'medium' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
82
- return this.createResource(schemas.Task(), task, timeoutSecs);
81
+ const { timeoutSecs = 'medium', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
82
+ return this.createResource(schemas.Task(), task, timeoutSecs, signal);
83
83
  }
84
84
  }