apify-client 3.0.0-beta.15 → 3.0.0-beta.17
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.d.ts +4 -19
- package/dist/apify_api_error.js +16 -15
- 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 +12 -12
- package/dist/bundle.js.map +1 -1
- package/dist/http_client.d.ts +18 -51
- package/dist/http_client.js +67 -38
- 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 +5 -17
- package/dist/resource_clients/log.js +46 -37
- package/dist/resource_clients/request_queue.d.ts +47 -19
- package/dist/resource_clients/request_queue.js +127 -73
- 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/statistics.d.ts +1 -6
- package/dist/statistics.js +2 -2
- package/dist/timeouts.d.ts +72 -0
- package/dist/timeouts.js +31 -0
- package/dist/utils.d.ts +1 -4
- package/dist/utils.js +14 -14
- package/package.json +1 -1
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import log from '@apify/log';
|
|
3
3
|
import { createHmacSignatureAsync, createStorageContentSignatureAsync } from '@apify/utilities';
|
|
4
|
-
import {
|
|
4
|
+
import { ResourceClient } from '../base/resource_client.js';
|
|
5
5
|
import * as schemas from '../schemas.js';
|
|
6
|
+
import { timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
|
|
6
7
|
import { anyObjectSchema, applyQueryParamsToUrl, catchNotFoundOrThrow, isBuffer, isNode, isStream, parseArgument, parseResponse, } from '../utils.js';
|
|
7
8
|
const listKeysOptionsSchema = z.strictObject({
|
|
8
9
|
limit: z.number().min(0).optional(),
|
|
@@ -10,6 +11,7 @@ const listKeysOptionsSchema = z.strictObject({
|
|
|
10
11
|
collection: z.string().optional(),
|
|
11
12
|
prefix: z.string().optional(),
|
|
12
13
|
signature: z.string().optional(),
|
|
14
|
+
...timeoutOptionsShape,
|
|
13
15
|
});
|
|
14
16
|
const nonEmptyKeySchema = z.string().min(1);
|
|
15
17
|
// `signature` is left out - this method produces one. The options type omits it to match.
|
|
@@ -19,6 +21,7 @@ const createKeysPublicUrlOptionsSchema = z.strictObject({
|
|
|
19
21
|
collection: z.string().optional(),
|
|
20
22
|
prefix: z.string().optional(),
|
|
21
23
|
expiresInSecs: z.number().optional(),
|
|
24
|
+
...timeoutOptionsShape,
|
|
22
25
|
});
|
|
23
26
|
const keySchema = z.string();
|
|
24
27
|
const getRecordOptionsSchema = z.strictObject({
|
|
@@ -26,6 +29,7 @@ const getRecordOptionsSchema = z.strictObject({
|
|
|
26
29
|
stream: z.boolean().optional(),
|
|
27
30
|
disableRedirect: z.boolean().optional(),
|
|
28
31
|
signature: z.string().optional(),
|
|
32
|
+
...timeoutOptionsShape,
|
|
29
33
|
});
|
|
30
34
|
const recordSchema = z.strictObject({
|
|
31
35
|
key: z.string(),
|
|
@@ -41,7 +45,7 @@ const recordSchema = z.strictObject({
|
|
|
41
45
|
contentType: z.string().min(1).optional(),
|
|
42
46
|
});
|
|
43
47
|
const recordOptionsSchema = z.strictObject({
|
|
44
|
-
|
|
48
|
+
...timeoutOptionsShape,
|
|
45
49
|
doNotRetryTimeouts: z.boolean().optional(),
|
|
46
50
|
});
|
|
47
51
|
/**
|
|
@@ -85,11 +89,14 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
85
89
|
/**
|
|
86
90
|
* Gets the key-value store object from the Apify API.
|
|
87
91
|
*
|
|
92
|
+
* @param options - Request options
|
|
93
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
88
94
|
* @returns The KeyValueStore object, or `undefined` if it does not exist
|
|
89
95
|
* @see https://docs.apify.com/api/v2/key-value-store-get
|
|
90
96
|
*/
|
|
91
|
-
async get() {
|
|
92
|
-
|
|
97
|
+
async get(options = {}) {
|
|
98
|
+
const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
99
|
+
return this.getResource(schemas.KeyValueStore(), {}, timeoutSecs);
|
|
93
100
|
}
|
|
94
101
|
/**
|
|
95
102
|
* Updates the key-value store with specified fields.
|
|
@@ -98,20 +105,26 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
98
105
|
* @param newFields.name - New name for the store
|
|
99
106
|
* @param newFields.title - New title for the store
|
|
100
107
|
* @param newFields.generalAccess - General resource access level ('FOLLOW_USER_SETTING', 'ANYONE_WITH_ID_CAN_READ' or 'RESTRICTED')
|
|
108
|
+
* @param options - Request options
|
|
109
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
101
110
|
* @returns The updated KeyValueStore object
|
|
102
111
|
* @see https://docs.apify.com/api/v2/key-value-store-put
|
|
103
112
|
*/
|
|
104
|
-
async update(newFields) {
|
|
113
|
+
async update(newFields, options = {}) {
|
|
105
114
|
parseArgument(newFields, anyObjectSchema);
|
|
106
|
-
|
|
115
|
+
const { timeoutSecs = 'long' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
116
|
+
return this.updateResource(schemas.KeyValueStore(), newFields, timeoutSecs);
|
|
107
117
|
}
|
|
108
118
|
/**
|
|
109
119
|
* Deletes the key-value store.
|
|
110
120
|
*
|
|
121
|
+
* @param options - Request options
|
|
122
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
111
123
|
* @see https://docs.apify.com/api/v2/key-value-store-delete
|
|
112
124
|
*/
|
|
113
|
-
async delete() {
|
|
114
|
-
|
|
125
|
+
async delete(options = {}) {
|
|
126
|
+
const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
127
|
+
return this.deleteResource(timeoutSecs);
|
|
115
128
|
}
|
|
116
129
|
/**
|
|
117
130
|
* Lists all keys in the key-value store.
|
|
@@ -124,6 +137,7 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
124
137
|
* @param options.exclusiveStartKey - Key to start listing from (for pagination). The listing starts with the next key after this one.
|
|
125
138
|
* @param options.collection - Filter keys by collection name.
|
|
126
139
|
* @param options.prefix - Filter keys that start with this prefix.
|
|
140
|
+
* @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
|
|
127
141
|
* @returns Object containing `items` array of key metadata, pagination info (`count`, `limit`, `isTruncated`, `nextExclusiveStartKey`)
|
|
128
142
|
* @see https://docs.apify.com/api/v2/key-value-store-keys-get
|
|
129
143
|
*
|
|
@@ -149,13 +163,13 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
149
163
|
* ```
|
|
150
164
|
*/
|
|
151
165
|
listKeys(options = {}) {
|
|
152
|
-
const parsed = parseArgument(options, listKeysOptionsSchema, 'KeyValueClientListKeysOptions');
|
|
166
|
+
const { timeoutSecs = 'medium', ...parsed } = parseArgument(options, listKeysOptionsSchema, 'KeyValueClientListKeysOptions');
|
|
153
167
|
const getPaginatedList = async (kvsListOptions = {}) => {
|
|
154
168
|
const response = await this.httpClient.call({
|
|
155
|
-
url: this.
|
|
169
|
+
url: this.buildUrl('keys'),
|
|
156
170
|
method: 'GET',
|
|
157
|
-
params: this.
|
|
158
|
-
|
|
171
|
+
params: this.buildParams(kvsListOptions),
|
|
172
|
+
timeoutSecs,
|
|
159
173
|
});
|
|
160
174
|
return parseResponse(response, schemas.ListOfKeys());
|
|
161
175
|
};
|
|
@@ -194,6 +208,8 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
194
208
|
* requiring an API token.
|
|
195
209
|
*
|
|
196
210
|
* @param key - The record key
|
|
211
|
+
* @param options - Request options
|
|
212
|
+
* @param options.timeoutSecs - Timeout for the API request that fetches the store. Default is `'long'`.
|
|
197
213
|
* @returns A public URL string for accessing the record
|
|
198
214
|
*
|
|
199
215
|
* @example
|
|
@@ -204,10 +220,11 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
204
220
|
* ```
|
|
205
221
|
* @since Added in 2.14.0
|
|
206
222
|
*/
|
|
207
|
-
async getRecordPublicUrl(key) {
|
|
223
|
+
async getRecordPublicUrl(key, options = {}) {
|
|
208
224
|
parseArgument(key, nonEmptyKeySchema);
|
|
209
|
-
const
|
|
210
|
-
const
|
|
225
|
+
const { timeoutSecs = 'long' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
226
|
+
const store = await this.get({ timeoutSecs });
|
|
227
|
+
const recordPublicUrl = new URL(this.buildPublicUrl(['records', key]));
|
|
211
228
|
if (store?.urlSigningSecretKey) {
|
|
212
229
|
const signature = await createHmacSignatureAsync(store.urlSigningSecretKey, key);
|
|
213
230
|
recordPublicUrl.searchParams.append('signature', signature);
|
|
@@ -224,6 +241,7 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
224
241
|
* @param options.expiresInSecs - Number of seconds until the signed URL expires. If omitted, the URL never expires.
|
|
225
242
|
* @param options.limit - Maximum number of keys to return.
|
|
226
243
|
* @param options.prefix - Filter keys by prefix.
|
|
244
|
+
* @param options.timeoutSecs - Timeout for the API request that fetches the store. Default is `'long'`.
|
|
227
245
|
* @returns A public URL string for accessing the keys list
|
|
228
246
|
*
|
|
229
247
|
* @example
|
|
@@ -238,10 +256,9 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
238
256
|
* @since Added in 2.13.0
|
|
239
257
|
*/
|
|
240
258
|
async createKeysPublicUrl(options = {}) {
|
|
241
|
-
const
|
|
242
|
-
const store = await this.get();
|
|
243
|
-
|
|
244
|
-
let createdPublicKeysUrl = new URL(this._publicUrl('keys'));
|
|
259
|
+
const { timeoutSecs = 'long', expiresInSecs, ...queryOptions } = parseArgument(options, createKeysPublicUrlOptionsSchema, 'KeyValueClientCreateKeysUrlOptions');
|
|
260
|
+
const store = await this.get({ timeoutSecs });
|
|
261
|
+
let createdPublicKeysUrl = new URL(this.buildPublicUrl('keys'));
|
|
245
262
|
if (store?.urlSigningSecretKey) {
|
|
246
263
|
const signature = await createStorageContentSignatureAsync({
|
|
247
264
|
resourceId: store.id,
|
|
@@ -259,6 +276,8 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
259
276
|
* This is more efficient than {@link getRecord} when you only need to check for existence.
|
|
260
277
|
*
|
|
261
278
|
* @param key - The record key to check
|
|
279
|
+
* @param options - Request options
|
|
280
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
262
281
|
* @returns `true` if the record exists, `false` if it does not
|
|
263
282
|
* @see https://docs.apify.com/api/v2/key-value-store-record-get
|
|
264
283
|
*
|
|
@@ -271,11 +290,13 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
271
290
|
* ```
|
|
272
291
|
* @since Added in 2.9.0
|
|
273
292
|
*/
|
|
274
|
-
async recordExists(key) {
|
|
293
|
+
async recordExists(key, options = {}) {
|
|
294
|
+
const { timeoutSecs = 'long' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
275
295
|
const requestOpts = {
|
|
276
|
-
url: this.
|
|
296
|
+
url: this.buildUrl(['records', key]),
|
|
277
297
|
method: 'HEAD',
|
|
278
|
-
params: this.
|
|
298
|
+
params: this.buildParams(),
|
|
299
|
+
timeoutSecs,
|
|
279
300
|
};
|
|
280
301
|
try {
|
|
281
302
|
await this.httpClient.call(requestOpts);
|
|
@@ -300,10 +321,10 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
300
321
|
if (parsed.signature)
|
|
301
322
|
queryParams.signature = parsed.signature;
|
|
302
323
|
const requestOpts = {
|
|
303
|
-
url: this.
|
|
324
|
+
url: this.buildUrl(['records', key]),
|
|
304
325
|
method: 'GET',
|
|
305
|
-
params: this.
|
|
306
|
-
|
|
326
|
+
params: this.buildParams(queryParams),
|
|
327
|
+
timeoutSecs: parsed.timeoutSecs ?? 'long',
|
|
307
328
|
};
|
|
308
329
|
if (parsed.buffer)
|
|
309
330
|
requestOpts.forceBuffer = true;
|
|
@@ -346,7 +367,7 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
346
367
|
* `'application/octet-stream'` fallback is treated as compressible, because
|
|
347
368
|
* it could hold any binary data.
|
|
348
369
|
* @param options - Storage options
|
|
349
|
-
* @param options.timeoutSecs - Timeout for the
|
|
370
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
350
371
|
* @param options.doNotRetryTimeouts - If `true`, don't retry on timeout errors. Default is `false`.
|
|
351
372
|
* @see https://docs.apify.com/api/v2/key-value-store-record-put
|
|
352
373
|
*
|
|
@@ -379,7 +400,7 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
379
400
|
const parsed = parseArgument(options, recordOptionsSchema, 'KeyValueStoreRecordOptions');
|
|
380
401
|
const { key } = record;
|
|
381
402
|
let { value, contentType } = record;
|
|
382
|
-
const { timeoutSecs, doNotRetryTimeouts } = parsed;
|
|
403
|
+
const { timeoutSecs = 'long', doNotRetryTimeouts } = parsed;
|
|
383
404
|
const isValueStreamOrBuffer = isStream(value) || isBuffer(value);
|
|
384
405
|
// To allow saving Objects to JSON without providing content type
|
|
385
406
|
if (!contentType) {
|
|
@@ -401,13 +422,13 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
401
422
|
}
|
|
402
423
|
}
|
|
403
424
|
const uploadOpts = {
|
|
404
|
-
url: this.
|
|
425
|
+
url: this.buildUrl(['records', key]),
|
|
405
426
|
method: 'PUT',
|
|
406
|
-
params: this.
|
|
427
|
+
params: this.buildParams(),
|
|
407
428
|
data: value,
|
|
408
429
|
headers: contentType ? { 'content-type': contentType } : undefined,
|
|
409
430
|
doNotRetryTimeouts,
|
|
410
|
-
|
|
431
|
+
timeoutSecs,
|
|
411
432
|
};
|
|
412
433
|
await this.httpClient.call(uploadOpts);
|
|
413
434
|
}
|
|
@@ -415,6 +436,8 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
415
436
|
* Deletes a record from the key-value store.
|
|
416
437
|
*
|
|
417
438
|
* @param key - The record key to delete
|
|
439
|
+
* @param options - Request options
|
|
440
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
418
441
|
* @see https://docs.apify.com/api/v2/key-value-store-record-delete
|
|
419
442
|
*
|
|
420
443
|
* @example
|
|
@@ -422,13 +445,14 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
422
445
|
* await client.keyValueStore('my-store').deleteRecord('temp-data');
|
|
423
446
|
* ```
|
|
424
447
|
*/
|
|
425
|
-
async deleteRecord(key) {
|
|
448
|
+
async deleteRecord(key, options = {}) {
|
|
426
449
|
parseArgument(key, keySchema);
|
|
450
|
+
const { timeoutSecs = 'short' } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
427
451
|
await this.httpClient.call({
|
|
428
|
-
url: this.
|
|
452
|
+
url: this.buildUrl(['records', key]),
|
|
429
453
|
method: 'DELETE',
|
|
430
|
-
params: this.
|
|
431
|
-
|
|
454
|
+
params: this.buildParams(),
|
|
455
|
+
timeoutSecs,
|
|
432
456
|
});
|
|
433
457
|
}
|
|
434
458
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { STORAGE_OWNERSHIP_FILTER } from '@apify/consts';
|
|
2
2
|
import type { ApiClientSubResourceOptions } 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 { PaginatedList, PaginationOptions } from '../utils.js';
|
|
5
6
|
import type { KeyValueStore } from './key_value_store.js';
|
|
6
7
|
/**
|
|
@@ -45,6 +46,7 @@ export declare class KeyValueStoreCollectionClient extends ResourceCollectionCli
|
|
|
45
46
|
* ```
|
|
46
47
|
*
|
|
47
48
|
* @param options - Pagination options.
|
|
49
|
+
* @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
|
|
48
50
|
* @returns A paginated iterator of Key-value stores.
|
|
49
51
|
* @see https://docs.apify.com/api/v2/key-value-stores-get
|
|
50
52
|
*/
|
|
@@ -54,12 +56,14 @@ export declare class KeyValueStoreCollectionClient extends ResourceCollectionCli
|
|
|
54
56
|
*
|
|
55
57
|
* @param name - Name of the key-value store. If not provided, a default store is used.
|
|
56
58
|
* @param options - Additional options like schema.
|
|
59
|
+
* @param options.schema - Schema of the key-value store.
|
|
60
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
57
61
|
* @returns The key-value store object.
|
|
58
62
|
* @see https://docs.apify.com/api/v2/key-value-stores-post
|
|
59
63
|
*/
|
|
60
64
|
getOrCreate(name?: string, options?: KeyValueStoreCollectionClientGetOrCreateOptions): Promise<KeyValueStore>;
|
|
61
65
|
}
|
|
62
|
-
export interface KeyValueStoreCollectionClientListOptions extends PaginationOptions {
|
|
66
|
+
export interface KeyValueStoreCollectionClientListOptions extends PaginationOptions, TimeoutOptions {
|
|
63
67
|
unnamed?: boolean;
|
|
64
68
|
desc?: boolean;
|
|
65
69
|
/**
|
|
@@ -71,7 +75,7 @@ export interface KeyValueStoreCollectionClientListOptions extends PaginationOpti
|
|
|
71
75
|
/**
|
|
72
76
|
* @since Added in 2.3.0
|
|
73
77
|
*/
|
|
74
|
-
export interface KeyValueStoreCollectionClientGetOrCreateOptions {
|
|
78
|
+
export interface KeyValueStoreCollectionClientGetOrCreateOptions extends TimeoutOptions {
|
|
75
79
|
schema?: Record<string, unknown>;
|
|
76
80
|
}
|
|
77
81
|
export type KeyValueStoreCollectionListResult = PaginatedList<KeyValueStore> & {
|
|
@@ -2,12 +2,14 @@ import { z } from 'zod';
|
|
|
2
2
|
import { STORAGE_OWNERSHIP_FILTER } from '@apify/consts';
|
|
3
3
|
import { ResourceCollectionClient } from '../base/resource_collection_client.js';
|
|
4
4
|
import * as schemas from '../schemas.js';
|
|
5
|
+
import { optionalTimeoutSchema, timeoutOptionsShape } from '../timeouts.js';
|
|
5
6
|
import { anyObjectSchema, paginationOptionsShape, parseArgument } from '../utils.js';
|
|
6
7
|
const listOptionsSchema = z.strictObject({
|
|
7
8
|
unnamed: z.boolean().optional(),
|
|
8
9
|
...paginationOptionsShape,
|
|
9
10
|
desc: z.boolean().optional(),
|
|
10
11
|
ownership: z.enum(STORAGE_OWNERSHIP_FILTER).optional(),
|
|
12
|
+
...timeoutOptionsShape,
|
|
11
13
|
});
|
|
12
14
|
const nameSchema = z.string().optional();
|
|
13
15
|
const schemaSchema = anyObjectSchema.optional();
|
|
@@ -58,24 +60,32 @@ export class KeyValueStoreCollectionClient extends ResourceCollectionClient {
|
|
|
58
60
|
* ```
|
|
59
61
|
*
|
|
60
62
|
* @param options - Pagination options.
|
|
63
|
+
* @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
|
|
61
64
|
* @returns A paginated iterator of Key-value stores.
|
|
62
65
|
* @see https://docs.apify.com/api/v2/key-value-stores-get
|
|
63
66
|
*/
|
|
64
67
|
list(options = {}) {
|
|
65
68
|
const parsed = parseArgument(options, listOptionsSchema, 'KeyValueStoreCollectionClientListOptions');
|
|
66
|
-
return this.
|
|
69
|
+
return this.listResourcesPaginated(schemas.ListOfKeyValueStores(), parsed, 'medium');
|
|
67
70
|
}
|
|
68
71
|
/**
|
|
69
72
|
* Gets or creates a key-value store with the specified name.
|
|
70
73
|
*
|
|
71
74
|
* @param name - Name of the key-value store. If not provided, a default store is used.
|
|
72
75
|
* @param options - Additional options like schema.
|
|
76
|
+
* @param options.schema - Schema of the key-value store.
|
|
77
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
73
78
|
* @returns The key-value store object.
|
|
74
79
|
* @see https://docs.apify.com/api/v2/key-value-stores-post
|
|
75
80
|
*/
|
|
76
81
|
async getOrCreate(name, options) {
|
|
77
82
|
parseArgument(name, nameSchema);
|
|
78
83
|
parseArgument(options?.schema, schemaSchema); // TODO: Add schema validation
|
|
79
|
-
|
|
84
|
+
parseArgument(options?.timeoutSecs, optionalTimeoutSchema);
|
|
85
|
+
// `timeoutSecs` is not part of the resource, so the body carries only the rest, and stays absent when
|
|
86
|
+
// there is nothing else to send.
|
|
87
|
+
const { timeoutSecs = 'short', ...resource } = options ?? {};
|
|
88
|
+
const hasResource = Object.keys(resource).length > 0;
|
|
89
|
+
return this.getOrCreateResource(schemas.KeyValueStore(), name, hasResource ? resource : undefined, timeoutSecs);
|
|
80
90
|
}
|
|
81
91
|
}
|
|
@@ -3,6 +3,7 @@ import type { Log } from '@apify/log';
|
|
|
3
3
|
import { Logger, LogLevel } from '@apify/log';
|
|
4
4
|
import type { ApiClientSubResourceOptions } from '../base/api_client.js';
|
|
5
5
|
import { ResourceClient } from '../base/resource_client.js';
|
|
6
|
+
import type { TimeoutOptions } from '../timeouts.js';
|
|
6
7
|
/**
|
|
7
8
|
* Client for accessing Actor run or build logs.
|
|
8
9
|
*
|
|
@@ -35,6 +36,7 @@ export declare class LogClient extends ResourceClient {
|
|
|
35
36
|
*
|
|
36
37
|
* @param options - Log retrieval options.
|
|
37
38
|
* @param options.raw - If `true`, returns raw log content without any processing. Default is `false`.
|
|
39
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
38
40
|
* @returns The log content as a string, or `undefined` if it does not exist. A chained client such as
|
|
39
41
|
* `run.log()` throws an `ApifyApiError` on a 404, since the run itself may be what is missing.
|
|
40
42
|
* @see https://docs.apify.com/api/v2/log-get
|
|
@@ -45,6 +47,7 @@ export declare class LogClient extends ResourceClient {
|
|
|
45
47
|
*
|
|
46
48
|
* @param options - Log retrieval options.
|
|
47
49
|
* @param options.raw - If `true`, returns raw log content without any processing. Default is `false`.
|
|
50
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
48
51
|
* @returns The log content as a Readable stream, or `undefined` if it does not exist. A chained client such as
|
|
49
52
|
* `run.log()` throws an `ApifyApiError` on a 404, since the run itself may be what is missing.
|
|
50
53
|
* @see https://docs.apify.com/api/v2/log-get
|
|
@@ -54,7 +57,7 @@ export declare class LogClient extends ResourceClient {
|
|
|
54
57
|
/**
|
|
55
58
|
* @since Added in 2.20.0
|
|
56
59
|
*/
|
|
57
|
-
export interface LogOptions {
|
|
60
|
+
export interface LogOptions extends TimeoutOptions {
|
|
58
61
|
/** @default false */
|
|
59
62
|
raw?: boolean;
|
|
60
63
|
}
|
|
@@ -71,13 +74,7 @@ export declare class LoggerActorRedirect extends Logger {
|
|
|
71
74
|
* @since Added in 2.20.0
|
|
72
75
|
*/
|
|
73
76
|
export declare class StreamedLog {
|
|
74
|
-
private
|
|
75
|
-
private streamBuffer;
|
|
76
|
-
private splitMarker;
|
|
77
|
-
private relevancyTimeLimit;
|
|
78
|
-
private logClient;
|
|
79
|
-
private streamingTask;
|
|
80
|
-
private stopLogging;
|
|
77
|
+
#private;
|
|
81
78
|
constructor(options: StreamedLogOptions);
|
|
82
79
|
/**
|
|
83
80
|
* Start log redirection.
|
|
@@ -87,15 +84,6 @@ export declare class StreamedLog {
|
|
|
87
84
|
* Stop log redirection.
|
|
88
85
|
*/
|
|
89
86
|
stop(): Promise<void>;
|
|
90
|
-
/**
|
|
91
|
-
* Get log stream from response and redirect it to another log.
|
|
92
|
-
*/
|
|
93
|
-
private streamLog;
|
|
94
|
-
private logStreamChunks;
|
|
95
|
-
/**
|
|
96
|
-
* Parse the buffer and log complete messages.
|
|
97
|
-
*/
|
|
98
|
-
private logBufferContent;
|
|
99
87
|
}
|
|
100
88
|
/**
|
|
101
89
|
* @since Added in 2.20.0
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import c from 'ansi-colors';
|
|
2
|
+
import { z } from 'zod';
|
|
2
3
|
import log, { Logger, LogLevel } from '@apify/log';
|
|
3
4
|
import { ResourceClient } from '../base/resource_client.js';
|
|
4
|
-
import {
|
|
5
|
+
import { timeoutOptionsShape } from '../timeouts.js';
|
|
6
|
+
import { cast, catchNotFoundForResourceOrThrow, parseArgument } from '../utils.js';
|
|
7
|
+
const logOptionsSchema = z.strictObject({ raw: z.boolean().optional(), ...timeoutOptionsShape });
|
|
5
8
|
/**
|
|
6
9
|
* Client for accessing Actor run or build logs.
|
|
7
10
|
*
|
|
@@ -39,15 +42,18 @@ export class LogClient extends ResourceClient {
|
|
|
39
42
|
*
|
|
40
43
|
* @param options - Log retrieval options.
|
|
41
44
|
* @param options.raw - If `true`, returns raw log content without any processing. Default is `false`.
|
|
45
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
42
46
|
* @returns The log content as a string, or `undefined` if it does not exist. A chained client such as
|
|
43
47
|
* `run.log()` throws an `ApifyApiError` on a 404, since the run itself may be what is missing.
|
|
44
48
|
* @see https://docs.apify.com/api/v2/log-get
|
|
45
49
|
*/
|
|
46
50
|
async get(options = {}) {
|
|
51
|
+
const { timeoutSecs = 'long', ...params } = parseArgument(options, logOptionsSchema, 'LogOptions');
|
|
47
52
|
const requestOpts = {
|
|
48
|
-
url: this.
|
|
53
|
+
url: this.buildUrl(),
|
|
49
54
|
method: 'GET',
|
|
50
|
-
params: this.
|
|
55
|
+
params: this.buildParams(params),
|
|
56
|
+
timeoutSecs,
|
|
51
57
|
};
|
|
52
58
|
try {
|
|
53
59
|
const response = await this.httpClient.call(requestOpts);
|
|
@@ -63,20 +69,23 @@ export class LogClient extends ResourceClient {
|
|
|
63
69
|
*
|
|
64
70
|
* @param options - Log retrieval options.
|
|
65
71
|
* @param options.raw - If `true`, returns raw log content without any processing. Default is `false`.
|
|
72
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
66
73
|
* @returns The log content as a Readable stream, or `undefined` if it does not exist. A chained client such as
|
|
67
74
|
* `run.log()` throws an `ApifyApiError` on a 404, since the run itself may be what is missing.
|
|
68
75
|
* @see https://docs.apify.com/api/v2/log-get
|
|
69
76
|
*/
|
|
70
77
|
async stream(options = {}) {
|
|
78
|
+
const { timeoutSecs = 'long', raw } = parseArgument(options, logOptionsSchema, 'LogOptions');
|
|
71
79
|
const params = {
|
|
72
80
|
stream: true,
|
|
73
|
-
raw
|
|
81
|
+
raw,
|
|
74
82
|
};
|
|
75
83
|
const requestOpts = {
|
|
76
|
-
url: this.
|
|
84
|
+
url: this.buildUrl(),
|
|
77
85
|
method: 'GET',
|
|
78
|
-
params: this.
|
|
86
|
+
params: this.buildParams(params),
|
|
79
87
|
responseType: 'stream',
|
|
88
|
+
timeoutSecs,
|
|
80
89
|
};
|
|
81
90
|
try {
|
|
82
91
|
const response = await this.httpClient.call(requestOpts);
|
|
@@ -121,39 +130,39 @@ export class LoggerActorRedirect extends Logger {
|
|
|
121
130
|
* @since Added in 2.20.0
|
|
122
131
|
*/
|
|
123
132
|
export class StreamedLog {
|
|
124
|
-
destinationLog;
|
|
125
|
-
streamBuffer = [];
|
|
126
|
-
splitMarker = /(?:\n|^)(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)/g;
|
|
127
|
-
relevancyTimeLimit;
|
|
128
|
-
logClient;
|
|
129
|
-
streamingTask = null;
|
|
130
|
-
stopLogging = false;
|
|
133
|
+
#destinationLog;
|
|
134
|
+
#streamBuffer = [];
|
|
135
|
+
#splitMarker = /(?:\n|^)(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)/g;
|
|
136
|
+
#relevancyTimeLimit;
|
|
137
|
+
#logClient;
|
|
138
|
+
#streamingTask = null;
|
|
139
|
+
#stopLogging = false;
|
|
131
140
|
constructor(options) {
|
|
132
141
|
const { toLog, logClient, fromStart = true } = options;
|
|
133
|
-
this
|
|
134
|
-
this
|
|
135
|
-
this
|
|
142
|
+
this.#destinationLog = toLog;
|
|
143
|
+
this.#logClient = logClient;
|
|
144
|
+
this.#relevancyTimeLimit = fromStart ? null : new Date();
|
|
136
145
|
}
|
|
137
146
|
/**
|
|
138
147
|
* Start log redirection.
|
|
139
148
|
*/
|
|
140
149
|
start() {
|
|
141
|
-
if (this
|
|
150
|
+
if (this.#streamingTask) {
|
|
142
151
|
throw new Error('Streaming task already active');
|
|
143
152
|
}
|
|
144
|
-
this
|
|
145
|
-
this
|
|
153
|
+
this.#stopLogging = false;
|
|
154
|
+
this.#streamingTask = this.#streamLog();
|
|
146
155
|
}
|
|
147
156
|
/**
|
|
148
157
|
* Stop log redirection.
|
|
149
158
|
*/
|
|
150
159
|
async stop() {
|
|
151
|
-
if (!this
|
|
160
|
+
if (!this.#streamingTask) {
|
|
152
161
|
throw new Error('Streaming task is not active');
|
|
153
162
|
}
|
|
154
|
-
this
|
|
163
|
+
this.#stopLogging = true;
|
|
155
164
|
try {
|
|
156
|
-
await this
|
|
165
|
+
await this.#streamingTask;
|
|
157
166
|
}
|
|
158
167
|
catch (err) {
|
|
159
168
|
if (!(err instanceof Error && err.name === 'AbortError')) {
|
|
@@ -161,30 +170,30 @@ export class StreamedLog {
|
|
|
161
170
|
}
|
|
162
171
|
}
|
|
163
172
|
finally {
|
|
164
|
-
this
|
|
173
|
+
this.#streamingTask = null;
|
|
165
174
|
}
|
|
166
175
|
}
|
|
167
176
|
/**
|
|
168
177
|
* Get log stream from response and redirect it to another log.
|
|
169
178
|
*/
|
|
170
|
-
async streamLog() {
|
|
179
|
+
async #streamLog() {
|
|
171
180
|
try {
|
|
172
|
-
const logStream = await this
|
|
181
|
+
const logStream = await this.#logClient.stream({ raw: true });
|
|
173
182
|
if (!logStream) {
|
|
174
183
|
return;
|
|
175
184
|
}
|
|
176
|
-
const lastChunkRemainder = await this
|
|
185
|
+
const lastChunkRemainder = await this.#logStreamChunks(logStream);
|
|
177
186
|
// Process whatever is left when exiting. Maybe it is incomplete, maybe it is last log without EOL.
|
|
178
187
|
const lastMessage = Buffer.from(lastChunkRemainder).toString().trim();
|
|
179
188
|
if (lastMessage.length) {
|
|
180
|
-
this
|
|
189
|
+
this.#destinationLog.info(lastMessage);
|
|
181
190
|
}
|
|
182
191
|
}
|
|
183
192
|
catch (err) {
|
|
184
193
|
log.warning(`Log redirection stopped due to error`, err);
|
|
185
194
|
}
|
|
186
195
|
}
|
|
187
|
-
async logStreamChunks(logStream) {
|
|
196
|
+
async #logStreamChunks(logStream) {
|
|
188
197
|
// Chunk may be incomplete. Keep remainder for next chunk.
|
|
189
198
|
let previousChunkRemainder = new Uint8Array();
|
|
190
199
|
for await (const chunk of logStream) {
|
|
@@ -196,10 +205,10 @@ export class StreamedLog {
|
|
|
196
205
|
const lastCompleteMessageIndex = chunkWithPreviousRemainder.lastIndexOf(0x0a);
|
|
197
206
|
previousChunkRemainder = chunkWithPreviousRemainder.slice(lastCompleteMessageIndex);
|
|
198
207
|
// Push complete part of the chunk to the buffer
|
|
199
|
-
this
|
|
200
|
-
this
|
|
208
|
+
this.#streamBuffer.push(Buffer.from(chunkWithPreviousRemainder.slice(0, lastCompleteMessageIndex)));
|
|
209
|
+
this.#logBufferContent();
|
|
201
210
|
// Keep processing the new data until stopped
|
|
202
|
-
if (this
|
|
211
|
+
if (this.#stopLogging) {
|
|
203
212
|
break;
|
|
204
213
|
}
|
|
205
214
|
}
|
|
@@ -208,26 +217,26 @@ export class StreamedLog {
|
|
|
208
217
|
/**
|
|
209
218
|
* Parse the buffer and log complete messages.
|
|
210
219
|
*/
|
|
211
|
-
logBufferContent() {
|
|
212
|
-
const allParts = Buffer.concat(this
|
|
220
|
+
#logBufferContent() {
|
|
221
|
+
const allParts = Buffer.concat(this.#streamBuffer).toString().split(this.#splitMarker).slice(1);
|
|
213
222
|
// Parse the buffer parts into complete messages
|
|
214
223
|
const messageMarkers = allParts.filter((_, i) => i % 2 === 0);
|
|
215
224
|
const messageContents = allParts.filter((_, i) => i % 2 !== 0);
|
|
216
|
-
this
|
|
225
|
+
this.#streamBuffer = [];
|
|
217
226
|
messageMarkers.forEach((marker, index) => {
|
|
218
227
|
const decodedMarker = marker;
|
|
219
228
|
const decodedContent = messageContents[index];
|
|
220
|
-
if (this
|
|
229
|
+
if (this.#relevancyTimeLimit) {
|
|
221
230
|
// Log only relevant messages. Ignore too old log messages.
|
|
222
231
|
const logTime = new Date(decodedMarker);
|
|
223
|
-
if (logTime < this
|
|
232
|
+
if (logTime < this.#relevancyTimeLimit) {
|
|
224
233
|
return;
|
|
225
234
|
}
|
|
226
235
|
}
|
|
227
236
|
const message = decodedMarker + decodedContent;
|
|
228
237
|
// Original log level information is not available. Log all on info level. Log level could be guessed for
|
|
229
238
|
// some logs, but for any multiline logs such guess would be probably correct only for the first line.
|
|
230
|
-
this
|
|
239
|
+
this.#destinationLog.info(message.trim());
|
|
231
240
|
});
|
|
232
241
|
}
|
|
233
242
|
}
|