wreq-js 0.2.0 → 1.0.0

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/types.d.ts CHANGED
@@ -1,111 +1,354 @@
1
1
  import type { BrowserProfile } from "./generated-types";
2
2
  export type { BrowserProfile };
3
3
  /**
4
- * HTTP method types
4
+ * Controls how cookies are scoped for a request.
5
+ * - "session": reuse an explicit Session or sessionId across calls.
6
+ * - "ephemeral": create an isolated, single-use session.
7
+ */
8
+ export type CookieMode = "session" | "ephemeral";
9
+ /**
10
+ * Minimal handle implemented by {@link Session}. Exposed so {@link RequestInit.session}
11
+ * can accept either a Session instance or a compatible object.
12
+ */
13
+ export interface SessionHandle {
14
+ readonly id: string;
15
+ }
16
+ /**
17
+ * A tuple of [name, value] pairs used for initializing headers.
18
+ * Both name and value must be strings.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * const headers: HeaderTuple = ['Content-Type', 'application/json'];
23
+ * ```
24
+ */
25
+ export type HeaderTuple = [string, string];
26
+ /**
27
+ * Represents various input types accepted when creating or initializing headers.
28
+ * Can be an iterable of header tuples, an array of tuples, or a plain object.
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * // As an object
33
+ * const headers: HeadersInit = { 'Content-Type': 'application/json' };
34
+ *
35
+ * // As an array of tuples
36
+ * const headers: HeadersInit = [['Content-Type', 'application/json']];
37
+ *
38
+ * // As an iterable
39
+ * const headers: HeadersInit = new Map([['Content-Type', 'application/json']]);
40
+ * ```
41
+ */
42
+ export type HeadersInit = Iterable<HeaderTuple> | Array<HeaderTuple> | Record<string, string | number | boolean | null | undefined>;
43
+ /**
44
+ * Represents the various types of data that can be used as a request body.
45
+ * Supports string, binary data (ArrayBuffer, ArrayBufferView), URL-encoded parameters, and Node.js Buffer.
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * // String body
50
+ * const body: BodyInit = JSON.stringify({ key: 'value' });
51
+ *
52
+ * // URLSearchParams
53
+ * const body: BodyInit = new URLSearchParams({ key: 'value' });
54
+ *
55
+ * // Buffer
56
+ * const body: BodyInit = Buffer.from('data');
57
+ * ```
58
+ */
59
+ export type BodyInit = string | ArrayBuffer | ArrayBufferView | URLSearchParams | Buffer;
60
+ /**
61
+ * Options for configuring a fetch request. Compatible with the standard Fetch API
62
+ * with additional wreq-specific extensions for browser impersonation, proxies, and timeouts.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const options: RequestInit = {
67
+ * method: 'POST',
68
+ * headers: { 'Content-Type': 'application/json' },
69
+ * body: JSON.stringify({ key: 'value' }),
70
+ * browser: 'chrome_142',
71
+ * proxy: 'http://proxy.example.com:8080',
72
+ * timeout: 5000
73
+ * };
74
+ * ```
75
+ */
76
+ export interface RequestInit {
77
+ /**
78
+ * A string to set request's method.
79
+ * @default 'GET'
80
+ */
81
+ method?: string;
82
+ /**
83
+ * A Headers object, an object literal, or an array of two-item arrays to set request's headers.
84
+ */
85
+ headers?: HeadersInit;
86
+ /**
87
+ * A BodyInit object or null to set request's body.
88
+ */
89
+ body?: BodyInit | null;
90
+ /**
91
+ * An AbortSignal to set request's signal.
92
+ */
93
+ signal?: AbortSignal | null;
94
+ /**
95
+ * A string indicating whether request follows redirects, results in an error upon
96
+ * encountering a redirect, or returns the redirect (in an opaque fashion).
97
+ * @default 'follow'
98
+ */
99
+ redirect?: "follow" | "manual" | "error";
100
+ /**
101
+ * Browser profile to impersonate for this request.
102
+ * Automatically applies browser-specific headers, TLS fingerprints, and HTTP/2 settings.
103
+ * @default 'chrome_142'
104
+ */
105
+ browser?: BrowserProfile;
106
+ /**
107
+ * Proxy URL to route the request through (e.g., 'http://proxy.example.com:8080').
108
+ * Supports HTTP and SOCKS5 proxies.
109
+ */
110
+ proxy?: string;
111
+ /**
112
+ * Request timeout in milliseconds. If the request takes longer than this value,
113
+ * it will be aborted.
114
+ * @default 30000
115
+ */
116
+ timeout?: number;
117
+ /**
118
+ * Controls how cookies are managed for this call.
119
+ * - "ephemeral": default when no session/sessionId is provided. Creates an isolated session per request.
120
+ * - "session": requires an explicit session or sessionId and reuses its cookie jar.
121
+ */
122
+ cookieMode?: CookieMode;
123
+ /**
124
+ * Session instance to bind this request to. When provided, {@link cookieMode}
125
+ * automatically behaves like `"session"`.
126
+ */
127
+ session?: SessionHandle;
128
+ /**
129
+ * Identifier of an existing session created elsewhere (e.g., via {@link createSession}).
130
+ */
131
+ sessionId?: string;
132
+ }
133
+ /**
134
+ * Configuration for {@link createSession}.
135
+ */
136
+ export interface CreateSessionOptions {
137
+ /**
138
+ * Provide a custom identifier instead of an auto-generated random ID.
139
+ */
140
+ sessionId?: string;
141
+ /**
142
+ * Browser profile to bind to this session. Defaults to 'chrome_142'.
143
+ */
144
+ browser?: BrowserProfile;
145
+ /**
146
+ * Optional proxy for every request made through the session.
147
+ */
148
+ proxy?: string;
149
+ /**
150
+ * Default timeout applied when {@link Session.fetch} is called without
151
+ * overriding `timeout`.
152
+ */
153
+ timeout?: number;
154
+ }
155
+ /**
156
+ * Standard HTTP request methods supported by wreq.
157
+ * Represents the most commonly used HTTP verbs for RESTful operations.
5
158
  */
