create-request 0.0.1-beta.9 → 1.1.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.
@@ -1,10 +1,49 @@
1
- import { HttpMethod, RequestPriority, CredentialsPolicy, RequestMode, RedirectMode, CacheMode, SameSitePolicy } from "./enums";
2
- import type { RequestError } from "./RequestError";
1
+ import { HttpMethod, RequestPriority, CredentialsPolicy, RequestMode, RedirectMode, SameSitePolicy } from "./enums";
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,15 @@ 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 GraphQLOptions {
60
+ throwOnError?: boolean;
61
+ }
62
+ export interface RequestOptions extends Omit<RequestInit, "signal" | "body" | "method" | "credentials" | "mode" | "redirect" | "priority"> {
21
63
  timeout?: number;
22
64
  retries?: number;
23
65
  onRetry?: RetryCallback;
24
- body?: any;
66
+ body?: Body;
25
67
  credentials?: CredentialsPolicy | RequestCredentials;
26
68
  mode?: RequestMode;
27
69
  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,61 +10,212 @@ 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
- * Get the singleton instance
19
+ * Get the singleton instance of the Config class
20
+ *
21
+ * @returns The global configuration instance
22
+ *
23
+ * @example
24
+ * const config = Config.getInstance();
25
+ * config.setCsrfToken('token123');
15
26
  */
16
27
  static getInstance(): Config;
17
28
  /**
18
29
  * Set a global CSRF token to be used for all requests
30
+ *
31
+ * @param token - The CSRF token value
32
+ * @returns The config instance for chaining
33
+ *
34
+ * @example
35
+ * Config.getInstance().setCsrfToken('myToken123');
19
36
  */
20
37
  setCsrfToken(token: string): Config;
21
38
  /**
22
- * Get the global CSRF token
39
+ * Get the global CSRF token that will be automatically applied to requests
40
+ *
41
+ * @returns The current CSRF token or null if not set
23
42
  */
24
43
  getCsrfToken(): string | null;
25
44
  /**
26
- * Set the CSRF header name (default is X-CSRF-Token)
45
+ * Set the CSRF header name used when sending the token
46
+ *
47
+ * @param name - The header name to use
48
+ * @returns The config instance for chaining
49
+ *
50
+ * @example
51
+ * Config.getInstance().setCsrfHeaderName('X-My-CSRF-Token');
27
52
  */
28
53
  setCsrfHeaderName(name: string): Config;
29
54
  /**
30
- * Get the CSRF header name
55
+ * Get the configured CSRF header name
56
+ *
57
+ * @returns The current CSRF header name
31
58
  */
32
59
  getCsrfHeaderName(): string;
33
60
  /**
34
- * Set the XSRF cookie name to look for (default is XSRF-TOKEN)
61
+ * Set the XSRF cookie name to look for when extracting tokens from cookies
62
+ *
63
+ * @param name - The cookie name to look for
64
+ * @returns The config instance for chaining
65
+ *
66
+ * @example
67
+ * Config.getInstance().setXsrfCookieName('MY-XSRF-COOKIE');
35
68
  */
36
69
  setXsrfCookieName(name: string): Config;
37
70
  /**
38
- * Get the XSRF cookie name
71
+ * Get the configured XSRF cookie name
72
+ *
73
+ * @returns The current XSRF cookie name
39
74
  */
40
75
  getXsrfCookieName(): string;
41
76
  /**
42
- * Set the XSRF header name to send the token in (default is X-XSRF-TOKEN)
77
+ * Set the XSRF header name for sending tokens extracted from cookies
78
+ *
79
+ * @param name - The header name to use
80
+ * @returns The config instance for chaining
43
81
  */
44
82
  setXsrfHeaderName(name: string): Config;
45
83
  /**
46
- * Get the XSRF header name
84
+ * Get the configured XSRF header name
85
+ *
86
+ * @returns The current XSRF header name
47
87
  */
48
88
  getXsrfHeaderName(): string;
49
89
  /**
50
- * Enable or disable automatic XSRF token extraction from cookies
90
+ * Enable or disable automatic extraction of XSRF tokens from cookies
91
+ * When enabled, the library will look for XSRF tokens in cookies and
92
+ * automatically add them to request headers.
93
+ *
94
+ * @param enable - Whether to enable this feature
95
+ * @returns The config instance for chaining
96
+ *
97
+ * @example
98
+ * Config.getInstance().setEnableAutoXsrf(false); // Disable XSRF extraction
51
99
  */
52
100
  setEnableAutoXsrf(enable: boolean): Config;
53
101
  /**
54
- * Check if automatic XSRF is enabled
102
+ * Check if automatic XSRF token extraction is enabled
103
+ *
104
+ * @returns True if automatic XSRF is enabled
55
105
  */
56
106
  isAutoXsrfEnabled(): boolean;
57
107
  /**
58
108
  * Enable or disable automatic addition of anti-CSRF headers
109
+ * When enabled, X-Requested-With: XMLHttpRequest will be added to all requests.
110
+ *
111
+ * @param enable - Whether to enable this feature
112
+ * @returns The config instance for chaining
59
113
  */
60
114
  setEnableAntiCsrf(enable: boolean): Config;
61
115
  /**
62
- * Check if anti-CSRF is enabled
116
+ * Check if anti-CSRF protection is enabled
117
+ *
118
+ * @returns True if anti-CSRF protection is enabled
63
119
  */
64
120
  isAntiCsrfEnabled(): boolean;
65
121
  /**
66
- * Reset all configuration options to their defaults
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[];
212
+ /**
213
+ * Reset all configuration options to their default values
214
+ *
215
+ * @returns The config instance for chaining
216
+ *
217
+ * @example
218
+ * Config.getInstance().reset();
67
219
  */
68
220
  reset(): Config;
69
221
  }
