apify-client 3.0.0-beta.107 → 3.0.0-beta.108

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.
@@ -21,8 +21,9 @@ export interface AxiosHttpClientOptions extends HttpClientOptions {
21
21
  *
22
22
  * It inherits the request pipeline from {@link HttpClient} and adds the transport: in Node.js, keep-alive agents
23
23
  * that honor the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables; in the browser, the XHR
24
- * adapter of axios. Network errors are retried, timeouts included, and a stream body is sent without following
25
- * redirects, since the part of it already sent could not be replayed.
24
+ * adapter of axios, or its fetch adapter for a request that must not follow redirects. 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.
26
27
  */
27
28
  export declare class AxiosHttpClient extends HttpClient {
28
29
  #private;
@@ -55,7 +56,8 @@ export declare class AxiosHttpClient extends HttpClient {
55
56
  /**
56
57
  * Sends the request through axios and hands the raw response back. A stream body is sent without following
57
58
  * redirects, since the part of it already sent could not be replayed, and so is a request with
58
- * `followRedirects: false`. In the browser, the browser follows redirects itself.
59
+ * `followRedirects: false`. Outside Node.js, such a request goes through `fetch`, since the browser follows the
60
+ * redirects of an XHR itself, and a redirect response fails the request: `fetch` hides it from the page.
59
61
  */
60
62
  sendRequest(request: HttpRequest): Promise<HttpResponse>;
61
63
  }
@@ -1,5 +1,6 @@
1
1
  import axios, { AxiosError, AxiosHeaders } from 'axios';
2
2
  import { runtime } from '#runtime';
3
+ import { ApifyClientError } from '../apify_client_error.js';
3
4
  import { isStream } from '../utils.js';
4
5
  import { HttpClient } from './base.js';
5
6
  /**
@@ -7,8 +8,9 @@ import { HttpClient } from './base.js';
7
8
  *
8
9
  * It inherits the request pipeline from {@link HttpClient} and adds the transport: in Node.js, keep-alive agents
9
10
  * that honor the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables; in the browser, the XHR
10
- * adapter of axios. Network errors are retried, timeouts included, and a stream body is sent without following
11
- * redirects, since the part of it already sent could not be replayed.
11
+ * adapter of axios, or its fetch adapter for a request that must not follow redirects. Network errors are retried,
12
+ * timeouts included, and a stream body is sent without following redirects, since the part of it already sent could
13
+ * not be replayed.
12
14
  */
13
15
  export class AxiosHttpClient extends HttpClient {
14
16
  /** The axios instance the requests go through. */
@@ -79,24 +81,32 @@ export class AxiosHttpClient extends HttpClient {
79
81
  /**
80
82
  * Sends the request through axios and hands the raw response back. A stream body is sent without following
81
83
  * redirects, since the part of it already sent could not be replayed, and so is a request with
82
- * `followRedirects: false`. In the browser, the browser follows redirects itself.
84
+ * `followRedirects: false`. Outside Node.js, such a request goes through `fetch`, since the browser follows the
85
+ * redirects of an XHR itself, and a redirect response fails the request: `fetch` hides it from the page.
83
86
  */
84
87
  async sendRequest(request) {
85
88
  await this.#ensureHttpAgents();
86
89
  const { method, url, headers, body, timeoutMillis, stream, followRedirects, signal } = request;
90
+ const useFetch = !followRedirects && !runtime.isNode;
87
91
  const response = await this.axios.request({
88
92
  method,
89
93
  url,
90
- headers,
94
+ // The fetch adapter adds a `User-Agent` of its own otherwise, which a browser lists in the CORS preflight.
95
+ headers: useFetch ? { 'User-Agent': null, ...headers } : headers,
91
96
  data: body,
92
97
  // Axios reads 0 as no timeout.
93
98
  timeout: timeoutMillis ?? 0,
94
99
  signal,
95
100
  responseType: stream ? 'stream' : 'arraybuffer',
96
- // See axios/axios#1045 for why axios must not buffer a stream body to follow redirects. The XHR adapter
97
- // ignores `maxRedirects`, since the browser follows redirects itself.
101
+ // See axios/axios#1045 for why axios must not buffer a stream body to follow redirects. The fetch
102
+ // adapter turns this into `redirect: 'manual'`.
98
103
  ...(isStream(body) || !followRedirects ? { maxRedirects: 0 } : {}),
104
+ ...(useFetch ? { adapter: 'fetch' } : {}),
99
105
  });
106
+ // `redirect: 'manual'` resolves to an opaque response with status 0 instead of the redirect.
107
+ if (useFetch && response.status === 0) {
108
+ throw new ApifyClientError(`${method} ${url} responded with a redirect, which the client does not follow.`);
109
+ }
100
110
  return {
101
111
  status: response.status,
102
112
  headers: AxiosHeaders.from(response.headers).toJSON(),
@@ -244,10 +244,16 @@ export interface HttpClientOptions {
244
244
  * import { ApifyClient, HttpClient } from 'apify-client';
245
245
  *
246
246
  * class FetchHttpClient extends HttpClient {
247
- * async sendRequest({ method, url, headers, body, timeoutMillis, signal }) {
247
+ * async sendRequest({ method, url, headers, body, timeoutMillis, followRedirects, signal }) {
248
248
  * const signals = signal ? [signal] : [];
249
249
  * if (timeoutMillis !== undefined) signals.push(AbortSignal.timeout(timeoutMillis));
250
- * const response = await fetch(url, { method, headers, body, signal: AbortSignal.any(signals) });
250
+ * const response = await fetch(url, {
251
+ * method,
252
+ * headers,
253
+ * body,
254
+ * redirect: followRedirects ? 'follow' : 'manual',
255
+ * signal: AbortSignal.any(signals),
256
+ * });
251
257
  * return {
252
258
  * status: response.status,
253
259
  * headers: Object.fromEntries(response.headers),
@@ -55,10 +55,16 @@ const UNSERIALIZABLE_OBJECT_TAGS = new Set(['FormData', 'ReadableStream']);
55
55
  * import { ApifyClient, HttpClient } from 'apify-client';
56
56
  *
57
57
  * class FetchHttpClient extends HttpClient {
58
- * async sendRequest({ method, url, headers, body, timeoutMillis, signal }) {
58
+ * async sendRequest({ method, url, headers, body, timeoutMillis, followRedirects, signal }) {
59
59
  * const signals = signal ? [signal] : [];
60
60
  * if (timeoutMillis !== undefined) signals.push(AbortSignal.timeout(timeoutMillis));
61
- * const response = await fetch(url, { method, headers, body, signal: AbortSignal.any(signals) });
61
+ * const response = await fetch(url, {
62
+ * method,
63
+ * headers,
64
+ * body,
65
+ * redirect: followRedirects ? 'follow' : 'manual',
66
+ * signal: AbortSignal.any(signals),
67
+ * });
62
68
  * return {
63
69
  * status: response.status,
64
70
  * headers: Object.fromEntries(response.headers),
@@ -71,9 +71,10 @@ export declare class ActorServerClient extends ResourceClient {
71
71
  * The method resolves to any response of the server, error statuses included. A rate-limit (429) or Service
72
72
  * Unavailable (503) response is retried with the client's retry settings, and the last one is returned once the
73
73
  * retries run out. The Actor's code can return these statuses too, and then it gets the request again. A redirect
74
- * is returned as it is, since following it would send the token to a host the Actor's code picked. A browser
75
- * follows redirects itself, though, so there the token goes with a redirect. Request bodies are sent uncompressed
76
- * unless `compression` is set, since the Actor's server may not decode a compressed body.
74
+ * is returned as it is, since following it would send the token to a host the Actor's code picked. In a
75
+ * browser the redirect response can't be read, so the request fails with an {@link ApifyClientError} instead.
76
+ * Request bodies are sent uncompressed unless `compression` is set, since the Actor's server may not decode a
77
+ * compressed body.
77
78
  *
78
79
  * @template T - Type of the parsed response body.
79
80
  * @param path - The path on the Actor's server, for example `/search`. It is always resolved against the server
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apify-client",
3
- "version": "3.0.0-beta.107",
3
+ "version": "3.0.0-beta.108",
4
4
  "description": "Apify API client for JavaScript",
5
5
  "engines": {
6
6
  "node": ">=22.15.0"