create-request 0.0.1-beta.2 → 0.0.1-beta.4
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 +188 -451
- package/dist/library/BaseRequest.d.ts +29 -3
- package/dist/library/BodyRequest.d.ts +14 -1
- package/dist/library/ResponseWrapper.d.ts +22 -11
- package/dist/library/enums.d.ts +24 -8
- package/dist/library/index.d.ts +5 -2
- package/dist/library/index.esm.js +390 -79
- package/dist/library/index.js +390 -78
- package/dist/library/types.d.ts +14 -3
- package/dist/library/utils/Config.d.ts +69 -0
- package/dist/library/utils/CookieUtils.d.ts +9 -0
- package/dist/library/utils/CsrfUtils.d.ts +24 -0
- package/package.json +6 -2
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { type HttpMethod, type RedirectMode, type RequestMode, type RequestPriority, CredentialsPolicy } from "./enums";
|
|
2
2
|
import { type ResponsePromise } from "./ResponseWrapper";
|
|
3
|
-
import type { RequestOptions, RetryCallback } from "./types";
|
|
3
|
+
import type { RequestOptions, RetryCallback, CookiesRecord, CookieOptions } from "./types";
|
|
4
4
|
export declare abstract class BaseRequest {
|
|
5
5
|
protected abstract method: HttpMethod;
|
|
6
6
|
protected requestOptions: RequestOptions;
|
|
7
7
|
protected abortController?: AbortController;
|
|
8
8
|
protected queryParams: URLSearchParams;
|
|
9
|
+
protected autoApplyCsrfProtection: boolean;
|
|
9
10
|
constructor();
|
|
10
11
|
withHeaders(headers: Record<string, string>): this;
|
|
11
12
|
withTimeout(timeout: number): this;
|
|
@@ -93,10 +94,35 @@ export declare abstract class BaseRequest {
|
|
|
93
94
|
withBearerToken(token: string): this;
|
|
94
95
|
/**
|
|
95
96
|
* Sets cookies for the request
|
|
96
|
-
* @param cookies
|
|
97
|
+
* @param cookies Object containing cookie name-value pairs or cookie options
|
|
97
98
|
* @returns The instance for chaining
|
|
98
99
|
*/
|
|
99
|
-
withCookies(cookies:
|
|
100
|
+
withCookies(cookies: CookiesRecord): this;
|
|
101
|
+
/**
|
|
102
|
+
* Set a single cookie
|
|
103
|
+
* @param name Cookie name
|
|
104
|
+
* @param value Cookie value or options object
|
|
105
|
+
* @returns The instance for chaining
|
|
106
|
+
*/
|
|
107
|
+
withCookie(name: string, value: string | CookieOptions): this;
|
|
108
|
+
/**
|
|
109
|
+
* Sets a CSRF token in the request headers
|
|
110
|
+
* @param token The CSRF token
|
|
111
|
+
* @param headerName The name of the header to use (default: X-CSRF-Token)
|
|
112
|
+
* @returns The instance for chaining
|
|
113
|
+
*/
|
|
114
|
+
withCsrfToken(token: string, headerName?: string): this;
|
|
115
|
+
/**
|
|
116
|
+
* Disables automatic anti-CSRF protection.
|
|
117
|
+
* By default, X-Requested-With: XMLHttpRequest header is sent with all requests.
|
|
118
|
+
* @returns The instance for chaining
|
|
119
|
+
*/
|
|
120
|
+
withoutCsrfProtection(): this;
|
|
121
|
+
/**
|
|
122
|
+
* Sets common security headers to help prevent CSRF attacks
|
|
123
|
+
* @returns The instance for chaining
|
|
124
|
+
*/
|
|
125
|
+
withAntiCsrfHeaders(): this;
|
|
100
126
|
/**
|
|
101
127
|
* Send the request to the specified URL
|
|
102
128
|
* @param url The URL to send the request to
|
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
import { BaseRequest } from "./BaseRequest";
|
|
2
2
|
import type { Body } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Base class for requests that can have a body (POST, PUT, PATCH)
|
|
5
|
+
*/
|
|
3
6
|
export declare abstract class BodyRequest extends BaseRequest {
|
|
7
|
+
protected body?: Body;
|
|
8
|
+
private bodyProcessed;
|
|
9
|
+
private bodyType?;
|
|
4
10
|
/**
|
|
5
|
-
* Sets the
|
|
11
|
+
* Sets the body of the request
|
|
12
|
+
* @param body - The request body
|
|
6
13
|
*/
|
|
7
14
|
withBody(body: Body): this;
|
|
15
|
+
private setContentTypeIfNeeded;
|
|
16
|
+
/**
|
|
17
|
+
* Send the request to the specified URL
|
|
18
|
+
* Overrides the base implementation to add body handling
|
|
19
|
+
*/
|
|
20
|
+
sendTo(url: string): ReturnType<BaseRequest["sendTo"]>;
|
|
8
21
|
}
|
|
@@ -24,31 +24,42 @@ export declare class ResponseWrapper {
|
|
|
24
24
|
/**
|
|
25
25
|
* Parse response as JSON
|
|
26
26
|
*/
|
|
27
|
-
|
|
27
|
+
getJson<T = unknown>(): Promise<T>;
|
|
28
28
|
/**
|
|
29
29
|
* Get response as text
|
|
30
30
|
*/
|
|
31
|
-
|
|
31
|
+
getText(): Promise<string>;
|
|
32
32
|
/**
|
|
33
33
|
* Get response as Blob
|
|
34
34
|
*/
|
|
35
|
-
|
|
35
|
+
getBlob(): Promise<Blob>;
|
|
36
36
|
/**
|
|
37
37
|
* Get response as ArrayBuffer
|
|
38
38
|
*/
|
|
39
|
-
|
|
39
|
+
getArrayBuffer(): Promise<ArrayBuffer>;
|
|
40
40
|
/**
|
|
41
|
-
* Get response as ReadableStream
|
|
41
|
+
* Get response as ReadableStream for true streaming consumption.
|
|
42
|
+
* This allows you to process data incrementally as it arrives.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const stream = await request.sendTo('https://example.com/large-file').getBody();
|
|
46
|
+
* const reader = stream.getReader();
|
|
47
|
+
*
|
|
48
|
+
* while (true) {
|
|
49
|
+
* const { done, value } = await reader.read();
|
|
50
|
+
* if (done) break;
|
|
51
|
+
* // Process chunk of data in 'value'
|
|
52
|
+
* }
|
|
42
53
|
*/
|
|
43
|
-
|
|
54
|
+
getBody(): ReadableStream<Uint8Array> | null;
|
|
44
55
|
}
|
|
45
56
|
/**
|
|
46
57
|
* Type for Promise with response data transformation methods
|
|
47
58
|
*/
|
|
48
59
|
export interface ResponsePromise<T = ResponseWrapper> extends Promise<T> {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
60
|
+
getJson<R = unknown>(): Promise<R>;
|
|
61
|
+
getText(): Promise<string>;
|
|
62
|
+
getBlob(): Promise<Blob>;
|
|
63
|
+
getArrayBuffer(): Promise<ArrayBuffer>;
|
|
64
|
+
getBody(): Promise<ReadableStream<Uint8Array> | null>;
|
|
54
65
|
}
|
package/dist/library/enums.d.ts
CHANGED
|
@@ -3,28 +3,28 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export declare enum HttpMethod {
|
|
5
5
|
GET = "GET",
|
|
6
|
-
POST = "POST",
|
|
7
6
|
PUT = "PUT",
|
|
8
|
-
|
|
9
|
-
PATCH = "PATCH",
|
|
7
|
+
POST = "POST",
|
|
10
8
|
HEAD = "HEAD",
|
|
9
|
+
PATCH = "PATCH",
|
|
10
|
+
DELETE = "DELETE",
|
|
11
11
|
OPTIONS = "OPTIONS"
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Enum for request priorities
|
|
15
15
|
*/
|
|
16
16
|
export declare enum RequestPriority {
|
|
17
|
-
HIGH = "high",
|
|
18
17
|
LOW = "low",
|
|
18
|
+
HIGH = "high",
|
|
19
19
|
AUTO = "auto"
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
22
|
* Enum for credentials policies
|
|
23
23
|
*/
|
|
24
24
|
export declare enum CredentialsPolicy {
|
|
25
|
+
OMIT = "omit",
|
|
25
26
|
INCLUDE = "include",
|
|
26
|
-
SAME_ORIGIN = "same-origin"
|
|
27
|
-
OMIT = "omit"
|
|
27
|
+
SAME_ORIGIN = "same-origin"
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
30
|
* Enum for request modes
|
|
@@ -39,8 +39,8 @@ export declare enum RequestMode {
|
|
|
39
39
|
* Enum for redirect modes
|
|
40
40
|
*/
|
|
41
41
|
export declare enum RedirectMode {
|
|
42
|
-
FOLLOW = "follow",
|
|
43
42
|
ERROR = "error",
|
|
43
|
+
FOLLOW = "follow",
|
|
44
44
|
MANUAL = "manual"
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
@@ -48,9 +48,25 @@ export declare enum RedirectMode {
|
|
|
48
48
|
*/
|
|
49
49
|
export declare enum CacheMode {
|
|
50
50
|
DEFAULT = "default",
|
|
51
|
-
NO_STORE = "no-store",
|
|
52
51
|
RELOAD = "reload",
|
|
52
|
+
NO_STORE = "no-store",
|
|
53
53
|
NO_CACHE = "no-cache",
|
|
54
54
|
FORCE_CACHE = "force-cache",
|
|
55
55
|
ONLY_IF_CACHED = "only-if-cached"
|
|
56
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Enum for cookie SameSite policies
|
|
59
|
+
*/
|
|
60
|
+
export declare enum SameSitePolicy {
|
|
61
|
+
LAX = "Lax",
|
|
62
|
+
NONE = "None",
|
|
63
|
+
STRICT = "Strict"
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Enum for body types
|
|
67
|
+
*/
|
|
68
|
+
export declare enum BodyType {
|
|
69
|
+
JSON = "json",
|
|
70
|
+
STRING = "string",
|
|
71
|
+
BINARY = "binary"
|
|
72
|
+
}
|
package/dist/library/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { get, put, head, post, patch, del, options } from "./requestFactories";
|
|
2
|
-
|
|
3
|
-
export
|
|
2
|
+
import { Config } from "./utils/Config";
|
|
3
|
+
export { HttpMethod, RequestPriority, CredentialsPolicy, RequestMode, RedirectMode, CacheMode, SameSitePolicy, } from "./enums";
|
|
4
|
+
export type { RetryCallback, RequestOptions, CookieOptions, CookiesRecord } from "./types";
|
|
4
5
|
export { RequestError } from "./RequestError";
|
|
5
6
|
export { ResponseWrapper } from "./ResponseWrapper";
|
|
7
|
+
export { CookieUtils } from "./utils/CookieUtils";
|
|
6
8
|
export { GetRequest, PostRequest, PutRequest, DeleteRequest, PatchRequest, HeadRequest, OptionsRequest, } from "./requestMethods";
|
|
7
9
|
declare const create: {
|
|
8
10
|
readonly get: typeof get;
|
|
@@ -12,5 +14,6 @@ declare const create: {
|
|
|
12
14
|
readonly patch: typeof patch;
|
|
13
15
|
readonly head: typeof head;
|
|
14
16
|
readonly options: typeof options;
|
|
17
|
+
readonly config: Config;
|
|
15
18
|
};
|
|
16
19
|
export default create;
|