@@ -1,4 +1,4 @@
1
- import type { CookiesRecord } from "../types";
1
+ import type { CookiesRecord } from "../types.js";
2
2
  export declare class CookieUtils {
3
3
  /**
4
4
  * Formats cookies for a request
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Parse size strings like "10MB" into bytes
3
+ * Supports KB, MB, and GB units. If no unit is specified, assumes bytes.
4
+ *
5
+ * @param size - Size as number (bytes) or string with units (e.g., "5MB")
6
+ * @returns Size in bytes
7
+ */
8
+ export declare function parseSize(size: string | number): number;
package/package.json CHANGED
@@ -1,28 +1,45 @@
1
1
  {
2
2
  "name": "create-request",
3
- "version": "0.0.1-beta.9",
3
+ "version": "1.1.0",
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
+ "sideEffects": false,
9
+ "type": "module",
8
10
  "files": [
9
11
  "dist/library"
10
12
  ],
13
+ "exports": {
14
+ ".": {
15
+ "import": {
16
+ "default": "./dist/library/index.esm.js",
17
+ "production": "./dist/library/index.esm.min.js"
18
+ },
19
+ "require": {
20
+ "default": "./dist/library/index.cjs",
21
+ "production": "./dist/library/index.min.cjs"
22
+ },
23
+ "types": "./dist/library/index.d.ts"
24
+ }
25
+ },
11
26
  "scripts": {
12
- "build": "rollup -c",
13
- "dev": "rollup -c -w",
14
- "test": "tsc -p tsconfig.test.json && node --test --experimental-test-coverage",
15
- "test:coverage": "tsc -p tsconfig.test.json && node --test --experimental-test-coverage dist/tests/test/",
16
- "pretest": "tsc -p tsconfig.test.json",
17
- "prepublishOnly": "npm run check && npm run build",
18
- "publish:beta": "npm run check && npm version prerelease --preid=beta && git push origin --tags && npm publish --tag latest",
19
- "disabled:publish:patch": "npm version patch && git push origin --tags && npm publish",
20
- "disabled:publish:minor": "npm version minor && git push origin --tags && npm publish",
21
- "disabled:publish:major": "npm version major && git push origin --tags && npm publish",
22
- "lint": "eslint 'src/**/*.ts' 'test/**/*.ts'",
23
- "lint:fix": "eslint 'src/**/*.ts' 'test/**/*.ts' --fix",
24
- "format": "prettier --write 'src/**/*.ts' 'test/**/*.ts'",
25
- "check": "npm run lint && npm run test"
27
+ "dev": "cross-env BUILD_ENV=development rollup -c rollup.config.js -w",
28
+ "build": "cross-env BUILD_ENV=production rollup -c rollup.config.js",
29
+ "analyze": "cross-env ANALYZE=true BUILD_ENV=production rollup -c rollup.config.js",
30
+ "lint": "eslint 'src/**/*.{js,ts,json}' 'test/**/*.{js,ts,json}'",
31
+ "lint:fix": "eslint \"**/*.ts\" --ignore-path .eslintignore --fix",
32
+ "format": "prettier --write \"**/*.{ts,js,json}\"",
33
+ "check": "concurrently \"npm run lint\" \"npm run test\" \"npm run check:size\"",
34
+ "check:size": "npx size-limit",
35
+ "test": "node --import tsx --test test/**/*.test.ts",
36
+ "test:coverage": "node --import tsx --experimental-test-coverage --test test/**/*.test.ts",
37
+ "preversion": "npm run check",
38
+ "prepublishOnly": "npm run build",
39
+ "publish:beta": "npm version prerelease --preid=beta && git push origin --tags && npm publish --tag latest",
40
+ "publish:patch": "npm version patch && git push origin --tags && npm publish",
41
+ "publish:minor": "npm version minor && git push origin --tags && npm publish",
42
+ "publish:major": "npm version major && git push origin --tags && npm publish"
26
43
  },
27
44
  "keywords": [
28
45
  "fetch",
@@ -34,26 +51,67 @@
34
51
  ],
35
52
  "author": "Daniel Amenou <amenou.daniel@gmail.com>",
36
53
  "license": "MIT",
54
+ "engines": {
55
+ "node": ">=18.3.0"
56
+ },
37
57
  "repository": {
38
58
  "type": "git",
39
59
  "url": "https://github.com/DanielAmenou/create-request.git"
40
60
  },
61
+ "homepage": "https://github.com/DanielAmenou/create-request#readme",
62
+ "bugs": {
63
+ "url": "https://github.com/DanielAmenou/create-request/issues"
64
+ },
65
+ "size-limit": [
66
+ {
67
+ "path": "dist/library/index.esm.js",
68
+ "limit": "6 KB",
69
+ "gzip": true,
70
+ "name": "ESM Standard"
71
+ },
72
+ {
73
+ "path": "dist/library/index.cjs",
74
+ "limit": "6.5 KB",
75
+ "gzip": true,
76
+ "name": "CJS Standard"
77
+ },
78
+ {
79
+ "path": "dist/library/index.esm.min.js",
80
+ "limit": "6 KB",
81
+ "gzip": true,
82
+ "name": "ESM Minified"
83
+ },
84
+ {
85
+ "path": "dist/library/index.min.cjs",
86
+ "limit": "6.5 KB",
87
+ "gzip": true,
88
+ "name": "CJS Minified"
89
+ }
90
+ ],
41
91
  "devDependencies": {
42
92
  "@rollup/plugin-commonjs": "^22.0.0",
43
93
  "@rollup/plugin-node-resolve": "^13.3.0",
94
+ "@rollup/plugin-terser": "^0.4.3",
44
95
  "@rollup/plugin-typescript": "^8.3.2",
96
+ "@size-limit/preset-small-lib": "^8.2.6",
45
97
  "@types/node": "^18.0.0",
46
98
  "@typescript-eslint/eslint-plugin": "^6.15.0",
47
99
  "@typescript-eslint/parser": "^6.15.0",
100
+ "concurrently": "^8.2.2",
101
+ "cross-env": "^7.0.3",
48
102
  "eslint": "^8.56.0",
49
103
  "eslint-config-prettier": "^9.1.0",
50
104
  "eslint-import-resolver-typescript": "^3.6.1",
51
105
  "eslint-plugin-import": "^2.29.1",
106
+ "eslint-plugin-jsonc": "^2.11.0",
52
107
  "eslint-plugin-prettier": "^5.0.1",
53
108
  "prettier": "^3.1.1",
54
109
  "rollup": "^2.75.5",
55
110
  "rollup-plugin-cleaner": "^1.0.0",
111
+ "rollup-plugin-visualizer": "^5.9.2",
112
+ "size-limit": "^8.2.6",
56
113
  "tslib": "^2.4.0",
114
+ "tsx": "^4.20.6",
57
115
  "typescript": "^4.7.3",
58
116
  "undici": "^5.28.5"
59
117
  }