6
159
  export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD";
7
160
  /**
8
- * Request options for making HTTP requests with browser impersonation
161
+ * Legacy request options interface. This interface is deprecated and will be removed in a future version.
162
+ *
163
+ * @deprecated Use {@link RequestInit} with the standard `fetch()` API instead.
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * // Old (deprecated):
168
+ * const options: RequestOptions = {
169
+ * url: 'https://api.example.com',
170
+ * method: 'POST',
171
+ * body: JSON.stringify({ data: 'value' })
172
+ * };
173
+ *
174
+ * // New (recommended):
175
+ * const response = await fetch('https://api.example.com', {
176
+ * method: 'POST',
177
+ * body: JSON.stringify({ data: 'value' })
178
+ * });
179
+ * ```
9
180
  */
10
181
  export interface RequestOptions {
11
182
  /**
12
- * The URL to request
183
+ * The URL to request.
13
184
  */
14
185
  url: string;
15
186
  /**
16
- * Browser profile to impersonate
17
- * @default 'chrome_137'
187
+ * Browser profile to impersonate.
188
+ * Automatically applies browser-specific headers, TLS fingerprints, and HTTP/2 settings.
189
+ * @default 'chrome_142'
18
190
  */
19
191
  browser?: BrowserProfile;
20
192
  /**
21
- * HTTP method
193
+ * HTTP method to use for the request.
22
194
  * @default 'GET'
23
195
  */
24
196
  method?: HttpMethod;
25
197
  /**
26
- * Additional headers to send with the request
27
- * Browser-specific headers will be automatically added
198
+ * Additional headers to send with the request.
199
+ * Browser-specific headers will be automatically added based on the selected browser profile.
28
200
  */
29
201
  headers?: Record<string, string>;
30
202
  /**
31
- * Request body (for POST, PUT, PATCH requests)
203
+ * Request body data (for POST, PUT, PATCH requests).
32
204
  */
33
205
  body?: string;
34
206
  /**
35
- * Proxy URL (e.g., 'http://proxy.example.com:8080')
207
+ * Proxy URL to route the request through (e.g., 'http://proxy.example.com:8080').
208
+ * Supports HTTP and SOCKS5 proxies.
36
209
  */
37
210
  proxy?: string;
38
211
  /**
39
- * Request timeout in milliseconds
212
+ * Request timeout in milliseconds. If the request takes longer than this value,
213
+ * it will be aborted.
40
214
  * @default 30000
41
215
  */
42
216
  timeout?: number;
217
+ /**
218
+ * Identifier for the session that should handle this request.
219
+ * @internal
220
+ */
221
+ sessionId?: string;
222
+ /**
223
+ * Internal flag indicating whether the session should be discarded once the
224
+ * request finishes.
225
+ * @internal
226
+ */
227
+ ephemeral?: boolean;
43
228
  }
