create-request 0.0.1-beta.23 → 0.0.1-beta.24

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.
@@ -3,87 +3,89 @@ import { GetRequest, PostRequest, PutRequest, DeleteRequest, PatchRequest, HeadR
3
3
  * Create a GET request
4
4
  * Used for retrieving resources from the server.
5
5
  *
6
+ * @param url - The URL to send the request to
6
7
  * @returns A new GET request instance
7
8
  *
8
9
  * @example
9
- * const users = await get()
10
- * .withQueryParams({ limit: 10 })
11
- * .sendTo('/api/users')
12
- * .getJson();
10
+ * const request = get('/api/users')
11
+ * .withQueryParams({ limit: 10 });
12
+ * const users = await request.getData();
13
13
  */
14
- export declare function get(): GetRequest;
14
+ export declare function get(url: string): GetRequest;
15
15
  /**
16
16
  * Create a POST request
17
17
  * Used for creating new resources or submitting data.
18
18
  *
19
+ * @param url - The URL to send the request to
19
20
  * @returns A new POST request instance
20
21
  *
21
22
  * @example
22
- * const newUser = await post()
23
- * .withJson({ name: 'John', email: 'john@example.com' })
24
- * .sendTo('/api/users')
25
- * .getJson();
23
+ * const request = post('/api/users')
24
+ * .withJson({ name: 'John', email: 'john@example.com' });
25
+ * const newUser = await request.getData();
26
26
  */
27
- export declare function post(): PostRequest;
27
+ export declare function post(url: string): PostRequest;
28
28
  /**
29
29
  * Create a PUT request
30
30
  * Used for replacing or updating an existing resource.
31
31
  *
32
+ * @param url - The URL to send the request to
32
33
  * @returns A new PUT request instance
33
34
  *
34
35
  * @example
35
- * const updatedUser = await put()
36
- * .withJson({ name: 'John Updated', email: 'john@example.com' })
37
- * .sendTo('/api/users/123')
38
- * .getJson();
36
+ * const request = put('/api/users/123')
37
+ * .withJson({ name: 'John Updated', email: 'john@example.com' });
38
+ * const updatedUser = await request.getData();
39
39
  */
40
- export declare function put(): PutRequest;
40
+ export declare function put(url: string): PutRequest;
41
41
  /**
42
42
  * Create a DELETE request
43
43
  * Used for removing resources from the server.
44
44
  *
45
+ * @param url - The URL to send the request to
45
46
  * @returns A new DELETE request instance
46
47
  *
47
48
  * @example
48
- * await del()
49
- * .sendTo('/api/users/123')
50
- * .getJson();
49
+ * const request = del('/api/users/123');
50
+ * await request.getData();
51
51
  */
52
- export declare function del(): DeleteRequest;
52
+ export declare function del(url: string): DeleteRequest;
53
53
  /**
54
54
  * Create a PATCH request
55
55
  * Used for applying partial modifications to a resource.
56
56
  *
57
+ * @param url - The URL to send the request to
57
58
  * @returns A new PATCH request instance
58
59
  *
59
60
  * @example
60
- * const patchedUser = await patch()
61
- * .withJson({ status: 'active' })
62
- * .sendTo('/api/users/123')
63
- * .getJson();
61
+ * const request = patch('/api/users/123')
62
+ * .withJson({ status: 'active' });
63
+ * const patchedUser = await request.getData();
64
64
  */
65
- export declare function patch(): PatchRequest;
65
+ export declare function patch(url: string): PatchRequest;
66
66
  /**
67
67
  * Create a HEAD request
68
68
  * Similar to GET but returns only headers without a body.
69
69
  *
70
+ * @param url - The URL to send the request to
70
71
  * @returns A new HEAD request instance
71
72
  *
72
73
  * @example
73
- * const response = await head()
74
- * .sendTo('/api/users/123');
74
+ * const request = head('/api/users/123');
75
+ * const response = await request.getResponse();
75
76
  * console.log(response.headers.get('Last-Modified'));
76
77
  */
77
- export declare function head(): HeadRequest;
78
+ export declare function head(url: string): HeadRequest;
78
79
  /**
79
80
  * Create an OPTIONS request
80
81
  * Used to describe the communication options for a resource.
81
82
  *
83
+ * @param url - The URL to send the request to
82
84
  * @returns A new OPTIONS request instance
83
85
  *
84
86
  * @example
85
- * const response = await options()
86
- * .sendTo('/api/users');
87
+ * const request = options('/api/users');
88
+ * const response = await request.getResponse();
87
89
  * console.log(response.headers.get('Allow'));
88
90
  */
89
- export declare function options(): OptionsRequest;
91
+ export declare function options(url: string): OptionsRequest;
@@ -6,79 +6,86 @@ import { HttpMethod } from "./enums.js";
6
6
  * Used for retrieving data from an API endpoint.
7
7
  *
8
8
  * @example
9
- * const request = new GetRequest()
10
- * .withQueryParams({ id: '123' })
11
- * .sendTo('/api/users');
9
+ * const request = new GetRequest('/api/users')
10
+ * .withQueryParams({ id: '123' });
11
+ * const data = await request.getData();
12
12
  */
13
13
  export declare class GetRequest extends BaseRequest {
14
14
  protected method: HttpMethod;
15
+ constructor(url: string);
15
16
  }
16
17
  /**
17
18
  * HTTP HEAD request implementation
18
19
  * Similar to GET but returns only headers without a response body.
19
20
  *
20
21
  * @example
21
- * const request = new HeadRequest()
22
- * .sendTo('/api/users/123');
22
+ * const request = new HeadRequest('/api/users/123');
23
+ * const response = await request.getResponse();
23
24
  */
24
25
  export declare class HeadRequest extends BaseRequest {
25
26
  protected method: HttpMethod;
27
+ constructor(url: string);
26
28
  }
27
29
  /**
28
30
  * HTTP OPTIONS request implementation
29
31
  * Used to describe the communication options for the target resource.
30
32
  *
31
33
  * @example
32
- * const request = new OptionsRequest()
33
- * .sendTo('/api/users');
34
+ * const request = new OptionsRequest('/api/users');
35
+ * const response = await request.getResponse();
34
36
  */
35
37
  export declare class OptionsRequest extends BaseRequest {
36
38
  protected method: HttpMethod;
39
+ constructor(url: string);
37
40
  }
38
41
  /**
39
42
  * HTTP DELETE request implementation
40
43
  * Used to delete a resource on the server.
41
44
  *
42
45
  * @example
43
- * const request = new DeleteRequest()
44
- * .sendTo('/api/users/123');
46
+ * const request = new DeleteRequest('/api/users/123');
47
+ * await request.getData();
45
48
  */
46
49
  export declare class DeleteRequest extends BaseRequest {
47
50
  protected method: HttpMethod;
51
+ constructor(url: string);
48
52
  }
49
53
  /**
50
54
  * HTTP POST request implementation
51
55
  * Used to create a new resource or submit data for processing.
52
56
  *
53
57
  * @example
54
- * const request = new PostRequest()
55
- * .withJson({ name: 'John', email: 'john@example.com' })
56
- * .sendTo('/api/users');
58
+ * const request = new PostRequest('/api/users')
59
+ * .withJson({ name: 'John', email: 'john@example.com' });
60
+ * const data = await request.getData();
57
61
  */
58
62
  export declare class PostRequest extends BodyRequest {
59
63
  protected method: HttpMethod;
64
+ constructor(url: string);
60
65
  }
61
66
  /**
62
67
  * HTTP PUT request implementation
63
68
  * Used to replace or update an existing resource.
64
69
  *
65
70
  * @example
66
- * const request = new PutRequest()
67
- * .withJson({ id: '123', name: 'John', email: 'john@example.com' })
68
- * .sendTo('/api/users/123');
71
+ * const request = new PutRequest('/api/users/123')
72
+ * .withJson({ id: '123', name: 'John', email: 'john@example.com' });
73
+ * const data = await request.getData();
69
74
  */
70
75
  export declare class PutRequest extends BodyRequest {
71
76
  protected method: HttpMethod;
77
+ constructor(url: string);
72
78
  }
73
79
  /**
74
80
  * HTTP PATCH request implementation
75
81
  * Used to apply partial modifications to a resource.
76
82
  *
77
83
  * @example
78
- * const request = new PatchRequest()
79
- * .withJson({ email: 'new.email@example.com' })
80
- * .sendTo('/api/users/123');
84
+ * const request = new PatchRequest('/api/users/123')
85
+ * .withJson({ email: 'new.email@example.com' });
86
+ * const data = await request.getData();
81
87
  */
82
88
  export declare class PatchRequest extends BodyRequest {
83
89
  protected method: HttpMethod;
90
+ constructor(url: string);
84
91
  }
@@ -1,10 +1,49 @@
1
- import { HttpMethod, RequestPriority, CredentialsPolicy, RequestMode, RedirectMode, CacheMode, SameSitePolicy } from "./enums";
1
+ import { HttpMethod, RequestPriority, CredentialsPolicy, RequestMode, RedirectMode, SameSitePolicy } from "./enums";
2
2
  import type { RequestError } from "./RequestError.js";
3
+ import type { ResponseWrapper } from "./ResponseWrapper.js";
3
4
  export type Body = null | Blob | string | object | FormData | ArrayBuffer | URLSearchParams | ReadableStream;
4
5
  export type RetryCallback = (options: {
5
6
  attempt: number;
6
7
  error: RequestError;
7
8
  }) => void | Promise<void>;
9
+ /**
10
+ * Configuration object passed to request interceptors
11
+ * Contains all the information needed to make the request
12
+ */
13
+ export interface RequestConfig {
14
+ url: string;
15
+ method: string;
16
+ headers: Record<string, string>;
17
+ body?: Body;
18
+ signal?: AbortSignal;
19
+ credentials?: RequestCredentials;
20
+ mode?: RequestMode | string;
21
+ redirect?: RedirectMode | string;
22
+ referrer?: string;
23
+ referrerPolicy?: ReferrerPolicy;
24
+ keepalive?: boolean;
25
+ priority?: RequestPriority | string;
26
+ }
27
+ /**
28
+ * Request interceptor that can modify the request configuration or return an early response
29
+ * If it returns a Response, the request will be short-circuited and that response will be used
30
+ * @param config - The request configuration
31
+ * @returns Modified config, a Response to short-circuit, or a Promise of either
32
+ */
33
+ export type RequestInterceptor = (config: RequestConfig) => RequestConfig | Response | Promise<RequestConfig | Response>;
34
+ /**
35
+ * Response interceptor that can transform the response
36
+ * @param response - The response wrapper
37
+ * @returns Modified response wrapper or a Promise of it
38
+ */
39
+ export type ResponseInterceptor = (response: ResponseWrapper) => ResponseWrapper | Promise<ResponseWrapper>;
40
+ /**
41
+ * Error interceptor that can handle or transform errors
42
+ * Can return a ResponseWrapper to recover from the error
43
+ * @param error - The error that occurred
44
+ * @returns Modified error, a ResponseWrapper to recover, or a Promise of either
45
+ */
46
+ export type ErrorInterceptor = (error: RequestError) => RequestError | ResponseWrapper | Promise<RequestError | ResponseWrapper>;
8
47
  export interface CookieOptions {
9
48
  value: string;
10
49
  secure?: boolean;
@@ -16,12 +55,12 @@ export interface CookieOptions {
16
55
  maxAge?: number;
17
56
  }
18
57
  export type CookiesRecord = Record<string, string | CookieOptions>;
19
- export { CacheMode, HttpMethod, RequestMode, RedirectMode, SameSitePolicy, RequestPriority, CredentialsPolicy };
20
- export interface RequestOptions extends Omit<RequestInit, "signal" | "body" | "method" | "credentials" | "mode" | "redirect" | "cache" | "priority"> {
58
+ export { HttpMethod, RequestMode, RedirectMode, SameSitePolicy, RequestPriority, CredentialsPolicy };
59
+ export interface RequestOptions extends Omit<RequestInit, "signal" | "body" | "method" | "credentials" | "mode" | "redirect" | "priority"> {
21
60
  timeout?: number;
22
61
  retries?: number;
23
62
  onRetry?: RetryCallback;
24
- body?: any;
63
+ body?: Body;
25
64
  credentials?: CredentialsPolicy | RequestCredentials;
26
65
  mode?: RequestMode;
27
66
  redirect?: RedirectMode | RequestRedirect;
@@ -1,3 +1,4 @@
1
+ import type { RequestInterceptor, ResponseInterceptor, ErrorInterceptor } from "../types.js";
1
2
  /**
2
3
  * Global configuration for create-request
3
4
  */
@@ -9,6 +10,10 @@ export declare class Config {
9
10
  private csrfToken;
10
11
  private enableAutoXsrf;
11
12
  private enableAntiCsrf;
13
+ private requestInterceptors;
14
+ private responseInterceptors;
15
+ private errorInterceptors;
16
+ private nextInterceptorId;
12
17
  private constructor();
13
18
  /**
14
19
  * Get the singleton instance of the Config class
@@ -113,6 +118,97 @@ export declare class Config {
113
118
  * @returns True if anti-CSRF protection is enabled
114
119
  */
115
120
  isAntiCsrfEnabled(): boolean;
121
+ /**
122
+ * Add a global request interceptor
123
+ * Request interceptors can modify the request configuration or return an early response
124
+ *
125
+ * @param interceptor - The request interceptor function
126
+ * @returns The interceptor ID for later removal
127
+ *
128
+ * @example
129
+ * const id = Config.getInstance().addRequestInterceptor((config) => {
130
+ * config.headers['X-Custom'] = 'value';
131
+ * return config;
132
+ * });
133
+ */
134
+ addRequestInterceptor(interceptor: RequestInterceptor): number;
135
+ /**
136
+ * Add a global response interceptor
137
+ * Response interceptors can transform the response
138
+ *
139
+ * @param interceptor - The response interceptor function
140
+ * @returns The interceptor ID for later removal
141
+ *
142
+ * @example
143
+ * const id = Config.getInstance().addResponseInterceptor((response) => {
144
+ * console.log('Response received:', response.status);
145
+ * return response;
146
+ * });
147
+ */
148
+ addResponseInterceptor(interceptor: ResponseInterceptor): number;
149
+ /**
150
+ * Add a global error interceptor
151
+ * Error interceptors can handle or transform errors
152
+ *
153
+ * @param interceptor - The error interceptor function
154
+ * @returns The interceptor ID for later removal
155
+ *
156
+ * @example
157
+ * const id = Config.getInstance().addErrorInterceptor((error) => {
158
+ * console.error('Request failed:', error);
159
+ * throw error;
160
+ * });
161
+ */
162
+ addErrorInterceptor(interceptor: ErrorInterceptor): number;
163
+ /**
164
+ * Remove a request interceptor by its ID
165
+ *
166
+ * @param id - The interceptor ID returned from addRequestInterceptor
167
+ *
168
+ * @example
169
+ * Config.getInstance().removeRequestInterceptor(id);
170
+ */
171
+ removeRequestInterceptor(id: number): void;
172
+ /**
173
+ * Remove a response interceptor by its ID
174
+ *
175
+ * @param id - The interceptor ID returned from addResponseInterceptor
176
+ *
177
+ * @example
178
+ * Config.getInstance().removeResponseInterceptor(id);
179
+ */
180
+ removeResponseInterceptor(id: number): void;
181
+ /**
182
+ * Remove an error interceptor by its ID
183
+ *
184
+ * @param id - The interceptor ID returned from addErrorInterceptor
185
+ *
186
+ * @example
187
+ * Config.getInstance().removeErrorInterceptor(id);
188
+ */
189
+ removeErrorInterceptor(id: number): void;
190
+ /**
191
+ * Clear all interceptors (request, response, and error)
192
+ *
193
+ * @example
194
+ * Config.getInstance().clearInterceptors();
195
+ */
196
+ clearInterceptors(): void;
197
+ /**
198
+ * Get all global request interceptors (in registration order)
199
+ * @internal
200
+ */
201
+ getRequestInterceptors(): RequestInterceptor[];
202
+ /**
203
+ * Get all global response interceptors (in registration order)
204
+ * @internal
205
+ */
206
+ getResponseInterceptors(): ResponseInterceptor[];
207
+ /**
208
+ * Get all global error interceptors (in registration order)
209
+ * @internal
210
+ */
211
+ getErrorInterceptors(): ErrorInterceptor[];
116
212
  /**
117
213
  * Reset all configuration options to their default values
118
214
  *
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "create-request",
3
- "version": "0.0.1-beta.23",
3
+ "version": "0.0.1-beta.24",
4
4
  "description": "A modern, chainable wrapper for fetch with automatic retries, timeouts, comprehensive error handling, and first-class TypeScript support",
5
- "main": "dist/library/index.js",
5
+ "main": "dist/library/index.cjs",
6
6
  "module": "dist/library/index.esm.js",
7
7
  "types": "dist/library/index.d.ts",
8
8
  "sideEffects": false,
@@ -17,8 +17,8 @@
17
17
  "production": "./dist/library/index.esm.min.js"
18
18
  },
19
19
  "require": {
20
- "default": "./dist/library/index.js",
21
- "production": "./dist/library/index.min.js"
20
+ "default": "./dist/library/index.cjs",
21
+ "production": "./dist/library/index.min.cjs"
22
22
  },
23
23
  "types": "./dist/library/index.d.ts"
24
24
  }
@@ -26,20 +26,16 @@
26
26
  "scripts": {
27
27
  "dev": "cross-env BUILD_ENV=development rollup -c rollup.config.js -w",
28
28
  "build": "cross-env BUILD_ENV=production rollup -c rollup.config.js",
29
- "build:min": "cross-env BUILD_ENV=production MINIFY=true rollup -c rollup.config.js",
30
- "build:all": "npm run build && npm run build:min",
31
29
  "analyze": "cross-env ANALYZE=true BUILD_ENV=production rollup -c rollup.config.js",
32
- "analyze:min": "cross-env ANALYZE=true MINIFY=true BUILD_ENV=production rollup -c rollup.config.js",
33
30
  "lint": "eslint 'src/**/*.{js,ts,json}' 'test/**/*.{js,ts,json}'",
34
31
  "lint:fix": "eslint \"**/*.ts\" --ignore-path .eslintignore --fix",
35
32
  "format": "prettier --write \"**/*.{ts,js,json}\"",
36
- "check": "concurrently \"npm run lint\" \"npm run test\" \"npx size-limit\"",
33
+ "check": "concurrently \"npm run lint\" \"npm run test\" \"npm run check:size\"",
37
34
  "check:size": "npx size-limit",
38
- "pretest": "tsc -p tsconfig.test.json",
39
- "test": "node --test",
40
- "test:coverage": "npm run test -- --experimental-test-coverage",
35
+ "test": "node --import tsx --test test/**/*.test.ts",
36
+ "test:coverage": "node --import tsx --experimental-test-coverage --test test/**/*.test.ts",
41
37
  "preversion": "npm run check",
42
- "prepublishOnly": "npm run build:all",
38
+ "prepublishOnly": "npm run build",
43
39
  "publish:beta": "npm version prerelease --preid=beta && git push origin --tags && npm publish --tag latest",
44
40
  "disabled:publish:patch": "npm version patch && git push origin --tags && npm publish",
45
41
  "disabled:publish:minor": "npm version minor && git push origin --tags && npm publish",
@@ -55,6 +51,9 @@
55
51
  ],
56
52
  "author": "Daniel Amenou <amenou.daniel@gmail.com>",
57
53
  "license": "MIT",
54
+ "engines": {
55
+ "node": ">=18.3.0"
56
+ },
58
57
  "repository": {
59
58
  "type": "git",
60
59
  "url": "https://github.com/DanielAmenou/create-request.git"
@@ -67,7 +66,7 @@
67
66
  "name": "ESM Standard"
68
67
  },
69
68
  {
70
- "path": "dist/library/index.js",
69
+ "path": "dist/library/index.cjs",
71
70
  "limit": "6 KB",
72
71
  "gzip": true,
73
72
  "name": "CJS Standard"
@@ -79,7 +78,7 @@
79
78
  "name": "ESM Minified"
80
79
  },
81
80
  {
82
- "path": "dist/library/index.min.js",
81
+ "path": "dist/library/index.min.cjs",
83
82
  "limit": "6 KB",
84
83
  "gzip": true,
85
84
  "name": "CJS Minified"
@@ -100,14 +99,15 @@
100
99
  "eslint-config-prettier": "^9.1.0",
101
100
  "eslint-import-resolver-typescript": "^3.6.1",
102
101
  "eslint-plugin-import": "^2.29.1",
103
- "eslint-plugin-prettier": "^5.0.1",
104
102
  "eslint-plugin-jsonc": "^2.11.0",
103
+ "eslint-plugin-prettier": "^5.0.1",
105
104
  "prettier": "^3.1.1",
106
105
  "rollup": "^2.75.5",
107
106
  "rollup-plugin-cleaner": "^1.0.0",
108
107
  "rollup-plugin-visualizer": "^5.9.2",
109
108
  "size-limit": "^8.2.6",
110
109
  "tslib": "^2.4.0",
110
+ "tsx": "^4.20.6",
111
111
  "typescript": "^4.7.3",
112
112
  "undici": "^5.28.5"
113
113
  }