@oxog/requestkit 1.0.0 → 1.2.1

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,351 @@
1
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
2
+ type ResponseType = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'stream' | 'raw';
3
+ type ErrorCode = 'ERR_NETWORK' | 'ERR_TIMEOUT' | 'ERR_CANCELED' | 'ERR_BAD_REQUEST' | 'ERR_BAD_RESPONSE';
4
+ interface Progress {
5
+ loaded: number;
6
+ total: number;
7
+ percent: number;
8
+ }
9
+ type ProgressCallback = (progress: Progress) => void;
10
+ interface ParamsSerializer {
11
+ encode?: (params: Record<string, unknown>) => string;
12
+ arrayFormat?: 'indices' | 'brackets' | 'repeat' | 'comma';
13
+ }
14
+ type TransformFn = (data: unknown, headers?: Headers) => unknown;
15
+ type RequestHook = (config: InternalRequestConfig) => InternalRequestConfig | Promise<InternalRequestConfig>;
16
+ type ResponseHook = (response: Response) => Response | Promise<Response>;
17
+ type ErrorHook = (error: RequestError) => unknown;
18
+ interface RetryConfig {
19
+ /** Number of retries after the first attempt (a request runs at most limit + 1 times) */
20
+ limit: number;
21
+ /** Methods to retry (default: GET, HEAD, OPTIONS, PUT, DELETE) */
22
+ methods?: HttpMethod[];
23
+ /** Status codes to retry (default: 408, 429, 500, 502, 503, 504) */
24
+ statusCodes?: number[];
25
+ /** Alias of `statusCodes` (used when `statusCodes` is not set) */
26
+ retryOn?: number[];
27
+ /** Initial delay in ms (default: 1000) */
28
+ delay?: number;
29
+ /** Backoff strategy (default: exponential) */
30
+ backoff?: 'linear' | 'exponential';
31
+ /** Max delay in ms, also caps a server's Retry-After (default: 30000) */
32
+ maxDelay?: number;
33
+ /** Randomize each backoff delay between 0 and the computed delay (default: false) */
34
+ jitter?: boolean;
35
+ /** Custom retry condition */
36
+ retryCondition?: (error: RequestError) => boolean;
37
+ /** Retry callback */
38
+ onRetry?: (attempt: number, error: RequestError, config: InternalRequestConfig) => void;
39
+ }
40
+ /** A Fetch-compatible transport called with the fully prepared request. */
41
+ type RequestTransport = (request: Request) => Promise<Response>;
42
+ interface ClientConfig {
43
+ /** Base URL for all requests */
44
+ baseURL?: string;
45
+ /**
46
+ * Allow absolute (`https://...`) and protocol-relative (`//host`) request
47
+ * URLs to bypass `baseURL` (default: true). Set to false when request paths
48
+ * can come from untrusted input so default headers never reach another host.
49
+ */
50
+ allowAbsoluteUrls?: boolean;
51
+ /** Default headers */
52
+ headers?: HeadersInit | Record<string, string>;
53
+ /** Timeout in ms (0 = no timeout) */
54
+ timeout?: number;
55
+ /** Retry config or number of retries */
56
+ retry?: number | RetryConfig;
57
+ /** Response type (default: json) */
58
+ responseType?: ResponseType;
59
+ /** Credentials mode */
60
+ credentials?: RequestCredentials;
61
+ /** Cache mode */
62
+ cache?: RequestCache;
63
+ /** Request mode */
64
+ mode?: RequestMode;
65
+ /** Fetch redirect mode (default: follow) */
66
+ redirect?: RequestRedirect;
67
+ /** Override native fetch for this client or request */
68
+ transport?: RequestTransport;
69
+ /** Query params serialization */
70
+ paramsSerializer?: ParamsSerializer;
71
+ /** Status validation (default: 200-299) */
72
+ validateStatus?: (status: number) => boolean;
73
+ /** Transform request data */
74
+ transformRequest?: TransformFn | TransformFn[];
75
+ /** Transform response data */
76
+ transformResponse?: TransformFn | TransformFn[];
77
+ /** Request hook */
78
+ onRequest?: RequestHook;
79
+ /** Response hook */
80
+ onResponse?: ResponseHook;
81
+ /** Error hook */
82
+ onError?: ErrorHook;
83
+ }
84
+ interface RequestOptions extends Omit<ClientConfig, 'baseURL'> {
85
+ /** HTTP method */
86
+ method?: HttpMethod;
87
+ /** URL params (query string) */
88
+ params?: Record<string, unknown> | URLSearchParams;
89
+ /** Request body */
90
+ body?: BodyInit | Record<string, unknown>;
91
+ /** JSON body (auto-serialized) */
92
+ json?: Record<string, unknown>;
93
+ /** Form body (FormData or URLSearchParams) */
94
+ form?: Record<string, unknown> | FormData;
95
+ /** Abort signal */
96
+ signal?: AbortSignal;
97
+ /** Axios-style cancel token (see `CancelToken.source()`) */
98
+ cancelToken?: CancelToken;
99
+ /** Upload progress callback */
100
+ onUploadProgress?: ProgressCallback;
101
+ /** Download progress callback */
102
+ onDownloadProgress?: ProgressCallback;
103
+ /** Return cancelable request */
104
+ cancelable?: boolean;
105
+ /** Return raw response with metadata */
106
+ raw?: boolean;
107
+ }
108
+ interface InternalRequestConfig extends RequestOptions {
109
+ url: string;
110
+ method: HttpMethod;
111
+ headers: Headers;
112
+ baseURL?: string;
113
+ /** Internal: retry attempt tracking */
114
+ _retry?: boolean;
115
+ /** Internal: current retry count */
116
+ _retryCount?: number;
117
+ }
118
+ interface RequestResponse<T = unknown> {
119
+ data: T;
120
+ status: number;
121
+ statusText: string;
122
+ headers: Headers;
123
+ config: InternalRequestConfig;
124
+ request?: Request;
125
+ ok: boolean;
126
+ }
127
+ interface RequestError extends Error {
128
+ name: 'RequestError';
129
+ message: string;
130
+ status?: number;
131
+ statusText?: string;
132
+ data?: unknown;
133
+ config: InternalRequestConfig;
134
+ request?: Request;
135
+ response?: Response;
136
+ code?: ErrorCode;
137
+ }
138
+ interface InterceptorHandler<T> {
139
+ fulfilled?: (value: T) => T | Promise<T>;
140
+ rejected?: (error: RequestError) => unknown;
141
+ }
142
+ interface InterceptorManager<T> {
143
+ /**
144
+ * Add an interceptor
145
+ * @returns Interceptor ID for removal
146
+ */
147
+ use(onFulfilled?: (value: T) => T | Promise<T>, onRejected?: (error: RequestError) => unknown): number;
148
+ /**
149
+ * Remove an interceptor by ID
150
+ */
151
+ eject(id: number): void;
152
+ /**
153
+ * Clear all interceptors
154
+ */
155
+ clear(): void;
156
+ }
157
+ interface Interceptors {
158
+ request: InterceptorManager<InternalRequestConfig>;
159
+ response: InterceptorManager<Response>;
160
+ }
161
+ interface CancelableRequest<T> {
162
+ data: Promise<T>;
163
+ cancel: () => void;
164
+ }
165
+ interface CancelToken {
166
+ promise: Promise<Cancel>;
167
+ reason?: Cancel;
168
+ throwIfRequested(): void;
169
+ }
170
+ interface CancelTokenSource {
171
+ token: CancelToken;
172
+ cancel: (message?: string) => void;
173
+ }
174
+ interface Cancel {
175
+ message?: string;
176
+ }
177
+ /** Options without `cancelable: true` or `raw: true`: the method resolves to the body. */
178
+ type PlainOptions = RequestOptions & {
179
+ cancelable?: false;
180
+ raw?: false;
181
+ };
182
+ /** Options that make a method resolve to a `CancelableRequest` (`cancelable: true`). */
183
+ type CancelableOptions = RequestOptions & {
184
+ cancelable: true;
185
+ };
186
+ /** Options that make a method resolve to a `RequestResponse` (`raw: true`). */
187
+ type RawOptions = RequestOptions & {
188
+ raw: true;
189
+ };
190
+ /**
191
+ * Result of a `raw: true` request. A type argument that is already a
192
+ * `RequestResponse` (as in `get<RequestResponse<User>>(url, { raw: true })`)
193
+ * is kept as-is; any other `T` describes the body: `RequestResponse<T>`.
194
+ */
195
+ type RawResult<T> = [T] extends [RequestResponse<unknown>] ? T : RequestResponse<T>;
196
+ /**
197
+ * Result of a `cancelable: true` request. A type argument that is already a
198
+ * `CancelableRequest` is kept as-is; any other `T` describes the body.
199
+ */
200
+ type CancelableResult<T> = [T] extends [CancelableRequest<unknown>] ? T : CancelableRequest<T>;
201
+ interface RequestClient {
202
+ get<T = unknown>(url: string, options?: PlainOptions): Promise<T>;
203
+ get<T = unknown>(url: string, options: CancelableOptions & RawOptions): Promise<CancelableResult<RawResult<T>>>;
204
+ get<T = unknown>(url: string, options: CancelableOptions): Promise<CancelableResult<T>>;
205
+ get<T = unknown>(url: string, options: RawOptions): Promise<RawResult<T>>;
206
+ get<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
207
+ post<T = unknown>(url: string, body?: unknown, options?: PlainOptions): Promise<T>;
208
+ post<T = unknown>(url: string, body: unknown, options: CancelableOptions & RawOptions): Promise<CancelableResult<RawResult<T>>>;
209
+ post<T = unknown>(url: string, body: unknown, options: CancelableOptions): Promise<CancelableResult<T>>;
210
+ post<T = unknown>(url: string, body: unknown, options: RawOptions): Promise<RawResult<T>>;
211
+ post<T = unknown>(url: string, body?: unknown, options?: RequestOptions): Promise<T>;
212
+ put<T = unknown>(url: string, body?: unknown, options?: PlainOptions): Promise<T>;
213
+ put<T = unknown>(url: string, body: unknown, options: CancelableOptions & RawOptions): Promise<CancelableResult<RawResult<T>>>;
214
+ put<T = unknown>(url: string, body: unknown, options: CancelableOptions): Promise<CancelableResult<T>>;
215
+ put<T = unknown>(url: string, body: unknown, options: RawOptions): Promise<RawResult<T>>;
216
+ put<T = unknown>(url: string, body?: unknown, options?: RequestOptions): Promise<T>;
217
+ patch<T = unknown>(url: string, body?: unknown, options?: PlainOptions): Promise<T>;
218
+ patch<T = unknown>(url: string, body: unknown, options: CancelableOptions & RawOptions): Promise<CancelableResult<RawResult<T>>>;
219
+ patch<T = unknown>(url: string, body: unknown, options: CancelableOptions): Promise<CancelableResult<T>>;
220
+ patch<T = unknown>(url: string, body: unknown, options: RawOptions): Promise<RawResult<T>>;
221
+ patch<T = unknown>(url: string, body?: unknown, options?: RequestOptions): Promise<T>;
222
+ delete<T = unknown>(url: string, options?: PlainOptions): Promise<T>;
223
+ delete<T = unknown>(url: string, options: CancelableOptions & RawOptions): Promise<CancelableResult<RawResult<T>>>;
224
+ delete<T = unknown>(url: string, options: CancelableOptions): Promise<CancelableResult<T>>;
225
+ delete<T = unknown>(url: string, options: RawOptions): Promise<RawResult<T>>;
226
+ delete<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
227
+ head(url: string, options?: PlainOptions): Promise<void>;
228
+ head(url: string, options: CancelableOptions & RawOptions): Promise<CancelableRequest<RequestResponse<void>>>;
229
+ head(url: string, options: CancelableOptions): Promise<CancelableRequest<void>>;
230
+ head(url: string, options: RawOptions): Promise<RequestResponse<void>>;
231
+ head(url: string, options?: RequestOptions): Promise<void>;
232
+ options(url: string, options?: PlainOptions): Promise<void>;
233
+ options(url: string, options: CancelableOptions & RawOptions): Promise<CancelableRequest<RequestResponse<void>>>;
234
+ options(url: string, options: CancelableOptions): Promise<CancelableRequest<void>>;
235
+ options(url: string, options: RawOptions): Promise<RequestResponse<void>>;
236
+ options(url: string, options?: RequestOptions): Promise<void>;
237
+ request<T = unknown>(url: string, options?: PlainOptions): Promise<T>;
238
+ request<T = unknown>(url: string, options: CancelableOptions & RawOptions): Promise<CancelableResult<RawResult<T>>>;
239
+ request<T = unknown>(url: string, options: CancelableOptions): Promise<CancelableResult<T>>;
240
+ request<T = unknown>(url: string, options: RawOptions): Promise<RawResult<T>>;
241
+ request<T = unknown>(url: string, options?: RequestOptions): Promise<T>;
242
+ request<T = unknown>(options: PlainOptions & {
243
+ url: string;
244
+ }): Promise<T>;
245
+ request<T = unknown>(options: CancelableOptions & RawOptions & {
246
+ url: string;
247
+ }): Promise<CancelableResult<RawResult<T>>>;
248
+ request<T = unknown>(options: CancelableOptions & {
249
+ url: string;
250
+ }): Promise<CancelableResult<T>>;
251
+ request<T = unknown>(options: RawOptions & {
252
+ url: string;
253
+ }): Promise<RawResult<T>>;
254
+ request<T = unknown>(options: RequestOptions & {
255
+ url: string;
256
+ }): Promise<T>;
257
+ interceptors: Interceptors;
258
+ defaults: ClientConfig;
259
+ extend(config: ClientConfig): RequestClient;
260
+ clone(): RequestClient;
261
+ }
262
+ interface UseRequestOptions<T> {
263
+ /** Don't fetch on mount */
264
+ manual?: boolean;
265
+ /** Success callback */
266
+ onSuccess?: (data: T) => void;
267
+ /** Error callback */
268
+ onError?: (error: RequestError) => void;
269
+ }
270
+ interface UseRequestResult<T> {
271
+ data: T | undefined;
272
+ loading: boolean;
273
+ error: RequestError | null;
274
+ execute: (options?: RequestOptions) => Promise<T>;
275
+ reset: () => void;
276
+ }
277
+ interface UseQueryOptions<T> {
278
+ /** Enable/disable query */
279
+ enabled?: boolean;
280
+ /** Time until data is stale (ms); stale data is refetched on window focus/reconnect */
281
+ staleTime?: number;
282
+ /**
283
+ * @deprecated Not implemented: useQuery keeps no shared cache, so this
284
+ * option has no effect.
285
+ */
286
+ cacheTime?: number;
287
+ /** Refetch on window focus */
288
+ refetchOnWindowFocus?: boolean;
289
+ /** Refetch on reconnect */
290
+ refetchOnReconnect?: boolean;
291
+ /** Polling interval (ms) */
292
+ refetchInterval?: number;
293
+ /** Retry failed requests */
294
+ retry?: number | boolean;
295
+ /** Success callback */
296
+ onSuccess?: (data: T) => void;
297
+ /** Error callback */
298
+ onError?: (error: RequestError) => void;
299
+ }
300
+ interface UseQueryResult<T> {
301
+ data: T | undefined;
302
+ loading: boolean;
303
+ error: RequestError | null;
304
+ isStale: boolean;
305
+ isFetching: boolean;
306
+ refetch: () => Promise<void>;
307
+ }
308
+ interface UseMutationOptions<T, V> {
309
+ /** Success callback */
310
+ onSuccess?: (data: T, variables: V) => void;
311
+ /** Error callback */
312
+ onError?: (error: RequestError, variables: V) => void;
313
+ /** Settled callback (always called) */
314
+ onSettled?: (data: T | undefined, error: RequestError | null, variables: V) => void;
315
+ }
316
+ interface UseMutationResult<T, V> {
317
+ data: T | undefined;
318
+ loading: boolean;
319
+ error: RequestError | null;
320
+ mutate: (variables: V) => void;
321
+ mutateAsync: (variables: V) => Promise<T>;
322
+ reset: () => void;
323
+ }
324
+ interface UseInfiniteQueryOptions<T> {
325
+ /** Get next page param from last page */
326
+ getNextPageParam: (lastPage: T, pages: T[]) => unknown | undefined;
327
+ /** Get previous page param from first page */
328
+ getPreviousPageParam?: (firstPage: T, pages: T[]) => unknown | undefined;
329
+ /** Enable/disable query */
330
+ enabled?: boolean;
331
+ /** @deprecated Not implemented: has no effect in useInfiniteQuery. */
332
+ staleTime?: number;
333
+ /** Success callback */
334
+ onSuccess?: (data: T[]) => void;
335
+ /** Error callback */
336
+ onError?: (error: RequestError) => void;
337
+ }
338
+ interface UseInfiniteQueryResult<T> {
339
+ data: T[] | undefined;
340
+ loading: boolean;
341
+ error: RequestError | null;
342
+ isFetchingNextPage: boolean;
343
+ isFetchingPreviousPage: boolean;
344
+ hasNextPage: boolean;
345
+ hasPreviousPage: boolean;
346
+ fetchNextPage: () => Promise<void>;
347
+ fetchPreviousPage: () => Promise<void>;
348
+ refetch: () => Promise<void>;
349
+ }
350
+
351
+ export type { ClientConfig as C, ErrorCode as E, HttpMethod as H, InternalRequestConfig as I, ParamsSerializer as P, RequestClient as R, TransformFn as T, UseRequestOptions as U, RequestError as a, CancelTokenSource as b, Cancel as c, ResponseType as d, RequestOptions as e, RequestTransport as f, RetryConfig as g, Progress as h, ProgressCallback as i, RequestHook as j, ResponseHook as k, ErrorHook as l, RequestResponse as m, InterceptorManager as n, InterceptorHandler as o, Interceptors as p, CancelableRequest as q, CancelToken as r, UseRequestResult as s, UseQueryOptions as t, UseQueryResult as u, UseMutationOptions as v, UseMutationResult as w, UseInfiniteQueryOptions as x, UseInfiniteQueryResult as y };
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@oxog/requestkit",
3
- "version": "1.0.0",
3
+ "version": "1.2.1",
4
4
  "description": "Zero-dependency HTTP client built on native fetch with interceptors, retry, and progress tracking",
