apify-client 3.0.0-beta.1 → 3.0.0-beta.100
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/README.md +21 -5
- package/dist/apify_api_error.d.ts +64 -22
- package/dist/apify_api_error.js +100 -30
- package/dist/apify_client.d.ts +150 -35
- package/dist/apify_client.js +198 -118
- package/dist/apify_client_error.d.ts +10 -0
- package/dist/apify_client_error.js +10 -0
- package/dist/base/api_client.d.ts +9 -9
- package/dist/base/api_client.js +41 -45
- package/dist/base/resource_client.d.ts +24 -10
- package/dist/base/resource_client.js +78 -48
- package/dist/base/resource_collection_client.d.ts +15 -6
- package/dist/base/resource_collection_client.js +40 -21
- package/dist/body_parser.d.ts +3 -4
- package/dist/body_parser.js +20 -32
- package/dist/bundle.js +47 -52
- package/dist/bundle.js.map +1 -1
- package/dist/generated/api.d.ts +3363 -1185
- package/dist/generated/api.js +1 -2
- package/dist/generated/schemas.d.ts +8146 -0
- package/dist/generated/schemas.js +1866 -0
- package/dist/http_clients/axios.d.ts +61 -0
- package/dist/http_clients/axios.js +119 -0
- package/dist/http_clients/base.d.ts +391 -0
- package/dist/http_clients/base.js +650 -0
- package/dist/http_clients/index.d.ts +4 -0
- package/dist/http_clients/index.js +2 -0
- package/dist/http_compressors/base.d.ts +40 -0
- package/dist/http_compressors/base.js +9 -0
- package/dist/http_compressors/brotli.d.ts +33 -0
- package/dist/http_compressors/brotli.js +39 -0
- package/dist/http_compressors/gzip.d.ts +33 -0
- package/dist/http_compressors/gzip.js +39 -0
- package/dist/http_compressors/index.d.ts +8 -0
- package/dist/http_compressors/index.js +3 -0
- package/dist/http_compressors/resolve.d.ts +24 -0
- package/dist/http_compressors/resolve.js +30 -0
- package/dist/http_compressors/zstd.d.ts +34 -0
- package/dist/http_compressors/zstd.js +43 -0
- package/dist/index.d.ts +38 -32
- package/dist/index.js +36 -36
- package/dist/invalid_response_body_error.d.ts +14 -0
- package/dist/invalid_response_body_error.js +18 -0
- package/dist/lazy_schema.d.ts +8 -0
- package/dist/lazy_schema.js +11 -0
- package/dist/models.d.ts +196 -278
- package/dist/models.js +14 -15
- package/dist/resource_clients/actor.d.ts +115 -45
- package/dist/resource_clients/actor.js +187 -116
- package/dist/resource_clients/actor_collection.d.ts +21 -17
- package/dist/resource_clients/actor_collection.js +26 -21
- package/dist/resource_clients/actor_env_var.d.ts +13 -6
- package/dist/resource_clients/actor_env_var.js +21 -14
- package/dist/resource_clients/actor_env_var_collection.d.ts +18 -22
- package/dist/resource_clients/actor_env_var_collection.js +23 -20
- package/dist/resource_clients/actor_server.d.ts +149 -0
- package/dist/resource_clients/actor_server.js +199 -0
- package/dist/resource_clients/actor_version.d.ts +30 -10
- package/dist/resource_clients/actor_version.js +29 -23
- package/dist/resource_clients/actor_version_collection.d.ts +20 -21
- package/dist/resource_clients/actor_version_collection.js +25 -20
- package/dist/resource_clients/build.d.ts +24 -11
- package/dist/resource_clients/build.js +43 -26
- package/dist/resource_clients/build_collection.d.ts +10 -8
- package/dist/resource_clients/build_collection.js +13 -13
- package/dist/resource_clients/dataset.d.ts +87 -40
- package/dist/resource_clients/dataset.js +159 -175
- package/dist/resource_clients/dataset_collection.d.ts +13 -9
- package/dist/resource_clients/dataset_collection.js +29 -21
- package/dist/resource_clients/key_value_store.d.ts +110 -28
- package/dist/resource_clients/key_value_store.js +177 -99
- package/dist/resource_clients/key_value_store_collection.d.ts +13 -9
- package/dist/resource_clients/key_value_store_collection.js +29 -21
- package/dist/resource_clients/log.d.ts +42 -23
- package/dist/resource_clients/log.js +156 -85
- package/dist/resource_clients/request_queue.d.ts +77 -38
- package/dist/resource_clients/request_queue.js +251 -172
- package/dist/resource_clients/request_queue_collection.d.ts +12 -8
- package/dist/resource_clients/request_queue_collection.js +23 -20
- package/dist/resource_clients/run.d.ts +134 -34
- package/dist/resource_clients/run.js +225 -84
- package/dist/resource_clients/run_collection.d.ts +8 -6
- package/dist/resource_clients/run_collection.js +18 -18
- package/dist/resource_clients/schedule.d.ts +20 -11
- package/dist/resource_clients/schedule.js +36 -30
- package/dist/resource_clients/schedule_collection.d.ts +10 -6
- package/dist/resource_clients/schedule_collection.js +20 -17
- package/dist/resource_clients/store_collection.d.ts +9 -7
- package/dist/resource_clients/store_collection.js +18 -18
- package/dist/resource_clients/task.d.ts +44 -21
- package/dist/resource_clients/task.js +116 -86
- package/dist/resource_clients/task_collection.d.ts +13 -9
- package/dist/resource_clients/task_collection.js +19 -16
- package/dist/resource_clients/user.d.ts +22 -13
- package/dist/resource_clients/user.js +46 -47
- package/dist/resource_clients/webhook.d.ts +21 -12
- package/dist/resource_clients/webhook.js +35 -30
- package/dist/resource_clients/webhook_collection.d.ts +11 -7
- package/dist/resource_clients/webhook_collection.js +20 -17
- package/dist/resource_clients/webhook_dispatch.d.ts +9 -6
- package/dist/resource_clients/webhook_dispatch.js +11 -10
- package/dist/resource_clients/webhook_dispatch_collection.d.ts +7 -5
- package/dist/resource_clients/webhook_dispatch_collection.js +13 -13
- package/dist/response_validation_error.d.ts +27 -0
- package/dist/response_validation_error.js +38 -0
- package/dist/runtime/node.d.ts +6 -0
- package/dist/runtime/node.js +87 -0
- package/dist/runtime/types.d.ts +60 -0
- package/dist/runtime/types.js +1 -0
- package/dist/runtime/web.d.ts +5 -0
- package/dist/runtime/web.js +21 -0
- package/dist/schemas.d.ts +381 -0
- package/dist/schemas.js +37 -0
- package/dist/statistics.d.ts +1 -6
- package/dist/statistics.js +7 -11
- package/dist/timeouts.d.ts +84 -0
- package/dist/timeouts.js +37 -0
- package/dist/timezones.d.ts +1 -1
- package/dist/timezones.js +1 -2
- package/dist/utils.d.ts +99 -70
- package/dist/utils.js +251 -229
- package/dist/wait_for_resources.d.ts +8 -0
- package/dist/wait_for_resources.js +39 -0
- package/package.json +48 -47
- package/dist/argument_validation_error.d.ts +0 -17
- package/dist/argument_validation_error.js +0 -157
- package/dist/http_client.d.ts +0 -88
- package/dist/http_client.js +0 -260
- package/dist/index.mjs +0 -42
- package/dist/interceptors.d.ts +0 -19
- package/dist/interceptors.js +0 -110
|
@@ -1,51 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
exclusiveStartKey:
|
|
13
|
-
collection:
|
|
14
|
-
prefix:
|
|
15
|
-
signature:
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import log from '@apify/log';
|
|
3
|
+
import { createHmacSignatureAsync, createStorageContentSignatureAsync } from '@apify/utilities';
|
|
4
|
+
import { ResourceClient } from '../base/resource_client.js';
|
|
5
|
+
import * as schemas from '../schemas.js';
|
|
6
|
+
import { timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
|
|
7
|
+
import { runtime } from '#runtime';
|
|
8
|
+
import { anyObjectSchema, applyQueryParamsToUrl, catchNotFoundOrThrow, isBlob, isBuffer, isStream, minForLimitParam, parseArgument, parseResponse, } from '../utils.js';
|
|
9
|
+
const listKeysOptionsSchema = z.strictObject({
|
|
10
|
+
limit: z.number().min(0).optional(),
|
|
11
|
+
chunkSize: z.number().positive().optional(),
|
|
12
|
+
exclusiveStartKey: z.string().optional(),
|
|
13
|
+
collection: z.string().optional(),
|
|
14
|
+
prefix: z.string().optional(),
|
|
15
|
+
signature: z.string().optional(),
|
|
16
|
+
...timeoutOptionsShape,
|
|
16
17
|
});
|
|
17
|
-
const nonEmptyKeySchema =
|
|
18
|
-
// `
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
const nonEmptyKeySchema = z.string().min(1);
|
|
19
|
+
// `chunkSize` (client-side only) and `signature` (which this method produces) are left out. The options type omits
|
|
20
|
+
// both to match.
|
|
21
|
+
const createKeysPublicUrlOptionsSchema = z.strictObject({
|
|
22
|
+
limit: z.number().min(0).optional(),
|
|
23
|
+
exclusiveStartKey: z.string().optional(),
|
|
24
|
+
collection: z.string().optional(),
|
|
25
|
+
prefix: z.string().optional(),
|
|
26
|
+
expiresInSecs: z.number().positive().optional(),
|
|
27
|
+
...timeoutOptionsShape,
|
|
25
28
|
});
|
|
26
|
-
const keySchema =
|
|
27
|
-
const getRecordOptionsSchema =
|
|
28
|
-
buffer:
|
|
29
|
-
stream:
|
|
30
|
-
disableRedirect:
|
|
31
|
-
signature:
|
|
29
|
+
const keySchema = z.string();
|
|
30
|
+
const getRecordOptionsSchema = z.strictObject({
|
|
31
|
+
buffer: z.boolean().optional(),
|
|
32
|
+
stream: z.boolean().optional(),
|
|
33
|
+
disableRedirect: z.boolean().optional(),
|
|
34
|
+
signature: z.string().optional(),
|
|
35
|
+
...timeoutOptionsShape,
|
|
32
36
|
});
|
|
33
|
-
const recordSchema =
|
|
34
|
-
key:
|
|
37
|
+
const recordSchema = z.strictObject({
|
|
38
|
+
key: z.string(),
|
|
35
39
|
// Values are arbitrary, and a `z.object()` arm would walk every key of a large buffer.
|
|
36
40
|
// Symbols, bigints, functions and non-finite numbers are rejected because they cannot be serialized
|
|
37
41
|
// to JSON - symbols and bigints throw, functions stringify to `undefined` (an empty request body),
|
|
38
42
|
// and `NaN` / `Infinity` would silently become `null`.
|
|
39
|
-
value:
|
|
43
|
+
value: z.custom((value) => value !== undefined &&
|
|
40
44
|
typeof value !== 'symbol' &&
|
|
41
45
|
typeof value !== 'bigint' &&
|
|
42
46
|
typeof value !== 'function' &&
|
|
43
|
-
(typeof value !== 'number' || Number.isFinite(value)), 'Expected a
|
|
44
|
-
contentType:
|
|
47
|
+
(typeof value !== 'number' || Number.isFinite(value)), 'Expected a JSON-serializable value, binary data, or a stream'),
|
|
48
|
+
contentType: z.string().min(1).optional(),
|
|
49
|
+
contentEncoding: z.string().trim().min(1).optional(),
|
|
45
50
|
});
|
|
46
|
-
const recordOptionsSchema =
|
|
47
|
-
|
|
48
|
-
doNotRetryTimeouts:
|
|
51
|
+
const recordOptionsSchema = z.strictObject({
|
|
52
|
+
...timeoutOptionsShape,
|
|
53
|
+
doNotRetryTimeouts: z.boolean().optional(),
|
|
49
54
|
});
|
|
50
55
|
/**
|
|
51
56
|
* Client for managing a specific key-value store.
|
|
@@ -75,7 +80,7 @@ const recordOptionsSchema = zod_1.z.strictObject({
|
|
|
75
80
|
*
|
|
76
81
|
* @see https://docs.apify.com/platform/storage/key-value-store
|
|
77
82
|
*/
|
|
78
|
-
class KeyValueStoreClient extends
|
|
83
|
+
export class KeyValueStoreClient extends ResourceClient {
|
|
79
84
|
/**
|
|
80
85
|
* @hidden
|
|
81
86
|
*/
|
|
@@ -88,11 +93,14 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
88
93
|
/**
|
|
89
94
|
* Gets the key-value store object from the Apify API.
|
|
90
95
|
*
|
|
96
|
+
* @param options - Request options
|
|
97
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
91
98
|
* @returns The KeyValueStore object, or `undefined` if it does not exist
|
|
92
99
|
* @see https://docs.apify.com/api/v2/key-value-store-get
|
|
93
100
|
*/
|
|
94
|
-
async get() {
|
|
95
|
-
|
|
101
|
+
async get(options = {}) {
|
|
102
|
+
const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
103
|
+
return this.getResource(schemas.KeyValueStoreResource(), {}, timeoutSecs, signal);
|
|
96
104
|
}
|
|
97
105
|
/**
|
|
98
106
|
* Updates the key-value store with specified fields.
|
|
@@ -101,20 +109,26 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
101
109
|
* @param newFields.name - New name for the store
|
|
102
110
|
* @param newFields.title - New title for the store
|
|
103
111
|
* @param newFields.generalAccess - General resource access level ('FOLLOW_USER_SETTING', 'ANYONE_WITH_ID_CAN_READ' or 'RESTRICTED')
|
|
112
|
+
* @param options - Request options
|
|
113
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
104
114
|
* @returns The updated KeyValueStore object
|
|
105
115
|
* @see https://docs.apify.com/api/v2/key-value-store-put
|
|
106
116
|
*/
|
|
107
|
-
async update(newFields) {
|
|
108
|
-
|
|
109
|
-
|
|
117
|
+
async update(newFields, options = {}) {
|
|
118
|
+
parseArgument(newFields, anyObjectSchema);
|
|
119
|
+
const { timeoutSecs = 'long', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
120
|
+
return this.updateResource(schemas.KeyValueStoreResource(), newFields, timeoutSecs, signal);
|
|
110
121
|
}
|
|
111
122
|
/**
|
|
112
123
|
* Deletes the key-value store.
|
|
113
124
|
*
|
|
125
|
+
* @param options - Request options
|
|
126
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
114
127
|
* @see https://docs.apify.com/api/v2/key-value-store-delete
|
|
115
128
|
*/
|
|
116
|
-
async delete() {
|
|
117
|
-
|
|
129
|
+
async delete(options = {}) {
|
|
130
|
+
const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
131
|
+
return this.deleteResource(timeoutSecs, signal);
|
|
118
132
|
}
|
|
119
133
|
/**
|
|
120
134
|
* Lists all keys in the key-value store.
|
|
@@ -124,9 +138,12 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
124
138
|
*
|
|
125
139
|
* @param options - Listing options
|
|
126
140
|
* @param options.limit - Maximum number of keys to return. Default is 1000.
|
|
141
|
+
* @param options.chunkSize - Maximum number of keys requested per API call when iterating across pages. `limit`
|
|
142
|
+
* then caps the total across all pages.
|
|
127
143
|
* @param options.exclusiveStartKey - Key to start listing from (for pagination). The listing starts with the next key after this one.
|
|
128
144
|
* @param options.collection - Filter keys by collection name.
|
|
129
145
|
* @param options.prefix - Filter keys that start with this prefix.
|
|
146
|
+
* @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
|
|
130
147
|
* @returns Object containing `items` array of key metadata, pagination info (`count`, `limit`, `isTruncated`, `nextExclusiveStartKey`)
|
|
131
148
|
* @see https://docs.apify.com/api/v2/key-value-store-keys-get
|
|
132
149
|
*
|
|
@@ -152,17 +169,19 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
152
169
|
* ```
|
|
153
170
|
*/
|
|
154
171
|
listKeys(options = {}) {
|
|
155
|
-
|
|
172
|
+
// `chunkSize` only sizes the page requests; it is not an API parameter, so it must not reach the query string.
|
|
173
|
+
const { chunkSize, timeoutSecs = 'medium', signal, ...parsed } = parseArgument(options, listKeysOptionsSchema, 'KeyValueClientListKeysOptions');
|
|
156
174
|
const getPaginatedList = async (kvsListOptions = {}) => {
|
|
157
175
|
const response = await this.httpClient.call({
|
|
158
|
-
url: this.
|
|
176
|
+
url: this.buildUrl('keys'),
|
|
159
177
|
method: 'GET',
|
|
160
|
-
params: this.
|
|
161
|
-
|
|
178
|
+
params: this.buildParams(kvsListOptions),
|
|
179
|
+
timeoutSecs,
|
|
180
|
+
signal,
|
|
162
181
|
});
|
|
163
|
-
return (
|
|
182
|
+
return parseResponse(response, schemas.ListOfKeys());
|
|
164
183
|
};
|
|
165
|
-
const paginatedListPromise = getPaginatedList(parsed);
|
|
184
|
+
const paginatedListPromise = getPaginatedList({ ...parsed, limit: minForLimitParam(parsed.limit, chunkSize) });
|
|
166
185
|
async function* asyncGenerator() {
|
|
167
186
|
let currentPage = await paginatedListPromise;
|
|
168
187
|
yield* currentPage.items;
|
|
@@ -175,7 +194,7 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
175
194
|
) {
|
|
176
195
|
const newOptions = {
|
|
177
196
|
...parsed,
|
|
178
|
-
limit: remainingItems,
|
|
197
|
+
limit: minForLimitParam(remainingItems, chunkSize),
|
|
179
198
|
exclusiveStartKey: currentPage.nextExclusiveStartKey,
|
|
180
199
|
};
|
|
181
200
|
currentPage = await getPaginatedList(newOptions);
|
|
@@ -197,6 +216,8 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
197
216
|
* requiring an API token.
|
|
198
217
|
*
|
|
199
218
|
* @param key - The record key
|
|
219
|
+
* @param options - Request options
|
|
220
|
+
* @param options.timeoutSecs - Timeout for the API request that fetches the store. Default is `'long'`.
|
|
200
221
|
* @returns A public URL string for accessing the record
|
|
201
222
|
*
|
|
202
223
|
* @example
|
|
@@ -207,12 +228,13 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
207
228
|
* ```
|
|
208
229
|
* @since Added in 2.14.0
|
|
209
230
|
*/
|
|
210
|
-
async getRecordPublicUrl(key) {
|
|
211
|
-
|
|
212
|
-
const
|
|
213
|
-
const
|
|
231
|
+
async getRecordPublicUrl(key, options = {}) {
|
|
232
|
+
parseArgument(key, nonEmptyKeySchema);
|
|
233
|
+
const { timeoutSecs = 'long', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
234
|
+
const store = await this.get({ timeoutSecs, signal });
|
|
235
|
+
const recordPublicUrl = new URL(this.buildPublicUrl(['records', key]));
|
|
214
236
|
if (store?.urlSigningSecretKey) {
|
|
215
|
-
const signature = await
|
|
237
|
+
const signature = await createHmacSignatureAsync(store.urlSigningSecretKey, key);
|
|
216
238
|
recordPublicUrl.searchParams.append('signature', signature);
|
|
217
239
|
}
|
|
218
240
|
return recordPublicUrl.toString();
|
|
@@ -224,9 +246,11 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
224
246
|
* the URL will include a cryptographic signature which allows access without authentication.
|
|
225
247
|
*
|
|
226
248
|
* @param options - URL generation options (extends all options from {@link listKeys})
|
|
227
|
-
* @param options.expiresInSecs - Number of seconds until the signed URL expires. If omitted, the URL
|
|
249
|
+
* @param options.expiresInSecs - Number of seconds until the signed URL expires. Must be positive. If omitted, the URL
|
|
250
|
+
* never expires.
|
|
228
251
|
* @param options.limit - Maximum number of keys to return.
|
|
229
252
|
* @param options.prefix - Filter keys by prefix.
|
|
253
|
+
* @param options.timeoutSecs - Timeout for the API request that fetches the store. Default is `'long'`.
|
|
230
254
|
* @returns A public URL string for accessing the keys list
|
|
231
255
|
*
|
|
232
256
|
* @example
|
|
@@ -241,19 +265,18 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
241
265
|
* @since Added in 2.13.0
|
|
242
266
|
*/
|
|
243
267
|
async createKeysPublicUrl(options = {}) {
|
|
244
|
-
const
|
|
245
|
-
const store = await this.get();
|
|
246
|
-
|
|
247
|
-
let createdPublicKeysUrl = new URL(this._publicUrl('keys'));
|
|
268
|
+
const { timeoutSecs = 'long', signal, expiresInSecs, ...queryOptions } = parseArgument(options, createKeysPublicUrlOptionsSchema, 'KeyValueClientCreateKeysUrlOptions');
|
|
269
|
+
const store = await this.get({ timeoutSecs, signal });
|
|
270
|
+
let createdPublicKeysUrl = new URL(this.buildPublicUrl('keys'));
|
|
248
271
|
if (store?.urlSigningSecretKey) {
|
|
249
|
-
const signature = await
|
|
272
|
+
const signature = await createStorageContentSignatureAsync({
|
|
250
273
|
resourceId: store.id,
|
|
251
274
|
urlSigningSecretKey: store.urlSigningSecretKey,
|
|
252
275
|
expiresInMillis: expiresInSecs ? expiresInSecs * 1000 : undefined,
|
|
253
276
|
});
|
|
254
277
|
createdPublicKeysUrl.searchParams.set('signature', signature);
|
|
255
278
|
}
|
|
256
|
-
createdPublicKeysUrl =
|
|
279
|
+
createdPublicKeysUrl = applyQueryParamsToUrl(createdPublicKeysUrl, queryOptions);
|
|
257
280
|
return createdPublicKeysUrl.toString();
|
|
258
281
|
}
|
|
259
282
|
/**
|
|
@@ -262,6 +285,8 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
262
285
|
* This is more efficient than {@link getRecord} when you only need to check for existence.
|
|
263
286
|
*
|
|
264
287
|
* @param key - The record key to check
|
|
288
|
+
* @param options - Request options
|
|
289
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
265
290
|
* @returns `true` if the record exists, `false` if it does not
|
|
266
291
|
* @see https://docs.apify.com/api/v2/key-value-store-record-get
|
|
267
292
|
*
|
|
@@ -274,42 +299,46 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
274
299
|
* ```
|
|
275
300
|
* @since Added in 2.9.0
|
|
276
301
|
*/
|
|
277
|
-
async recordExists(key) {
|
|
302
|
+
async recordExists(key, options = {}) {
|
|
303
|
+
const { timeoutSecs = 'long', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
278
304
|
const requestOpts = {
|
|
279
|
-
url: this.
|
|
305
|
+
url: this.buildUrl(['records', key]),
|
|
280
306
|
method: 'HEAD',
|
|
281
|
-
params: this.
|
|
307
|
+
params: this.buildParams(),
|
|
308
|
+
timeoutSecs,
|
|
309
|
+
signal,
|
|
282
310
|
};
|
|
283
311
|
try {
|
|
284
312
|
await this.httpClient.call(requestOpts);
|
|
285
313
|
return true;
|
|
286
314
|
}
|
|
287
315
|
catch (err) {
|
|
288
|
-
|
|
316
|
+
catchNotFoundOrThrow(err);
|
|
289
317
|
}
|
|
290
318
|
return false;
|
|
291
319
|
}
|
|
292
320
|
async getRecord(key, options = {}) {
|
|
293
|
-
|
|
294
|
-
const parsed =
|
|
295
|
-
if (parsed.stream && !
|
|
321
|
+
parseArgument(key, keySchema);
|
|
322
|
+
const parsed = parseArgument(options, getRecordOptionsSchema, 'KeyValueClientGetRecordOptions');
|
|
323
|
+
if (parsed.stream && !runtime.isNode) {
|
|
296
324
|
throw new Error('The stream option can only be used in Node.js environment.');
|
|
297
325
|
}
|
|
298
326
|
if ('disableRedirect' in parsed) {
|
|
299
|
-
|
|
327
|
+
log.deprecated('The disableRedirect option for getRecord() is deprecated. ' +
|
|
300
328
|
'It has no effect and will be removed in the following major release.');
|
|
301
329
|
}
|
|
302
330
|
const queryParams = { attachment: 'true' };
|
|
303
331
|
if (parsed.signature)
|
|
304
332
|
queryParams.signature = parsed.signature;
|
|
305
333
|
const requestOpts = {
|
|
306
|
-
url: this.
|
|
334
|
+
url: this.buildUrl(['records', key]),
|
|
307
335
|
method: 'GET',
|
|
308
|
-
params: this.
|
|
309
|
-
|
|
336
|
+
params: this.buildParams(queryParams),
|
|
337
|
+
timeoutSecs: parsed.timeoutSecs ?? 'long',
|
|
338
|
+
signal: parsed.signal,
|
|
310
339
|
};
|
|
311
340
|
if (parsed.buffer)
|
|
312
|
-
requestOpts.
|
|
341
|
+
requestOpts.responseType = 'buffer';
|
|
313
342
|
if (parsed.stream)
|
|
314
343
|
requestOpts.responseType = 'stream';
|
|
315
344
|
try {
|
|
@@ -321,7 +350,7 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
321
350
|
};
|
|
322
351
|
}
|
|
323
352
|
catch (err) {
|
|
324
|
-
|
|
353
|
+
catchNotFoundOrThrow(err);
|
|
325
354
|
}
|
|
326
355
|
return undefined;
|
|
327
356
|
}
|
|
@@ -332,19 +361,39 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
332
361
|
* The content type is automatically determined based on the value type, but can be
|
|
333
362
|
* overridden using the `contentType` property.
|
|
334
363
|
*
|
|
364
|
+
* A string is sent as it is, even under `application/json`, so a string stored under a JSON content type has
|
|
365
|
+
* to be valid JSON already, or {@link getRecord} fails to parse it back.
|
|
366
|
+
*
|
|
335
367
|
* **Note about streams:** If the value is a stream object (has `.pipe` and `.on` methods),
|
|
336
|
-
* the upload cannot be retried on failure or follow redirects. For reliable uploads,
|
|
337
|
-
*
|
|
368
|
+
* the upload cannot be retried on failure or follow redirects. For reliable uploads of a file,
|
|
369
|
+
* pass a `Blob` from `fs.openAsBlob()` instead: it is read again for every attempt and never held
|
|
370
|
+
* in memory whole.
|
|
338
371
|
*
|
|
339
372
|
* @param record - The record to store
|
|
340
373
|
* @param record.key - Record key (unique identifier)
|
|
341
|
-
* @param record.value - Record value (
|
|
374
|
+
* @param record.value - Record value (a JSON-serializable value, Buffer, ArrayBuffer, typed array, Blob, or
|
|
375
|
+
* Readable)
|
|
342
376
|
* @param record.contentType - Optional MIME type. Auto-detected if not provided:
|
|
343
377
|
* - Objects: `'application/json; charset=utf-8'`
|
|
344
378
|
* - Strings: `'text/plain; charset=utf-8'`
|
|
345
|
-
* -
|
|
379
|
+
* - Blobs: their `type`, or `'application/octet-stream'` when it is empty
|
|
380
|
+
* - Binary values and streams: `'application/octet-stream'`
|
|
381
|
+
*
|
|
382
|
+
* Worth setting for media and archives: the client skips compressing a body
|
|
383
|
+
* whose content type already carries its own compression. The
|
|
384
|
+
* `'application/octet-stream'` fallback is treated as compressible, because
|
|
385
|
+
* it could hold any binary data.
|
|
386
|
+
* @param record.contentEncoding - Optional encoding already applied to `value`, sent as the `Content-Encoding`
|
|
387
|
+
* header. Set it to upload a pre-compressed value: the client then sends the
|
|
388
|
+
* bytes as they are and does not compress them again. The API accepts `gzip`,
|
|
389
|
+
* `br`, `zstd`, `deflate`, and `identity`, and stores the record exactly as
|
|
390
|
+
* uploaded, so this is also the encoding the record is served with. Only binary
|
|
391
|
+
* values and streams can carry a compression, so any encoding other than
|
|
392
|
+
* `identity` throws a `TypeError` when `value` is a string, or a plain object,
|
|
393
|
+
* array, or other value that the client serializes to JSON. Node.js only, since
|
|
394
|
+
* browsers cannot send the header to the API.
|
|
346
395
|
* @param options - Storage options
|
|
347
|
-
* @param options.timeoutSecs - Timeout for the
|
|
396
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
348
397
|
* @param options.doNotRetryTimeouts - If `true`, don't retry on timeout errors. Default is `false`.
|
|
349
398
|
* @see https://docs.apify.com/api/v2/key-value-store-record-put
|
|
350
399
|
*
|
|
@@ -370,18 +419,40 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
370
419
|
* value: imageBuffer,
|
|
371
420
|
* contentType: 'image/png'
|
|
372
421
|
* });
|
|
422
|
+
*
|
|
423
|
+
* // Store a gzipped file without compressing it again
|
|
424
|
+
* const gzippedReport = await readFile('report.json.gz');
|
|
425
|
+
* await client.keyValueStore('my-store').setRecord({
|
|
426
|
+
* key: 'report.json',
|
|
427
|
+
* value: gzippedReport,
|
|
428
|
+
* contentType: 'application/json',
|
|
429
|
+
* contentEncoding: 'gzip'
|
|
430
|
+
* });
|
|
373
431
|
* ```
|
|
374
432
|
*/
|
|
375
433
|
async setRecord(record, options = {}) {
|
|
376
|
-
|
|
377
|
-
const parsed =
|
|
378
|
-
const { key } = record;
|
|
434
|
+
parseArgument(record, recordSchema);
|
|
435
|
+
const parsed = parseArgument(options, recordOptionsSchema, 'KeyValueStoreRecordOptions');
|
|
436
|
+
const { key, contentEncoding } = record;
|
|
379
437
|
let { value, contentType } = record;
|
|
380
|
-
const { timeoutSecs, doNotRetryTimeouts } = parsed;
|
|
381
|
-
|
|
438
|
+
const { timeoutSecs = 'long', signal, doNotRetryTimeouts } = parsed;
|
|
439
|
+
if (contentEncoding !== undefined && !runtime.isNode) {
|
|
440
|
+
throw new Error('The contentEncoding option can only be used in Node.js environment.');
|
|
441
|
+
}
|
|
442
|
+
const isBinaryValue = isStream(value) || isBuffer(value) || isBlob(value);
|
|
443
|
+
// The client forwards the header without inspecting the body, so a string or a value serialized to JSON, which
|
|
444
|
+
// cannot hold compressed bytes, would be stored under an encoding that misdescribes it.
|
|
445
|
+
const declaresCompression = contentEncoding !== undefined && contentEncoding.trim().toLowerCase() !== 'identity';
|
|
446
|
+
if (declaresCompression && !isBinaryValue) {
|
|
447
|
+
throw new TypeError(`Cannot upload a value of type ${typeof value} with Content-Encoding: ${contentEncoding}. ` +
|
|
448
|
+
'An encoding other than identity declares the value is already compressed, so pass the ' +
|
|
449
|
+
'compressed bytes as a Buffer, an ArrayBuffer, a typed array, a Blob, or a readable stream.');
|
|
450
|
+
}
|
|
382
451
|
// To allow saving Objects to JSON without providing content type
|
|
383
452
|
if (!contentType) {
|
|
384
|
-
if (
|
|
453
|
+
if (isBlob(value))
|
|
454
|
+
contentType = value.type || 'application/octet-stream';
|
|
455
|
+
else if (isBinaryValue)
|
|
385
456
|
contentType = 'application/octet-stream';
|
|
386
457
|
else if (typeof value === 'string')
|
|
387
458
|
contentType = 'text/plain; charset=utf-8';
|
|
@@ -389,7 +460,7 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
389
460
|
contentType = 'application/json; charset=utf-8';
|
|
390
461
|
}
|
|
391
462
|
const isContentTypeJson = contentType.startsWith('application/json');
|
|
392
|
-
if (isContentTypeJson && !
|
|
463
|
+
if (isContentTypeJson && !isBinaryValue && typeof value !== 'string') {
|
|
393
464
|
try {
|
|
394
465
|
value = JSON.stringify(value, null, 2);
|
|
395
466
|
}
|
|
@@ -399,13 +470,17 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
399
470
|
}
|
|
400
471
|
}
|
|
401
472
|
const uploadOpts = {
|
|
402
|
-
url: this.
|
|
473
|
+
url: this.buildUrl(['records', key]),
|
|
403
474
|
method: 'PUT',
|
|
404
|
-
params: this.
|
|
475
|
+
params: this.buildParams(),
|
|
405
476
|
data: value,
|
|
406
|
-
headers:
|
|
477
|
+
headers: {
|
|
478
|
+
'content-type': contentType,
|
|
479
|
+
...(contentEncoding !== undefined && { 'content-encoding': contentEncoding }),
|
|
480
|
+
},
|
|
407
481
|
doNotRetryTimeouts,
|
|
408
|
-
|
|
482
|
+
timeoutSecs,
|
|
483
|
+
signal,
|
|
409
484
|
};
|
|
410
485
|
await this.httpClient.call(uploadOpts);
|
|
411
486
|
}
|
|
@@ -413,6 +488,8 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
413
488
|
* Deletes a record from the key-value store.
|
|
414
489
|
*
|
|
415
490
|
* @param key - The record key to delete
|
|
491
|
+
* @param options - Request options
|
|
492
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
416
493
|
* @see https://docs.apify.com/api/v2/key-value-store-record-delete
|
|
417
494
|
*
|
|
418
495
|
* @example
|
|
@@ -420,14 +497,15 @@ class KeyValueStoreClient extends resource_client_1.ResourceClient {
|
|
|
420
497
|
* await client.keyValueStore('my-store').deleteRecord('temp-data');
|
|
421
498
|
* ```
|
|
422
499
|
*/
|
|
423
|
-
async deleteRecord(key) {
|
|
424
|
-
|
|
500
|
+
async deleteRecord(key, options = {}) {
|
|
501
|
+
parseArgument(key, keySchema);
|
|
502
|
+
const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
425
503
|
await this.httpClient.call({
|
|
426
|
-
url: this.
|
|
504
|
+
url: this.buildUrl(['records', key]),
|
|
427
505
|
method: 'DELETE',
|
|
428
|
-
params: this.
|
|
429
|
-
|
|
506
|
+
params: this.buildParams(),
|
|
507
|
+
timeoutSecs,
|
|
508
|
+
signal,
|
|
430
509
|
});
|
|
431
510
|
}
|
|
432
511
|
}
|
|
433
|
-
exports.KeyValueStoreClient = KeyValueStoreClient;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { STORAGE_OWNERSHIP_FILTER } from '@apify/consts';
|
|
2
|
-
import type { ApiClientSubResourceOptions } from '../base/api_client';
|
|
3
|
-
import { ResourceCollectionClient } from '../base/resource_collection_client';
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
2
|
+
import type { ApiClientSubResourceOptions } from '../base/api_client.js';
|
|
3
|
+
import { ResourceCollectionClient } from '../base/resource_collection_client.js';
|
|
4
|
+
import type { TimeoutOptions } from '../timeouts.js';
|
|
5
|
+
import type { PaginatedList, PaginationOptions } from '../utils.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<
|
|
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<
|
|
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<
|
|
81
|
+
export type KeyValueStoreCollectionListResult = PaginatedList<KeyValueStoreResource> & {
|
|
78
82
|
unnamed: boolean;
|
|
79
83
|
};
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { STORAGE_OWNERSHIP_FILTER } from '@apify/consts';
|
|
3
|
+
import { ResourceCollectionClient } from '../base/resource_collection_client.js';
|
|
4
|
+
import * as schemas from '../schemas.js';
|
|
5
|
+
import { optionalSignalSchema, optionalTimeoutSchema, timeoutOptionsShape } from '../timeouts.js';
|
|
6
|
+
import { anyObjectSchema, paginationOptionsShape, parseArgument } from '../utils.js';
|
|
7
|
+
const listOptionsSchema = z.strictObject({
|
|
8
|
+
unnamed: z.boolean().optional(),
|
|
9
|
+
...paginationOptionsShape,
|
|
10
|
+
desc: z.boolean().optional(),
|
|
11
|
+
ownership: z.enum(STORAGE_OWNERSHIP_FILTER).optional(),
|
|
12
|
+
...timeoutOptionsShape,
|
|
13
13
|
});
|
|
14
|
-
const nameSchema =
|
|
15
|
-
const schemaSchema =
|
|
14
|
+
const nameSchema = z.string().optional();
|
|
15
|
+
const schemaSchema = anyObjectSchema.optional();
|
|
16
16
|
/**
|
|
17
17
|
* Client for managing the collection of Key-value stores in your account.
|
|
18
18
|
*
|
|
@@ -33,7 +33,7 @@ const schemaSchema = utils_1.anyObjectSchema.optional();
|
|
|
33
33
|
*
|
|
34
34
|
* @see https://docs.apify.com/platform/storage/key-value-store
|
|
35
35
|
*/
|
|
36
|
-
class KeyValueStoreCollectionClient extends
|
|
36
|
+
export class KeyValueStoreCollectionClient extends ResourceCollectionClient {
|
|
37
37
|
/**
|
|
38
38
|
* @hidden
|
|
39
39
|
*/
|
|
@@ -60,25 +60,33 @@ class KeyValueStoreCollectionClient extends resource_collection_client_1.Resourc
|
|
|
60
60
|
* ```
|
|
61
61
|
*
|
|
62
62
|
* @param options - Pagination options.
|
|
63
|
+
* @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
|
|
63
64
|
* @returns A paginated iterator of Key-value stores.
|
|
64
65
|
* @see https://docs.apify.com/api/v2/key-value-stores-get
|
|
65
66
|
*/
|
|
66
67
|
list(options = {}) {
|
|
67
|
-
const parsed =
|
|
68
|
-
return this.
|
|
68
|
+
const parsed = parseArgument(options, listOptionsSchema, 'KeyValueStoreCollectionClientListOptions');
|
|
69
|
+
return this.listResourcesPaginated(schemas.ListOfKeyValueStores(), parsed, 'medium');
|
|
69
70
|
}
|
|
70
71
|
/**
|
|
71
72
|
* Gets or creates a key-value store with the specified name.
|
|
72
73
|
*
|
|
73
74
|
* @param name - Name of the key-value store. If not provided, a default store is used.
|
|
74
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'`.
|
|
75
78
|
* @returns The key-value store object.
|
|
76
79
|
* @see https://docs.apify.com/api/v2/key-value-stores-post
|
|
77
80
|
*/
|
|
78
81
|
async getOrCreate(name, options) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
parseArgument(name, nameSchema);
|
|
83
|
+
parseArgument(options?.schema, schemaSchema); // TODO: Add schema validation
|
|
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.KeyValueStoreResource(), name, hasResource ? resource : undefined, timeoutSecs, signal);
|
|
82
91
|
}
|
|
83
92
|
}
|
|
84
|
-
exports.KeyValueStoreCollectionClient = KeyValueStoreCollectionClient;
|