apify-client 3.0.0-beta.9 → 3.0.0-rc.0

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 (116) hide show
  1. package/README.md +10 -2
  2. package/dist/apify_api_error.d.ts +11 -26
  3. package/dist/apify_api_error.js +31 -22
  4. package/dist/apify_client.d.ts +125 -10
  5. package/dist/apify_client.js +133 -48
  6. package/dist/base/api_client.d.ts +7 -7
  7. package/dist/base/api_client.js +36 -24
  8. package/dist/base/resource_client.d.ts +21 -8
  9. package/dist/base/resource_client.js +67 -33
  10. package/dist/base/resource_collection_client.d.ts +12 -4
  11. package/dist/base/resource_collection_client.js +34 -11
  12. package/dist/body_parser.d.ts +3 -4
  13. package/dist/body_parser.js +19 -26
  14. package/dist/bundle.js +27 -22
  15. package/dist/bundle.js.map +1 -1
  16. package/dist/generated/api.d.ts +785 -290
  17. package/dist/generated/schemas.d.ts +720 -747
  18. package/dist/generated/schemas.js +166 -162
  19. package/dist/http_clients/axios.d.ts +60 -0
  20. package/dist/http_clients/axios.js +117 -0
  21. package/dist/http_clients/base.d.ts +331 -0
  22. package/dist/http_clients/base.js +563 -0
  23. package/dist/http_clients/index.d.ts +4 -0
  24. package/dist/http_clients/index.js +2 -0
  25. package/dist/http_compressors/base.d.ts +40 -0
  26. package/dist/http_compressors/base.js +9 -0
  27. package/dist/http_compressors/brotli.d.ts +33 -0
  28. package/dist/http_compressors/brotli.js +39 -0
  29. package/dist/http_compressors/gzip.d.ts +33 -0
  30. package/dist/http_compressors/gzip.js +39 -0
  31. package/dist/http_compressors/index.d.ts +6 -0
  32. package/dist/http_compressors/index.js +2 -0
  33. package/dist/http_compressors/resolve.d.ts +23 -0
  34. package/dist/http_compressors/resolve.js +27 -0
  35. package/dist/index.d.ts +4 -1
  36. package/dist/index.js +3 -1
  37. package/dist/invalid_response_body_error.d.ts +13 -0
  38. package/dist/invalid_response_body_error.js +17 -0
  39. package/dist/models.d.ts +162 -194
  40. package/dist/models.js +6 -4
  41. package/dist/resource_clients/actor.d.ts +74 -34
  42. package/dist/resource_clients/actor.js +87 -56
  43. package/dist/resource_clients/actor_collection.d.ts +18 -12
  44. package/dist/resource_clients/actor_collection.js +11 -3
  45. package/dist/resource_clients/actor_env_var.d.ts +11 -4
  46. package/dist/resource_clients/actor_env_var.js +16 -6
  47. package/dist/resource_clients/actor_env_var_collection.d.ts +9 -4
  48. package/dist/resource_clients/actor_env_var_collection.js +11 -4
  49. package/dist/resource_clients/actor_version.d.ts +20 -5
  50. package/dist/resource_clients/actor_version.js +19 -9
  51. package/dist/resource_clients/actor_version_collection.d.ts +11 -4
  52. package/dist/resource_clients/actor_version_collection.js +13 -4
  53. package/dist/resource_clients/build.d.ts +20 -7
  54. package/dist/resource_clients/build.js +35 -15
  55. package/dist/resource_clients/build_collection.d.ts +7 -5
  56. package/dist/resource_clients/build_collection.js +4 -1
  57. package/dist/resource_clients/dataset.d.ts +53 -35
  58. package/dist/resource_clients/dataset.js +105 -80
  59. package/dist/resource_clients/dataset_collection.d.ts +10 -6
  60. package/dist/resource_clients/dataset_collection.js +13 -2
  61. package/dist/resource_clients/key_value_store.d.ts +62 -20
  62. package/dist/resource_clients/key_value_store.js +78 -40
  63. package/dist/resource_clients/key_value_store_collection.d.ts +10 -6
  64. package/dist/resource_clients/key_value_store_collection.js +13 -2
  65. package/dist/resource_clients/log.d.ts +13 -20
  66. package/dist/resource_clients/log.js +141 -59
  67. package/dist/resource_clients/request_queue.d.ts +68 -31
  68. package/dist/resource_clients/request_queue.js +173 -91
  69. package/dist/resource_clients/request_queue_collection.d.ts +9 -5
  70. package/dist/resource_clients/request_queue_collection.js +9 -3
  71. package/dist/resource_clients/run.d.ts +119 -28
  72. package/dist/resource_clients/run.js +190 -49
  73. package/dist/resource_clients/run_collection.d.ts +5 -3
  74. package/dist/resource_clients/run_collection.js +4 -1
  75. package/dist/resource_clients/schedule.d.ts +16 -7
  76. package/dist/resource_clients/schedule.js +30 -22
  77. package/dist/resource_clients/schedule_collection.d.ts +6 -2
  78. package/dist/resource_clients/schedule_collection.js +9 -3
  79. package/dist/resource_clients/store_collection.d.ts +6 -4
  80. package/dist/resource_clients/store_collection.js +4 -1
  81. package/dist/resource_clients/task.d.ts +36 -13
  82. package/dist/resource_clients/task.js +79 -46
  83. package/dist/resource_clients/task_collection.d.ts +9 -5
  84. package/dist/resource_clients/task_collection.js +9 -3
  85. package/dist/resource_clients/user.d.ts +19 -10
  86. package/dist/resource_clients/user.js +42 -38
  87. package/dist/resource_clients/webhook.d.ts +17 -8
  88. package/dist/resource_clients/webhook.js +30 -22
  89. package/dist/resource_clients/webhook_collection.d.ts +8 -4
  90. package/dist/resource_clients/webhook_collection.js +9 -3
  91. package/dist/resource_clients/webhook_dispatch.d.ts +4 -1
  92. package/dist/resource_clients/webhook_dispatch.js +7 -2
  93. package/dist/resource_clients/webhook_dispatch_collection.d.ts +3 -1
  94. package/dist/resource_clients/webhook_dispatch_collection.js +4 -1
  95. package/dist/runtime/node.d.ts +6 -0
  96. package/dist/runtime/node.js +49 -0
  97. package/dist/runtime/types.d.ts +52 -0
  98. package/dist/runtime/types.js +1 -0
  99. package/dist/runtime/web.d.ts +5 -0
  100. package/dist/runtime/web.js +18 -0
  101. package/dist/schemas.d.ts +1 -1
  102. package/dist/schemas.js +1 -1
  103. package/dist/statistics.d.ts +1 -6
  104. package/dist/statistics.js +2 -2
  105. package/dist/timeouts.d.ts +84 -0
  106. package/dist/timeouts.js +37 -0
  107. package/dist/timezones.d.ts +1 -1
  108. package/dist/utils.d.ts +82 -45
  109. package/dist/utils.js +197 -146
  110. package/dist/wait_for_resources.d.ts +8 -0
  111. package/dist/wait_for_resources.js +39 -0
  112. package/package.json +32 -29
  113. package/dist/http_client.d.ts +0 -88
  114. package/dist/http_client.js +0 -255
  115. package/dist/interceptors.d.ts +0 -19
  116. package/dist/interceptors.js +0 -105