44
229
  /**
45
- * Response object returned from HTTP requests
230
+ * Internal response payload returned from the native Rust binding.
231
+ * This interface represents the raw response data before it's converted
232
+ * to a standard Response object.
233
+ *
234
+ * @internal
46
235
  */
47
- export interface Response {
236
+ export interface NativeResponse {
48
237
  /**
49
- * HTTP status code
238
+ * HTTP status code (e.g., 200, 404, 500).
50
239
  */
51
240
  status: number;
52
241
  /**
53
- * Response headers
242
+ * Response headers as key-value pairs.
243
+ * Header names are normalized to lowercase.
54
244
  */
55
245
  headers: Record<string, string>;
56
246
  /**
57
- * Response body as string
247
+ * Response body as a UTF-8 encoded string.
58
248
  */
59
249
  body: string;
60
250
  /**
61
- * Cookies set by the server
251
+ * Cookies set by the server as key-value pairs.
62
252
  */
63
253
  cookies: Record<string, string>;
64
254
  /**
65
- * Final URL after redirects
255
+ * Final URL after following any redirects.
256
+ * If no redirects occurred, this will match the original request URL.
66
257
  */
67
258
  url: string;
68
259
  }
69
260
  /**
70
- * WebSocket options for creating a connection
261
+ * Configuration options for creating a WebSocket connection.
262
+ * Supports browser impersonation and proxies, similar to HTTP requests.
263
+ *
264
+ * @example
265
+ * ```typescript
266
+ * const ws = await createWebSocket({
267
+ * url: 'wss://echo.websocket.org',
268
+ * browser: 'chrome_142',
269
+ * headers: { 'Authorization': 'Bearer token' },
270
+ * onMessage: (data) => {
271
+ * console.log('Received:', data);
272
+ * },
273
+ * onClose: () => {
274
+ * console.log('Connection closed');
275
+ * },
276
+ * onError: (error) => {
277
+ * console.error('WebSocket error:', error);
278
+ * }
279
+ * });
280
+ * ```
71
281
  */
72
282
  export interface WebSocketOptions {
73
283
  /**
74
- * The WebSocket URL to connect to (wss:// or ws://)
284
+ * The WebSocket URL to connect to. Must use wss:// (secure) or ws:// (insecure) protocol.
75
285
  */
76
286
  url: string;
77
287
  /**
78
- * Browser profile to impersonate
79
- * @default 'chrome_137'
288
+ * Browser profile to impersonate for the WebSocket upgrade request.
289
+ * Automatically applies browser-specific headers and TLS fingerprints.
290
+ * @default 'chrome_142'
80
291
  */
81
292
  browser?: BrowserProfile;
82
293
  /**
83
- * Additional headers to send with the WebSocket upgrade request
294
+ * Additional headers to send with the WebSocket upgrade request.
295
+ * Common headers include Authorization, Origin, or custom application headers.
84
296
  */
85
297
  headers?: Record<string, string>;
86
298
  /**
87
- * Proxy URL (e.g., 'http://proxy.example.com:8080')
299
+ * Proxy URL to route the connection through (e.g., 'http://proxy.example.com:8080').
300
+ * Supports HTTP and SOCKS5 proxies.
88
301
  */
89
302
  proxy?: string;
90
303
  /**
91
- * Callback for incoming messages (required)
304
+ * Callback function invoked when a message is received from the server.
305
+ * The data parameter will be a string for text frames or a Buffer for binary frames.
306
+ *
307
+ * @param data - The received message as a string or Buffer
92
308
  */
93
309
  onMessage: (data: string | Buffer) => void;
94
310
  /**
95
- * Callback for connection close event
311
+ * Callback function invoked when the WebSocket connection is closed.
312
+ * This is called for both clean closes and connection errors.
96
313
  */
97
314
  onClose?: () => void;
98
315
  /**
99
- * Callback for error events
316
+ * Callback function invoked when a connection or protocol error occurs.
317
+ *
318
+ * @param error - A string describing the error that occurred
100
319
  */
101
320
  onError?: (error: string) => void;
102
321
  }
103
322
  /**
104
- * Internal WebSocket connection object returned from native binding
323
+ * Internal WebSocket connection object returned from the native Rust binding.
324
+ * This interface contains the connection ID used to reference the WebSocket
325
+ * in subsequent operations like sending messages or closing the connection.
326
+ *
327
+ * @internal
105
328
  */
106
329
  export interface NativeWebSocketConnection {
330
+ /**
331
+ * Unique identifier for this WebSocket connection.
332
+ * Used internally to track and manage the connection.
333
+ * @internal
334
+ */
107
335
  _id: number;
108
336
  }
337
+ /**
338
+ * Error thrown when a request fails. This can occur due to network errors,
339
+ * timeouts, invalid URLs, or other request-related issues.
340
+ *
341
+ * @example
342
+ * ```typescript
343
+ * try {
344
+ * const response = await fetch('https://api.example.com');
345
+ * } catch (error) {
346
+ * if (error instanceof RequestError) {
347
+ * console.error('Request failed:', error.message);
348
+ * }
349
+ * }
350
+ * ```
351
+ */
109
352
  export declare class RequestError extends Error {
110
353
  constructor(message: string);
111
354
  }
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,YAAY,EAAE,cAAc,EAAE,CAAC;AAE/B;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,GAAG,MAAM,CAAC;AAE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MAAM,EAAE,MAAM,CAAC;IAEf;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEhC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEhC;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAC;CACb;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;;;OAIG;IACH,OAAO,CAAC,EAAE,cAAc,CAAC;IAEzB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEjC;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;;;;OAKG;IACH,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,KAAK,IAAI,CAAC;IAE3C;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IAErB;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC;AAED;;;;;;GAMG;AACH,MAAM,WAAW,yBAAyB;IACxC;;;;OAIG;IACH,GAAG,EAAE,MAAM,CAAC;CACb;AAED;;;;;;;;;;;;;;GAcG;AACH,qBAAa,YAAa,SAAQ,KAAK;gBACzB,OAAO,EAAE,MAAM;CAI5B"}
package/dist/types.js CHANGED
@@ -1,6 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RequestError = void 0;
4
+ /**
5
+ * Error thrown when a request fails. This can occur due to network errors,
6
+ * timeouts, invalid URLs, or other request-related issues.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * try {
11
+ * const response = await fetch('https://api.example.com');
12
+ * } catch (error) {
13
+ * if (error instanceof RequestError) {
14
+ * console.error('Request failed:', error.message);
15
+ * }
16
+ * }
17
+ * ```
18
+ */
4
19
  class RequestError extends Error {
5
20
  constructor(message) {
6
21
  super(message);
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAmIA,MAAa,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AALD,oCAKC"}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AA6XA;;;;;;;;;;;;;;GAcG;AACH,MAAa,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AALD,oCAKC"}
package/dist/wreq-js.d.ts CHANGED
@@ -1,26 +1,72 @@
1
- import type { BrowserProfile, NativeWebSocketConnection, RequestOptions, Response, WebSocketOptions } from "./types";
1
+ import type { BodyInit, BrowserProfile, CreateSessionOptions, HeadersInit, NativeResponse, NativeWebSocketConnection, RequestOptions, SessionHandle, WebSocketOptions, RequestInit as WreqRequestInit } from "./types";
2
2
  import { RequestError } from "./types";
3
+ type SessionDefaults = {
4
+ browser: BrowserProfile;
5
+ proxy?: string;
6
+ timeout?: number;
7
+ };
8
+ export declare class Headers implements Iterable<[string, string]> {
9
+ private readonly store;
10
+ constructor(init?: HeadersInit);
11
+ private applyInit;
12
+ private normalizeName;
13
+ private assertValue;
14
+ append(name: string, value: unknown): void;
15
+ set(name: string, value: unknown): void;
16
+ get(name: string): string | null;
17
+ has(name: string): boolean;
18
+ delete(name: string): void;
19
+ entries(): IterableIterator<[string, string]>;
20
+ keys(): IterableIterator<string>;
21
+ values(): IterableIterator<string>;
22
+ forEach(callback: (value: string, name: string, parent: Headers) => void, thisArg?: unknown): void;
23
+ [Symbol.iterator](): IterableIterator<[string, string]>;
24
+ toObject(): Record<string, string>;
25
+ }
26
+ type ResponseType = "basic" | "cors" | "error" | "opaque" | "opaqueredirect";
27
+ export declare class Response {
28
+ readonly status: number;
29
+ readonly statusText: string;
30
+ readonly ok: boolean;
31
+ readonly headers: Headers;
32
+ readonly url: string;
33
+ readonly redirected: boolean;
34
+ readonly type: ResponseType;
35
+ readonly cookies: Record<string, string>;
36
+ readonly body: string;
37
+ bodyUsed: boolean;
38
+ private readonly payload;
39
+ private readonly requestUrl;
40
+ constructor(payload: NativeResponse, requestUrl: string);
41
+ json<T = unknown>(): Promise<T>;
42
+ text(): Promise<string>;
43
+ clone(): Response;
44
+ private assertBodyAvailable;
45
+ }
46
+ export declare class Session implements SessionHandle {
47
+ readonly id: string;
48
+ private disposed;
49
+ private readonly defaults;
50
+ constructor(id: string, defaults: SessionDefaults);
51
+ get closed(): boolean;
52
+ private ensureActive;
53
+ private enforceBrowser;
54
+ private enforceProxy;
55
+ fetch(input: string | URL, init?: WreqRequestInit): Promise<Response>;
56
+ clearCookies(): Promise<void>;
57
+ close(): Promise<void>;
58
+ }
3
59
  /**
4
- * Make an HTTP request with browser impersonation
5
- *
6
- * @param options - Request options
7
- * @returns Promise that resolves to the response
8
- *
9
- * @example
10
- * ```typescript
11
- * import { request } from 'wreq-js';
60
+ * Fetch-compatible entry point that adds browser impersonation controls.
12
61
  *
13
- * const response = await request({
14
- * url: 'https://example.com/api',
15
- * browser: 'chrome_137',
16
- * headers: {
17
- * 'Custom-Header': 'value'
18
- * }
19
- * });
20
- *
21
- * console.log(response.status); // 200
22
- * console.log(response.body); // Response body
23
- * ```
62
+ * @param input - Request URL (string or URL instance)
63
+ * @param init - Fetch-compatible init options
64
+ */
65
+ export declare function fetch(input: string | URL, init?: WreqRequestInit): Promise<Response>;
66
+ export declare function createSession(options?: CreateSessionOptions): Promise<Session>;
67
+ export declare function withSession<T>(fn: (session: Session) => Promise<T> | T, options?: CreateSessionOptions): Promise<T>;
68
+ /**
69
+ * @deprecated Use {@link fetch} instead.
24
70
  */
25
71
  export declare function request(options: RequestOptions): Promise<Response>;
26
72
  /**
@@ -38,40 +84,13 @@ export declare function request(options: RequestOptions): Promise<Response>;
38
84
  */
39
85
  export declare function getProfiles(): BrowserProfile[];
40
86
  /**
41
- * Convenience function for GET requests
42
- *
43
- * @param url - URL to request
44
- * @param options - Additional request options
45
- * @returns Promise that resolves to the response
46
- *
47
- * @example
48
- * ```typescript
49
- * import { get } from 'wreq-js';
50
- *
51
- * const response = await get('https://example.com/api');
52
- * ```
87
+ * Convenience helper for GET requests using {@link fetch}.
53
88
  */
54
- export declare function get(url: string, options?: Omit<RequestOptions, "url" | "method">): Promise<Response>;
89
+ export declare function get(url: string, init?: Omit<WreqRequestInit, "method">): Promise<Response>;
55
90
  /**
56
- * Convenience function for POST requests
57
- *
58
- * @param url - URL to request
59
- * @param body - Request body
60
- * @param options - Additional request options
61
- * @returns Promise that resolves to the response
62
- *
63
- * @example
64
- * ```typescript
65
- * import { post } from 'wreq-js';
66
- *
67
- * const response = await post(
68
- * 'https://example.com/api',
69
- * JSON.stringify({ foo: 'bar' }),
70
- * { headers: { 'Content-Type': 'application/json' } }
71
- * );
72
- * ```
91
+ * Convenience helper for POST requests using {@link fetch}.
73
92
  */
74
- export declare function post(url: string, body?: string, options?: Omit<RequestOptions, "url" | "method" | "body">): Promise<Response>;
93
+ export declare function post(url: string, body?: BodyInit | null, init?: Omit<WreqRequestInit, "method" | "body">): Promise<Response>;
75
94
  /**
76
95
  * WebSocket connection class
77
96
  *
@@ -81,7 +100,7 @@ export declare function post(url: string, body?: string, options?: Omit<RequestO
81
100
  *
82
101
  * const ws = await websocket({
83
102
  * url: 'wss://echo.websocket.org',
84
- * browser: 'chrome_137',
103
+ * browser: 'chrome_142',
85
104
  * onMessage: (data) => {
86
105
  * console.log('Received:', data);
87
106
  * },
@@ -124,15 +143,21 @@ export declare class WebSocket {
124
143
  * @returns Promise that resolves to the WebSocket instance
125
144
  */
126
145
  export declare function websocket(options: WebSocketOptions): Promise<WebSocket>;
127
- export type { BrowserProfile, HttpMethod, RequestOptions, Response, WebSocketOptions, } from "./types";
128
- export type { RequestError };
146
+ export type { BodyInit, BrowserProfile, CookieMode, CreateSessionOptions, HeadersInit, HttpMethod, RequestInit, RequestOptions, SessionHandle, WebSocketOptions, } from "./types";
147
+ export { RequestError };
129
148
  declare const _default: {
149
+ fetch: typeof fetch;
130
150
  request: typeof request;
131
151
  get: typeof get;
132
152
  post: typeof post;
133
153
  getProfiles: typeof getProfiles;
154
+ createSession: typeof createSession;
155
+ withSession: typeof withSession;
134
156
  websocket: typeof websocket;
135
157
  WebSocket: typeof WebSocket;
158
+ Headers: typeof Headers;
159
+ Response: typeof Response;
160
+ Session: typeof Session;
136
161
  };
137
162
  export default _default;
138
163
  //# sourceMappingURL=wreq-js.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"wreq-js.d.ts","sourceRoot":"","sources":["../src/wreq-js.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,yBAAyB,EAAE,cAAc,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AACrH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AA+EvC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAsB,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC,CAkBxE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,IAAI,cAAc,EAAE,CAM9C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,cAAc,EAAE,KAAK,GAAG,QAAQ,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAE1G;AAED;;;;;;;;;;;;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