@crawlee/utils 4.0.0-beta.257 → 4.0.0-beta.258

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/internal.d.ts CHANGED
@@ -3,6 +3,7 @@ export type { CheerioAPI, Cheerio, Element } from './internals/cheerio.js';
3
3
  export { extractUrlsFromCheerio } from './internals/cheerio.js';
4
4
  export { tryAbsoluteURL } from './internals/extract-urls.js';
5
5
  export { expandShadowRoots, URL_NO_COMMAS_REGEX, URL_WITH_COMMAS_REGEX } from './internals/general.js';
6
+ export { processHttpRequestOptions } from './internals/http-request.js';
6
7
  export * from './internals/iterables.js';
7
8
  export { MutationJournal } from './internals/mutation-journal.js';
8
9
  export * from './internals/url.js';
package/internal.js CHANGED
@@ -2,6 +2,7 @@ export * from './internals/blocked.js';
2
2
  export { extractUrlsFromCheerio } from './internals/cheerio.js';
3
3
  export { tryAbsoluteURL } from './internals/extract-urls.js';
4
4
  export { expandShadowRoots, URL_NO_COMMAS_REGEX, URL_WITH_COMMAS_REGEX } from './internals/general.js';
5
+ export { processHttpRequestOptions } from './internals/http-request.js';
5
6
  export * from './internals/iterables.js';
6
7
  export { MutationJournal } from './internals/mutation-journal.js';
7
8
  export * from './internals/url.js';
@@ -0,0 +1,5 @@
1
+ import type { HttpRequest, HttpRequestOptions } from '@crawlee/types';
2
+ /**
3
+ * Converts {@link HttpRequestOptions} to a {@link HttpRequest}.
4
+ */
5
+ export declare function processHttpRequestOptions({ searchParams, form, json, username, password, ...request }: HttpRequestOptions): HttpRequest;
@@ -0,0 +1,36 @@
1
+ import { Readable } from 'node:stream';
2
+ import { applySearchParams } from './url.js';
3
+ /**
4
+ * Converts {@link HttpRequestOptions} to a {@link HttpRequest}.
5
+ */
6
+ export function processHttpRequestOptions({ searchParams, form, json, username, password, ...request }) {
7
+ const url = new URL(request.url);
8
+ const headers = new Headers(request.headers);
9
+ applySearchParams(url, searchParams);
10
+ if ([request.body, form, json].filter((value) => value !== undefined).length > 1) {
11
+ throw new Error('At most one of `body`, `form` and `json` may be specified in sendRequest arguments');
12
+ }
13
+ const body = (() => {
14
+ if (form !== undefined) {
15
+ return Readable.from(new URLSearchParams(form).toString());
16
+ }
17
+ if (json !== undefined) {
18
+ return Readable.from(JSON.stringify(json));
19
+ }
20
+ if (request.body !== undefined) {
21
+ return Readable.from(request.body);
22
+ }
23
+ return undefined;
24
+ })();
25
+ if (form !== undefined && !headers.has('content-type')) {
26
+ headers.set('content-type', 'application/x-www-form-urlencoded');
27
+ }
28
+ if (json !== undefined && !headers.has('content-type')) {
29
+ headers.set('content-type', 'application/json');
30
+ }
31
+ if (username !== undefined || password !== undefined) {
32
+ const encodedAuth = Buffer.from(`${username ?? ''}:${password ?? ''}`).toString('base64');
33
+ headers.set('authorization', `Basic ${encodedAuth}`);
34
+ }
35
+ return { ...request, body, url, headers };
36
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crawlee/utils",
3
- "version": "4.0.0-beta.257",
3
+ "version": "4.0.0-beta.258",
4
4
  "description": "A set of shared utilities that can be used by crawlers",
5
5
  "engines": {
6
6
  "node": ">=22.13.0"
@@ -43,8 +43,8 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "@apify/validations": "^1.1.2",
46
- "@crawlee/http-client": "4.0.0-beta.257",
47
- "@crawlee/types": "4.0.0-beta.257",
46
+ "@crawlee/http-client": "4.0.0-beta.258",
47
+ "@crawlee/types": "4.0.0-beta.258",
48
48
  "@types/sax": "^1.2.7",
49
49
  "cheerio": "^1.2.0",
50
50
  "domhandler": "^6.0.1",
@@ -63,5 +63,5 @@
63
63
  }
64
64
  }
65
65
  },
66
- "gitHead": "d8f0dc1eb055b27aa07827a505c8f5a0aaaa5abd"
66
+ "gitHead": "a285256b541becf78e187043be064db917c643ea"
67
67
  }