apify-client 3.0.0-beta.12 → 3.0.0-beta.120
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 +23 -2
- package/dist/apify_api_error.d.ts +14 -28
- package/dist/apify_api_error.js +33 -23
- package/dist/apify_client.d.ts +125 -10
- package/dist/apify_client.js +133 -48
- package/dist/apify_client_error.d.ts +10 -0
- package/dist/apify_client_error.js +10 -0
- package/dist/axios.d.ts +2 -0
- package/dist/axios.js +1 -0
- package/dist/base/api_client.d.ts +7 -7
- package/dist/base/api_client.js +36 -36
- package/dist/base/resource_client.d.ts +13 -8
- package/dist/base/resource_client.js +60 -34
- 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 +4 -5
- package/dist/body_parser.js +19 -26
- package/dist/bundle.js +41 -35
- package/dist/bundle.js.map +1 -1
- package/dist/generated/api.d.ts +3259 -1060
- package/dist/generated/schemas.d.ts +2608 -1028
- package/dist/generated/schemas.js +600 -238
- package/dist/http_clients/axios.d.ts +80 -0
- package/dist/http_clients/axios.js +165 -0
- package/dist/http_clients/base.d.ts +402 -0
- package/dist/http_clients/base.js +680 -0
- package/dist/http_clients/fetch.d.ts +53 -0
- package/dist/http_clients/fetch.js +219 -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 +7 -2
- package/dist/index.js +5 -1
- package/dist/invalid_response_body_error.d.ts +14 -0
- package/dist/invalid_response_body_error.js +18 -0
- package/dist/models.d.ts +177 -240
- package/dist/models.js +6 -4
- package/dist/resource_clients/actor.d.ts +94 -23
- package/dist/resource_clients/actor.js +129 -43
- package/dist/resource_clients/actor_collection.d.ts +15 -11
- package/dist/resource_clients/actor_collection.js +9 -3
- package/dist/resource_clients/actor_env_var.d.ts +11 -4
- package/dist/resource_clients/actor_env_var.js +16 -6
- package/dist/resource_clients/actor_env_var_collection.d.ts +9 -4
- package/dist/resource_clients/actor_env_var_collection.js +11 -4
- package/dist/resource_clients/actor_server.d.ts +149 -0
- package/dist/resource_clients/actor_server.js +195 -0
- package/dist/resource_clients/actor_version.d.ts +19 -6
- package/dist/resource_clients/actor_version.js +19 -8
- package/dist/resource_clients/actor_version_collection.d.ts +10 -4
- package/dist/resource_clients/actor_version_collection.js +12 -4
- package/dist/resource_clients/build.d.ts +18 -7
- package/dist/resource_clients/build.js +33 -15
- package/dist/resource_clients/build_collection.d.ts +7 -5
- package/dist/resource_clients/build_collection.js +4 -1
- package/dist/resource_clients/dataset.d.ts +83 -37
- package/dist/resource_clients/dataset.js +99 -109
- package/dist/resource_clients/dataset_collection.d.ts +10 -6
- package/dist/resource_clients/dataset_collection.js +13 -2
- package/dist/resource_clients/key_value_store.d.ts +108 -33
- package/dist/resource_clients/key_value_store.js +134 -53
- package/dist/resource_clients/key_value_store_collection.d.ts +10 -6
- package/dist/resource_clients/key_value_store_collection.js +13 -2
- package/dist/resource_clients/log.d.ts +41 -25
- package/dist/resource_clients/log.js +142 -65
- package/dist/resource_clients/request_queue.d.ts +77 -31
- package/dist/resource_clients/request_queue.js +178 -93
- package/dist/resource_clients/request_queue_collection.d.ts +9 -5
- package/dist/resource_clients/request_queue_collection.js +9 -3
- package/dist/resource_clients/run.d.ts +119 -29
- package/dist/resource_clients/run.js +188 -49
- package/dist/resource_clients/run_collection.d.ts +5 -3
- package/dist/resource_clients/run_collection.js +4 -1
- package/dist/resource_clients/schedule.d.ts +15 -6
- package/dist/resource_clients/schedule.js +25 -10
- 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 +10 -4
- package/dist/resource_clients/store_collection.js +4 -1
- package/dist/resource_clients/task.d.ts +35 -12
- package/dist/resource_clients/task.js +75 -35
- package/dist/resource_clients/task_collection.d.ts +9 -5
- package/dist/resource_clients/task_collection.js +9 -3
- package/dist/resource_clients/user.d.ts +16 -7
- package/dist/resource_clients/user.js +34 -16
- package/dist/resource_clients/webhook.d.ts +16 -7
- package/dist/resource_clients/webhook.js +25 -10
- package/dist/resource_clients/webhook_collection.d.ts +8 -4
- 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/response_validation_error.d.ts +2 -1
- package/dist/response_validation_error.js +2 -1
- package/dist/runtime/node.d.ts +6 -0
- package/dist/runtime/node.js +111 -0
- package/dist/runtime/types.d.ts +82 -0
- package/dist/runtime/types.js +1 -0
- package/dist/runtime/web.d.ts +5 -0
- package/dist/runtime/web.js +60 -0
- package/dist/schemas.d.ts +372 -4
- package/dist/schemas.js +26 -4
- 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 +115 -61
- package/dist/utils.js +220 -161
- package/dist/wait_for_resources.d.ts +8 -0
- package/dist/wait_for_resources.js +39 -0
- package/package.json +53 -38
- 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
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { HttpClientOptions, HttpRequest, HttpResponse } from './base.js';
|
|
2
|
+
import { HttpClient } from './base.js';
|
|
3
|
+
/**
|
|
4
|
+
* A function with the signature of the global [`fetch`](https://developer.mozilla.org/docs/Web/API/Window/fetch).
|
|
5
|
+
*/
|
|
6
|
+
export type FetchFunction = typeof globalThis.fetch;
|
|
7
|
+
/**
|
|
8
|
+
* Configuration of {@link FetchHttpClient}.
|
|
9
|
+
*/
|
|
10
|
+
export interface FetchHttpClientOptions extends HttpClientOptions {
|
|
11
|
+
/**
|
|
12
|
+
* The `fetch` implementation the requests go through. Pass a wrapper around the global `fetch` to adjust every
|
|
13
|
+
* request, or the `fetch` of another library, such as `undici` with a dispatcher of your own, to control the
|
|
14
|
+
* connections. A stream body reaches it as an async iterable of byte chunks, or as the stream itself in Bun. In a
|
|
15
|
+
* browser or an edge runtime, a web `ReadableStream` body reaches it as a web stream.
|
|
16
|
+
* @default globalThis.fetch
|
|
17
|
+
*/
|
|
18
|
+
fetch?: FetchFunction;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The default HTTP client of {@link ApifyClient}, built on the `fetch` of the runtime, so it works the same way in
|
|
22
|
+
* Node.js, browsers and edge runtimes and brings no dependency of its own.
|
|
23
|
+
*
|
|
24
|
+
* It inherits the request pipeline from {@link HttpClient} and adds the transport. Network errors are retried,
|
|
25
|
+
* timeouts included, and a stream body is sent without following redirects, since the part of it already sent could
|
|
26
|
+
* not be replayed. The timeout of a request runs until the response headers arrive and starts over with every chunk
|
|
27
|
+
* of the response body and of a stream body, so a large download or upload does not time out while it makes progress.
|
|
28
|
+
*
|
|
29
|
+
* In Node.js, `fetch` keeps connections alive on its own, and goes through a proxy only when Node.js is told to
|
|
30
|
+
* read the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables: start it with `NODE_USE_ENV_PROXY=1`
|
|
31
|
+
* (Node.js 22.21 and 24.0) or `--use-env-proxy` (Node.js 22.21 and 24.5).
|
|
32
|
+
*/
|
|
33
|
+
export declare class FetchHttpClient extends HttpClient {
|
|
34
|
+
#private;
|
|
35
|
+
constructor(options?: FetchHttpClientOptions);
|
|
36
|
+
/**
|
|
37
|
+
* A timeout is the `TimeoutError` this client aborts a late request with, or a `fetch` failure caused by one of
|
|
38
|
+
* the connection, headers and body timeouts of Node.js.
|
|
39
|
+
*/
|
|
40
|
+
isTimeoutError(error: unknown): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* A network failure is the `TypeError` that `fetch` rejects with: a connection that could not be made, dropped
|
|
43
|
+
* mid-way or timed out. In Node.js it carries the failure as its `cause`, which tells it apart from a request
|
|
44
|
+
* that was never sent, such as one with an invalid URL. Bun rejects with an `Error` that has a `code` instead.
|
|
45
|
+
*/
|
|
46
|
+
isRetryableTransportError(error: unknown): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Sends the request through `fetch` and hands the raw response back. A stream body is sent without following
|
|
49
|
+
* redirects, and so is a request with `followRedirects: false`. A browser hides such a redirect response from
|
|
50
|
+
* the page, so there it fails the request.
|
|
51
|
+
*/
|
|
52
|
+
sendRequest(request: HttpRequest): Promise<HttpResponse>;
|
|
53
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { runtime } from '#runtime';
|
|
2
|
+
import { ApifyClientError } from '../apify_client_error.js';
|
|
3
|
+
import { isAnyStream, isStream, isWebStream } from '../utils.js';
|
|
4
|
+
import { HttpClient } from './base.js';
|
|
5
|
+
/** Error codes of the timeouts that Node.js reports as the `cause` of a failed `fetch`. */
|
|
6
|
+
const TIMEOUT_ERROR_CODES = new Set([
|
|
7
|
+
'ETIMEDOUT',
|
|
8
|
+
'UND_ERR_CONNECT_TIMEOUT',
|
|
9
|
+
'UND_ERR_HEADERS_TIMEOUT',
|
|
10
|
+
'UND_ERR_BODY_TIMEOUT',
|
|
11
|
+
]);
|
|
12
|
+
/** Error codes of a request that Node.js or Bun refused to send, so sending it again fails the same way. */
|
|
13
|
+
const INVALID_REQUEST_ERROR_CODES = new Set([
|
|
14
|
+
'ERR_INVALID_URL',
|
|
15
|
+
'ERR_INVALID_ARG_TYPE',
|
|
16
|
+
'ERR_INVALID_ARG_VALUE',
|
|
17
|
+
'UND_ERR_INVALID_ARG',
|
|
18
|
+
'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH',
|
|
19
|
+
]);
|
|
20
|
+
/**
|
|
21
|
+
* The default HTTP client of {@link ApifyClient}, built on the `fetch` of the runtime, so it works the same way in
|
|
22
|
+
* Node.js, browsers and edge runtimes and brings no dependency of its own.
|
|
23
|
+
*
|
|
24
|
+
* It inherits the request pipeline from {@link HttpClient} and adds the transport. Network errors are retried,
|
|
25
|
+
* timeouts included, and a stream body is sent without following redirects, since the part of it already sent could
|
|
26
|
+
* not be replayed. The timeout of a request runs until the response headers arrive and starts over with every chunk
|
|
27
|
+
* of the response body and of a stream body, so a large download or upload does not time out while it makes progress.
|
|
28
|
+
*
|
|
29
|
+
* In Node.js, `fetch` keeps connections alive on its own, and goes through a proxy only when Node.js is told to
|
|
30
|
+
* read the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables: start it with `NODE_USE_ENV_PROXY=1`
|
|
31
|
+
* (Node.js 22.21 and 24.0) or `--use-env-proxy` (Node.js 22.21 and 24.5).
|
|
32
|
+
*/
|
|
33
|
+
export class FetchHttpClient extends HttpClient {
|
|
34
|
+
#fetch;
|
|
35
|
+
constructor(options = {}) {
|
|
36
|
+
super(options);
|
|
37
|
+
this.#fetch = options.fetch;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A timeout is the `TimeoutError` this client aborts a late request with, or a `fetch` failure caused by one of
|
|
41
|
+
* the connection, headers and body timeouts of Node.js.
|
|
42
|
+
*/
|
|
43
|
+
isTimeoutError(error) {
|
|
44
|
+
if (super.isTimeoutError(error))
|
|
45
|
+
return true;
|
|
46
|
+
const code = causeCode(error);
|
|
47
|
+
return code !== undefined && TIMEOUT_ERROR_CODES.has(code);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A network failure is the `TypeError` that `fetch` rejects with: a connection that could not be made, dropped
|
|
51
|
+
* mid-way or timed out. In Node.js it carries the failure as its `cause`, which tells it apart from a request
|
|
52
|
+
* that was never sent, such as one with an invalid URL. Bun rejects with an `Error` that has a `code` instead.
|
|
53
|
+
*/
|
|
54
|
+
isRetryableTransportError(error) {
|
|
55
|
+
if (this.isTimeoutError(error))
|
|
56
|
+
return true;
|
|
57
|
+
const code = causeCode(error) ?? errorCode(error);
|
|
58
|
+
if (code !== undefined && INVALID_REQUEST_ERROR_CODES.has(code))
|
|
59
|
+
return false;
|
|
60
|
+
if (runtime.fetchFailsWithCause)
|
|
61
|
+
return error instanceof TypeError && error.cause !== undefined;
|
|
62
|
+
// Browsers and Deno fail with a bare `TypeError` whatever the reason, and Bun with an error that has a `code`.
|
|
63
|
+
return error instanceof TypeError || code !== undefined;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Sends the request through `fetch` and hands the raw response back. A stream body is sent without following
|
|
67
|
+
* redirects, and so is a request with `followRedirects: false`. A browser hides such a redirect response from
|
|
68
|
+
* the page, so there it fails the request.
|
|
69
|
+
*/
|
|
70
|
+
async sendRequest(request) {
|
|
71
|
+
const { method, url, headers, body, timeoutMillis, stream, followRedirects, signal } = request;
|
|
72
|
+
// `AbortSignal.timeout()` could neither be restarted nor called off.
|
|
73
|
+
const timeout = new AbortController();
|
|
74
|
+
let timer;
|
|
75
|
+
let settled = false;
|
|
76
|
+
const restartTimeout = () => {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
// The transport may still read a stream body after the response arrived.
|
|
79
|
+
if (timeoutMillis === undefined || settled)
|
|
80
|
+
return;
|
|
81
|
+
timer = setTimeout(() => timeout.abort(new DOMException(`timeout of ${timeoutMillis}ms exceeded`, 'TimeoutError')), timeoutMillis);
|
|
82
|
+
};
|
|
83
|
+
restartTimeout();
|
|
84
|
+
let sentBody;
|
|
85
|
+
try {
|
|
86
|
+
// Browsers and Deno reject an invalid URL with the same bare `TypeError` as a network failure.
|
|
87
|
+
const baseUrl = globalThis.location?.href;
|
|
88
|
+
if (!runtime.fetchFailsWithCause && !URL.canParse(url, baseUrl)) {
|
|
89
|
+
throw Object.assign(new TypeError(`Invalid URL: ${url}`), { code: 'ERR_INVALID_URL' });
|
|
90
|
+
}
|
|
91
|
+
sentBody = trackProgress(body, restartTimeout);
|
|
92
|
+
const response = await (this.#fetch ?? globalThis.fetch)(url, {
|
|
93
|
+
method,
|
|
94
|
+
headers,
|
|
95
|
+
body: sentBody,
|
|
96
|
+
redirect: followRedirects && !isAnyStream(body) ? 'follow' : 'manual',
|
|
97
|
+
signal: signal ? AbortSignal.any([signal, timeout.signal]) : timeout.signal,
|
|
98
|
+
// Required for a stream body, and ignored for any other.
|
|
99
|
+
...(isAnyStream(body) ? { duplex: 'half' } : {}),
|
|
100
|
+
});
|
|
101
|
+
// `redirect: 'manual'` resolves to an opaque response with status 0 in a browser.
|
|
102
|
+
if (response.type === 'opaqueredirect') {
|
|
103
|
+
throw new ApifyClientError(`${method} ${url} responded with a redirect, which the client does not follow.`);
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
status: response.status,
|
|
107
|
+
headers: toResponseHeaders(response.headers),
|
|
108
|
+
// The timeout covers a streamed response only until its headers, and the caller's signal throughout.
|
|
109
|
+
body: stream ? (response.body ?? new Blob([]).stream()) : await readBody(response, restartTimeout),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
// A `fetch` that refused the request never took the body over, which keeps the caller's stream locked.
|
|
114
|
+
if (isWebStream(sentBody) && !sentBody.locked)
|
|
115
|
+
sentBody.cancel(error).catch(() => { });
|
|
116
|
+
if (!this.#fetch && this.isRetryableTransportError(error))
|
|
117
|
+
this.#warnAboutUnusedProxy();
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
settled = true;
|
|
122
|
+
clearTimeout(timer);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** A proxy the environment names and Node.js ignores is the likeliest reason for a connection to fail. */
|
|
126
|
+
#warnAboutUnusedProxy() {
|
|
127
|
+
const hint = runtime.unusedProxyHint();
|
|
128
|
+
if (hint)
|
|
129
|
+
this.logger.warningOnce(hint);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/** Bun reads a stream body ahead of sending it, so reading it there says nothing about the progress. */
|
|
133
|
+
const isBun = 'Bun' in globalThis;
|
|
134
|
+
/** A larger chunk of a stream body is passed on in pieces, since a `Blob` held in memory arrives as a single one. */
|
|
135
|
+
const MAX_BODY_CHUNK_BYTES = 64 * 1024;
|
|
136
|
+
/** Makes a stream body report every chunk that `fetch` reads from it, and leaves any other body as it is. */
|
|
137
|
+
function trackProgress(body, onChunk) {
|
|
138
|
+
if (isWebStream(body))
|
|
139
|
+
return withWebProgress(body, onChunk);
|
|
140
|
+
// Node.js reads an async iterable, which the DOM types do not know about.
|
|
141
|
+
if (isStream(body) && !isBun)
|
|
142
|
+
return withProgress(body, onChunk);
|
|
143
|
+
return body;
|
|
144
|
+
}
|
|
145
|
+
/** Passes a stream body on piece by piece, so that a large upload counts as alive for as long as it is being read. */
|
|
146
|
+
async function* withProgress(body, onChunk) {
|
|
147
|
+
for await (const chunk of body) {
|
|
148
|
+
// Deno refuses the string chunks of a stream with an encoding.
|
|
149
|
+
const bytes = typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk;
|
|
150
|
+
if (!(bytes instanceof Uint8Array) || bytes.byteLength <= MAX_BODY_CHUNK_BYTES) {
|
|
151
|
+
onChunk();
|
|
152
|
+
yield bytes;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
for (let offset = 0; offset < bytes.byteLength; offset += MAX_BODY_CHUNK_BYTES) {
|
|
156
|
+
onChunk();
|
|
157
|
+
yield bytes.subarray(offset, offset + MAX_BODY_CHUNK_BYTES);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** Does for a web stream body what {@link withProgress} does for an async iterable. */
|
|
162
|
+
function withWebProgress(body, onChunk) {
|
|
163
|
+
const reader = body.getReader();
|
|
164
|
+
let rest;
|
|
165
|
+
return new ReadableStream({
|
|
166
|
+
async pull(controller) {
|
|
167
|
+
if (!rest?.byteLength) {
|
|
168
|
+
const { done, value } = await reader.read();
|
|
169
|
+
if (done)
|
|
170
|
+
return controller.close();
|
|
171
|
+
rest = value;
|
|
172
|
+
}
|
|
173
|
+
onChunk();
|
|
174
|
+
controller.enqueue(rest.subarray(0, MAX_BODY_CHUNK_BYTES));
|
|
175
|
+
rest = rest.subarray(MAX_BODY_CHUNK_BYTES);
|
|
176
|
+
},
|
|
177
|
+
async cancel(reason) {
|
|
178
|
+
await reader.cancel(reason);
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
// Reads a piece only once `fetch` asks for it.
|
|
182
|
+
{ highWaterMark: 0 });
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Reads the response body chunk by chunk, so that a large download counts as alive for as long as bytes arrive.
|
|
186
|
+
*/
|
|
187
|
+
async function readBody(response, onChunk) {
|
|
188
|
+
const chunks = [];
|
|
189
|
+
let length = 0;
|
|
190
|
+
const reader = response.body?.getReader();
|
|
191
|
+
for (let chunk = await reader?.read(); chunk && !chunk.done; chunk = await reader?.read()) {
|
|
192
|
+
onChunk();
|
|
193
|
+
chunks.push(chunk.value);
|
|
194
|
+
length += chunk.value.byteLength;
|
|
195
|
+
}
|
|
196
|
+
const bytes = new Uint8Array(length);
|
|
197
|
+
let offset = 0;
|
|
198
|
+
for (const chunk of chunks) {
|
|
199
|
+
bytes.set(chunk, offset);
|
|
200
|
+
offset += chunk.byteLength;
|
|
201
|
+
}
|
|
202
|
+
return bytes;
|
|
203
|
+
}
|
|
204
|
+
function toResponseHeaders(headers) {
|
|
205
|
+
const result = Object.fromEntries(headers);
|
|
206
|
+
// `fetch` joins repeated headers with a comma, which a cookie's `Expires` date makes ambiguous.
|
|
207
|
+
const cookies = headers.getSetCookie?.() ?? [];
|
|
208
|
+
if (cookies.length > 0)
|
|
209
|
+
result['set-cookie'] = cookies;
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
/** Reads the `code` of the error that caused a `fetch` failure, which is where Node.js puts the network error. */
|
|
213
|
+
function causeCode(error) {
|
|
214
|
+
return errorCode(error instanceof Error ? error.cause : undefined);
|
|
215
|
+
}
|
|
216
|
+
function errorCode(error) {
|
|
217
|
+
const code = error?.code;
|
|
218
|
+
return typeof code === 'string' ? code : undefined;
|
|
219
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { ApifyRequestConfig, ApifyResponse, ApifyResponseType, HttpClientOptions, HttpMethod, HttpRequest, HttpRequestBody, HttpResponse, HttpResponseBody, HttpResponseHeaders, } from './base.js';
|
|
2
|
+
export { HttpClient } from './base.js';
|
|
3
|
+
export type { FetchFunction, FetchHttpClientOptions } from './fetch.js';
|
|
4
|
+
export { FetchHttpClient } from './fetch.js';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Strategy for compressing HTTP request bodies.
|
|
4
|
+
*
|
|
5
|
+
* Implement this interface to create a custom compressor. Set `contentEncoding` to the value that should be sent in
|
|
6
|
+
* the `Content-Encoding` header and implement `compress()`. The client calls it only for bodies that are large
|
|
7
|
+
* enough to benefit from compression, whose content type does not already carry its own compression, and which the
|
|
8
|
+
* caller did not send with a `Content-Encoding` of their own.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```javascript
|
|
12
|
+
* import { ApifyClient } from 'apify-client';
|
|
13
|
+
*
|
|
14
|
+
* const identityCompressor = {
|
|
15
|
+
* contentEncoding: 'identity',
|
|
16
|
+
*
|
|
17
|
+
* async compress(data) {
|
|
18
|
+
* return data;
|
|
19
|
+
* },
|
|
20
|
+
* };
|
|
21
|
+
*
|
|
22
|
+
* const client = new ApifyClient({ token: 'my-token', compression: identityCompressor });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export interface HttpCompressor {
|
|
26
|
+
/** Value sent in the `Content-Encoding` header, for example `gzip` or `br`. */
|
|
27
|
+
readonly contentEncoding: string;
|
|
28
|
+
/**
|
|
29
|
+
* Compresses a request body.
|
|
30
|
+
*
|
|
31
|
+
* @param data - The raw bytes to compress.
|
|
32
|
+
* @returns The compressed bytes.
|
|
33
|
+
*/
|
|
34
|
+
compress(data: Buffer): Promise<Buffer>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Schema accepting anything shaped like an {@link HttpCompressor}.
|
|
38
|
+
* @internal
|
|
39
|
+
*/
|
|
40
|
+
export declare const httpCompressorSchema: z.ZodCustom<HttpCompressor, HttpCompressor>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Schema accepting anything shaped like an {@link HttpCompressor}.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export const httpCompressorSchema = z.custom((value) => typeof value === 'object' &&
|
|
7
|
+
value !== null &&
|
|
8
|
+
typeof value.contentEncoding === 'string' &&
|
|
9
|
+
typeof value.compress === 'function', { error: 'Invalid input: expected an HttpCompressor' });
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { HttpCompressor } from './base.js';
|
|
2
|
+
/**
|
|
3
|
+
* Compresses request bodies using brotli.
|
|
4
|
+
*
|
|
5
|
+
* Built on the `node:zlib` module, so it works wherever the client runs on Node.js.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```javascript
|
|
9
|
+
* import { ApifyClient, BrotliHttpCompressor } from 'apify-client';
|
|
10
|
+
*
|
|
11
|
+
* const client = new ApifyClient({ token: 'my-token', compression: new BrotliHttpCompressor({ quality: 11 }) });
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare class BrotliHttpCompressor implements HttpCompressor {
|
|
15
|
+
#private;
|
|
16
|
+
readonly contentEncoding = "br";
|
|
17
|
+
/**
|
|
18
|
+
* @param options - Compressor options.
|
|
19
|
+
* @throws {ArgumentValidationError} If `quality` is out of the valid range.
|
|
20
|
+
*/
|
|
21
|
+
constructor(options?: BrotliHttpCompressorOptions);
|
|
22
|
+
compress(data: Buffer): Promise<Buffer>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for {@link BrotliHttpCompressor}.
|
|
26
|
+
*/
|
|
27
|
+
export interface BrotliHttpCompressorOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Compression level, from `0` (the fastest) to `11` (the best compression).
|
|
30
|
+
* @default 6
|
|
31
|
+
*/
|
|
32
|
+
quality?: number;
|
|
33
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { runtime } from '#runtime';
|
|
3
|
+
import { parseArgument } from '../utils.js';
|
|
4
|
+
/** Lowest valid brotli quality, the fastest with the least compression. */
|
|
5
|
+
const MIN_QUALITY = 0;
|
|
6
|
+
/** Highest valid brotli quality, the slowest with the best compression. */
|
|
7
|
+
const MAX_QUALITY = 11;
|
|
8
|
+
/** Middle of the range, where brotli already beats gzip at a comparable CPU cost. */
|
|
9
|
+
const DEFAULT_QUALITY = 6;
|
|
10
|
+
const optionsSchema = z.strictObject({
|
|
11
|
+
quality: z.int().min(MIN_QUALITY).max(MAX_QUALITY).default(DEFAULT_QUALITY),
|
|
12
|
+
});
|
|
13
|
+
/**
|
|
14
|
+
* Compresses request bodies using brotli.
|
|
15
|
+
*
|
|
16
|
+
* Built on the `node:zlib` module, so it works wherever the client runs on Node.js.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```javascript
|
|
20
|
+
* import { ApifyClient, BrotliHttpCompressor } from 'apify-client';
|
|
21
|
+
*
|
|
22
|
+
* const client = new ApifyClient({ token: 'my-token', compression: new BrotliHttpCompressor({ quality: 11 }) });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export class BrotliHttpCompressor {
|
|
26
|
+
contentEncoding = 'br';
|
|
27
|
+
#quality;
|
|
28
|
+
/**
|
|
29
|
+
* @param options - Compressor options.
|
|
30
|
+
* @throws {ArgumentValidationError} If `quality` is out of the valid range.
|
|
31
|
+
*/
|
|
32
|
+
constructor(options = {}) {
|
|
33
|
+
const { quality } = parseArgument(options, optionsSchema, 'BrotliHttpCompressorOptions');
|
|
34
|
+
this.#quality = quality;
|
|
35
|
+
}
|
|
36
|
+
async compress(data) {
|
|
37
|
+
return runtime.compress(data, { algorithm: 'br', quality: this.#quality });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { HttpCompressor } from './base.js';
|
|
2
|
+
/**
|
|
3
|
+
* Compresses request bodies using gzip.
|
|
4
|
+
*
|
|
5
|
+
* Built on the `node:zlib` module, so it works wherever the client runs on Node.js.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```javascript
|
|
9
|
+
* import { ApifyClient, GzipHttpCompressor } from 'apify-client';
|
|
10
|
+
*
|
|
11
|
+
* const client = new ApifyClient({ token: 'my-token', compression: new GzipHttpCompressor({ quality: 1 }) });
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare class GzipHttpCompressor implements HttpCompressor {
|
|
15
|
+
#private;
|
|
16
|
+
readonly contentEncoding = "gzip";
|
|
17
|
+
/**
|
|
18
|
+
* @param options - Compressor options.
|
|
19
|
+
* @throws {ArgumentValidationError} If `quality` is out of the valid range.
|
|
20
|
+
*/
|
|
21
|
+
constructor(options?: GzipHttpCompressorOptions);
|
|
22
|
+
compress(data: Buffer): Promise<Buffer>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for {@link GzipHttpCompressor}.
|
|
26
|
+
*/
|
|
27
|
+
export interface GzipHttpCompressorOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Compression level, from `1` (the fastest) to `9` (the best compression).
|
|
30
|
+
* @default 6
|
|
31
|
+
*/
|
|
32
|
+
quality?: number;
|
|
33
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { runtime } from '#runtime';
|
|
3
|
+
import { parseArgument } from '../utils.js';
|
|
4
|
+
/** Lowest valid gzip quality, the fastest with the least compression. */
|
|
5
|
+
const MIN_QUALITY = 1;
|
|
6
|
+
/** Highest valid gzip quality, the slowest with the best compression. */
|
|
7
|
+
const MAX_QUALITY = 9;
|
|
8
|
+
/** zlib's own default. The levels above it roughly double the CPU cost for about a percent fewer bytes. */
|
|
9
|
+
const DEFAULT_QUALITY = 6;
|
|
10
|
+
const optionsSchema = z.strictObject({
|
|
11
|
+
quality: z.int().min(MIN_QUALITY).max(MAX_QUALITY).default(DEFAULT_QUALITY),
|
|
12
|
+
});
|
|
13
|
+
/**
|
|
14
|
+
* Compresses request bodies using gzip.
|
|
15
|
+
*
|
|
16
|
+
* Built on the `node:zlib` module, so it works wherever the client runs on Node.js.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```javascript
|
|
20
|
+
* import { ApifyClient, GzipHttpCompressor } from 'apify-client';
|
|
21
|
+
*
|
|
22
|
+
* const client = new ApifyClient({ token: 'my-token', compression: new GzipHttpCompressor({ quality: 1 }) });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export class GzipHttpCompressor {
|
|
26
|
+
contentEncoding = 'gzip';
|
|
27
|
+
#quality;
|
|
28
|
+
/**
|
|
29
|
+
* @param options - Compressor options.
|
|
30
|
+
* @throws {ArgumentValidationError} If `quality` is out of the valid range.
|
|
31
|
+
*/
|
|
32
|
+
constructor(options = {}) {
|
|
33
|
+
const { quality } = parseArgument(options, optionsSchema, 'GzipHttpCompressorOptions');
|
|
34
|
+
this.#quality = quality;
|
|
35
|
+
}
|
|
36
|
+
async compress(data) {
|
|
37
|
+
return runtime.compress(data, { algorithm: 'gzip', quality: this.#quality });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { HttpCompressor } from './base.js';
|
|
2
|
+
export { BrotliHttpCompressor } from './brotli.js';
|
|
3
|
+
export type { BrotliHttpCompressorOptions } from './brotli.js';
|
|
4
|
+
export { GzipHttpCompressor } from './gzip.js';
|
|
5
|
+
export type { GzipHttpCompressorOptions } from './gzip.js';
|
|
6
|
+
export type { HttpCompressionAlgorithm } from './resolve.js';
|
|
7
|
+
export { ZstdHttpCompressor } from './zstd.js';
|
|
8
|
+
export type { ZstdHttpCompressorOptions } from './zstd.js';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { HttpCompressor } from './base.js';
|
|
3
|
+
/**
|
|
4
|
+
* Compression algorithms the `compression` option of {@link ApifyClient} accepts by name. Each name selects the
|
|
5
|
+
* matching built-in compressor with its default quality: `'brotli'` a {@link BrotliHttpCompressor}, `'gzip'` a
|
|
6
|
+
* {@link GzipHttpCompressor}, `'zstd'` a {@link ZstdHttpCompressor}. The Apify API also accepts `deflate` and
|
|
7
|
+
* `identity`, which a custom {@link HttpCompressor} covers.
|
|
8
|
+
*/
|
|
9
|
+
export type HttpCompressionAlgorithm = 'brotli' | 'gzip' | 'zstd';
|
|
10
|
+
/**
|
|
11
|
+
* Schema of the `compression` option: an algorithm name or an {@link HttpCompressor}.
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export declare const compressionSchema: z.ZodUnion<readonly [z.ZodEnum<{
|
|
15
|
+
brotli: "brotli";
|
|
16
|
+
gzip: "gzip";
|
|
17
|
+
zstd: "zstd";
|
|
18
|
+
}>, z.ZodCustom<HttpCompressor, HttpCompressor>]>;
|
|
19
|
+
/**
|
|
20
|
+
* Turns the `compression` option into a ready-to-use {@link HttpCompressor}: a name into the matching built-in
|
|
21
|
+
* compressor with its default quality, a compressor into itself.
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export declare function resolveCompressor(compression: HttpCompressionAlgorithm | HttpCompressor): HttpCompressor;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { httpCompressorSchema } from './base.js';
|
|
3
|
+
import { BrotliHttpCompressor } from './brotli.js';
|
|
4
|
+
import { GzipHttpCompressor } from './gzip.js';
|
|
5
|
+
import { ZstdHttpCompressor } from './zstd.js';
|
|
6
|
+
/**
|
|
7
|
+
* Schema of the `compression` option: an algorithm name or an {@link HttpCompressor}.
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export const compressionSchema = z.union([
|
|
11
|
+
z.enum(['brotli', 'gzip', 'zstd']),
|
|
12
|
+
httpCompressorSchema,
|
|
13
|
+
]);
|
|
14
|
+
/**
|
|
15
|
+
* Turns the `compression` option into a ready-to-use {@link HttpCompressor}: a name into the matching built-in
|
|
16
|
+
* compressor with its default quality, a compressor into itself.
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export function resolveCompressor(compression) {
|
|
20
|
+
if (typeof compression !== 'string')
|
|
21
|
+
return compression;
|
|
22
|
+
switch (compression) {
|
|
23
|
+
case 'gzip':
|
|
24
|
+
return new GzipHttpCompressor();
|
|
25
|
+
case 'brotli':
|
|
26
|
+
return new BrotliHttpCompressor();
|
|
27
|
+
case 'zstd':
|
|
28
|
+
return new ZstdHttpCompressor();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { HttpCompressor } from './base.js';
|
|
2
|
+
/**
|
|
3
|
+
* Compresses request bodies using zstd.
|
|
4
|
+
*
|
|
5
|
+
* Built on the `node:zlib` module, so it works wherever the client runs on Node.js.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```javascript
|
|
9
|
+
* import { ApifyClient, ZstdHttpCompressor } from 'apify-client';
|
|
10
|
+
*
|
|
11
|
+
* const client = new ApifyClient({ token: 'my-token', compression: new ZstdHttpCompressor({ quality: 19 }) });
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare class ZstdHttpCompressor implements HttpCompressor {
|
|
15
|
+
#private;
|
|
16
|
+
readonly contentEncoding = "zstd";
|
|
17
|
+
/**
|
|
18
|
+
* @param options - Compressor options.
|
|
19
|
+
* @throws {ArgumentValidationError} If `quality` is out of the valid range.
|
|
20
|
+
*/
|
|
21
|
+
constructor(options?: ZstdHttpCompressorOptions);
|
|
22
|
+
compress(data: Buffer): Promise<Buffer>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for {@link ZstdHttpCompressor}.
|
|
26
|
+
*/
|
|
27
|
+
export interface ZstdHttpCompressorOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Compression level, from `1` (the fastest) to `19` (the best compression). zstd's levels 20 to 22 are not
|
|
30
|
+
* accepted: their window exceeds the 8 MB that HTTP decoders are required to support.
|
|
31
|
+
* @default 3
|
|
32
|
+
*/
|
|
33
|
+
quality?: number;
|
|
34
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { runtime } from '#runtime';
|
|
3
|
+
import { parseArgument } from '../utils.js';
|
|
4
|
+
/** Lowest valid zstd quality, the fastest with the least compression. */
|
|
5
|
+
const MIN_QUALITY = 1;
|
|
6
|
+
/**
|
|
7
|
+
* Highest quality the compressor accepts. zstd itself goes up to 22, but levels 20 to 22 use a window above the 8 MB
|
|
8
|
+
* that RFC 9659 requires HTTP decoders to support, so a record stored at those levels could fail to decode in a
|
|
9
|
+
* browser that accepts zstd. Level 19 is the last one within the limit.
|
|
10
|
+
*/
|
|
11
|
+
const MAX_QUALITY = 19;
|
|
12
|
+
/** zstd's own default. Fast, with a ratio close to gzip at its best level. */
|
|
13
|
+
const DEFAULT_QUALITY = 3;
|
|
14
|
+
const optionsSchema = z.strictObject({
|
|
15
|
+
quality: z.int().min(MIN_QUALITY).max(MAX_QUALITY).default(DEFAULT_QUALITY),
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* Compresses request bodies using zstd.
|
|
19
|
+
*
|
|
20
|
+
* Built on the `node:zlib` module, so it works wherever the client runs on Node.js.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```javascript
|
|
24
|
+
* import { ApifyClient, ZstdHttpCompressor } from 'apify-client';
|
|
25
|
+
*
|
|
26
|
+
* const client = new ApifyClient({ token: 'my-token', compression: new ZstdHttpCompressor({ quality: 19 }) });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export class ZstdHttpCompressor {
|
|
30
|
+
contentEncoding = 'zstd';
|
|
31
|
+
#quality;
|
|
32
|
+
/**
|
|
33
|
+
* @param options - Compressor options.
|
|
34
|
+
* @throws {ArgumentValidationError} If `quality` is out of the valid range.
|
|
35
|
+
*/
|
|
36
|
+
constructor(options = {}) {
|
|
37
|
+
const { quality } = parseArgument(options, optionsSchema, 'ZstdHttpCompressorOptions');
|
|
38
|
+
this.#quality = quality;
|
|
39
|
+
}
|
|
40
|
+
async compress(data) {
|
|
41
|
+
return runtime.compress(data, { algorithm: 'zstd', quality: this.#quality });
|
|
42
|
+
}
|
|
43
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './resource_clients/actor.js';
|
|
|
3
3
|
export * from './resource_clients/actor_collection.js';
|
|
4
4
|
export * from './resource_clients/actor_env_var.js';
|
|
5
5
|
export * from './resource_clients/actor_env_var_collection.js';
|
|
6
|
+
export * from './resource_clients/actor_server.js';
|
|
6
7
|
export * from './resource_clients/actor_version.js';
|
|
7
8
|
export * from './resource_clients/actor_version_collection.js';
|
|
8
9
|
export * from './resource_clients/build.js';
|
|
@@ -26,8 +27,12 @@ export * from './resource_clients/webhook.js';
|
|
|
26
27
|
export * from './resource_clients/webhook_collection.js';
|
|
27
28
|
export * from './resource_clients/webhook_dispatch.js';
|
|
28
29
|
export * from './resource_clients/webhook_dispatch_collection.js';
|
|
30
|
+
export * from './apify_client_error.js';
|
|
29
31
|
export * from './apify_api_error.js';
|
|
30
32
|
export { ArgumentValidationError } from '@apify/validations';
|
|
31
33
|
export * from './response_validation_error.js';
|
|
32
|
-
export
|
|
33
|
-
export
|
|
34
|
+
export * from './invalid_response_body_error.js';
|
|
35
|
+
export * from './http_clients/index.js';
|
|
36
|
+
export * from './http_compressors/index.js';
|
|
37
|
+
export type { AnyString, PaginatedList, Dictionary, JsonValue, TypedArray } from './utils.js';
|
|
38
|
+
export type { Timeout, TimeoutOptions, TimeoutTier } from './timeouts.js';
|