create-request 0.0.1-beta.2

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.
@@ -0,0 +1,119 @@
1
+ import { type HttpMethod, type RedirectMode, type RequestMode, type RequestPriority, CredentialsPolicy } from "./enums";
2
+ import { type ResponsePromise } from "./ResponseWrapper";
3
+ import type { RequestOptions, RetryCallback } from "./types";
4
+ export declare abstract class BaseRequest {
5
+ protected abstract method: HttpMethod;
6
+ protected requestOptions: RequestOptions;
7
+ protected abortController?: AbortController;
8
+ protected queryParams: URLSearchParams;
9
+ constructor();
10
+ withHeaders(headers: Record<string, string>): this;
11
+ withTimeout(timeout: number): this;
12
+ withRetries(retries: number): this;
13
+ onRetry(callback: RetryCallback): this;
14
+ /**
15
+ * Sets the credentials policy for the request
16
+ * @param credentialsPolicy - Can be 'omit', 'same-origin', or 'include'
17
+ * @returns The instance for chaining
18
+ */
19
+ withCredentials(credentialsPolicy?: CredentialsPolicy): this;
20
+ /**
21
+ * Allows providing an external AbortController to cancel the request
22
+ * @param controller - The AbortController to use for this request
23
+ * @returns The instance for chaining
24
+ */
25
+ withAbortController(controller: AbortController): this;
26
+ /**
27
+ * Sets the referrer for the request
28
+ * @param referrer - The referrer URL or policy
29
+ * @returns The instance for chaining
30
+ */
31
+ withReferrer(referrer: string): this;
32
+ /**
33
+ * Sets the redirect policy for the request
34
+ * @param redirect - Can be 'follow', 'error', or 'manual'
35
+ * @returns The instance for chaining
36
+ */
37
+ withRedirect(redirect: RedirectMode): this;
38
+ /**
39
+ * Sets the keepalive flag for the request
40
+ * @param keepalive - Whether to allow the request to outlive the page
41
+ * @returns The instance for chaining
42
+ */
43
+ withKeepAlive(keepalive: boolean): this;
44
+ /**
45
+ * Sets the priority of the request
46
+ * @param priority - Can be 'high', 'low', or 'auto'
47
+ * @returns The instance for chaining
48
+ */
49
+ withPriority(priority: RequestPriority): this;
50
+ /**
51
+ * Adds query parameters to the request URL
52
+ * @param params - An object containing the query parameters
53
+ * @returns The instance for chaining
54
+ */
55
+ withQueryParams(params: Record<string, string | string[] | number | boolean | null | undefined>): this;
56
+ /**
57
+ * Adds a single query parameter
58
+ * @param key - The parameter name
59
+ * @param value - The parameter value
60
+ * @returns The instance for chaining
61
+ */
62
+ withQueryParam(key: string, value: string | number | boolean | null | undefined): this;
63
+ /**
64
+ * Sets the mode of the request
65
+ * @param mode - The mode for the request (cors, no-cors, same-origin, navigate)
66
+ * @returns The instance for chaining
67
+ */
68
+ withMode(mode: RequestMode): this;
69
+ /**
70
+ * Shorthand for setting the Content-Type header
71
+ * @param contentType - The content type
72
+ * @returns The instance for chaining
73
+ */
74
+ withContentType(contentType: string): this;
75
+ /**
76
+ * Shorthand for setting the Authorization header
77
+ * @param authValue - The authorization value
78
+ * @returns The instance for chaining
79
+ */
80
+ withAuthorization(authValue: string): this;
81
+ /**
82
+ * Shorthand for setting up Basic authentication
83
+ * @param username - The username
84
+ * @param password - The password
85
+ * @returns The instance for chaining
86
+ */
87
+ withBasicAuth(username: string, password: string): this;
88
+ /**
89
+ * Sets the bearer token for authentication
90
+ * @param token - The bearer token
91
+ * @returns The instance for chaining
92
+ */
93
+ withBearerToken(token: string): this;
94
+ /**
95
+ * Sets cookies for the request
96
+ * @param cookies - An object containing cookie name-value pairs
97
+ * @returns The instance for chaining
98
+ */
99
+ withCookies(cookies: Record<string, string>): this;
100
+ /**
101
+ * Send the request to the specified URL
102
+ * @param url The URL to send the request to
103
+ */
104
+ sendTo(url: string): ResponsePromise;
105
+ /**
106
+ * Formats the URL with any query parameters
107
+ * @param url The base URL
108
+ * @returns The URL with query parameters appended
109
+ */
110
+ private formatUrlWithQueryParams;
111
+ /**
112
+ * Executes a request with configured retry logic
113
+ * @param url The formatted URL to send the request to
114
+ * @param fetchOptions The fetch options to use
115
+ * @returns A wrapped response object
116
+ */
117
+ private executeWithRetries;
118
+ private executeRequest;
119
+ }
@@ -0,0 +1,8 @@
1
+ import { BaseRequest } from "./BaseRequest";
2
+ import type { Body } from "./types";
3
+ export declare abstract class BodyRequest extends BaseRequest {
4
+ /**
5
+ * Sets the request body with appropriate content type handling
6
+ */
7
+ withBody(body: Body): this;
8
+ }
@@ -0,0 +1,18 @@
1
+ export declare class RequestError extends Error {
2
+ readonly status?: number;
3
+ readonly response?: Response;
4
+ readonly url: string;
5
+ readonly method: string;
6
+ readonly timeoutError?: boolean;
7
+ constructor(message: string, url: string, method: string, options?: {
8
+ status?: number;
9
+ response?: Response;
10
+ timeoutError?: boolean;
11
+ });
12
+ /**
13
+ * Static methods for creating specific types of RequestError
14
+ */
15
+ static timeout(url: string, method: string, timeoutMs: number): RequestError;
16
+ static fromResponse(response: Response, url: string, method: string): RequestError;
17
+ static networkError(url: string, method: string, originalError: Error): RequestError;
18
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Wrapper for HTTP responses with methods to transform the response data
3
+ */
4
+ export declare class ResponseWrapper {
5
+ readonly ok: boolean;
6
+ private response;
7
+ constructor(response: Response);
8
+ /**
9
+ * Get the raw Response object
10
+ */
11
+ get raw(): Response;
12
+ /**
13
+ * Get the HTTP status code
14
+ */
15
+ get status(): number;
16
+ /**
17
+ * Get the HTTP status text
18
+ */
19
+ get statusText(): string;
20
+ /**
21
+ * Get response headers
22
+ */
23
+ get headers(): Headers;
24
+ /**
25
+ * Parse response as JSON
26
+ */
27
+ json<T = unknown>(): Promise<T>;
28
+ /**
29
+ * Get response as text
30
+ */
31
+ text(): Promise<string>;
32
+ /**
33
+ * Get response as Blob
34
+ */
35
+ blob(): Promise<Blob>;
36
+ /**
37
+ * Get response as ArrayBuffer
38
+ */
39
+ arrayBuffer(): Promise<ArrayBuffer>;
40
+ /**
41
+ * Get response as ReadableStream
42
+ */
43
+ body(): ReadableStream<Uint8Array> | null;
44
+ }
45
+ /**
46
+ * Type for Promise with response data transformation methods
47
+ */
48
+ export interface ResponsePromise<T = ResponseWrapper> extends Promise<T> {
49
+ json<R = unknown>(): Promise<R>;
50
+ text(): Promise<string>;
51
+ blob(): Promise<Blob>;
52
+ arrayBuffer(): Promise<ArrayBuffer>;
53
+ body(): Promise<ReadableStream<Uint8Array> | null>;
54
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Enum for HTTP methods
3
+ */
4
+ export declare enum HttpMethod {
5
+ GET = "GET",
6
+ POST = "POST",
7
+ PUT = "PUT",
8
+ DELETE = "DELETE",
9
+ PATCH = "PATCH",
10
+ HEAD = "HEAD",
11
+ OPTIONS = "OPTIONS"
12
+ }
13
+ /**
14
+ * Enum for request priorities
15
+ */
16
+ export declare enum RequestPriority {
17
+ HIGH = "high",
18
+ LOW = "low",
19
+ AUTO = "auto"
20
+ }
21
+ /**
22
+ * Enum for credentials policies
23
+ */
24
+ export declare enum CredentialsPolicy {
25
+ INCLUDE = "include",
26
+ SAME_ORIGIN = "same-origin",
27
+ OMIT = "omit"
28
+ }
29
+ /**
30
+ * Enum for request modes
31
+ */
32
+ export declare enum RequestMode {
33
+ CORS = "cors",
34
+ NO_CORS = "no-cors",
35
+ SAME_ORIGIN = "same-origin",
36
+ NAVIGATE = "navigate"
37
+ }
38
+ /**
39
+ * Enum for redirect modes
40
+ */
41
+ export declare enum RedirectMode {
42
+ FOLLOW = "follow",
43
+ ERROR = "error",
44
+ MANUAL = "manual"
45
+ }
46
+ /**
47
+ * Enum for cache modes
48
+ */
49
+ export declare enum CacheMode {
50
+ DEFAULT = "default",
51
+ NO_STORE = "no-store",
52
+ RELOAD = "reload",
53
+ NO_CACHE = "no-cache",
54
+ FORCE_CACHE = "force-cache",
55
+ ONLY_IF_CACHED = "only-if-cached"
56
+ }
@@ -0,0 +1,16 @@
1
+ import { get, put, head, post, patch, del, options } from "./requestFactories";
2
+ export { HttpMethod, RequestPriority, CredentialsPolicy, RequestMode, RedirectMode, CacheMode, } from "./enums";
3
+ export type { RetryCallback, RequestOptions } from "./types";
4
+ export { RequestError } from "./RequestError";
5
+ export { ResponseWrapper } from "./ResponseWrapper";
6
+ export { GetRequest, PostRequest, PutRequest, DeleteRequest, PatchRequest, HeadRequest, OptionsRequest, } from "./requestMethods";
7
+ declare const create: {
8
+ readonly get: typeof get;
9
+ readonly post: typeof post;
10
+ readonly put: typeof put;
11
+ readonly del: typeof del;
12
+ readonly patch: typeof patch;
13
+ readonly head: typeof head;
14
+ readonly options: typeof options;
15
+ };
16
+ export default create;