apify-client 3.0.0-beta.15 → 3.0.0-beta.16
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.
- package/dist/apify_api_error.js +23 -7
- package/dist/apify_client.d.ts +22 -3
- package/dist/apify_client.js +35 -29
- package/dist/base/api_client.d.ts +6 -6
- package/dist/base/api_client.js +11 -10
- package/dist/base/resource_client.d.ts +13 -8
- package/dist/base/resource_client.js +35 -19
- package/dist/base/resource_collection_client.d.ts +6 -4
- package/dist/base/resource_collection_client.js +17 -11
- package/dist/bundle.js +13 -13
- package/dist/bundle.js.map +1 -1
- package/dist/http_client.d.ts +34 -13
- package/dist/http_client.js +58 -28
- package/dist/index.d.ts +1 -0
- package/dist/resource_clients/actor.d.ts +26 -11
- package/dist/resource_clients/actor.js +62 -33
- package/dist/resource_clients/actor_collection.d.ts +6 -2
- package/dist/resource_clients/actor_collection.js +9 -3
- package/dist/resource_clients/actor_env_var.d.ts +10 -3
- package/dist/resource_clients/actor_env_var.js +16 -6
- package/dist/resource_clients/actor_env_var_collection.d.ts +7 -2
- package/dist/resource_clients/actor_env_var_collection.js +11 -4
- package/dist/resource_clients/actor_version.d.ts +10 -3
- package/dist/resource_clients/actor_version.js +18 -8
- package/dist/resource_clients/actor_version_collection.d.ts +7 -2
- package/dist/resource_clients/actor_version_collection.js +11 -4
- package/dist/resource_clients/build.d.ts +16 -6
- package/dist/resource_clients/build.js +30 -15
- package/dist/resource_clients/build_collection.d.ts +3 -1
- package/dist/resource_clients/build_collection.js +4 -1
- package/dist/resource_clients/dataset.d.ts +21 -7
- package/dist/resource_clients/dataset.js +56 -34
- package/dist/resource_clients/dataset_collection.d.ts +6 -2
- package/dist/resource_clients/dataset_collection.js +12 -2
- package/dist/resource_clients/key_value_store.d.ts +31 -11
- package/dist/resource_clients/key_value_store.js +58 -34
- package/dist/resource_clients/key_value_store_collection.d.ts +6 -2
- package/dist/resource_clients/key_value_store_collection.js +12 -2
- package/dist/resource_clients/log.d.ts +4 -1
- package/dist/resource_clients/log.js +15 -6
- package/dist/resource_clients/request_queue.d.ts +52 -18
- package/dist/resource_clients/request_queue.js +114 -60
- package/dist/resource_clients/request_queue_collection.d.ts +6 -2
- package/dist/resource_clients/request_queue_collection.js +9 -3
- package/dist/resource_clients/run.d.ts +33 -13
- package/dist/resource_clients/run.js +65 -35
- package/dist/resource_clients/run_collection.d.ts +3 -1
- package/dist/resource_clients/run_collection.js +4 -1
- package/dist/resource_clients/schedule.d.ts +13 -4
- package/dist/resource_clients/schedule.js +23 -9
- package/dist/resource_clients/schedule_collection.d.ts +6 -2
- package/dist/resource_clients/schedule_collection.js +9 -3
- package/dist/resource_clients/store_collection.d.ts +3 -1
- package/dist/resource_clients/store_collection.js +4 -1
- package/dist/resource_clients/task.d.ts +27 -9
- package/dist/resource_clients/task.js +60 -31
- package/dist/resource_clients/task_collection.d.ts +6 -2
- package/dist/resource_clients/task_collection.js +9 -3
- package/dist/resource_clients/user.d.ts +14 -5
- package/dist/resource_clients/user.js +30 -14
- package/dist/resource_clients/webhook.d.ts +13 -4
- package/dist/resource_clients/webhook.js +24 -10
- package/dist/resource_clients/webhook_collection.d.ts +6 -2
- package/dist/resource_clients/webhook_collection.js +9 -3
- package/dist/resource_clients/webhook_dispatch.d.ts +4 -1
- package/dist/resource_clients/webhook_dispatch.js +7 -2
- package/dist/resource_clients/webhook_dispatch_collection.d.ts +3 -1
- package/dist/resource_clients/webhook_dispatch_collection.js +4 -1
- package/dist/timeouts.d.ts +72 -0
- package/dist/timeouts.js +31 -0
- package/package.json +1 -1
package/dist/apify_api_error.js
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
import { isomorphicBufferToString } from './body_parser.js';
|
|
2
2
|
import { isBuffer } from './utils.js';
|
|
3
3
|
/**
|
|
4
|
-
* Examples of capturing groups for "...at ActorCollectionClient.
|
|
5
|
-
* 0: "at ActorCollectionClient.
|
|
4
|
+
* Examples of capturing groups for "...at ActorCollectionClient.listResources (/Users/..."
|
|
5
|
+
* 0: "at ActorCollectionClient.listResources ("
|
|
6
6
|
* 1: undefined
|
|
7
7
|
* 2: "ActorCollectionClient"
|
|
8
8
|
* 3: undefined
|
|
9
|
-
* 4: "
|
|
9
|
+
* 4: "listResources"
|
|
10
10
|
* @private
|
|
11
11
|
*/
|
|
12
|
-
const CLIENT_METHOD_REGEX = /at( async)? ([A-Za-z]+(Collection)?Client)\.
|
|
12
|
+
const CLIENT_METHOD_REGEX = /at( async)? ([A-Za-z]+(Collection)?Client)\.([A-Za-z]+) \(/;
|
|
13
|
+
/**
|
|
14
|
+
* A public method that returns the promise of a shared helper without awaiting it leaves only the helper on
|
|
15
|
+
* the stack, so the helper is reported under the method the caller invoked.
|
|
16
|
+
* @private
|
|
17
|
+
*/
|
|
18
|
+
const PUBLIC_METHOD_BY_HELPER = {
|
|
19
|
+
getResource: 'get',
|
|
20
|
+
updateResource: 'update',
|
|
21
|
+
deleteResource: 'delete',
|
|
22
|
+
waitForJobFinish: 'waitForFinish',
|
|
23
|
+
listResources: 'list',
|
|
24
|
+
createResource: 'create',
|
|
25
|
+
getOrCreateResource: 'getOrCreate',
|
|
26
|
+
addRequestBatch: 'batchAddRequests',
|
|
27
|
+
addRequestBatchWithRetries: 'batchAddRequests',
|
|
28
|
+
};
|
|
13
29
|
/**
|
|
14
30
|
* An `ApifyApiError` is thrown for successful HTTP requests that reach the API,
|
|
15
31
|
* but the API responds with an error response. Typically, those are rate limit
|
|
@@ -129,9 +145,9 @@ export class ApifyApiError extends Error {
|
|
|
129
145
|
}
|
|
130
146
|
_extractClientAndMethodFromStack() {
|
|
131
147
|
const match = this.stack.match(CLIENT_METHOD_REGEX);
|
|
132
|
-
if (match)
|
|
133
|
-
return
|
|
134
|
-
return
|
|
148
|
+
if (!match)
|
|
149
|
+
return 'unknown';
|
|
150
|
+
return `${match[2]}.${PUBLIC_METHOD_BY_HELPER[match[4]] ?? match[4]}`;
|
|
135
151
|
}
|
|
136
152
|
/**
|
|
137
153
|
* Creates a better looking and more informative stack that will be printed
|
package/dist/apify_client.d.ts
CHANGED
|
@@ -59,7 +59,7 @@ export declare class ApifyClient {
|
|
|
59
59
|
logger: Log;
|
|
60
60
|
httpClient: HttpClient;
|
|
61
61
|
constructor(options?: ApifyClientOptions);
|
|
62
|
-
private
|
|
62
|
+
private subClientOptions;
|
|
63
63
|
/**
|
|
64
64
|
* Returns a client for managing Actors in your account.
|
|
65
65
|
*
|
|
@@ -364,8 +364,27 @@ export interface ApifyClientOptions {
|
|
|
364
364
|
minDelayBetweenRetriesMillis?: number;
|
|
365
365
|
/** @default [] */
|
|
366
366
|
requestInterceptors?: RequestInterceptorFunction[];
|
|
367
|
-
/**
|
|
368
|
-
|
|
367
|
+
/**
|
|
368
|
+
* Duration of the `short` timeout tier, in seconds: simple metadata reads and writes.
|
|
369
|
+
* @default 5
|
|
370
|
+
*/
|
|
371
|
+
timeoutShortSecs?: number;
|
|
372
|
+
/**
|
|
373
|
+
* Duration of the `medium` timeout tier, in seconds: listing, batch and trigger operations.
|
|
374
|
+
* @default 30
|
|
375
|
+
*/
|
|
376
|
+
timeoutMediumSecs?: number;
|
|
377
|
+
/**
|
|
378
|
+
* Duration of the `long` timeout tier, in seconds: downloads, uploads and streaming.
|
|
379
|
+
* @default 360
|
|
380
|
+
*/
|
|
381
|
+
timeoutLongSecs?: number;
|
|
382
|
+
/**
|
|
383
|
+
* Cap on the timeout of a single request attempt, in seconds. It bounds the doubling of the timeout across
|
|
384
|
+
* retries, and caps tier and per-call timeouts alike, so raise it to allow a request timeout above 360 s.
|
|
385
|
+
* @default 360
|
|
386
|
+
*/
|
|
387
|
+
timeoutMaxSecs?: number;
|
|
369
388
|
token?: string;
|
|
370
389
|
/**
|
|
371
390
|
* @since Added in 2.10.0
|
package/dist/apify_client.js
CHANGED
|
@@ -26,22 +26,25 @@ import { WebhookCollectionClient } from './resource_clients/webhook_collection.j
|
|
|
26
26
|
import { WebhookDispatchClient } from './resource_clients/webhook_dispatch.js';
|
|
27
27
|
import { WebhookDispatchCollectionClient } from './resource_clients/webhook_dispatch_collection.js';
|
|
28
28
|
import { Statistics } from './statistics.js';
|
|
29
|
+
import { DEFAULT_TIMEOUT_LONG_SECS, DEFAULT_TIMEOUT_MAX_SECS, DEFAULT_TIMEOUT_MEDIUM_SECS, DEFAULT_TIMEOUT_SHORT_SECS, } from './timeouts.js';
|
|
29
30
|
import { parseArgument } from './utils.js';
|
|
30
|
-
const DEFAULT_TIMEOUT_SECS = 360;
|
|
31
31
|
const clientOptionsSchema = z.strictObject({
|
|
32
32
|
baseUrl: z.string().default('https://api.apify.com'),
|
|
33
33
|
publicBaseUrl: z.string().default('https://api.apify.com'),
|
|
34
34
|
maxRetries: z.number().default(8),
|
|
35
35
|
minDelayBetweenRetriesMillis: z.number().default(500),
|
|
36
36
|
requestInterceptors: z.array(z.unknown()).default([]),
|
|
37
|
-
|
|
37
|
+
timeoutShortSecs: z.number().positive().default(DEFAULT_TIMEOUT_SHORT_SECS),
|
|
38
|
+
timeoutMediumSecs: z.number().positive().default(DEFAULT_TIMEOUT_MEDIUM_SECS),
|
|
39
|
+
timeoutLongSecs: z.number().positive().default(DEFAULT_TIMEOUT_LONG_SECS),
|
|
40
|
+
timeoutMaxSecs: z.number().positive().default(DEFAULT_TIMEOUT_MAX_SECS),
|
|
38
41
|
token: z.string().optional(),
|
|
39
42
|
userAgentSuffix: z.union([z.string(), z.array(z.string())]).optional(),
|
|
40
43
|
});
|
|
41
44
|
const resourceIdSchema = z.string().min(1);
|
|
42
45
|
const requestQueueOptionsSchema = z.strictObject({
|
|
43
46
|
clientKey: z.string().min(1).optional(),
|
|
44
|
-
timeoutSecs: z.number().optional(),
|
|
47
|
+
timeoutSecs: z.number().positive().optional(),
|
|
45
48
|
});
|
|
46
49
|
/**
|
|
47
50
|
* The official JavaScript client for the Apify API.
|
|
@@ -76,7 +79,7 @@ export class ApifyClient {
|
|
|
76
79
|
httpClient;
|
|
77
80
|
constructor(options = {}) {
|
|
78
81
|
const parsed = parseArgument(options, clientOptionsSchema, 'ApifyClientOptions');
|
|
79
|
-
const { baseUrl, publicBaseUrl, maxRetries, minDelayBetweenRetriesMillis, requestInterceptors,
|
|
82
|
+
const { baseUrl, publicBaseUrl, maxRetries, minDelayBetweenRetriesMillis, requestInterceptors, timeoutShortSecs, timeoutMediumSecs, timeoutLongSecs, timeoutMaxSecs, token, } = parsed;
|
|
80
83
|
const tempBaseUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, baseUrl.length - 1) : baseUrl;
|
|
81
84
|
this.baseUrl = `${tempBaseUrl}/v2`;
|
|
82
85
|
const tempPublicBaseUrl = publicBaseUrl.endsWith('/')
|
|
@@ -91,13 +94,16 @@ export class ApifyClient {
|
|
|
91
94
|
maxRetries,
|
|
92
95
|
minDelayBetweenRetriesMillis,
|
|
93
96
|
requestInterceptors,
|
|
94
|
-
|
|
97
|
+
timeoutShortSecs,
|
|
98
|
+
timeoutMediumSecs,
|
|
99
|
+
timeoutLongSecs,
|
|
100
|
+
timeoutMaxSecs,
|
|
95
101
|
logger: this.logger,
|
|
96
102
|
token: this.token,
|
|
97
103
|
userAgentSuffix: parsed.userAgentSuffix,
|
|
98
104
|
});
|
|
99
105
|
}
|
|
100
|
-
|
|
106
|
+
subClientOptions() {
|
|
101
107
|
return {
|
|
102
108
|
baseUrl: this.baseUrl,
|
|
103
109
|
publicBaseUrl: this.publicBaseUrl,
|
|
@@ -114,7 +120,7 @@ export class ApifyClient {
|
|
|
114
120
|
* @see https://docs.apify.com/api/v2/acts-get
|
|
115
121
|
*/
|
|
116
122
|
actors() {
|
|
117
|
-
return new ActorCollectionClient(this.
|
|
123
|
+
return new ActorCollectionClient(this.subClientOptions());
|
|
118
124
|
}
|
|
119
125
|
/**
|
|
120
126
|
* Returns a client for a specific Actor.
|
|
@@ -136,7 +142,7 @@ export class ApifyClient {
|
|
|
136
142
|
parseArgument(id, resourceIdSchema);
|
|
137
143
|
return new ActorClient({
|
|
138
144
|
id,
|
|
139
|
-
...this.
|
|
145
|
+
...this.subClientOptions(),
|
|
140
146
|
});
|
|
141
147
|
}
|
|
142
148
|
/**
|
|
@@ -148,7 +154,7 @@ export class ApifyClient {
|
|
|
148
154
|
* @see https://docs.apify.com/api/v2/actor-builds-get
|
|
149
155
|
*/
|
|
150
156
|
builds() {
|
|
151
|
-
return new BuildCollectionClient(this.
|
|
157
|
+
return new BuildCollectionClient(this.subClientOptions());
|
|
152
158
|
}
|
|
153
159
|
/**
|
|
154
160
|
* Returns a client for a specific Actor build.
|
|
@@ -163,7 +169,7 @@ export class ApifyClient {
|
|
|
163
169
|
parseArgument(id, resourceIdSchema);
|
|
164
170
|
return new BuildClient({
|
|
165
171
|
id,
|
|
166
|
-
...this.
|
|
172
|
+
...this.subClientOptions(),
|
|
167
173
|
});
|
|
168
174
|
}
|
|
169
175
|
/**
|
|
@@ -175,7 +181,7 @@ export class ApifyClient {
|
|
|
175
181
|
* @see https://docs.apify.com/api/v2/datasets-get
|
|
176
182
|
*/
|
|
177
183
|
datasets() {
|
|
178
|
-
return new DatasetCollectionClient(this.
|
|
184
|
+
return new DatasetCollectionClient(this.subClientOptions());
|
|
179
185
|
}
|
|
180
186
|
/**
|
|
181
187
|
* Returns a client for a specific dataset.
|
|
@@ -204,7 +210,7 @@ export class ApifyClient {
|
|
|
204
210
|
parseArgument(id, resourceIdSchema);
|
|
205
211
|
return new DatasetClient({
|
|
206
212
|
id,
|
|
207
|
-
...this.
|
|
213
|
+
...this.subClientOptions(),
|
|
208
214
|
});
|
|
209
215
|
}
|
|
210
216
|
/**
|
|
@@ -216,7 +222,7 @@ export class ApifyClient {
|
|
|
216
222
|
* @see https://docs.apify.com/api/v2/key-value-stores-get
|
|
217
223
|
*/
|
|
218
224
|
keyValueStores() {
|
|
219
|
-
return new KeyValueStoreCollectionClient(this.
|
|
225
|
+
return new KeyValueStoreCollectionClient(this.subClientOptions());
|
|
220
226
|
}
|
|
221
227
|
/**
|
|
222
228
|
* Returns a client for a specific key-value store.
|
|
@@ -241,7 +247,7 @@ export class ApifyClient {
|
|
|
241
247
|
parseArgument(id, resourceIdSchema);
|
|
242
248
|
return new KeyValueStoreClient({
|
|
243
249
|
id,
|
|
244
|
-
...this.
|
|
250
|
+
...this.subClientOptions(),
|
|
245
251
|
});
|
|
246
252
|
}
|
|
247
253
|
/**
|
|
@@ -255,7 +261,7 @@ export class ApifyClient {
|
|
|
255
261
|
parseArgument(buildOrRunId, resourceIdSchema);
|
|
256
262
|
return new LogClient({
|
|
257
263
|
id: buildOrRunId,
|
|
258
|
-
...this.
|
|
264
|
+
...this.subClientOptions(),
|
|
259
265
|
});
|
|
260
266
|
}
|
|
261
267
|
/**
|
|
@@ -267,7 +273,7 @@ export class ApifyClient {
|
|
|
267
273
|
* @see https://docs.apify.com/api/v2/request-queues-get
|
|
268
274
|
*/
|
|
269
275
|
requestQueues() {
|
|
270
|
-
return new RequestQueueCollectionClient(this.
|
|
276
|
+
return new RequestQueueCollectionClient(this.subClientOptions());
|
|
271
277
|
}
|
|
272
278
|
/**
|
|
273
279
|
* Returns a client for a specific request queue.
|
|
@@ -295,7 +301,7 @@ export class ApifyClient {
|
|
|
295
301
|
const parsed = parseArgument(options, requestQueueOptionsSchema, 'RequestQueueUserOptions');
|
|
296
302
|
const apiClientOptions = {
|
|
297
303
|
id,
|
|
298
|
-
...this.
|
|
304
|
+
...this.subClientOptions(),
|
|
299
305
|
};
|
|
300
306
|
return new RequestQueueClient(apiClientOptions, parsed);
|
|
301
307
|
}
|
|
@@ -309,7 +315,7 @@ export class ApifyClient {
|
|
|
309
315
|
*/
|
|
310
316
|
runs() {
|
|
311
317
|
return new RunCollectionClient({
|
|
312
|
-
...this.
|
|
318
|
+
...this.subClientOptions(),
|
|
313
319
|
resourcePath: 'actor-runs',
|
|
314
320
|
});
|
|
315
321
|
}
|
|
@@ -336,7 +342,7 @@ export class ApifyClient {
|
|
|
336
342
|
parseArgument(id, resourceIdSchema);
|
|
337
343
|
return new RunClient({
|
|
338
344
|
id,
|
|
339
|
-
...this.
|
|
345
|
+
...this.subClientOptions(),
|
|
340
346
|
});
|
|
341
347
|
}
|
|
342
348
|
/**
|
|
@@ -348,7 +354,7 @@ export class ApifyClient {
|
|
|
348
354
|
* @see https://docs.apify.com/api/v2/actor-tasks-get
|
|
349
355
|
*/
|
|
350
356
|
tasks() {
|
|
351
|
-
return new TaskCollectionClient(this.
|
|
357
|
+
return new TaskCollectionClient(this.subClientOptions());
|
|
352
358
|
}
|
|
353
359
|
/**
|
|
354
360
|
* Returns a client for a specific Actor task.
|
|
@@ -369,7 +375,7 @@ export class ApifyClient {
|
|
|
369
375
|
parseArgument(id, resourceIdSchema);
|
|
370
376
|
return new TaskClient({
|
|
371
377
|
id,
|
|
372
|
-
...this.
|
|
378
|
+
...this.subClientOptions(),
|
|
373
379
|
});
|
|
374
380
|
}
|
|
375
381
|
/**
|
|
@@ -381,7 +387,7 @@ export class ApifyClient {
|
|
|
381
387
|
* @see https://docs.apify.com/api/v2/schedules-get
|
|
382
388
|
*/
|
|
383
389
|
schedules() {
|
|
384
|
-
return new ScheduleCollectionClient(this.
|
|
390
|
+
return new ScheduleCollectionClient(this.subClientOptions());
|
|
385
391
|
}
|
|
386
392
|
/**
|
|
387
393
|
* Returns a client for a specific schedule.
|
|
@@ -396,7 +402,7 @@ export class ApifyClient {
|
|
|
396
402
|
parseArgument(id, resourceIdSchema);
|
|
397
403
|
return new ScheduleClient({
|
|
398
404
|
id,
|
|
399
|
-
...this.
|
|
405
|
+
...this.subClientOptions(),
|
|
400
406
|
});
|
|
401
407
|
}
|
|
402
408
|
/**
|
|
@@ -412,7 +418,7 @@ export class ApifyClient {
|
|
|
412
418
|
parseArgument(id, resourceIdSchema);
|
|
413
419
|
return new UserClient({
|
|
414
420
|
id,
|
|
415
|
-
...this.
|
|
421
|
+
...this.subClientOptions(),
|
|
416
422
|
});
|
|
417
423
|
}
|
|
418
424
|
/**
|
|
@@ -424,7 +430,7 @@ export class ApifyClient {
|
|
|
424
430
|
* @see https://docs.apify.com/api/v2/webhooks-get
|
|
425
431
|
*/
|
|
426
432
|
webhooks() {
|
|
427
|
-
return new WebhookCollectionClient(this.
|
|
433
|
+
return new WebhookCollectionClient(this.subClientOptions());
|
|
428
434
|
}
|
|
429
435
|
/**
|
|
430
436
|
* Returns a client for a specific webhook.
|
|
@@ -439,7 +445,7 @@ export class ApifyClient {
|
|
|
439
445
|
parseArgument(id, resourceIdSchema);
|
|
440
446
|
return new WebhookClient({
|
|
441
447
|
id,
|
|
442
|
-
...this.
|
|
448
|
+
...this.subClientOptions(),
|
|
443
449
|
});
|
|
444
450
|
}
|
|
445
451
|
/**
|
|
@@ -451,7 +457,7 @@ export class ApifyClient {
|
|
|
451
457
|
* @see https://docs.apify.com/api/v2/webhook-dispatches-get
|
|
452
458
|
*/
|
|
453
459
|
webhookDispatches() {
|
|
454
|
-
return new WebhookDispatchCollectionClient(this.
|
|
460
|
+
return new WebhookDispatchCollectionClient(this.subClientOptions());
|
|
455
461
|
}
|
|
456
462
|
/**
|
|
457
463
|
* Returns a client for a specific webhook dispatch.
|
|
@@ -464,7 +470,7 @@ export class ApifyClient {
|
|
|
464
470
|
parseArgument(id, resourceIdSchema);
|
|
465
471
|
return new WebhookDispatchClient({
|
|
466
472
|
id,
|
|
467
|
-
...this.
|
|
473
|
+
...this.subClientOptions(),
|
|
468
474
|
});
|
|
469
475
|
}
|
|
470
476
|
/**
|
|
@@ -476,7 +482,7 @@ export class ApifyClient {
|
|
|
476
482
|
* @see https://docs.apify.com/api/v2/store-get
|
|
477
483
|
*/
|
|
478
484
|
store() {
|
|
479
|
-
return new StoreCollectionClient(this.
|
|
485
|
+
return new StoreCollectionClient(this.subClientOptions());
|
|
480
486
|
}
|
|
481
487
|
/**
|
|
482
488
|
* Sets a status message for the current Actor run.
|
|
@@ -30,15 +30,15 @@ export declare abstract class ApiClient {
|
|
|
30
30
|
httpClient: HttpClient;
|
|
31
31
|
params?: Record<string, unknown>;
|
|
32
32
|
constructor(options: ApiClientOptions);
|
|
33
|
-
protected
|
|
34
|
-
protected
|
|
35
|
-
protected
|
|
36
|
-
protected
|
|
37
|
-
protected
|
|
33
|
+
protected subResourceOptions<T>(moreOptions?: T): BaseOptions & T;
|
|
34
|
+
protected buildUrl(path?: string | string[]): string;
|
|
35
|
+
protected buildPublicUrl(path?: string | string[]): string;
|
|
36
|
+
protected buildParams<T>(endpointParams?: T): Record<string, unknown>;
|
|
37
|
+
protected toSafeId(id: string): string;
|
|
38
38
|
/**
|
|
39
39
|
* Returns async iterator to iterate through all items and Promise that can be awaited to get first page of results.
|
|
40
40
|
*/
|
|
41
|
-
protected
|
|
41
|
+
protected listPaginatedFromCallback<T extends PaginationOptions, Data, R extends PaginatedResponse<Data>>(getPaginatedList: (options?: T) => Promise<R>, options?: T): AsyncIterable<Data> & Promise<R>;
|
|
42
42
|
}
|
|
43
43
|
export interface BaseOptions {
|
|
44
44
|
baseUrl: string;
|
package/dist/base/api_client.js
CHANGED
|
@@ -16,7 +16,7 @@ export class ApiClient {
|
|
|
16
16
|
constructor(options) {
|
|
17
17
|
const { baseUrl, publicBaseUrl, apifyClient, httpClient, resourcePath, id, params = {} } = options;
|
|
18
18
|
this.id = id;
|
|
19
|
-
this.safeId = id && this.
|
|
19
|
+
this.safeId = id && this.toSafeId(id);
|
|
20
20
|
this.baseUrl = baseUrl;
|
|
21
21
|
this.publicBaseUrl = publicBaseUrl;
|
|
22
22
|
this.resourcePath = resourcePath;
|
|
@@ -25,35 +25,35 @@ export class ApiClient {
|
|
|
25
25
|
this.httpClient = httpClient;
|
|
26
26
|
this.params = params;
|
|
27
27
|
}
|
|
28
|
-
|
|
28
|
+
subResourceOptions(moreOptions) {
|
|
29
29
|
const baseOptions = {
|
|
30
|
-
baseUrl: this.
|
|
30
|
+
baseUrl: this.buildUrl(),
|
|
31
31
|
publicBaseUrl: this.publicBaseUrl,
|
|
32
32
|
apifyClient: this.apifyClient,
|
|
33
33
|
httpClient: this.httpClient,
|
|
34
|
-
params: this.
|
|
34
|
+
params: this.buildParams(),
|
|
35
35
|
};
|
|
36
36
|
return { ...baseOptions, ...moreOptions };
|
|
37
37
|
}
|
|
38
|
-
|
|
38
|
+
buildUrl(path) {
|
|
39
39
|
return path ? `${this.url}/${toPath(path)}` : this.url;
|
|
40
40
|
}
|
|
41
|
-
|
|
41
|
+
buildPublicUrl(path) {
|
|
42
42
|
const url = this.id
|
|
43
43
|
? `${this.publicBaseUrl}/${this.resourcePath}/${toPathSegment(this.safeId)}`
|
|
44
44
|
: `${this.publicBaseUrl}/${this.resourcePath}`;
|
|
45
45
|
return path ? `${url}/${toPath(path)}` : url;
|
|
46
46
|
}
|
|
47
|
-
|
|
47
|
+
buildParams(endpointParams) {
|
|
48
48
|
return { ...this.params, ...endpointParams };
|
|
49
49
|
}
|
|
50
|
-
|
|
50
|
+
toSafeId(id) {
|
|
51
51
|
return id.replaceAll('/', '~');
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
54
|
* Returns async iterator to iterate through all items and Promise that can be awaited to get first page of results.
|
|
55
55
|
*/
|
|
56
|
-
|
|
56
|
+
listPaginatedFromCallback(getPaginatedList, options = {}) {
|
|
57
57
|
const minForLimitParam = (a, b) => {
|
|
58
58
|
// API treats 0 as undefined for limit parameter
|
|
59
59
|
if (a === 0)
|
|
@@ -67,7 +67,8 @@ export class ApiClient {
|
|
|
67
67
|
return Math.min(a, b);
|
|
68
68
|
};
|
|
69
69
|
// `chunkSize` only sizes this loop's requests; it is not an API parameter, so it must not reach
|
|
70
|
-
// `
|
|
70
|
+
// `buildParams()` and the query string. The same goes for `timeoutSecs`, which callers take out before
|
|
71
|
+
// calling this, since it also picks the timeout of every page request.
|
|
71
72
|
const { chunkSize, ...listOptions } = options;
|
|
72
73
|
const paginatedListPromise = getPaginatedList({
|
|
73
74
|
...listOptions,
|
|
@@ -1,33 +1,38 @@
|
|
|
1
1
|
import type { ACT_JOB_STATUSES } from '@apify/consts';
|
|
2
2
|
import type { z } from 'zod';
|
|
3
|
+
import type { Timeout, TimeoutOptions, TimeoutTier } from '../timeouts.js';
|
|
3
4
|
import { ApiClient } from './api_client.js';
|
|
4
|
-
export declare const SMALL_TIMEOUT_MILLIS: number;
|
|
5
|
-
export declare const MEDIUM_TIMEOUT_MILLIS: number;
|
|
6
|
-
export declare const DEFAULT_TIMEOUT_MILLIS: number;
|
|
7
5
|
/**
|
|
8
6
|
* Resource client.
|
|
9
7
|
* @private
|
|
10
8
|
*/
|
|
11
9
|
export declare class ResourceClient extends ApiClient {
|
|
10
|
+
/**
|
|
11
|
+
* Picks the timeout of a request that asks the API to hold its response with `waitForFinish`. The request
|
|
12
|
+
* gets the hold the caller asked for plus the round trip its tier allows, so the client does not abort a
|
|
13
|
+
* request while the API is still holding it - which for `start()` and `build()` would retry a call that
|
|
14
|
+
* creates a resource. An explicit per-call `timeoutSecs` is used as given.
|
|
15
|
+
*/
|
|
16
|
+
protected timeoutForWaitForFinish(timeoutSecs: Timeout | undefined, tier: TimeoutTier, waitForFinishSecs: number | undefined): Timeout;
|
|
12
17
|
/**
|
|
13
18
|
* A 404 resolves to `undefined` only when the client names its resource by ID. A chained client without one, such
|
|
14
19
|
* as `run.dataset()`, throws it instead (see `catchNotFoundForResourceOrThrow()`).
|
|
15
20
|
*/
|
|
16
|
-
protected
|
|
17
|
-
protected
|
|
21
|
+
protected getResource<T, R>(schema: z.ZodType, options: T, timeoutSecs: Timeout): Promise<R | undefined>;
|
|
22
|
+
protected updateResource<T, R>(schema: z.ZodType, newFields: T, timeoutSecs: Timeout): Promise<R>;
|
|
18
23
|
/**
|
|
19
24
|
* A 404 is swallowed, keeping the DELETE idempotent, only when the client names its resource by ID. A chained client
|
|
20
25
|
* without one throws it instead (see `catchNotFoundForResourceOrThrow()`).
|
|
21
26
|
*/
|
|
22
|
-
protected
|
|
27
|
+
protected deleteResource(timeoutSecs: Timeout): Promise<void>;
|
|
23
28
|
/**
|
|
24
29
|
* This function is used in Build and Run endpoints so it's kept
|
|
25
30
|
* here to stay DRY.
|
|
26
31
|
*/
|
|
27
|
-
protected
|
|
32
|
+
protected waitForJobFinish<R extends {
|
|
28
33
|
status: (typeof ACT_JOB_STATUSES)[keyof typeof ACT_JOB_STATUSES];
|
|
29
34
|
}>(schema: z.ZodType, options?: WaitForFinishOptions): Promise<R>;
|
|
30
35
|
}
|
|
31
|
-
export interface WaitForFinishOptions {
|
|
36
|
+
export interface WaitForFinishOptions extends TimeoutOptions {
|
|
32
37
|
waitSecs?: number;
|
|
33
38
|
}
|
|
@@ -7,24 +7,39 @@ import { ApiClient } from './api_client.js';
|
|
|
7
7
|
* 999999 seconds is more than 10 days.
|
|
8
8
|
*/
|
|
9
9
|
const MAX_WAIT_FOR_FINISH = 999999;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export const DEFAULT_TIMEOUT_MILLIS = 360 * 1000; // 6 minutes
|
|
10
|
+
/** The API holds a `waitForFinish` response for at most a minute, however long the parameter asks for. */
|
|
11
|
+
const MAX_WAIT_FOR_FINISH_HOLD_SECS = 60;
|
|
13
12
|
/**
|
|
14
13
|
* Resource client.
|
|
15
14
|
* @private
|
|
16
15
|
*/
|
|
17
16
|
export class ResourceClient extends ApiClient {
|
|
17
|
+
/**
|
|
18
|
+
* Picks the timeout of a request that asks the API to hold its response with `waitForFinish`. The request
|
|
19
|
+
* gets the hold the caller asked for plus the round trip its tier allows, so the client does not abort a
|
|
20
|
+
* request while the API is still holding it - which for `start()` and `build()` would retry a call that
|
|
21
|
+
* creates a resource. An explicit per-call `timeoutSecs` is used as given.
|
|
22
|
+
*/
|
|
23
|
+
timeoutForWaitForFinish(timeoutSecs, tier, waitForFinishSecs) {
|
|
24
|
+
if (timeoutSecs !== undefined)
|
|
25
|
+
return timeoutSecs;
|
|
26
|
+
if (waitForFinishSecs === undefined)
|
|
27
|
+
return tier;
|
|
28
|
+
const holdSecs = Math.min(waitForFinishSecs, MAX_WAIT_FOR_FINISH_HOLD_SECS);
|
|
29
|
+
if (holdSecs <= 0)
|
|
30
|
+
return tier;
|
|
31
|
+
return holdSecs + this.httpClient.timeoutMillis[tier] / 1000;
|
|
32
|
+
}
|
|
18
33
|
/**
|
|
19
34
|
* A 404 resolves to `undefined` only when the client names its resource by ID. A chained client without one, such
|
|
20
35
|
* as `run.dataset()`, throws it instead (see `catchNotFoundForResourceOrThrow()`).
|
|
21
36
|
*/
|
|
22
|
-
async
|
|
37
|
+
async getResource(schema, options, timeoutSecs) {
|
|
23
38
|
const requestOpts = {
|
|
24
|
-
url: this.
|
|
39
|
+
url: this.buildUrl(),
|
|
25
40
|
method: 'GET',
|
|
26
|
-
params: this.
|
|
27
|
-
|
|
41
|
+
params: this.buildParams(options),
|
|
42
|
+
timeoutSecs,
|
|
28
43
|
};
|
|
29
44
|
try {
|
|
30
45
|
const response = await this.httpClient.call(requestOpts);
|
|
@@ -35,13 +50,13 @@ export class ResourceClient extends ApiClient {
|
|
|
35
50
|
}
|
|
36
51
|
return undefined;
|
|
37
52
|
}
|
|
38
|
-
async
|
|
53
|
+
async updateResource(schema, newFields, timeoutSecs) {
|
|
39
54
|
const response = await this.httpClient.call({
|
|
40
|
-
url: this.
|
|
55
|
+
url: this.buildUrl(),
|
|
41
56
|
method: 'PUT',
|
|
42
|
-
params: this.
|
|
57
|
+
params: this.buildParams(),
|
|
43
58
|
data: newFields,
|
|
44
|
-
|
|
59
|
+
timeoutSecs,
|
|
45
60
|
});
|
|
46
61
|
return parseResponse(response, schema);
|
|
47
62
|
}
|
|
@@ -49,13 +64,13 @@ export class ResourceClient extends ApiClient {
|
|
|
49
64
|
* A 404 is swallowed, keeping the DELETE idempotent, only when the client names its resource by ID. A chained client
|
|
50
65
|
* without one throws it instead (see `catchNotFoundForResourceOrThrow()`).
|
|
51
66
|
*/
|
|
52
|
-
async
|
|
67
|
+
async deleteResource(timeoutSecs) {
|
|
53
68
|
try {
|
|
54
69
|
await this.httpClient.call({
|
|
55
|
-
url: this.
|
|
70
|
+
url: this.buildUrl(),
|
|
56
71
|
method: 'DELETE',
|
|
57
|
-
params: this.
|
|
58
|
-
|
|
72
|
+
params: this.buildParams(),
|
|
73
|
+
timeoutSecs,
|
|
59
74
|
});
|
|
60
75
|
}
|
|
61
76
|
catch (err) {
|
|
@@ -66,8 +81,8 @@ export class ResourceClient extends ApiClient {
|
|
|
66
81
|
* This function is used in Build and Run endpoints so it's kept
|
|
67
82
|
* here to stay DRY.
|
|
68
83
|
*/
|
|
69
|
-
async
|
|
70
|
-
const { waitSecs = MAX_WAIT_FOR_FINISH } = options;
|
|
84
|
+
async waitForJobFinish(schema, options = {}) {
|
|
85
|
+
const { waitSecs = MAX_WAIT_FOR_FINISH, timeoutSecs = 'noTimeout' } = options;
|
|
71
86
|
const waitMillis = waitSecs * 1000;
|
|
72
87
|
let job;
|
|
73
88
|
const startedAt = Date.now();
|
|
@@ -83,9 +98,10 @@ export class ResourceClient extends ApiClient {
|
|
|
83
98
|
const remainingWaitSeconds = Math.round((waitMillis - millisSinceStart) / 1000);
|
|
84
99
|
const waitForFinish = Math.max(0, remainingWaitSeconds);
|
|
85
100
|
const requestOpts = {
|
|
86
|
-
url: this.
|
|
101
|
+
url: this.buildUrl(),
|
|
87
102
|
method: 'GET',
|
|
88
|
-
params: this.
|
|
103
|
+
params: this.buildParams({ waitForFinish }),
|
|
104
|
+
timeoutSecs,
|
|
89
105
|
};
|
|
90
106
|
try {
|
|
91
107
|
const response = await this.httpClient.call(requestOpts);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { z } from 'zod';
|
|
2
|
+
import type { Timeout, TimeoutOptions } from '../timeouts.js';
|
|
2
3
|
import type { PaginatedResponse, PaginationOptions } from '../utils.js';
|
|
3
4
|
import { ApiClient } from './api_client.js';
|
|
4
5
|
/**
|
|
@@ -9,11 +10,12 @@ export declare class ResourceCollectionClient extends ApiClient {
|
|
|
9
10
|
/**
|
|
10
11
|
* @private
|
|
11
12
|
*/
|
|
12
|
-
protected
|
|
13
|
+
protected listResources<T, R>(schema: z.ZodType, options: T | undefined, timeoutSecs: Timeout): Promise<R>;
|
|
13
14
|
/**
|
|
14
15
|
* Returns async iterator to iterate through all items and Promise that can be awaited to get first page of results.
|
|
16
|
+
* `defaultTimeoutSecs` applies to every page request unless `options.timeoutSecs` overrides it.
|
|
15
17
|
*/
|
|
16
|
-
protected
|
|
17
|
-
protected
|
|
18
|
-
protected
|
|
18
|
+
protected listResourcesPaginated<T extends PaginationOptions & TimeoutOptions, Data, R extends PaginatedResponse<Data>>(schema: z.ZodType, options: T, defaultTimeoutSecs: Timeout): AsyncIterable<Data> & Promise<R>;
|
|
19
|
+
protected createResource<D, R>(schema: z.ZodType, resource: D, timeoutSecs: Timeout): Promise<R>;
|
|
20
|
+
protected getOrCreateResource<D, R>(schema: z.ZodType, name: string | undefined, resource: D | undefined, timeoutSecs: Timeout): Promise<R>;
|
|
19
21
|
}
|
|
@@ -8,35 +8,41 @@ export class ResourceCollectionClient extends ApiClient {
|
|
|
8
8
|
/**
|
|
9
9
|
* @private
|
|
10
10
|
*/
|
|
11
|
-
async
|
|
11
|
+
async listResources(schema, options, timeoutSecs) {
|
|
12
12
|
const response = await this.httpClient.call({
|
|
13
|
-
url: this.
|
|
13
|
+
url: this.buildUrl(),
|
|
14
14
|
method: 'GET',
|
|
15
|
-
params: this.
|
|
15
|
+
params: this.buildParams(options),
|
|
16
|
+
timeoutSecs,
|
|
16
17
|
});
|
|
17
18
|
return parseResponse(response, schema);
|
|
18
19
|
}
|
|
19
20
|
/**
|
|
20
21
|
* Returns async iterator to iterate through all items and Promise that can be awaited to get first page of results.
|
|
22
|
+
* `defaultTimeoutSecs` applies to every page request unless `options.timeoutSecs` overrides it.
|
|
21
23
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
listResourcesPaginated(schema, options, defaultTimeoutSecs) {
|
|
25
|
+
// `timeoutSecs` only times the page requests; it is not an API parameter, so it must not reach the query string.
|
|
26
|
+
const { timeoutSecs = defaultTimeoutSecs, ...listOptions } = options;
|
|
27
|
+
return this.listPaginatedFromCallback(async (pageOptions) => this.listResources(schema, pageOptions, timeoutSecs), listOptions);
|
|
24
28
|
}
|
|
25
|
-
async
|
|
29
|
+
async createResource(schema, resource, timeoutSecs) {
|
|
26
30
|
const response = await this.httpClient.call({
|
|
27
|
-
url: this.
|
|
31
|
+
url: this.buildUrl(),
|
|
28
32
|
method: 'POST',
|
|
29
|
-
params: this.
|
|
33
|
+
params: this.buildParams(),
|
|
30
34
|
data: resource,
|
|
35
|
+
timeoutSecs,
|
|
31
36
|
});
|
|
32
37
|
return parseResponse(response, schema);
|
|
33
38
|
}
|
|
34
|
-
async
|
|
39
|
+
async getOrCreateResource(schema, name, resource, timeoutSecs) {
|
|
35
40
|
const response = await this.httpClient.call({
|
|
36
|
-
url: this.
|
|
41
|
+
url: this.buildUrl(),
|
|
37
42
|
method: 'POST',
|
|
38
|
-
params: this.
|
|
43
|
+
params: this.buildParams({ name }),
|
|
39
44
|
data: resource,
|
|
45
|
+
timeoutSecs,
|
|
40
46
|
});
|
|
41
47
|
return parseResponse(response, schema);
|
|
42
48
|
}
|