@@ -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 { optionalSignalSchema, 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,33 @@ export class DatasetCollectionClient 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 Datasets.
62
65
  * @see https://docs.apify.com/api/v2/datasets-get
63
66
  */
64
67
  list(options = {}) {
65
68
  const parsed = parseArgument(options, listOptionsSchema, 'DatasetCollectionClientListOptions');
66
- return this._listPaginated(schemas.ListOfDatasets(), parsed);
69
+ return this.listResourcesPaginated(schemas.ListOfDatasets(), parsed, 'medium');
67
70
  }
68
71
  /**
69
72
  * Gets or creates a dataset with the specified name.
70
73
  *
71
74
  * @param name - Name of the dataset. If not provided, a default dataset is used.
72
75
  * @param options - Additional options like schema.
76
+ * @param options.schema - Schema of the dataset.
77
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
73
78
  * @returns The dataset object.
74
79
  * @see https://docs.apify.com/api/v2/datasets-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
- return this._getOrCreate(schemas.Dataset(), name, options);
84
+ parseArgument(options?.timeoutSecs, optionalTimeoutSchema);
85
+ parseArgument(options?.signal, optionalSignalSchema);
86
+ // `timeoutSecs` and `signal` are not part of the resource, so the body carries only the rest, and stays
87
+ // absent when there is nothing else to send.
88
+ const { timeoutSecs = 'short', signal, ...resource } = options ?? {};
89
+ const hasResource = Object.keys(resource).length > 0;
90
+ return this.getOrCreateResource(schemas.DatasetResource(), name, hasResource ? resource : undefined, timeoutSecs, signal);
80
91
  }
81
92
  }
@@ -1,10 +1,11 @@
1
1
  import type { Readable } from 'node:stream';
2
- import type { JsonValue } from 'type-fest';
2
+ import type { JsonValue, TypedArray } from 'type-fest';
3
3
  import type { STORAGE_GENERAL_ACCESS } from '@apify/consts';
4
4
  import type { ApiClientSubResourceOptions } from '../base/api_client.js';
5
5
  import { ResourceClient } from '../base/resource_client.js';
6
- import type { KeyValueClientListKeysResult, KeyValueListItem, KeyValueStore } from '../models.js';
7
- export type { KeyValueClientListKeysResult, KeyValueListItem, KeyValueStore, KeyValueStoreStats } from '../models.js';
6
+ import type { KeyValueStoreResource, KeyValueStoreKey, ListOfKeys } from '../models.js';
7
+ import type { TimeoutOptions } from '../timeouts.js';
8
+ export type { KeyValueStoreResource, KeyValueStoreKey, KeyValueStoreStats, ListOfKeys } from '../models.js';
8
9
  /**
9
10
  * Client for managing a specific key-value store.
10
11
  *
@@ -41,10 +42,12 @@ export declare class KeyValueStoreClient extends ResourceClient {
41
42
  /**
42
43
  * Gets the key-value store object from the Apify API.
43
44
  *
45
+ * @param options - Request options
46
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
44
47
  * @returns The KeyValueStore object, or `undefined` if it does not exist
45
48
  * @see https://docs.apify.com/api/v2/key-value-store-get
46
49
  */
47
- get(): Promise<KeyValueStore | undefined>;
50
+ get(options?: TimeoutOptions): Promise<KeyValueStoreResource | undefined>;
48
51
  /**
49
52
  * Updates the key-value store with specified fields.
50
53
  *
@@ -52,16 +55,20 @@ export declare class KeyValueStoreClient extends ResourceClient {
52
55
  * @param newFields.name - New name for the store
53
56
  * @param newFields.title - New title for the store
54
57
  * @param newFields.generalAccess - General resource access level ('FOLLOW_USER_SETTING', 'ANYONE_WITH_ID_CAN_READ' or 'RESTRICTED')
58
+ * @param options - Request options
59
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
55
60
  * @returns The updated KeyValueStore object
56
61
  * @see https://docs.apify.com/api/v2/key-value-store-put
57
62
  */
58
- update(newFields: KeyValueClientUpdateOptions): Promise<KeyValueStore>;
63
+ update(newFields: KeyValueClientUpdateOptions, options?: TimeoutOptions): Promise<KeyValueStoreResource>;
59
64
  /**
60
65
  * Deletes the key-value store.
61
66
  *
67
+ * @param options - Request options
68
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
62
69
  * @see https://docs.apify.com/api/v2/key-value-store-delete
63
70
  */
64
- delete(): Promise<void>;
71
+ delete(options?: TimeoutOptions): Promise<void>;
65
72
  /**
66
73
  * Lists all keys in the key-value store.
67
74
  *
@@ -73,6 +80,7 @@ export declare class KeyValueStoreClient extends ResourceClient {
73
80
  * @param options.exclusiveStartKey - Key to start listing from (for pagination). The listing starts with the next key after this one.
74
81
  * @param options.collection - Filter keys by collection name.
75
82
  * @param options.prefix - Filter keys that start with this prefix.
83
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
76
84
  * @returns Object containing `items` array of key metadata, pagination info (`count`, `limit`, `isTruncated`, `nextExclusiveStartKey`)
77
85
  * @see https://docs.apify.com/api/v2/key-value-store-keys-get
78
86
  *
@@ -97,7 +105,7 @@ export declare class KeyValueStoreClient extends ResourceClient {
97
105
  * } while (result.isTruncated);
98
106
  * ```
99
107
  */
100
- listKeys(options?: KeyValueClientListKeysOptions): Promise<KeyValueClientListKeysResult> & AsyncIterable<KeyValueListItem>;
108
+ listKeys(options?: KeyValueClientListKeysOptions): Promise<ListOfKeys> & AsyncIterable<KeyValueStoreKey>;
101
109
  /**
102
110
  * Generates a public URL for accessing a specific record in the key-value store.
103
111
  *
@@ -106,6 +114,8 @@ export declare class KeyValueStoreClient extends ResourceClient {
106
114
  * requiring an API token.
107
115
  *
108
116
  * @param key - The record key
117
+ * @param options - Request options
118
+ * @param options.timeoutSecs - Timeout for the API request that fetches the store. Default is `'long'`.
109
119
  * @returns A public URL string for accessing the record
110
120
  *
111
121
  * @example
@@ -116,7 +126,7 @@ export declare class KeyValueStoreClient extends ResourceClient {
116
126
  * ```
117
127
  * @since Added in 2.14.0
118
128
  */
119
- getRecordPublicUrl(key: string): Promise<string>;
129
+ getRecordPublicUrl(key: string, options?: TimeoutOptions): Promise<string>;
120
130
  /**
121
131
  * Generates a public URL for accessing the list of keys in the key-value store.
122
132
  *
@@ -127,6 +137,7 @@ export declare class KeyValueStoreClient extends ResourceClient {
127
137
  * @param options.expiresInSecs - Number of seconds until the signed URL expires. If omitted, the URL never expires.
128
138
  * @param options.limit - Maximum number of keys to return.
129
139
  * @param options.prefix - Filter keys by prefix.
140
+ * @param options.timeoutSecs - Timeout for the API request that fetches the store. Default is `'long'`.
130
141
  * @returns A public URL string for accessing the keys list
131
142
  *
132
143
  * @example
@@ -147,6 +158,8 @@ export declare class KeyValueStoreClient extends ResourceClient {
147
158
  * This is more efficient than {@link getRecord} when you only need to check for existence.
148
159
  *
149
160
  * @param key - The record key to check
161
+ * @param options - Request options
162
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
150
163
  * @returns `true` if the record exists, `false` if it does not
151
164
  * @see https://docs.apify.com/api/v2/key-value-store-record-get
152
165
  *
@@ -159,15 +172,27 @@ export declare class KeyValueStoreClient extends ResourceClient {
159
172
  * ```
160
173
  * @since Added in 2.9.0
161
174
  */
162
- recordExists(key: string): Promise<boolean>;
175
+ recordExists(key: string, options?: TimeoutOptions): Promise<boolean>;
163
176
  /**
177
+ * By default, the value is parsed by the record's content type: `application/json` is parsed into a value,
178
+ * `text/*` and `application/*xml` are decoded to a string, and anything else is returned as a Buffer (Node.js)
179
+ * or ArrayBuffer (browser). A record whose body does not parse as its content type claims, such as a non-JSON
180
+ * string stored under `application/json`, makes the call throw an {@link InvalidResponseBodyError}, which
181
+ * carries the raw body in `error.response`.
182
+ *
164
183
  * You can use the `buffer` option to get the value in a Buffer (Node.js)
165
- * or ArrayBuffer (browser) format. In Node.js (not in browser) you can also
184
+ * or ArrayBuffer (browser) format, which skips the parsing. In Node.js (not in browser) you can also
166
185
  * use the `stream` option to get a Readable stream.
167
186
  *
168
187
  * When the record does not exist, the function resolves to `undefined`. It does
169
- * NOT resolve to a `KeyValueStore` record with an `undefined` value.
188
+ * NOT resolve to a `KeyValueStoreRecord` with an `undefined` value.
170
189
  *
190
+ * @param key - The record key
191
+ * @param options - Retrieval options
192
+ * @param options.buffer - If `true`, the value is returned as a Buffer (Node.js) or ArrayBuffer (browser).
193
+ * @param options.stream - If `true`, the value is returned as a Readable stream. Node.js only.
194
+ * @param options.signature - Signature of a public URL, for reading a record without a token.
195
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
171
196
  * @see https://docs.apify.com/api/v2/key-value-store-record-get
172
197
  */
173
198
  getRecord(key: string): Promise<KeyValueStoreRecord<JsonValue> | undefined>;
@@ -179,19 +204,27 @@ export declare class KeyValueStoreClient extends ResourceClient {
179
204
  * The content type is automatically determined based on the value type, but can be
180
205
  * overridden using the `contentType` property.
181
206
  *
207
+ * A string is sent as it is, even under `application/json`, so a string stored under a JSON content type has
208
+ * to be valid JSON already, or {@link getRecord} fails to parse it back.
209
+ *
182
210
  * **Note about streams:** If the value is a stream object (has `.pipe` and `.on` methods),
183
211
  * the upload cannot be retried on failure or follow redirects. For reliable uploads,
184
212
  * buffer the entire stream into memory first.
185
213
  *
186
214
  * @param record - The record to store
187
215
  * @param record.key - Record key (unique identifier)
188
- * @param record.value - Record value (object, string, Buffer, or Stream)
216
+ * @param record.value - Record value (a JSON-serializable value, Buffer, ArrayBuffer, typed array, or Readable)
189
217
  * @param record.contentType - Optional MIME type. Auto-detected if not provided:
190
218
  * - Objects: `'application/json; charset=utf-8'`
191
219
  * - Strings: `'text/plain; charset=utf-8'`
192
- * - Buffers/Streams: `'application/octet-stream'`
220
+ * - Binary values and streams: `'application/octet-stream'`
221
+ *
222
+ * Worth setting for media and archives: the client skips compressing a body
223
+ * whose content type already carries its own compression. The
224
+ * `'application/octet-stream'` fallback is treated as compressible, because
225
+ * it could hold any binary data.
193
226
  * @param options - Storage options
194
- * @param options.timeoutSecs - Timeout for the upload in seconds. Default varies by value size.
227
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
195
228
  * @param options.doNotRetryTimeouts - If `true`, don't retry on timeout errors. Default is `false`.
196
229
  * @see https://docs.apify.com/api/v2/key-value-store-record-put
197
230
  *
@@ -219,11 +252,13 @@ export declare class KeyValueStoreClient extends ResourceClient {
219
252
  * });
220
253
  * ```
221
254
  */
222
- setRecord(record: KeyValueStoreRecord<JsonValue>, options?: KeyValueStoreRecordOptions): Promise<void>;
255
+ setRecord(record: KeyValueStoreRecord<KeyValueStoreRecordValue>, options?: KeyValueStoreRecordOptions): Promise<void>;
223
256
  /**
224
257
  * Deletes a record from the key-value store.
225
258
  *
226
259
  * @param key - The record key to delete
260
+ * @param options - Request options
261
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
227
262
  * @see https://docs.apify.com/api/v2/key-value-store-record-delete
228
263
  *
229
264
  * @example
@@ -231,7 +266,7 @@ export declare class KeyValueStoreClient extends ResourceClient {
231
266
  * await client.keyValueStore('my-store').deleteRecord('temp-data');
232
267
  * ```
233
268
  */
234
- deleteRecord(key: string): Promise<void>;
269
+ deleteRecord(key: string, options?: TimeoutOptions): Promise<void>;
235
270
  }
