apify-client 3.0.0-beta.5 → 3.0.0-beta.51
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 +13 -3
- package/dist/apify_api_error.d.ts +62 -21
- package/dist/apify_api_error.js +96 -23
- package/dist/apify_client.d.ts +125 -10
- package/dist/apify_client.js +133 -48
- package/dist/base/api_client.d.ts +7 -7
- package/dist/base/api_client.js +36 -24
- package/dist/base/resource_client.d.ts +21 -8
- package/dist/base/resource_client.js +54 -26
- package/dist/base/resource_collection_client.d.ts +12 -4
- package/dist/base/resource_collection_client.js +34 -11
- package/dist/body_parser.d.ts +3 -4
- package/dist/body_parser.js +19 -26
- package/dist/bundle.js +27 -22
- package/dist/bundle.js.map +1 -1
- package/dist/generated/api.d.ts +610 -112
- package/dist/generated/schemas.d.ts +726 -703
- package/dist/generated/schemas.js +92 -85
- package/dist/http_clients/axios.d.ts +60 -0
- package/dist/http_clients/axios.js +117 -0
- package/dist/http_clients/base.d.ts +333 -0
- package/dist/http_clients/base.js +563 -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 +6 -0
- package/dist/http_compressors/index.js +2 -0
- package/dist/http_compressors/resolve.d.ts +23 -0
- package/dist/http_compressors/resolve.js +27 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +3 -1
- package/dist/invalid_response_body_error.d.ts +13 -0
- package/dist/invalid_response_body_error.js +17 -0
- package/dist/models.d.ts +19 -22
- package/dist/resource_clients/actor.d.ts +72 -32
- package/dist/resource_clients/actor.js +87 -56
- package/dist/resource_clients/actor_collection.d.ts +9 -5
- package/dist/resource_clients/actor_collection.js +11 -3
- package/dist/resource_clients/actor_env_var.d.ts +10 -3
- package/dist/resource_clients/actor_env_var.js +16 -6
- package/dist/resource_clients/actor_env_var_collection.d.ts +14 -18
- package/dist/resource_clients/actor_env_var_collection.js +17 -11
- package/dist/resource_clients/actor_version.d.ts +18 -3
- package/dist/resource_clients/actor_version.js +19 -9
- package/dist/resource_clients/actor_version_collection.d.ts +16 -17
- package/dist/resource_clients/actor_version_collection.js +19 -11
- package/dist/resource_clients/build.d.ts +18 -6
- package/dist/resource_clients/build.js +34 -15
- package/dist/resource_clients/build_collection.d.ts +3 -1
- package/dist/resource_clients/build_collection.js +4 -1
- package/dist/resource_clients/dataset.d.ts +51 -33
- package/dist/resource_clients/dataset.js +105 -80
- package/dist/resource_clients/dataset_collection.d.ts +6 -2
- package/dist/resource_clients/dataset_collection.js +13 -2
- package/dist/resource_clients/key_value_store.d.ts +58 -16
- package/dist/resource_clients/key_value_store.js +78 -40
- package/dist/resource_clients/key_value_store_collection.d.ts +6 -2
- package/dist/resource_clients/key_value_store_collection.js +13 -2
- package/dist/resource_clients/log.d.ts +12 -20
- package/dist/resource_clients/log.js +93 -58
- package/dist/resource_clients/request_queue.d.ts +56 -26
- package/dist/resource_clients/request_queue.js +176 -103
- package/dist/resource_clients/request_queue_collection.d.ts +6 -2
- package/dist/resource_clients/request_queue_collection.js +9 -3
- package/dist/resource_clients/run.d.ts +108 -18
- package/dist/resource_clients/run.js +183 -43
- package/dist/resource_clients/run_collection.d.ts +3 -1
- package/dist/resource_clients/run_collection.js +4 -1
- package/dist/resource_clients/schedule.d.ts +14 -5
- package/dist/resource_clients/schedule.js +29 -21
- package/dist/resource_clients/schedule_collection.d.ts +6 -2
- package/dist/resource_clients/schedule_collection.js +9 -3
- package/dist/resource_clients/store_collection.d.ts +3 -1
- package/dist/resource_clients/store_collection.js +4 -1
- package/dist/resource_clients/task.d.ts +33 -10
- package/dist/resource_clients/task.js +79 -46
- package/dist/resource_clients/task_collection.d.ts +6 -2
- package/dist/resource_clients/task_collection.js +9 -3
- package/dist/resource_clients/user.d.ts +17 -8
- package/dist/resource_clients/user.js +42 -38
- package/dist/resource_clients/webhook.d.ts +14 -5
- package/dist/resource_clients/webhook.js +30 -22
- package/dist/resource_clients/webhook_collection.d.ts +6 -2
- package/dist/resource_clients/webhook_collection.js +9 -3
- package/dist/resource_clients/webhook_dispatch.d.ts +4 -1
- package/dist/resource_clients/webhook_dispatch.js +7 -2
- package/dist/resource_clients/webhook_dispatch_collection.d.ts +3 -1
- package/dist/resource_clients/webhook_dispatch_collection.js +4 -1
- package/dist/runtime/node.d.ts +6 -0
- package/dist/runtime/node.js +49 -0
- package/dist/runtime/types.d.ts +52 -0
- package/dist/runtime/types.js +1 -0
- package/dist/runtime/web.d.ts +5 -0
- package/dist/runtime/web.js +18 -0
- package/dist/schemas.d.ts +1 -1
- package/dist/schemas.js +1 -1
- package/dist/statistics.d.ts +1 -6
- package/dist/statistics.js +2 -2
- package/dist/timeouts.d.ts +84 -0
- package/dist/timeouts.js +37 -0
- package/dist/timezones.d.ts +1 -1
- package/dist/utils.d.ts +81 -52
- package/dist/utils.js +201 -170
- package/dist/wait_for_resources.d.ts +8 -0
- package/dist/wait_for_resources.js +39 -0
- package/package.json +32 -29
- package/dist/http_client.d.ts +0 -88
- package/dist/http_client.js +0 -255
- package/dist/interceptors.d.ts +0 -19
- package/dist/interceptors.js +0 -105
package/README.md
CHANGED
|
@@ -47,7 +47,9 @@ Besides greatly simplifying the process of querying the Apify API, the client pr
|
|
|
47
47
|
Based on the endpoint, the client automatically extracts the relevant data and returns it in the
|
|
48
48
|
expected format. Date strings are automatically converted to `Date` objects. For exceptions,
|
|
49
49
|
we throw an `ApifyApiError`, which wraps the plain JSON errors returned by API and enriches
|
|
50
|
-
them with other context for easier debugging.
|
|
50
|
+
them with other context for easier debugging. The error is an instance of the subclass matching the
|
|
51
|
+
HTTP status code, such as `NotFoundError` or `RateLimitError`, so a `catch` block can tell them apart
|
|
52
|
+
with `instanceof`.
|
|
51
53
|
|
|
52
54
|
### Retries with exponential backoff
|
|
53
55
|
|
|
@@ -57,6 +59,12 @@ By default, it will retry up to 8 times. First retry will be attempted after ~50
|
|
|
57
59
|
and so on. You can configure those parameters using the `maxRetries` and `minDelayBetweenRetriesMillis`
|
|
58
60
|
options of the `ApifyClient` constructor.
|
|
59
61
|
|
|
62
|
+
### Pluggable HTTP client
|
|
63
|
+
|
|
64
|
+
The client ships with an axios-based HTTP client and accepts a custom one. Extend `HttpClient`, implement
|
|
65
|
+
`sendRequest()` over the HTTP library of your choice and plug it in with `ApifyClient.withCustomHttpClient()`. The
|
|
66
|
+
custom client inherits the retries, timeouts, compression and error handling of the built-in one.
|
|
67
|
+
|
|
60
68
|
### Convenience functions and options
|
|
61
69
|
|
|
62
70
|
Some actions can't be performed by the API itself, such as indefinite waiting for an actor run to finish
|
|
@@ -143,13 +151,15 @@ property to get only a subset of results. Other props are also available, depend
|
|
|
143
151
|
|
|
144
152
|
## Bundled environments
|
|
145
153
|
|
|
146
|
-
The package includes a pre-built browser bundle that
|
|
154
|
+
The package includes a pre-built browser bundle that bundlers targeting browsers resolve automatically. You can also import it explicitly:
|
|
147
155
|
|
|
148
156
|
```typescript
|
|
149
157
|
import { ApifyClient } from 'apify-client/browser';
|
|
150
158
|
```
|
|
151
159
|
|
|
152
|
-
|
|
160
|
+
Only two parts of the client need Node.js built-ins: the HTTP agents and request compression. The `node` condition selects the Node.js implementation of those, and every other target gets one built on Web APIs. Log streaming, proxy support, and request compression are only available in Node.js.
|
|
161
|
+
|
|
162
|
+
For details, see [Bundled environments](https://docs.apify.com/api/client/js/docs/concepts/bundled-environments).
|
|
153
163
|
|
|
154
164
|
## API Reference
|
|
155
165
|
|
|
@@ -1,16 +1,28 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { LiteralUnion } from 'type-fest';
|
|
2
|
+
import type { ApifyResponse } from './http_clients/base.js';
|
|
3
|
+
import type { ApifyApiErrorType } from './models.js';
|
|
4
|
+
export type { ApifyApiErrorType } from './models.js';
|
|
2
5
|
/**
|
|
3
6
|
* An `ApifyApiError` is thrown for successful HTTP requests that reach the API,
|
|
4
7
|
* but the API responds with an error response. Typically, those are rate limit
|
|
5
8
|
* errors and internal errors, which are automatically retried, or validation
|
|
6
9
|
* errors, which are thrown immediately, because a correction by the user is
|
|
7
10
|
* needed.
|
|
11
|
+
*
|
|
12
|
+
* The thrown error is an instance of the subclass matching the HTTP status code of the response:
|
|
13
|
+
* {@link InvalidRequestError} (400), {@link UnauthorizedError} (401), {@link ForbiddenError} (403),
|
|
14
|
+
* {@link NotFoundError} (404), {@link ConflictError} (409), {@link RateLimitError} (429) or
|
|
15
|
+
* {@link ServerError} (5xx). Any other status code is thrown as a plain `ApifyApiError`. Every
|
|
16
|
+
* subclass extends `ApifyApiError`, so `instanceof ApifyApiError` matches all of them. Errors that
|
|
17
|
+
* share a status code are told apart by their `type`.
|
|
8
18
|
*/
|
|
9
19
|
export declare class ApifyApiError extends Error {
|
|
20
|
+
#private;
|
|
10
21
|
name: string;
|
|
11
22
|
/**
|
|
12
|
-
* The
|
|
13
|
-
*
|
|
23
|
+
* The resource client and the method parsed from the stack trace, such as `ActorClient.getResource`. A public
|
|
24
|
+
* method that delegates to a shared helper of its class is reported under the helper. The value is `unknown`
|
|
25
|
+
* when the stack trace cannot be parsed.
|
|
14
26
|
*/
|
|
15
27
|
clientMethod: string;
|
|
16
28
|
/**
|
|
@@ -18,9 +30,10 @@ export declare class ApifyApiError extends Error {
|
|
|
18
30
|
*/
|
|
19
31
|
statusCode: number;
|
|
20
32
|
/**
|
|
21
|
-
* The type of the error, as returned by the API.
|
|
33
|
+
* The type of the error, as returned by the API. Typed as the known {@link ApifyApiErrorType}
|
|
34
|
+
* values for autocompletion, while still accepting any string the API may return.
|
|
22
35
|
*/
|
|
23
|
-
type?: string
|
|
36
|
+
type?: LiteralUnion<ApifyApiErrorType, string>;
|
|
24
37
|
/**
|
|
25
38
|
* Number of the API call attempt.
|
|
26
39
|
*/
|
|
@@ -45,22 +58,50 @@ export declare class ApifyApiError extends Error {
|
|
|
45
58
|
/**
|
|
46
59
|
* @hidden
|
|
47
60
|
*/
|
|
48
|
-
constructor(response:
|
|
49
|
-
private _safelyParsePathFromResponse;
|
|
50
|
-
private _extractClientAndMethodFromStack;
|
|
61
|
+
constructor(response: ApifyResponse, attempt: number);
|
|
51
62
|
/**
|
|
52
|
-
* Creates a
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* Example:
|
|
56
|
-
*
|
|
57
|
-
* ApifyApiError: Actor task was not found
|
|
58
|
-
* clientMethod: TaskClient.start
|
|
59
|
-
* statusCode: 404
|
|
60
|
-
* type: record-not-found
|
|
61
|
-
* attempt: 1
|
|
62
|
-
* httpMethod: post
|
|
63
|
-
* path: /v2/actor-tasks/user~my-task/runs
|
|
63
|
+
* Creates the error for a failed response as an instance of the subclass matching its HTTP status code.
|
|
64
|
+
* @hidden
|
|
64
65
|
*/
|
|
65
|
-
|
|
66
|
+
static fromResponse(response: ApifyResponse, attempt: number): ApifyApiError;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Thrown when the Apify API responds with HTTP 400 Bad Request, typically because the request
|
|
70
|
+
* failed validation.
|
|
71
|
+
*/
|
|
72
|
+
export declare class InvalidRequestError extends ApifyApiError {
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Thrown when the Apify API responds with HTTP 401 Unauthorized, because the token is missing
|
|
76
|
+
* or invalid.
|
|
77
|
+
*/
|
|
78
|
+
export declare class UnauthorizedError extends ApifyApiError {
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Thrown when the Apify API responds with HTTP 403 Forbidden, because the token lacks the
|
|
82
|
+
* permission for the operation.
|
|
83
|
+
*/
|
|
84
|
+
export declare class ForbiddenError extends ApifyApiError {
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Thrown when the Apify API responds with HTTP 404 Not Found.
|
|
88
|
+
*/
|
|
89
|
+
export declare class NotFoundError extends ApifyApiError {
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Thrown when the Apify API responds with HTTP 409 Conflict.
|
|
93
|
+
*/
|
|
94
|
+
export declare class ConflictError extends ApifyApiError {
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Thrown when the Apify API responds with HTTP 429 Too Many Requests. The client retries such
|
|
98
|
+
* requests, so the error surfaces once the retries are exhausted.
|
|
99
|
+
*/
|
|
100
|
+
export declare class RateLimitError extends ApifyApiError {
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Thrown when the Apify API responds with an HTTP 5xx status. The client retries such requests,
|
|
104
|
+
* so the error surfaces once the retries are exhausted.
|
|
105
|
+
*/
|
|
106
|
+
export declare class ServerError extends ApifyApiError {
|
|
66
107
|
}
|
package/dist/apify_api_error.js
CHANGED
|
@@ -1,27 +1,41 @@
|
|
|
1
|
-
import { isomorphicBufferToString } from './body_parser.js';
|
|
2
1
|
import { isBuffer } from './utils.js';
|
|
3
2
|
/**
|
|
4
|
-
* Examples of capturing groups for "...at ActorCollectionClient.
|
|
5
|
-
* 0: "at ActorCollectionClient.
|
|
3
|
+
* Examples of capturing groups for "...at ActorCollectionClient.listResources (/Users/..."
|
|
4
|
+
* 0: "at ActorCollectionClient.listResources ("
|
|
6
5
|
* 1: undefined
|
|
7
6
|
* 2: "ActorCollectionClient"
|
|
8
7
|
* 3: undefined
|
|
9
|
-
* 4: "
|
|
8
|
+
* 4: "listResources"
|
|
9
|
+
*
|
|
10
|
+
* The error is created inside the HTTP client's pipeline, whose frames sit above the resource client's. The
|
|
11
|
+
* pipeline's own methods are private, so V8 prints them with a `#` that no method name can match. Its public
|
|
12
|
+
* `call()` prints under the name of the transport's class, which can be anything, so the lookaheads skip a class
|
|
13
|
+
* named after `HttpClient` and the method `call`. A transport named `SomethingClient` whose `call()` override
|
|
14
|
+
* awaits a helper of its own is reported under that helper. `ActorClient.call()` and `TaskClient.call()` reach the
|
|
15
|
+
* pipeline through `start()`, whose frame sits closer to the error and matches first.
|
|
10
16
|
* @private
|
|
11
17
|
*/
|
|
12
|
-
const CLIENT_METHOD_REGEX = /at( async)? ([A-Za-z]+(Collection)?Client)\.
|
|
18
|
+
const CLIENT_METHOD_REGEX = /at( async)? (?![A-Za-z]*HttpClient\.)([A-Za-z]+(Collection)?Client)\.(?!call \()([A-Za-z]+) \(/;
|
|
13
19
|
/**
|
|
14
20
|
* An `ApifyApiError` is thrown for successful HTTP requests that reach the API,
|
|
15
21
|
* but the API responds with an error response. Typically, those are rate limit
|
|
16
22
|
* errors and internal errors, which are automatically retried, or validation
|
|
17
23
|
* errors, which are thrown immediately, because a correction by the user is
|
|
18
24
|
* needed.
|
|
25
|
+
*
|
|
26
|
+
* The thrown error is an instance of the subclass matching the HTTP status code of the response:
|
|
27
|
+
* {@link InvalidRequestError} (400), {@link UnauthorizedError} (401), {@link ForbiddenError} (403),
|
|
28
|
+
* {@link NotFoundError} (404), {@link ConflictError} (409), {@link RateLimitError} (429) or
|
|
29
|
+
* {@link ServerError} (5xx). Any other status code is thrown as a plain `ApifyApiError`. Every
|
|
30
|
+
* subclass extends `ApifyApiError`, so `instanceof ApifyApiError` matches all of them. Errors that
|
|
31
|
+
* share a status code are told apart by their `type`.
|
|
19
32
|
*/
|
|
20
33
|
export class ApifyApiError extends Error {
|
|
21
34
|
name;
|
|
22
35
|
/**
|
|
23
|
-
* The
|
|
24
|
-
*
|
|
36
|
+
* The resource client and the method parsed from the stack trace, such as `ActorClient.getResource`. A public
|
|
37
|
+
* method that delegates to a shared helper of its class is reported under the helper. The value is `unknown`
|
|
38
|
+
* when the stack trace cannot be parsed.
|
|
25
39
|
*/
|
|
26
40
|
clientMethod;
|
|
27
41
|
/**
|
|
@@ -29,7 +43,8 @@ export class ApifyApiError extends Error {
|
|
|
29
43
|
*/
|
|
30
44
|
statusCode;
|
|
31
45
|
/**
|
|
32
|
-
* The type of the error, as returned by the API.
|
|
46
|
+
* The type of the error, as returned by the API. Typed as the known {@link ApifyApiErrorType}
|
|
47
|
+
* values for autocompletion, while still accepting any string the API may return.
|
|
33
48
|
*/
|
|
34
49
|
type;
|
|
35
50
|
/**
|
|
@@ -61,14 +76,16 @@ export class ApifyApiError extends Error {
|
|
|
61
76
|
let type;
|
|
62
77
|
let responseData = response.data;
|
|
63
78
|
let errorData;
|
|
64
|
-
//
|
|
65
|
-
//
|
|
79
|
+
// A `responseType: 'buffer'` request (e.g. `downloadItems()`) and a failed streaming request, whose body
|
|
80
|
+
// `HttpClient` has read into a buffer, both arrive unparsed. Parse the body here to get at the error.
|
|
66
81
|
if (isBuffer(responseData)) {
|
|
82
|
+
const body = new TextDecoder().decode(response.data);
|
|
67
83
|
try {
|
|
68
|
-
responseData = JSON.parse(
|
|
84
|
+
responseData = JSON.parse(body);
|
|
69
85
|
}
|
|
70
86
|
catch {
|
|
71
|
-
//
|
|
87
|
+
// A body that is not JSON at all, such as an HTML error page from a proxy, is kept as text.
|
|
88
|
+
responseData = body;
|
|
72
89
|
}
|
|
73
90
|
}
|
|
74
91
|
if (responseData && responseData.error) {
|
|
@@ -89,18 +106,26 @@ export class ApifyApiError extends Error {
|
|
|
89
106
|
}
|
|
90
107
|
super(message);
|
|
91
108
|
this.name = this.constructor.name;
|
|
92
|
-
this.clientMethod = this
|
|
109
|
+
this.clientMethod = this.#extractClientAndMethodFromStack();
|
|
93
110
|
this.statusCode = response.status;
|
|
94
111
|
this.type = type;
|
|
95
112
|
this.attempt = attempt;
|
|
96
113
|
this.httpMethod = response.config?.method;
|
|
97
|
-
this.path = this
|
|
114
|
+
this.path = this.#safelyParsePathFromResponse(response);
|
|
98
115
|
const stack = this.stack;
|
|
99
116
|
this.originalStack = stack.slice(stack.indexOf('\n'));
|
|
100
|
-
this.stack = this
|
|
117
|
+
this.stack = this.#createApiStack();
|
|
101
118
|
this.data = errorData;
|
|
102
119
|
}
|
|
103
|
-
|
|
120
|
+
/**
|
|
121
|
+
* Creates the error for a failed response as an instance of the subclass matching its HTTP status code.
|
|
122
|
+
* @hidden
|
|
123
|
+
*/
|
|
124
|
+
static fromResponse(response, attempt) {
|
|
125
|
+
const ErrorClass = ERROR_CLASS_BY_STATUS[response.status] ?? (response.status >= 500 ? ServerError : ApifyApiError);
|
|
126
|
+
return new ErrorClass(response, attempt);
|
|
127
|
+
}
|
|
128
|
+
#safelyParsePathFromResponse(response) {
|
|
104
129
|
const urlString = response.config?.url;
|
|
105
130
|
let url;
|
|
106
131
|
try {
|
|
@@ -111,11 +136,11 @@ export class ApifyApiError extends Error {
|
|
|
111
136
|
}
|
|
112
137
|
return url.pathname + url.search;
|
|
113
138
|
}
|
|
114
|
-
|
|
139
|
+
#extractClientAndMethodFromStack() {
|
|
115
140
|
const match = this.stack.match(CLIENT_METHOD_REGEX);
|
|
116
|
-
if (match)
|
|
117
|
-
return
|
|
118
|
-
return
|
|
141
|
+
if (!match)
|
|
142
|
+
return 'unknown';
|
|
143
|
+
return `${match[2]}.${match[4]}`;
|
|
119
144
|
}
|
|
120
145
|
/**
|
|
121
146
|
* Creates a better looking and more informative stack that will be printed
|
|
@@ -123,15 +148,15 @@ export class ApifyApiError extends Error {
|
|
|
123
148
|
*
|
|
124
149
|
* Example:
|
|
125
150
|
*
|
|
126
|
-
*
|
|
151
|
+
* NotFoundError: Actor task was not found
|
|
127
152
|
* clientMethod: TaskClient.start
|
|
128
153
|
* statusCode: 404
|
|
129
154
|
* type: record-not-found
|
|
130
155
|
* attempt: 1
|
|
131
|
-
* httpMethod:
|
|
156
|
+
* httpMethod: POST
|
|
132
157
|
* path: /v2/actor-tasks/user~my-task/runs
|
|
133
158
|
*/
|
|
134
|
-
|
|
159
|
+
#createApiStack() {
|
|
135
160
|
const { name, ...props } = this;
|
|
136
161
|
const stack = Object.entries(props)
|
|
137
162
|
.map(([k, v]) => {
|
|
@@ -145,3 +170,51 @@ export class ApifyApiError extends Error {
|
|
|
145
170
|
return `${name}: ${this.message}\n${stack}`;
|
|
146
171
|
}
|
|
147
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Thrown when the Apify API responds with HTTP 400 Bad Request, typically because the request
|
|
175
|
+
* failed validation.
|
|
176
|
+
*/
|
|
177
|
+
export class InvalidRequestError extends ApifyApiError {
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Thrown when the Apify API responds with HTTP 401 Unauthorized, because the token is missing
|
|
181
|
+
* or invalid.
|
|
182
|
+
*/
|
|
183
|
+
export class UnauthorizedError extends ApifyApiError {
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Thrown when the Apify API responds with HTTP 403 Forbidden, because the token lacks the
|
|
187
|
+
* permission for the operation.
|
|
188
|
+
*/
|
|
189
|
+
export class ForbiddenError extends ApifyApiError {
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Thrown when the Apify API responds with HTTP 404 Not Found.
|
|
193
|
+
*/
|
|
194
|
+
export class NotFoundError extends ApifyApiError {
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Thrown when the Apify API responds with HTTP 409 Conflict.
|
|
198
|
+
*/
|
|
199
|
+
export class ConflictError extends ApifyApiError {
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Thrown when the Apify API responds with HTTP 429 Too Many Requests. The client retries such
|
|
203
|
+
* requests, so the error surfaces once the retries are exhausted.
|
|
204
|
+
*/
|
|
205
|
+
export class RateLimitError extends ApifyApiError {
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Thrown when the Apify API responds with an HTTP 5xx status. The client retries such requests,
|
|
209
|
+
* so the error surfaces once the retries are exhausted.
|
|
210
|
+
*/
|
|
211
|
+
export class ServerError extends ApifyApiError {
|
|
212
|
+
}
|
|
213
|
+
const ERROR_CLASS_BY_STATUS = {
|
|
214
|
+
400: InvalidRequestError,
|
|
215
|
+
401: UnauthorizedError,
|
|
216
|
+
403: ForbiddenError,
|
|
217
|
+
404: NotFoundError,
|
|
218
|
+
409: ConflictError,
|
|
219
|
+
429: RateLimitError,
|
|
220
|
+
};
|
package/dist/apify_client.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { SetStatusMessageOptions } from '@crawlee/types';
|
|
2
1
|
import type { Log } from '@apify/log';
|
|
3
|
-
import { HttpClient } from './
|
|
4
|
-
import type {
|
|
2
|
+
import { HttpClient } from './http_clients/base.js';
|
|
3
|
+
import type { HttpCompressor } from './http_compressors/base.js';
|
|
4
|
+
import type { HttpCompressionAlgorithm } from './http_compressors/resolve.js';
|
|
5
5
|
import { ActorClient } from './resource_clients/actor.js';
|
|
6
6
|
import { ActorCollectionClient } from './resource_clients/actor_collection.js';
|
|
7
7
|
import { BuildClient } from './resource_clients/build.js';
|
|
@@ -49,17 +49,66 @@ import { Statistics } from './statistics.js';
|
|
|
49
49
|
* @see https://docs.apify.com/api/v2
|
|
50
50
|
*/
|
|
51
51
|
export declare class ApifyClient {
|
|
52
|
+
#private;
|
|
52
53
|
baseUrl: string;
|
|
53
54
|
/**
|
|
54
55
|
* @since Added in 2.17.0
|
|
55
56
|
*/
|
|
56
57
|
publicBaseUrl: string;
|
|
57
58
|
token?: string;
|
|
59
|
+
/**
|
|
60
|
+
* Statistics of the API calls made through the client. With a custom HTTP client, this is the client's own
|
|
61
|
+
* `stats` object, so it only reflects what that client records.
|
|
62
|
+
*/
|
|
58
63
|
stats: Statistics;
|
|
59
64
|
logger: Log;
|
|
60
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Creates a client that sends its requests through the built-in {@link AxiosHttpClient}, configured from
|
|
67
|
+
* `options`: the API token, retries, the timeout tiers, request compression and default headers. The HTTP
|
|
68
|
+
* client itself is created when {@link httpClient} is first read. To send the requests through a custom HTTP
|
|
69
|
+
* client, use {@link ApifyClient.withCustomHttpClient} instead.
|
|
70
|
+
*
|
|
71
|
+
* @param options - Configuration of the client and of its HTTP client.
|
|
72
|
+
* @throws {ArgumentValidationError} When an option is unknown or has an invalid value.
|
|
73
|
+
*/
|
|
61
74
|
constructor(options?: ApifyClientOptions);
|
|
62
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Creates an `ApifyClient` that sends its requests through a custom HTTP client.
|
|
77
|
+
*
|
|
78
|
+
* The custom client owns its transport configuration: retries, timeouts, default headers and whatever its
|
|
79
|
+
* transport needs are set on the client itself. This is also how the built-in {@link AxiosHttpClient} is
|
|
80
|
+
* configured beyond what the constructor options offer, for example with axios request interceptors. The
|
|
81
|
+
* token is set as the client's `Authorization` header, unless the client already has one configured.
|
|
82
|
+
*
|
|
83
|
+
* @param options - The token, the API URLs and the HTTP client to use.
|
|
84
|
+
* @returns A client whose {@link httpClient} is the given one.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```javascript
|
|
88
|
+
* import { ApifyClient, HttpClient } from 'apify-client';
|
|
89
|
+
*
|
|
90
|
+
* class MyHttpClient extends HttpClient {
|
|
91
|
+
* async sendRequest({ method, url, headers, body, timeoutMillis, stream, signal }) {
|
|
92
|
+
* // Send the request with the HTTP library of your choice and return { status, headers, body }.
|
|
93
|
+
* }
|
|
94
|
+
* }
|
|
95
|
+
*
|
|
96
|
+
* const client = ApifyClient.withCustomHttpClient({ token: 'my-token', httpClient: new MyHttpClient() });
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
static withCustomHttpClient(options: ApifyClientCustomHttpClientOptions): ApifyClient;
|
|
100
|
+
/**
|
|
101
|
+
* The HTTP client the requests go through: the custom one when set through
|
|
102
|
+
* {@link ApifyClient.withCustomHttpClient}, and otherwise an {@link AxiosHttpClient} configured from the
|
|
103
|
+
* constructor options, created on first access. Assigning a client applies {@link token} to it through
|
|
104
|
+
* {@link HttpClient.setDefaultAuthorization} and points {@link stats} at its statistics, so the counters keep
|
|
105
|
+
* tracking the calls the client actually makes. Resource clients hold on to the HTTP client they were created
|
|
106
|
+
* with, so assign before reaching for them. Assigning does not close the client it replaces. A default client
|
|
107
|
+
* that already sent a request keeps its keep-alive agents open until {@link HttpClient.close} is called on it,
|
|
108
|
+
* so close it yourself, or assign before the first request.
|
|
109
|
+
*/
|
|
110
|
+
get httpClient(): HttpClient;
|
|
111
|
+
set httpClient(httpClient: HttpClient);
|
|
63
112
|
/**
|
|
64
113
|
* Returns a client for managing Actors in your account.
|
|
65
114
|
*
|
|
@@ -347,13 +396,41 @@ export declare class ApifyClient {
|
|
|
347
396
|
*/
|
|
348
397
|
setStatusMessage(message: string, options?: SetStatusMessageOptions): Promise<void>;
|
|
349
398
|
}
|
|
399
|
+
/**
|
|
400
|
+
* Options for {@apilink ApifyClient.setStatusMessage}. Structurally identical to crawlee's
|
|
401
|
+
* `SetStatusMessageOptions`, so the client stays a drop-in `StorageClient` implementation
|
|
402
|
+
* without a dependency on the crawlee packages.
|
|
403
|
+
*/
|
|
404
|
+
export interface SetStatusMessageOptions {
|
|
405
|
+
/** If true, the status message is marked as the final one for the run. */
|
|
406
|
+
isStatusMessageTerminal?: boolean;
|
|
407
|
+
/** The log level for the status message. */
|
|
408
|
+
level?: 'DEBUG' | 'INFO' | 'WARNING' | 'ERROR';
|
|
409
|
+
}
|
|
350
410
|
/**
|
|
351
411
|
* Configuration options for ApifyClient.
|
|
352
412
|
*/
|
|
353
413
|
export interface ApifyClientOptions {
|
|
354
|
-
/**
|
|
414
|
+
/**
|
|
415
|
+
* URL of the Apify API, with or without the `/v2` version path, which is appended when missing.
|
|
416
|
+
* @default https://api.apify.com
|
|
417
|
+
*/
|
|
355
418
|
baseUrl?: string;
|
|
356
419
|
/**
|
|
420
|
+
* Compression of request bodies: the name of a built-in algorithm, or an {@link HttpCompressor} for a custom
|
|
421
|
+
* quality or algorithm. Bodies are compressed only in Node.js, and only when they are large enough to benefit
|
|
422
|
+
* and their content type does not already carry its own compression.
|
|
423
|
+
* @default 'brotli'
|
|
424
|
+
*/
|
|
425
|
+
compression?: HttpCompressionAlgorithm | HttpCompressor;
|
|
426
|
+
/**
|
|
427
|
+
* Additional headers sent with every request, such as an integration platform marker. They win over the
|
|
428
|
+
* built-in defaults, and a header set on a single call wins over them.
|
|
429
|
+
*/
|
|
430
|
+
headers?: Record<string, string>;
|
|
431
|
+
/**
|
|
432
|
+
* Publicly accessible URL of the Apify API, used in the URLs the client generates. Like `baseUrl`, with or
|
|
433
|
+
* without the `/v2` version path.
|
|
357
434
|
* @default https://api.apify.com
|
|
358
435
|
* @since Added in 2.17.0
|
|
359
436
|
*/
|
|
@@ -362,13 +439,51 @@ export interface ApifyClientOptions {
|
|
|
362
439
|
maxRetries?: number;
|
|
363
440
|
/** @default 500 */
|
|
364
441
|
minDelayBetweenRetriesMillis?: number;
|
|
365
|
-
/**
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
442
|
+
/**
|
|
443
|
+
* Duration of the `short` timeout tier, in seconds: simple metadata reads and writes.
|
|
444
|
+
* @default 5
|
|
445
|
+
*/
|
|
446
|
+
timeoutShortSecs?: number;
|
|
447
|
+
/**
|
|
448
|
+
* Duration of the `medium` timeout tier, in seconds: listing, batch and trigger operations.
|
|
449
|
+
* @default 30
|
|
450
|
+
*/
|
|
451
|
+
timeoutMediumSecs?: number;
|
|
452
|
+
/**
|
|
453
|
+
* Duration of the `long` timeout tier, in seconds: downloads, uploads and streaming.
|
|
454
|
+
* @default 360
|
|
455
|
+
*/
|
|
456
|
+
timeoutLongSecs?: number;
|
|
457
|
+
/**
|
|
458
|
+
* Cap on the timeout of a single request attempt, in seconds. It bounds the doubling of the timeout across
|
|
459
|
+
* retries, and caps tier and per-call timeouts alike, so raise it to allow a request timeout above 360 s.
|
|
460
|
+
* @default 360
|
|
461
|
+
*/
|
|
462
|
+
timeoutMaxSecs?: number;
|
|
369
463
|
token?: string;
|
|
370
464
|
/**
|
|
371
465
|
* @since Added in 2.10.0
|
|
372
466
|
*/
|
|
373
467
|
userAgentSuffix?: string | string[];
|
|
374
468
|
}
|
|
469
|
+
/**
|
|
470
|
+
* Configuration options for {@link ApifyClient.withCustomHttpClient}. Retries, timeouts and headers are configured
|
|
471
|
+
* on the HTTP client itself.
|
|
472
|
+
*/
|
|
473
|
+
export interface ApifyClientCustomHttpClientOptions {
|
|
474
|
+
/**
|
|
475
|
+
* URL of the Apify API, with or without the `/v2` version path, which is appended when missing.
|
|
476
|
+
* @default https://api.apify.com
|
|
477
|
+
*/
|
|
478
|
+
baseUrl?: string;
|
|
479
|
+
/**
|
|
480
|
+
* Publicly accessible URL of the Apify API, used in the URLs the client generates. Like `baseUrl`, with or
|
|
481
|
+
* without the `/v2` version path.
|
|
482
|
+
* @default https://api.apify.com
|
|
483
|
+
*/
|
|
484
|
+
publicBaseUrl?: string;
|
|
485
|
+
/** Set as the HTTP client's `Authorization` header, unless it already has one. */
|
|
486
|
+
token?: string;
|
|
487
|
+
/** The HTTP client to send the requests through. */
|
|
488
|
+
httpClient: HttpClient;
|
|
489
|
+
}
|