apify-client 3.0.0-beta.60 → 3.0.0-beta.62
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 +2 -1
- package/dist/apify_api_error.js +2 -1
- package/dist/apify_client_error.d.ts +10 -0
- package/dist/apify_client_error.js +10 -0
- package/dist/bundle.js +11 -11
- package/dist/bundle.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/invalid_response_body_error.d.ts +2 -1
- package/dist/invalid_response_body_error.js +2 -1
- package/dist/resource_clients/key_value_store.d.ts +29 -1
- package/dist/resource_clients/key_value_store.js +35 -2
- package/dist/response_validation_error.d.ts +2 -1
- package/dist/response_validation_error.js +2 -1
- package/dist/utils.d.ts +0 -13
- package/dist/utils.js +7 -13
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export * from './resource_clients/webhook.js';
|
|
|
26
26
|
export * from './resource_clients/webhook_collection.js';
|
|
27
27
|
export * from './resource_clients/webhook_dispatch.js';
|
|
28
28
|
export * from './resource_clients/webhook_dispatch_collection.js';
|
|
29
|
+
export * from './apify_client_error.js';
|
|
29
30
|
export * from './apify_api_error.js';
|
|
30
31
|
export { ArgumentValidationError } from '@apify/validations';
|
|
31
32
|
export * from './response_validation_error.js';
|
package/dist/index.js
CHANGED
|
@@ -26,6 +26,7 @@ export * from './resource_clients/webhook.js';
|
|
|
26
26
|
export * from './resource_clients/webhook_collection.js';
|
|
27
27
|
export * from './resource_clients/webhook_dispatch.js';
|
|
28
28
|
export * from './resource_clients/webhook_dispatch_collection.js';
|
|
29
|
+
export * from './apify_client_error.js';
|
|
29
30
|
export * from './apify_api_error.js';
|
|
30
31
|
export { ArgumentValidationError } from '@apify/validations';
|
|
31
32
|
export * from './response_validation_error.js';
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { ApifyClientError } from './apify_client_error.js';
|
|
1
2
|
import type { HttpResponse } from './http_clients/base.js';
|
|
2
3
|
/**
|
|
3
4
|
* Thrown when a response body does not parse as its content type claims, typically a JSON document cut short by a
|
|
4
5
|
* connection dropped mid-response. The client retries such requests, so the error surfaces once the retries are
|
|
5
6
|
* exhausted.
|
|
6
7
|
*/
|
|
7
|
-
export declare class InvalidResponseBodyError extends
|
|
8
|
+
export declare class InvalidResponseBodyError extends ApifyClientError {
|
|
8
9
|
code: string;
|
|
9
10
|
/** The response whose body did not parse, with the body as the transport returned it. */
|
|
10
11
|
response: HttpResponse;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { ApifyClientError } from './apify_client_error.js';
|
|
1
2
|
/**
|
|
2
3
|
* Thrown when a response body does not parse as its content type claims, typically a JSON document cut short by a
|
|
3
4
|
* connection dropped mid-response. The client retries such requests, so the error surfaces once the retries are
|
|
4
5
|
* exhausted.
|
|
5
6
|
*/
|
|
6
|
-
export class InvalidResponseBodyError extends
|
|
7
|
+
export class InvalidResponseBodyError extends ApifyClientError {
|
|
7
8
|
code;
|
|
8
9
|
/** The response whose body did not parse, with the body as the transport returned it. */
|
|
9
10
|
response;
|
|
@@ -225,6 +225,15 @@ export declare class KeyValueStoreClient extends ResourceClient {
|
|
|
225
225
|
* whose content type already carries its own compression. The
|
|
226
226
|
* `'application/octet-stream'` fallback is treated as compressible, because
|
|
227
227
|
* it could hold any binary data.
|
|
228
|
+
* @param record.contentEncoding - Optional encoding already applied to `value`, sent as the `Content-Encoding`
|
|
229
|
+
* header. Set it to upload a pre-compressed value: the client then sends the
|
|
230
|
+
* bytes as they are and does not compress them again. The API accepts `gzip`,
|
|
231
|
+
* `br`, `deflate`, and `identity`, and stores the record exactly as uploaded, so
|
|
232
|
+
* this is also the encoding the record is served with. Only binary values and
|
|
233
|
+
* streams can carry a compression, so any encoding other than `identity` throws
|
|
234
|
+
* a `TypeError` when `value` is a string, or a plain object, array, or other
|
|
235
|
+
* value that the client serializes to JSON. Node.js only, since browsers cannot
|
|
236
|
+
* send the header to the API.
|
|
228
237
|
* @param options - Storage options
|
|
229
238
|
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
230
239
|
* @param options.doNotRetryTimeouts - If `true`, don't retry on timeout errors. Default is `false`.
|
|
@@ -252,9 +261,18 @@ export declare class KeyValueStoreClient extends ResourceClient {
|
|
|
252
261
|
* value: imageBuffer,
|
|
253
262
|
* contentType: 'image/png'
|
|
254
263
|
* });
|
|
264
|
+
*
|
|
265
|
+
* // Store a gzipped file without compressing it again
|
|
266
|
+
* const gzippedReport = await readFile('report.json.gz');
|
|
267
|
+
* await client.keyValueStore('my-store').setRecord({
|
|
268
|
+
* key: 'report.json',
|
|
269
|
+
* value: gzippedReport,
|
|
270
|
+
* contentType: 'application/json',
|
|
271
|
+
* contentEncoding: 'gzip'
|
|
272
|
+
* });
|
|
255
273
|
* ```
|
|
256
274
|
*/
|
|
257
|
-
setRecord(record:
|
|
275
|
+
setRecord(record: KeyValueStoreRecordInput, options?: KeyValueStoreRecordOptions): Promise<void>;
|
|
258
276
|
/**
|
|
259
277
|
* Deletes a record from the key-value store.
|
|
260
278
|
*
|
|
@@ -345,6 +363,16 @@ export interface KeyValueStoreRecord<T> {
|
|
|
345
363
|
value: T;
|
|
346
364
|
contentType?: string;
|
|
347
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* A record to store with {@link KeyValueStoreClient.setRecord}.
|
|
368
|
+
*/
|
|
369
|
+
export interface KeyValueStoreRecordInput extends KeyValueStoreRecord<KeyValueStoreRecordValue> {
|
|
370
|
+
/**
|
|
371
|
+
* Encoding already applied to `value`, sent as the `Content-Encoding` header. See
|
|
372
|
+
* {@link KeyValueStoreClient.setRecord}.
|
|
373
|
+
*/
|
|
374
|
+
contentEncoding?: string;
|
|
375
|
+
}
|
|
348
376
|
/**
|
|
349
377
|
* Options for storing a record in a Key-Value Store.
|
|
350
378
|
* @since Added in 2.12.4
|
|
@@ -46,6 +46,7 @@ const recordSchema = z.strictObject({
|
|
|
46
46
|
typeof value !== 'function' &&
|
|
47
47
|
(typeof value !== 'number' || Number.isFinite(value)), 'Expected a JSON-serializable value, binary data, or a stream'),
|
|
48
48
|
contentType: z.string().min(1).optional(),
|
|
49
|
+
contentEncoding: z.string().trim().min(1).optional(),
|
|
49
50
|
});
|
|
50
51
|
const recordOptionsSchema = z.strictObject({
|
|
51
52
|
...timeoutOptionsShape,
|
|
@@ -378,6 +379,15 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
378
379
|
* whose content type already carries its own compression. The
|
|
379
380
|
* `'application/octet-stream'` fallback is treated as compressible, because
|
|
380
381
|
* it could hold any binary data.
|
|
382
|
+
* @param record.contentEncoding - Optional encoding already applied to `value`, sent as the `Content-Encoding`
|
|
383
|
+
* header. Set it to upload a pre-compressed value: the client then sends the
|
|
384
|
+
* bytes as they are and does not compress them again. The API accepts `gzip`,
|
|
385
|
+
* `br`, `deflate`, and `identity`, and stores the record exactly as uploaded, so
|
|
386
|
+
* this is also the encoding the record is served with. Only binary values and
|
|
387
|
+
* streams can carry a compression, so any encoding other than `identity` throws
|
|
388
|
+
* a `TypeError` when `value` is a string, or a plain object, array, or other
|
|
389
|
+
* value that the client serializes to JSON. Node.js only, since browsers cannot
|
|
390
|
+
* send the header to the API.
|
|
381
391
|
* @param options - Storage options
|
|
382
392
|
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
383
393
|
* @param options.doNotRetryTimeouts - If `true`, don't retry on timeout errors. Default is `false`.
|
|
@@ -405,15 +415,35 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
405
415
|
* value: imageBuffer,
|
|
406
416
|
* contentType: 'image/png'
|
|
407
417
|
* });
|
|
418
|
+
*
|
|
419
|
+
* // Store a gzipped file without compressing it again
|
|
420
|
+
* const gzippedReport = await readFile('report.json.gz');
|
|
421
|
+
* await client.keyValueStore('my-store').setRecord({
|
|
422
|
+
* key: 'report.json',
|
|
423
|
+
* value: gzippedReport,
|
|
424
|
+
* contentType: 'application/json',
|
|
425
|
+
* contentEncoding: 'gzip'
|
|
426
|
+
* });
|
|
408
427
|
* ```
|
|
409
428
|
*/
|
|
410
429
|
async setRecord(record, options = {}) {
|
|
411
430
|
parseArgument(record, recordSchema);
|
|
412
431
|
const parsed = parseArgument(options, recordOptionsSchema, 'KeyValueStoreRecordOptions');
|
|
413
|
-
const { key } = record;
|
|
432
|
+
const { key, contentEncoding } = record;
|
|
414
433
|
let { value, contentType } = record;
|
|
415
434
|
const { timeoutSecs = 'long', signal, doNotRetryTimeouts } = parsed;
|
|
435
|
+
if (contentEncoding !== undefined && !runtime.isNode) {
|
|
436
|
+
throw new Error('The contentEncoding option can only be used in Node.js environment.');
|
|
437
|
+
}
|
|
416
438
|
const isValueStreamOrBuffer = isStream(value) || isBuffer(value);
|
|
439
|
+
// The client forwards the header without inspecting the body, so a string or a value serialized to JSON, which
|
|
440
|
+
// cannot hold compressed bytes, would be stored under an encoding that misdescribes it.
|
|
441
|
+
const declaresCompression = contentEncoding !== undefined && contentEncoding.trim().toLowerCase() !== 'identity';
|
|
442
|
+
if (declaresCompression && !isValueStreamOrBuffer) {
|
|
443
|
+
throw new TypeError(`Cannot upload a value of type ${typeof value} with Content-Encoding: ${contentEncoding}. ` +
|
|
444
|
+
'An encoding other than identity declares the value is already compressed, so pass the ' +
|
|
445
|
+
'compressed bytes as a Buffer, an ArrayBuffer, a typed array, or a readable stream.');
|
|
446
|
+
}
|
|
417
447
|
// To allow saving Objects to JSON without providing content type
|
|
418
448
|
if (!contentType) {
|
|
419
449
|
if (isValueStreamOrBuffer)
|
|
@@ -438,7 +468,10 @@ export class KeyValueStoreClient extends ResourceClient {
|
|
|
438
468
|
method: 'PUT',
|
|
439
469
|
params: this.buildParams(),
|
|
440
470
|
data: value,
|
|
441
|
-
headers:
|
|
471
|
+
headers: {
|
|
472
|
+
'content-type': contentType,
|
|
473
|
+
...(contentEncoding !== undefined && { 'content-encoding': contentEncoding }),
|
|
474
|
+
},
|
|
442
475
|
doNotRetryTimeouts,
|
|
443
476
|
timeoutSecs,
|
|
444
477
|
signal,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { z } from 'zod';
|
|
2
|
+
import { ApifyClientError } from './apify_client_error.js';
|
|
2
3
|
/**
|
|
3
4
|
* Thrown when an API response does not match the schema the client expects for it.
|
|
4
5
|
*
|
|
@@ -12,7 +13,7 @@ import type { z } from 'zod';
|
|
|
12
13
|
* {@link https://zod.dev | zod} issues are available on `issues`, and the original `ZodError` on `cause`,
|
|
13
14
|
* for programmatic inspection.
|
|
14
15
|
*/
|
|
15
|
-
export declare class ResponseValidationError extends
|
|
16
|
+
export declare class ResponseValidationError extends ApifyClientError {
|
|
16
17
|
/** Structured issues from the underlying schema check. */
|
|
17
18
|
readonly issues: z.ZodError['issues'];
|
|
18
19
|
/** HTTP method of the request whose response failed validation, upper-cased. */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ArgumentValidationError } from '@apify/validations';
|
|
2
|
+
import { ApifyClientError } from './apify_client_error.js';
|
|
2
3
|
const REPORT_HINT = 'The API returned something its OpenAPI specification does not describe. ' +
|
|
3
4
|
'Please report this at https://github.com/apify/apify-client-js/issues.';
|
|
4
5
|
/**
|
|
@@ -14,7 +15,7 @@ const REPORT_HINT = 'The API returned something its OpenAPI specification does n
|
|
|
14
15
|
* {@link https://zod.dev | zod} issues are available on `issues`, and the original `ZodError` on `cause`,
|
|
15
16
|
* for programmatic inspection.
|
|
16
17
|
*/
|
|
17
|
-
export class ResponseValidationError extends
|
|
18
|
+
export class ResponseValidationError extends ApifyClientError {
|
|
18
19
|
/** Structured issues from the underlying schema check. */
|
|
19
20
|
issues;
|
|
20
21
|
/** HTTP method of the request whose response failed validation, upper-cased. */
|
package/dist/utils.d.ts
CHANGED
|
@@ -27,14 +27,6 @@ export declare function isNonArrayObject(value: unknown): value is Record<string
|
|
|
27
27
|
* @internal
|
|
28
28
|
*/
|
|
29
29
|
export declare const anyObjectSchema: z.ZodCustom<Record<string, unknown>, Record<string, unknown>>;
|
|
30
|
-
/**
|
|
31
|
-
* Generic interface for objects that may contain a data property.
|
|
32
|
-
*
|
|
33
|
-
* @template R - The type of the data property
|
|
34
|
-
*/
|
|
35
|
-
export interface MaybeData<R> {
|
|
36
|
-
data?: R;
|
|
37
|
-
}
|
|
38
30
|
/**
|
|
39
31
|
* Turns a JSON API response into the value a resource method returns: unwraps the `data` envelope and validates the
|
|
40
32
|
* result against `schema`, one of the schemas generated from the OpenAPI specification. The validated copy is what
|
|
@@ -46,11 +38,6 @@ export interface MaybeData<R> {
|
|
|
46
38
|
* @internal
|
|
47
39
|
*/
|
|
48
40
|
export declare function parseResponse<R>(response: ApifyResponse, schema: z.ZodType): R;
|
|
49
|
-
/**
|
|
50
|
-
* Returns object's 'data' property or throws if parameter is not an object,
|
|
51
|
-
* or an object without a 'data' property.
|
|
52
|
-
*/
|
|
53
|
-
export declare function pluckData<R>(obj: MaybeData<R>): R;
|
|
54
41
|
/**
|
|
55
42
|
* Swallows a 404 Not Found API error and rethrows anything else.
|
|
56
43
|
*/
|
package/dist/utils.js
CHANGED
|
@@ -80,6 +80,7 @@ export const anyObjectSchema = z.custom(isNonArrayObject, {
|
|
|
80
80
|
// any tree-shaking bundler drops it and every message degrades to a bare "Invalid input". Passing it
|
|
81
81
|
// in per parse keeps them intact without reaching into the zod config the whole process shares.
|
|
82
82
|
const { localeError } = z.locales.en();
|
|
83
|
+
const RESPONSE_ENVELOPE_SCHEMA = z.object({ data: z.unknown() });
|
|
83
84
|
/**
|
|
84
85
|
* Turns a JSON API response into the value a resource method returns: unwraps the `data` envelope and validates the
|
|
85
86
|
* result against `schema`, one of the schemas generated from the OpenAPI specification. The validated copy is what
|
|
@@ -91,25 +92,18 @@ const { localeError } = z.locales.en();
|
|
|
91
92
|
* @internal
|
|
92
93
|
*/
|
|
93
94
|
export function parseResponse(response, schema) {
|
|
94
|
-
const
|
|
95
|
+
const { method = 'GET', url = '' } = response.config;
|
|
96
|
+
const envelope = RESPONSE_ENVELOPE_SCHEMA.safeParse(response.data, { error: localeError });
|
|
97
|
+
if (!envelope.success) {
|
|
98
|
+
throw new ResponseValidationError(envelope.error, response.data, { method, url });
|
|
99
|
+
}
|
|
100
|
+
const { data } = envelope.data;
|
|
95
101
|
const result = schema.safeParse(data, { error: localeError });
|
|
96
102
|
if (!result.success) {
|
|
97
|
-
const { method = 'GET', url = '' } = response.config;
|
|
98
103
|
throw new ResponseValidationError(result.error, data, { method, url });
|
|
99
104
|
}
|
|
100
105
|
return result.data;
|
|
101
106
|
}
|
|
102
|
-
/**
|
|
103
|
-
* Returns object's 'data' property or throws if parameter is not an object,
|
|
104
|
-
* or an object without a 'data' property.
|
|
105
|
-
*/
|
|
106
|
-
export function pluckData(obj) {
|
|
107
|
-
if (typeof obj === 'object' && obj) {
|
|
108
|
-
if (typeof obj.data !== 'undefined')
|
|
109
|
-
return obj.data;
|
|
110
|
-
}
|
|
111
|
-
throw new Error(`Expected response object with a "data" property, but received: ${obj}`);
|
|
112
|
-
}
|
|
113
107
|
/**
|
|
114
108
|
* Swallows a 404 Not Found API error and rethrows anything else.
|
|
115
109
|
*/
|