236
271
  /**
237
272
  * Options for updating a Key-Value Store.
@@ -250,7 +285,7 @@ export interface KeyValueClientUpdateOptions {
250
285
  /**
251
286
  * Options for listing keys in a Key-Value Store.
252
287
  */
253
- export interface KeyValueClientListKeysOptions {
288
+ export interface KeyValueClientListKeysOptions extends TimeoutOptions {
254
289
  limit?: number;
255
290
  exclusiveStartKey?: string;
256
291
  collection?: string;
@@ -276,7 +311,7 @@ export interface KeyValueClientCreateKeysUrlOptions extends Omit<KeyValueClientL
276
311
  /**
277
312
  * Options for retrieving a record from a Key-Value Store.
278
313
  */
279
- export interface KeyValueClientGetRecordOptions {
314
+ export interface KeyValueClientGetRecordOptions extends TimeoutOptions {
280
315
  buffer?: boolean;
281
316
  stream?: boolean;
282
317
  /**
@@ -284,6 +319,14 @@ export interface KeyValueClientGetRecordOptions {
284
319
  */
285
320
  signature?: string;
286
321
  }
322
+ /**
323
+ * A value that `setRecord` accepts.
324
+ *
325
+ * Anything JSON-serializable, or binary content the client uploads as bytes instead of JSON-encoding
326
+ * it: a `Buffer`, an `ArrayBuffer`, a typed array, or a readable stream. `Buffer` is a `Uint8Array`, so
327
+ * `TypedArray` covers it.
328
+ */
329
+ export type KeyValueStoreRecordValue = JsonValue | ArrayBuffer | TypedArray | Readable;
287
330
  /**
288
331
  * Represents a record (key-value pair) in a Key-Value Store.
289
332
  *
@@ -298,8 +341,7 @@ export interface KeyValueStoreRecord<T> {
298
341
  * Options for storing a record in a Key-Value Store.
299
342
  * @since Added in 2.12.4
300
343
  */
301
- export interface KeyValueStoreRecordOptions {
302
- timeoutSecs?: number;
344
+ export interface KeyValueStoreRecordOptions extends TimeoutOptions {
303
345
  doNotRetryTimeouts?: boolean;
304
346
  }
305
347
  /**
@@ -1,15 +1,18 @@
1
1
  import { z } from 'zod';
2
2
  import log from '@apify/log';
3
3
  import { createHmacSignatureAsync, createStorageContentSignatureAsync } from '@apify/utilities';
4
- import { DEFAULT_TIMEOUT_MILLIS, MEDIUM_TIMEOUT_MILLIS, ResourceClient, SMALL_TIMEOUT_MILLIS, } from '../base/resource_client.js';
4
+ import { ResourceClient } from '../base/resource_client.js';
5
5
  import * as schemas from '../schemas.js';
6
- import { anyObjectSchema, applyQueryParamsToUrl, catchNotFoundOrThrow, isBuffer, isNode, isStream, parseArgument, parseResponse, } from '../utils.js';
6
+ import { timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
7
+ import { runtime } from '#runtime';
8
+ import { anyObjectSchema, applyQueryParamsToUrl, catchNotFoundOrThrow, isBuffer, isStream, parseArgument, parseResponse, } from '../utils.js';
7
9
  const listKeysOptionsSchema = z.strictObject({
8
10
  limit: z.number().min(0).optional(),
9
11
  exclusiveStartKey: z.string().optional(),
10
12
  collection: z.string().optional(),
11
13
  prefix: z.string().optional(),
12
14
  signature: z.string().optional(),
15
+ ...timeoutOptionsShape,
13
16
  });
14
17
  const nonEmptyKeySchema = z.string().min(1);
15
18
  // `signature` is left out - this method produces one. The options type omits it to match.
@@ -19,6 +22,7 @@ const createKeysPublicUrlOptionsSchema = z.strictObject({
19
22
  collection: z.string().optional(),
20
23
  prefix: z.string().optional(),
21
24
  expiresInSecs: z.number().optional(),
25
+ ...timeoutOptionsShape,
22
26
  });
23
27
  const keySchema = z.string();
24
28
  const getRecordOptionsSchema = z.strictObject({
@@ -26,6 +30,7 @@ const getRecordOptionsSchema = z.strictObject({
26
30
  stream: z.boolean().optional(),
27
31
  disableRedirect: z.boolean().optional(),
28
32
  signature: z.string().optional(),
33
+ ...timeoutOptionsShape,
29
34
  });
30
35
  const recordSchema = z.strictObject({
31
36
  key: z.string(),
@@ -37,11 +42,11 @@ const recordSchema = z.strictObject({
37
42
  typeof value !== 'symbol' &&
38
43
  typeof value !== 'bigint' &&
39
44
  typeof value !== 'function' &&
40
- (typeof value !== 'number' || Number.isFinite(value)), 'Expected a defined, JSON-serializable value'),
45
+ (typeof value !== 'number' || Number.isFinite(value)), 'Expected a JSON-serializable value, binary data, or a stream'),
41
46
  contentType: z.string().min(1).optional(),
42
47
  });
43
48
  const recordOptionsSchema = z.strictObject({
44
- timeoutSecs: z.number().optional(),
49
+ ...timeoutOptionsShape,
45
50
  doNotRetryTimeouts: z.boolean().optional(),
46
51
  });
47
52
  /**
@@ -85,11 +90,14 @@ export class KeyValueStoreClient extends ResourceClient {
85
90
  /**
86
91
  * Gets the key-value store object from the Apify API.
87
92
  *
93
+ * @param options - Request options
94
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
88
95
  * @returns The KeyValueStore object, or `undefined` if it does not exist
89
96
  * @see https://docs.apify.com/api/v2/key-value-store-get
90
97
  */
91
- async get() {
92
- return this._get(schemas.KeyValueStore(), {}, SMALL_TIMEOUT_MILLIS);
98
+ async get(options = {}) {
99
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
100
+ return this.getResource(schemas.KeyValueStoreResource(), {}, timeoutSecs, signal);
93
101
  }
94
102
  /**
95
103
  * Updates the key-value store with specified fields.
@@ -98,20 +106,26 @@ export class KeyValueStoreClient extends ResourceClient {
98
106
  * @param newFields.name - New name for the store
99
107
  * @param newFields.title - New title for the store
100
108
  * @param newFields.generalAccess - General resource access level ('FOLLOW_USER_SETTING', 'ANYONE_WITH_ID_CAN_READ' or 'RESTRICTED')
109
+ * @param options - Request options
110
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
101
111
  * @returns The updated KeyValueStore object
102
112
  * @see https://docs.apify.com/api/v2/key-value-store-put
103
113
  */
104
- async update(newFields) {
114
+ async update(newFields, options = {}) {
105
115
  parseArgument(newFields, anyObjectSchema);
106
- return this._update(schemas.KeyValueStore(), newFields, DEFAULT_TIMEOUT_MILLIS);
116
+ const { timeoutSecs = 'long', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
117
+ return this.updateResource(schemas.KeyValueStoreResource(), newFields, timeoutSecs, signal);
107
118
  }
108
119
  /**
109
120
  * Deletes the key-value store.
110
121
  *
122
+ * @param options - Request options
123
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
111
124
  * @see https://docs.apify.com/api/v2/key-value-store-delete
112
125
  */
113
- async delete() {
114
- return this._delete(SMALL_TIMEOUT_MILLIS);
126
+ async delete(options = {}) {
127
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
128
+ return this.deleteResource(timeoutSecs, signal);
115
129
  }
116
130
  /**
117
131
  * Lists all keys in the key-value store.
@@ -124,6 +138,7 @@ export class KeyValueStoreClient extends ResourceClient {
124
138
  * @param options.exclusiveStartKey - Key to start listing from (for pagination). The listing starts with the next key after this one.
125
139
  * @param options.collection - Filter keys by collection name.
126
140
  * @param options.prefix - Filter keys that start with this prefix.
141
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
127
142
  * @returns Object containing `items` array of key metadata, pagination info (`count`, `limit`, `isTruncated`, `nextExclusiveStartKey`)
128
143
  * @see https://docs.apify.com/api/v2/key-value-store-keys-get
129
144
  *
@@ -149,13 +164,14 @@ export class KeyValueStoreClient extends ResourceClient {
149
164
  * ```
150
165
  */
151
166
  listKeys(options = {}) {
152
- const parsed = parseArgument(options, listKeysOptionsSchema, 'KeyValueClientListKeysOptions');
167
+ const { timeoutSecs = 'medium', signal, ...parsed } = parseArgument(options, listKeysOptionsSchema, 'KeyValueClientListKeysOptions');
153
168
  const getPaginatedList = async (kvsListOptions = {}) => {
154
169
  const response = await this.httpClient.call({
155
- url: this._url('keys'),
170
+ url: this.buildUrl('keys'),
156
171
  method: 'GET',
157
- params: this._params(kvsListOptions),
158
- timeout: MEDIUM_TIMEOUT_MILLIS,
172
+ params: this.buildParams(kvsListOptions),
173
+ timeoutSecs,
174
+ signal,
159
175
  });
160
176
  return parseResponse(response, schemas.ListOfKeys());
161
177
  };
@@ -194,6 +210,8 @@ export class KeyValueStoreClient extends ResourceClient {
194
210
  * requiring an API token.
195
211
  *
196
212
  * @param key - The record key
213
+ * @param options - Request options
214
+ * @param options.timeoutSecs - Timeout for the API request that fetches the store. Default is `'long'`.
197
215
  * @returns A public URL string for accessing the record
198
216
  *
199
217
  * @example
@@ -204,10 +222,11 @@ export class KeyValueStoreClient extends ResourceClient {
204
222
  * ```
205
223
  * @since Added in 2.14.0
206
224
  */
207
- async getRecordPublicUrl(key) {
225
+ async getRecordPublicUrl(key, options = {}) {
208
226
  parseArgument(key, nonEmptyKeySchema);
209
- const store = await this.get();
210
- const recordPublicUrl = new URL(this._publicUrl(['records', key]));
227
+ const { timeoutSecs = 'long', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
228
+ const store = await this.get({ timeoutSecs, signal });
229
+ const recordPublicUrl = new URL(this.buildPublicUrl(['records', key]));
211
230
  if (store?.urlSigningSecretKey) {
212
231
  const signature = await createHmacSignatureAsync(store.urlSigningSecretKey, key);
213
232
  recordPublicUrl.searchParams.append('signature', signature);
@@ -224,6 +243,7 @@ export class KeyValueStoreClient extends ResourceClient {
224
243
  * @param options.expiresInSecs - Number of seconds until the signed URL expires. If omitted, the URL never expires.
225
244
  * @param options.limit - Maximum number of keys to return.
226
245
  * @param options.prefix - Filter keys by prefix.
246
+ * @param options.timeoutSecs - Timeout for the API request that fetches the store. Default is `'long'`.
227
247
  * @returns A public URL string for accessing the keys list
228
248
  *
229
249
  * @example
@@ -238,10 +258,9 @@ export class KeyValueStoreClient extends ResourceClient {
238
258
  * @since Added in 2.13.0
239
259
  */
240
260
  async createKeysPublicUrl(options = {}) {
241
- const parsed = parseArgument(options, createKeysPublicUrlOptionsSchema, 'KeyValueClientCreateKeysUrlOptions');
242
- const store = await this.get();
243
- const { expiresInSecs, ...queryOptions } = parsed;
244
- let createdPublicKeysUrl = new URL(this._publicUrl('keys'));
261
+ const { timeoutSecs = 'long', signal, expiresInSecs, ...queryOptions } = parseArgument(options, createKeysPublicUrlOptionsSchema, 'KeyValueClientCreateKeysUrlOptions');
262
+ const store = await this.get({ timeoutSecs, signal });
263
+ let createdPublicKeysUrl = new URL(this.buildPublicUrl('keys'));
245
264
  if (store?.urlSigningSecretKey) {
246
265
  const signature = await createStorageContentSignatureAsync({
247
266
  resourceId: store.id,
@@ -259,6 +278,8 @@ export class KeyValueStoreClient extends ResourceClient {
259
278
  * This is more efficient than {@link getRecord} when you only need to check for existence.
260
279
  *
261
280
  * @param key - The record key to check
281
+ * @param options - Request options
282
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
262
283
  * @returns `true` if the record exists, `false` if it does not
263
284
  * @see https://docs.apify.com/api/v2/key-value-store-record-get
264
285
  *
@@ -271,11 +292,14 @@ export class KeyValueStoreClient extends ResourceClient {
271
292
  * ```
272
293
  * @since Added in 2.9.0
273
294
  */
274
- async recordExists(key) {
295
+ async recordExists(key, options = {}) {
296
+ const { timeoutSecs = 'long', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
275
297
  const requestOpts = {
276
- url: this._url(['records', key]),
298
+ url: this.buildUrl(['records', key]),
277
299
  method: 'HEAD',
278
- params: this._params(),
300
+ params: this.buildParams(),
301
+ timeoutSecs,
302
+ signal,
279
303
  };
280
304
  try {
281
305
  await this.httpClient.call(requestOpts);
@@ -289,7 +313,7 @@ export class KeyValueStoreClient extends ResourceClient {
289
313
  async getRecord(key, options = {}) {
290
314
  parseArgument(key, keySchema);
291
315
  const parsed = parseArgument(options, getRecordOptionsSchema, 'KeyValueClientGetRecordOptions');
292
- if (parsed.stream && !isNode()) {
316
+ if (parsed.stream && !runtime.isNode) {
293
317
  throw new Error('The stream option can only be used in Node.js environment.');
294
318
  }
295
319
  if ('disableRedirect' in parsed) {
@@ -300,13 +324,14 @@ export class KeyValueStoreClient extends ResourceClient {
300
324
  if (parsed.signature)
301
325
  queryParams.signature = parsed.signature;
302
326
  const requestOpts = {
303
- url: this._url(['records', key]),
327
+ url: this.buildUrl(['records', key]),
304
328
  method: 'GET',
305
- params: this._params(queryParams),
306
- timeout: DEFAULT_TIMEOUT_MILLIS,
329
+ params: this.buildParams(queryParams),
330
+ timeoutSecs: parsed.timeoutSecs ?? 'long',
331
+ signal: parsed.signal,
307
332
  };
308
333
  if (parsed.buffer)
309
- requestOpts.forceBuffer = true;
334
+ requestOpts.responseType = 'buffer';
310
335
  if (parsed.stream)
311
336
  requestOpts.responseType = 'stream';
312
337
  try {
@@ -329,19 +354,27 @@ export class KeyValueStoreClient extends ResourceClient {
329
354
  * The content type is automatically determined based on the value type, but can be
330
355
  * overridden using the `contentType` property.
331
356
  *
357
+ * A string is sent as it is, even under `application/json`, so a string stored under a JSON content type has
358
+ * to be valid JSON already, or {@link getRecord} fails to parse it back.
359
+ *
332
360
  * **Note about streams:** If the value is a stream object (has `.pipe` and `.on` methods),
333
361
  * the upload cannot be retried on failure or follow redirects. For reliable uploads,
334
362
  * buffer the entire stream into memory first.
335
363
  *
336
364
  * @param record - The record to store
337
365
  * @param record.key - Record key (unique identifier)
338
- * @param record.value - Record value (object, string, Buffer, or Stream)
366
+ * @param record.value - Record value (a JSON-serializable value, Buffer, ArrayBuffer, typed array, or Readable)
339
367
  * @param record.contentType - Optional MIME type. Auto-detected if not provided:
340
368
  * - Objects: `'application/json; charset=utf-8'`
341
369
  * - Strings: `'text/plain; charset=utf-8'`
342
- * - Buffers/Streams: `'application/octet-stream'`
370
+ * - Binary values and streams: `'application/octet-stream'`
371
+ *
372
+ * Worth setting for media and archives: the client skips compressing a body
373
+ * whose content type already carries its own compression. The
374
+ * `'application/octet-stream'` fallback is treated as compressible, because
375
+ * it could hold any binary data.
343
376
  * @param options - Storage options
344
- * @param options.timeoutSecs - Timeout for the upload in seconds. Default varies by value size.
377
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
345
378
  * @param options.doNotRetryTimeouts - If `true`, don't retry on timeout errors. Default is `false`.
346
379
  * @see https://docs.apify.com/api/v2/key-value-store-record-put
347
380
  *
@@ -374,7 +407,7 @@ export class KeyValueStoreClient extends ResourceClient {
374
407
  const parsed = parseArgument(options, recordOptionsSchema, 'KeyValueStoreRecordOptions');
375
408
  const { key } = record;
376
409
  let { value, contentType } = record;
377
- const { timeoutSecs, doNotRetryTimeouts } = parsed;
410
+ const { timeoutSecs = 'long', signal, doNotRetryTimeouts } = parsed;
378
411
  const isValueStreamOrBuffer = isStream(value) || isBuffer(value);
379
412
  // To allow saving Objects to JSON without providing content type
380
413
  if (!contentType) {
@@ -396,13 +429,14 @@ export class KeyValueStoreClient extends ResourceClient {
396
429
  }
397
430
  }
398
431
  const uploadOpts = {
399
- url: this._url(['records', key]),
432
+ url: this.buildUrl(['records', key]),
400
433
  method: 'PUT',
401
- params: this._params(),
434
+ params: this.buildParams(),
402
435
  data: value,
403
436
  headers: contentType ? { 'content-type': contentType } : undefined,
404
437
  doNotRetryTimeouts,
405
- timeout: timeoutSecs !== undefined ? timeoutSecs * 1000 : DEFAULT_TIMEOUT_MILLIS,
438
+ timeoutSecs,
439
+ signal,
406
440
  };
407
441
  await this.httpClient.call(uploadOpts);
408
442
  }
@@ -410,6 +444,8 @@ export class KeyValueStoreClient extends ResourceClient {
410
444
  * Deletes a record from the key-value store.
411
445
  *
412
446
  * @param key - The record key to delete
447
+ * @param options - Request options
448
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
413
449
  * @see https://docs.apify.com/api/v2/key-value-store-record-delete
414
450
  *
415
451
  * @example
@@ -417,13 +453,15 @@ export class KeyValueStoreClient extends ResourceClient {
417
453
  * await client.keyValueStore('my-store').deleteRecord('temp-data');
418
454
  * ```
419
455
  */
420
- async deleteRecord(key) {
456
+ async deleteRecord(key, options = {}) {
421
457
  parseArgument(key, keySchema);
458
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
422
459
  await this.httpClient.call({
423
- url: this._url(['records', key]),
460
+ url: this.buildUrl(['records', key]),
424
461
  method: 'DELETE',
425
- params: this._params(),
426
- timeout: SMALL_TIMEOUT_MILLIS,
462
+ params: this.buildParams(),
463
+ timeoutSecs,
464
+ signal,
427
465
  });
428
466
  }
429
467
  }
@@ -1,8 +1,9 @@
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
- import type { KeyValueStore } from './key_value_store.js';
6
+ import type { KeyValueStoreResource } from './key_value_store.js';
6
7
  /**
7
8
  * Client for managing the collection of Key-value stores in your account.
8
9
  *
@@ -45,21 +46,24 @@ 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
  */
51
- list(options?: KeyValueStoreCollectionClientListOptions): Promise<KeyValueStoreCollectionListResult> & AsyncIterable<KeyValueStore>;
53
+ list(options?: KeyValueStoreCollectionClientListOptions): Promise<KeyValueStoreCollectionListResult> & AsyncIterable<KeyValueStoreResource>;
52
54
  /**
53
55
  * Gets or creates a key-value store with the specified name.
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
- getOrCreate(name?: string, options?: KeyValueStoreCollectionClientGetOrCreateOptions): Promise<KeyValueStore>;
64
+ getOrCreate(name?: string, options?: KeyValueStoreCollectionClientGetOrCreateOptions): Promise<KeyValueStoreResource>;
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,9 +75,9 @@ 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
- export type KeyValueStoreCollectionListResult = PaginatedList<KeyValueStore> & {
81
+ export type KeyValueStoreCollectionListResult = PaginatedList<KeyValueStoreResource> & {
78
82
  unnamed: boolean;
79
83
  };