tanstack-fetch 1.0.0 → 1.0.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,132 @@
1
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
2
+ type ClientSource = 'browser' | 'ssr' | 'edge';
3
+ type IncomingHeaders = {
4
+ cookie?: string;
5
+ authorization?: string;
6
+ requestId?: string;
7
+ };
8
+ type PathParams = Record<string, string | number>;
9
+ type QueryParams = Record<string, string | number | boolean | undefined | null>;
10
+ type PluginName = 'trace' | 'ssr-forward' | 'retry-idempotent' | 'sse-resume';
11
+ type MaybePromise<T> = T | Promise<T>;
12
+
13
+ type OkResult<T> = {
14
+ ok: true;
15
+ status: number;
16
+ data: T;
17
+ headers: Headers;
18
+ };
19
+ type ErrResult<E> = {
20
+ ok: false;
21
+ status: number;
22
+ error: E;
23
+ headers: Headers;
24
+ };
25
+ type FetchResult<T, E = FetchErrorInfo> = OkResult<T> | ErrResult<E>;
26
+ type FetchErrorInfo = {
27
+ status: number;
28
+ code: string;
29
+ message: string;
30
+ body: unknown;
31
+ };
32
+ /** @deprecated Use FetchResult */
33
+ type HttpResult<T, E = FetchErrorInfo> = FetchResult<T, E>;
34
+ /** @deprecated Use FetchErrorInfo */
35
+ type HttpError = FetchErrorInfo;
36
+
37
+ type RequestContext = {
38
+ request: {
39
+ method: HttpMethod;
40
+ url: URL;
41
+ headers: Headers;
42
+ body?: unknown;
43
+ signal?: AbortSignal;
44
+ };
45
+ response?: Response;
46
+ data?: unknown;
47
+ error?: FetchErrorInfo;
48
+ incoming?: IncomingHeaders;
49
+ meta: {
50
+ attempt: number;
51
+ maxRetries: number;
52
+ source: ClientSource;
53
+ operation?: string;
54
+ requestId?: string;
55
+ lastEventId?: string;
56
+ };
57
+ };
58
+ type SseEventContext<T = unknown> = RequestContext & {
59
+ event: {
60
+ event?: string;
61
+ data: T;
62
+ id?: string;
63
+ retry?: number;
64
+ };
65
+ };
66
+ type InterceptorDecision<T> = {
67
+ action: 'continue';
68
+ context?: T;
69
+ } | {
70
+ action: 'skip';
71
+ } | {
72
+ action: 'drop';
73
+ } | {
74
+ action: 'retry';
75
+ delayMs?: number;
76
+ } | {
77
+ action: 'short-circuit';
78
+ result: FetchResult<unknown, unknown>;
79
+ };
80
+ type InterceptorHandler<T> = (context: T) => MaybePromise<InterceptorDecision<T> | void>;
81
+ type HttpInterceptor = {
82
+ name: string;
83
+ order?: number;
84
+ match?: {
85
+ operation?: string;
86
+ method?: HttpMethod;
87
+ pathPrefix?: string;
88
+ status?: number;
89
+ };
90
+ onRequest?: InterceptorHandler<RequestContext>;
91
+ onRequestError?: InterceptorHandler<RequestContext>;
92
+ onResponse?: InterceptorHandler<RequestContext>;
93
+ onResponseError?: InterceptorHandler<RequestContext>;
94
+ onSseOpen?: InterceptorHandler<RequestContext>;
95
+ onSseEvent?: InterceptorHandler<SseEventContext>;
96
+ onSseError?: InterceptorHandler<RequestContext>;
97
+ onSseReconnect?: InterceptorHandler<RequestContext>;
98
+ };
99
+
100
+ type StatusHandlerInput = {
101
+ status: number;
102
+ error: FetchErrorInfo;
103
+ context: RequestContext;
104
+ };
105
+ type StatusHandlerResult = void | {
106
+ action: 'continue';
107
+ } | {
108
+ action: 'retry';
109
+ delayMs?: number;
110
+ };
111
+ type StatusHandler = (input: StatusHandlerInput) => MaybePromise<StatusHandlerResult>;
112
+ type StatusHandlers = {
113
+ 401?: StatusHandler;
114
+ 403?: StatusHandler;
115
+ 404?: StatusHandler;
116
+ 500?: StatusHandler;
117
+ '4xx'?: StatusHandler;
118
+ '5xx'?: StatusHandler;
119
+ default?: StatusHandler;
120
+ } & {
121
+ [status: number]: StatusHandler | undefined;
122
+ };
123
+ type AuthConfig = {
124
+ /** Return access token (or null/undefined to skip). */
125
+ getToken: () => MaybePromise<string | null | undefined>;
126
+ /** Header name. Default: `authorization`. */
127
+ header?: string;
128
+ /** Prefix before token. Default: `Bearer`. Use `''` for raw token. */
129
+ scheme?: string;
130
+ };
131
+
132
+ export type { AuthConfig as A, ClientSource as C, FetchResult as F, HttpError as H, IncomingHeaders as I, MaybePromise as M, PluginName as P, QueryParams as Q, RequestContext as R, StatusHandler as S, FetchErrorInfo as a, HttpInterceptor as b, HttpMethod as c, HttpResult as d, StatusHandlerInput as e, StatusHandlerResult as f, StatusHandlers as g, PathParams as h };
@@ -0,0 +1,132 @@
1
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
2
+ type ClientSource = 'browser' | 'ssr' | 'edge';
3
+ type IncomingHeaders = {
4
+ cookie?: string;
5
+ authorization?: string;
6
+ requestId?: string;
7
+ };
8
+ type PathParams = Record<string, string | number>;
9
+ type QueryParams = Record<string, string | number | boolean | undefined | null>;
10
+ type PluginName = 'trace' | 'ssr-forward' | 'retry-idempotent' | 'sse-resume';
11
+ type MaybePromise<T> = T | Promise<T>;
12
+
13
+ type OkResult<T> = {
14
+ ok: true;
15
+ status: number;
16
+ data: T;
17
+ headers: Headers;
18
+ };
19
+ type ErrResult<E> = {
20
+ ok: false;
21
+ status: number;
22
+ error: E;
23
+ headers: Headers;
24
+ };
25
+ type FetchResult<T, E = FetchErrorInfo> = OkResult<T> | ErrResult<E>;
26
+ type FetchErrorInfo = {
27
+ status: number;
28
+ code: string;
29
+ message: string;
30
+ body: unknown;
31
+ };
32
+ /** @deprecated Use FetchResult */
33
+ type HttpResult<T, E = FetchErrorInfo> = FetchResult<T, E>;
34
+ /** @deprecated Use FetchErrorInfo */
35
+ type HttpError = FetchErrorInfo;
36
+
37
+ type RequestContext = {
38
+ request: {
39
+ method: HttpMethod;
40
+ url: URL;
41
+ headers: Headers;
42
+ body?: unknown;
43
+ signal?: AbortSignal;
44
+ };
45
+ response?: Response;
46
+ data?: unknown;
47
+ error?: FetchErrorInfo;
48
+ incoming?: IncomingHeaders;
49
+ meta: {
50
+ attempt: number;
51
+ maxRetries: number;
52
+ source: ClientSource;
53
+ operation?: string;
54
+ requestId?: string;
55
+ lastEventId?: string;
56
+ };
57
+ };
58
+ type SseEventContext<T = unknown> = RequestContext & {
59
+ event: {
60
+ event?: string;
61
+ data: T;
62
+ id?: string;
63
+ retry?: number;
64
+ };
65
+ };
66
+ type InterceptorDecision<T> = {
67
+ action: 'continue';
68
+ context?: T;
69
+ } | {
70
+ action: 'skip';
71
+ } | {
72
+ action: 'drop';
73
+ } | {
74
+ action: 'retry';
75
+ delayMs?: number;
76
+ } | {
77
+ action: 'short-circuit';
78
+ result: FetchResult<unknown, unknown>;
79
+ };
80
+ type InterceptorHandler<T> = (context: T) => MaybePromise<InterceptorDecision<T> | void>;
81
+ type HttpInterceptor = {
82
+ name: string;
83
+ order?: number;
84
+ match?: {
85
+ operation?: string;
86
+ method?: HttpMethod;
87
+ pathPrefix?: string;
88
+ status?: number;
89
+ };
90
+ onRequest?: InterceptorHandler<RequestContext>;
91
+ onRequestError?: InterceptorHandler<RequestContext>;
92
+ onResponse?: InterceptorHandler<RequestContext>;
93
+ onResponseError?: InterceptorHandler<RequestContext>;
94
+ onSseOpen?: InterceptorHandler<RequestContext>;
95
+ onSseEvent?: InterceptorHandler<SseEventContext>;
96
+ onSseError?: InterceptorHandler<RequestContext>;
97
+ onSseReconnect?: InterceptorHandler<RequestContext>;
98
+ };
99
+
100
+ type StatusHandlerInput = {
101
+ status: number;
102
+ error: FetchErrorInfo;
103
+ context: RequestContext;
104
+ };
105
+ type StatusHandlerResult = void | {
106
+ action: 'continue';
107
+ } | {
108
+ action: 'retry';
109
+ delayMs?: number;
110
+ };
111
+ type StatusHandler = (input: StatusHandlerInput) => MaybePromise<StatusHandlerResult>;
112
+ type StatusHandlers = {
113
+ 401?: StatusHandler;
114
+ 403?: StatusHandler;
115
+ 404?: StatusHandler;
116
+ 500?: StatusHandler;
117
+ '4xx'?: StatusHandler;
118
+ '5xx'?: StatusHandler;
119
+ default?: StatusHandler;
120
+ } & {
121
+ [status: number]: StatusHandler | undefined;
122
+ };
123
+ type AuthConfig = {
124
+ /** Return access token (or null/undefined to skip). */
125
+ getToken: () => MaybePromise<string | null | undefined>;
126
+ /** Header name. Default: `authorization`. */
127
+ header?: string;
128
+ /** Prefix before token. Default: `Bearer`. Use `''` for raw token. */
129
+ scheme?: string;
130
+ };
131
+
132
+ export type { AuthConfig as A, ClientSource as C, FetchResult as F, HttpError as H, IncomingHeaders as I, MaybePromise as M, PluginName as P, QueryParams as Q, RequestContext as R, StatusHandler as S, FetchErrorInfo as a, HttpInterceptor as b, HttpMethod as c, HttpResult as d, StatusHandlerInput as e, StatusHandlerResult as f, StatusHandlers as g, PathParams as h };