5
5
  "author": "Ersin KOÇ",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/ersinkoc/requestkit.git"
9
+ "url": "git+https://github.com/ersinkoc/RequestKit.git"
10
10
  },
11
11
  "homepage": "https://requestkit.oxog.dev",
12
12
  "bugs": {
13
- "url": "https://github.com/ersinkoc/requestkit/issues"
13
+ "url": "https://github.com/ersinkoc/RequestKit/issues"
14
14
  },
15
15
  "keywords": [
16
16
  "http",
@@ -50,7 +50,8 @@
50
50
  "types": "./dist/react/index.d.cts",
51
51
  "default": "./dist/react/index.cjs"
52
52
  }
53
- }
53
+ },
54
+ "./package.json": "./package.json"
54
55
  },
55
56
  "files": [
56
57
  "dist",
@@ -65,27 +66,29 @@
65
66
  "test": "vitest run",
66
67
  "test:watch": "vitest",
67
68
  "test:coverage": "vitest run --coverage",
68
- "lint": "eslint src --ext .ts,.tsx",
69
- "typecheck": "tsc --noEmit",
69
+ "lint": "eslint src tests scripts --max-warnings=0",
70
+ "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
70
71
  "clean": "rimraf dist",
71
- "prepublishOnly": "npm run clean && npm run build && npm run test"
72
+ "test:package": "node scripts/check-package.mjs",
73
+ "prepublishOnly": "npm run check && npm run build && npm run test:package",
74
+ "check": "npm run lint && npm run typecheck && npm run test:coverage"
72
75
  },
73
76
  "devDependencies": {
74
- "@eslint/js": "^9.39.2",
77
+ "@eslint/js": "10.0.1",
75
78
  "@testing-library/react": "^14.1.2",
76
79
  "@types/node": "^20.10.6",
77
80
  "@types/react": "^18.2.46",
78
81
  "@types/react-dom": "^18.2.18",
79
82
  "@vitest/coverage-v8": "^1.1.3",
80
- "eslint-plugin-react": "^7.37.5",
81
- "eslint-plugin-react-hooks": "^7.0.1",
83
+ "eslint": "10.16.0",
84
+ "eslint-plugin-react-hooks": "7.1.1",
82
85
  "jsdom": "^23.2.0",
83
86
  "react": "^18.2.0",
84
87
  "react-dom": "^18.2.0",
85
88
  "rimraf": "^5.0.5",
86
89
  "tsup": "^8.0.1",
87
90
  "typescript": "^5.3.3",
88
- "typescript-eslint": "^8.50.1",
91
+ "typescript-eslint": "8.74.0",
89
92
  "vitest": "^1.1.3"
90
93
  },
91
94
  "peerDependencies": {