tanstack-fetch 1.0.0 → 1.0.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.
- package/README.md +134 -3
- package/dist/cli.js +22 -239
- package/dist/client.type-BBYqVrTM.d.cts +156 -0
- package/dist/client.type-Btgj3NQn.d.ts +156 -0
- package/dist/config.type-eG_cuxpu.d.cts +132 -0
- package/dist/config.type-eG_cuxpu.d.ts +132 -0
- package/dist/index.cjs +2 -881
- package/dist/index.d.cts +23 -17
- package/dist/index.d.ts +23 -17
- package/dist/index.js +2 -3
- package/dist/plugins.cjs +1 -0
- package/dist/plugins.d.cts +19 -0
- package/dist/plugins.d.ts +19 -0
- package/dist/plugins.js +1 -0
- package/dist/react.cjs +1 -951
- package/dist/react.d.cts +6 -6
- package/dist/react.d.ts +6 -6
- package/dist/react.js +1 -91
- package/dist/sse.cjs +7 -0
- package/dist/sse.d.cts +9 -0
- package/dist/sse.d.ts +9 -0
- package/dist/sse.js +7 -0
- package/package.json +19 -6
- package/dist/chunk-T27KPHCL.js +0 -867
- package/dist/chunk-T27KPHCL.js.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/fetch-error-B10Od9AT.d.cts +0 -257
- package/dist/fetch-error-B10Od9AT.d.ts +0 -257
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/react.cjs.map +0 -1
- package/dist/react.js.map +0 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,23 +1,29 @@
|
|
|
1
|
-
import { C as CreateFetchOptions, F as FetchClient,
|
|
2
|
-
export {
|
|
1
|
+
import { C as CreateFetchOptions, F as FetchClient, a as FormDataFields } from './client.type-BBYqVrTM.cjs';
|
|
2
|
+
export { b as CreateClientOptions, c as FormDataFieldValue, H as HttpClient, R as RequestOptions, S as SseCallOptions, d as SseEvent, e as SseHandlers, f as SseSubscription, U as UploadCallOptions, g as UploadOptions, h as UploadProgressEvent, i as UploadProgressHandler } from './client.type-BBYqVrTM.cjs';
|
|
3
|
+
import { F as FetchResult, a as FetchErrorInfo } from './config.type-eG_cuxpu.cjs';
|
|
4
|
+
export { A as AuthConfig, H as HttpError, b as HttpInterceptor, c as HttpMethod, d as HttpResult, I as IncomingHeaders, P as PluginName, R as RequestContext, S as StatusHandler, e as StatusHandlerInput, f as StatusHandlerResult, g as StatusHandlers } from './config.type-eG_cuxpu.cjs';
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
/** Tiny HTTP client (no SSE). For streams use `tanstack-fetch/sse`. */
|
|
7
|
+
declare const createFetch: (options?: CreateFetchOptions) => Omit<FetchClient, "sse">;
|
|
5
8
|
/** @deprecated Use createFetch */
|
|
6
|
-
declare const createClient: (options?: CreateFetchOptions) => FetchClient
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
declare const
|
|
17
|
-
|
|
18
|
-
declare const
|
|
9
|
+
declare const createClient: (options?: CreateFetchOptions) => Omit<FetchClient, "sse">;
|
|
10
|
+
|
|
11
|
+
type FetchError = Error & {
|
|
12
|
+
readonly name: 'FetchError';
|
|
13
|
+
readonly status: number;
|
|
14
|
+
readonly code: string;
|
|
15
|
+
readonly body: unknown;
|
|
16
|
+
readonly headers: Headers;
|
|
17
|
+
readonly result: FetchResult<never, FetchErrorInfo>;
|
|
18
|
+
};
|
|
19
|
+
declare const createFetchError: (result: FetchResult<never, FetchErrorInfo>) => FetchError;
|
|
20
|
+
declare const isFetchError: (error: unknown) => error is FetchError;
|
|
21
|
+
declare const isAbortError: (error: unknown) => boolean;
|
|
22
|
+
|
|
23
|
+
/** Build `FormData` from a plain object (files, strings, numbers, arrays). */
|
|
24
|
+
declare const createFormData: (fields?: FormDataFields) => FormData;
|
|
19
25
|
|
|
20
26
|
declare const unwrap: <T>(result: FetchResult<T, FetchErrorInfo>) => T;
|
|
21
27
|
declare const unwrapAsync: <T>(promise: Promise<FetchResult<T, FetchErrorInfo>>) => Promise<T>;
|
|
22
28
|
|
|
23
|
-
export {
|
|
29
|
+
export { CreateFetchOptions, FetchClient, type FetchError, FetchErrorInfo, FetchResult, FormDataFields, createClient, createFetch, createFetchError, createFormData, isAbortError, isFetchError, unwrap, unwrapAsync };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,29 @@
|
|
|
1
|
-
import { C as CreateFetchOptions, F as FetchClient,
|
|
2
|
-
export {
|
|
1
|
+
import { C as CreateFetchOptions, F as FetchClient, a as FormDataFields } from './client.type-Btgj3NQn.js';
|
|
2
|
+
export { b as CreateClientOptions, c as FormDataFieldValue, H as HttpClient, R as RequestOptions, S as SseCallOptions, d as SseEvent, e as SseHandlers, f as SseSubscription, U as UploadCallOptions, g as UploadOptions, h as UploadProgressEvent, i as UploadProgressHandler } from './client.type-Btgj3NQn.js';
|
|
3
|
+
import { F as FetchResult, a as FetchErrorInfo } from './config.type-eG_cuxpu.js';
|
|
4
|
+
export { A as AuthConfig, H as HttpError, b as HttpInterceptor, c as HttpMethod, d as HttpResult, I as IncomingHeaders, P as PluginName, R as RequestContext, S as StatusHandler, e as StatusHandlerInput, f as StatusHandlerResult, g as StatusHandlers } from './config.type-eG_cuxpu.js';
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
/** Tiny HTTP client (no SSE). For streams use `tanstack-fetch/sse`. */
|
|
7
|
+
declare const createFetch: (options?: CreateFetchOptions) => Omit<FetchClient, "sse">;
|
|
5
8
|
/** @deprecated Use createFetch */
|
|
6
|
-
declare const createClient: (options?: CreateFetchOptions) => FetchClient
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
declare const
|
|
17
|
-
|
|
18
|
-
declare const
|
|
9
|
+
declare const createClient: (options?: CreateFetchOptions) => Omit<FetchClient, "sse">;
|
|
10
|
+
|
|
11
|
+
type FetchError = Error & {
|
|
12
|
+
readonly name: 'FetchError';
|
|
13
|
+
readonly status: number;
|
|
14
|
+
readonly code: string;
|
|
15
|
+
readonly body: unknown;
|
|
16
|
+
readonly headers: Headers;
|
|
17
|
+
readonly result: FetchResult<never, FetchErrorInfo>;
|
|
18
|
+
};
|
|
19
|
+
declare const createFetchError: (result: FetchResult<never, FetchErrorInfo>) => FetchError;
|
|
20
|
+
declare const isFetchError: (error: unknown) => error is FetchError;
|
|
21
|
+
declare const isAbortError: (error: unknown) => boolean;
|
|
22
|
+
|
|
23
|
+
/** Build `FormData` from a plain object (files, strings, numbers, arrays). */
|
|
24
|
+
declare const createFormData: (fields?: FormDataFields) => FormData;
|
|
19
25
|
|
|
20
26
|
declare const unwrap: <T>(result: FetchResult<T, FetchErrorInfo>) => T;
|
|
21
27
|
declare const unwrapAsync: <T>(promise: Promise<FetchResult<T, FetchErrorInfo>>) => Promise<T>;
|
|
22
28
|
|
|
23
|
-
export {
|
|
29
|
+
export { CreateFetchOptions, FetchClient, type FetchError, FetchErrorInfo, FetchResult, FormDataFields, createClient, createFetch, createFetchError, createFormData, isAbortError, isFetchError, unwrap, unwrapAsync };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export { createAuthInterceptor, createClient, createFetch, createFetchError, createRetryIdempotentInterceptor, createSseResumeInterceptor, createSsrForwardInterceptor, createStatusInterceptor, createTraceInterceptor, isAbortError, isFetchError, unwrap, unwrapAsync } from './chunk-T27KPHCL.js';
|
|
2
|
-
|
|
3
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
var F=e=>({name:"auth",order:15,onRequest:async t=>{let r=await e.getToken();if(!r)return {action:"continue",context:t};let o=e.header??"authorization",n=e.scheme===void 0?"Bearer":e.scheme,s=n?`${n} ${r}`:r;return t.request.headers.set(o,s),{action:"continue",context:t}}});var Y=(e,t)=>{let r=t[e];return r||(e>=500&&e<600&&t["5xx"]?t["5xx"]:e>=400&&e<500&&t["4xx"]?t["4xx"]:t.default)},R=e=>({name:"on-status",order:95,onResponseError:async t=>{let r=t.error?.status??t.response?.status;if(r===void 0||!t.error)return {action:"continue",context:t};let o=Y(r,e);if(!o)return {action:"continue",context:t};let n=await o({status:r,error:t.error,context:t});return n?.action==="retry"?{action:"retry",delayMs:n.delayMs}:{action:"continue",context:t}}});var Z=e=>{let t={...e.onStatus??{}};return e.onUnauthorized&&t[401]===void 0&&(t[401]=e.onUnauthorized),e.onForbidden&&t[403]===void 0&&(t[403]=e.onForbidden),e.onNotFound&&t[404]===void 0&&(t[404]=e.onNotFound),e.onServerError&&t["5xx"]===void 0&&t[500]===void 0&&(t["5xx"]=e.onServerError),Object.entries(t).some(([,o])=>!!o)?t:void 0},ee=e=>{if(e.auth)return e.auth;if(e.getToken)return {getToken:e.getToken}},H=e=>{let t=[],r=ee(e);r&&t.push(F(r));let o=Z(e);return o&&t.push(R(o)),t};var I=new Set(["GET","HEAD","OPTIONS"]),T=new Set([502,503,504]);var b=()=>({name:"retry-idempotent",order:80,onResponseError:e=>I.has(e.request.method)&&e.meta.attempt<e.meta.maxRetries&&!!(e.error&&T.has(e.error.status))?{action:"retry",delayMs:200*(e.meta.attempt+1)}:{action:"continue",context:e}});var w=()=>({name:"sse-resume",order:20,onSseEvent:e=>e.event.event==="ping"||e.event.event==="heartbeat"?{action:"drop"}:{action:"continue",context:e},onSseReconnect:e=>(e.meta.lastEventId&&e.request.headers.set("last-event-id",e.meta.lastEventId),{action:"continue",context:e})});var O=()=>({name:"ssr-forward",order:10,onRequest:e=>e.meta.source==="browser"?{action:"skip"}:(e.incoming?.cookie&&e.request.headers.set("cookie",e.incoming.cookie),e.incoming?.authorization&&e.request.headers.set("authorization",e.incoming.authorization),e.incoming?.requestId&&e.request.headers.set("x-request-id",e.incoming.requestId),{action:"continue",context:e})});var q=()=>({name:"trace",order:0,onRequest:e=>{let t=e.incoming?.requestId??globalThis.crypto.randomUUID();return e.request.headers.set("x-request-id",t),e.meta.requestId=t,{action:"continue",context:e}}});var S={trace:q,"ssr-forward":O,"retry-idempotent":b,"sse-resume":w};var f=e=>{let t=e.ok?{status:e.status,code:"UNKNOWN",message:"Request failed",body:e.data}:e.error,r=new Error(t.message);return Object.defineProperties(r,{name:{value:"FetchError"},status:{value:t.status},code:{value:t.code},body:{value:t.body},headers:{value:e.headers},result:{value:e.ok?{ok:false,status:t.status,error:t,headers:e.headers}:e}}),r},te=e=>!!(e&&typeof e=="object"&&e.name==="FetchError"&&"status"in e&&"code"in e),E=e=>e instanceof DOMException&&e.name==="AbortError"||e instanceof Error&&e.name==="AbortError";var oe=(e,t)=>{let r=e.match;return r?!(r.operation&&r.operation!==t.meta.operation||r.method&&r.method!==t.request.method||r.pathPrefix&&!t.request.url.pathname.startsWith(r.pathPrefix)||r.status!==void 0&&t.response?.status!==r.status):true},x=(e,t)=>{let r=new Set(t?.eject??[]);return [...e,...t?.use??[]].filter(n=>!r.has(n.name)).sort((n,s)=>(n.order??100)-(s.order??100))},m=async(e,t,r)=>{let o=r;for(let n of e){if(!oe(n,o))continue;let s=t(n);if(!s)continue;let a=await s(o)??{action:"continue",context:o};if(a.action!=="skip"){if(a.action==="drop")return {type:"drop"};if(a.action==="retry")return {type:"retry",delayMs:a.delayMs};if(a.action==="short-circuit")return {type:"short-circuit",result:a.result};o=a.context??o;}}return {type:"continue",context:o}};var ne=e=>!!(e&&e.includes("json")),C=async(e,t)=>{if(e.status===204)return null;if(t==="blob")return e.blob();if(t==="text")return e.text();let r=e.headers.get("content-type");if(t==="json"||ne(r)){let o=await e.text();return o?JSON.parse(o):null}return e.text()},A=(e,t)=>{if(e!=null)return typeof e=="string"||e instanceof FormData||e instanceof Blob||e instanceof URLSearchParams||e instanceof ArrayBuffer?e:(t.has("content-type")||t.set("content-type","application/json"),JSON.stringify(e))};var h=(e,t)=>{let r=t&&typeof t=="object"?t:void 0,o=typeof r?.code=="string"?r.code:"HTTP_ERROR",n=typeof r?.message=="string"?r.message:`Request failed with status ${e}`;return {status:e,code:o,message:n,body:t}},P=(e,t,r)=>({ok:true,status:e,data:t,headers:r}),g=(e,t,r)=>({ok:false,status:e,error:t,headers:r}),k=e=>{if(e.ok)return e.data;throw f(e)},se=async e=>k(await e);var ae=e=>{let t=new Headers;for(let r of e.trim().split(/[\r\n]+/)){let o=r.indexOf(":");o!==-1&&t.append(r.slice(0,o).trim(),r.slice(o+1).trim());}return t},ie=e=>{let t=e.lengthComputable?e.total:void 0;return {loaded:e.loaded,total:t,progress:t&&t>0?e.loaded/t:void 0}},U=e=>new Promise((t,r)=>{let o=new XMLHttpRequest;o.open(e.method,e.url.toString(),true),o.responseType="arraybuffer",o.withCredentials=e.credentials==="include",e.headers.forEach((s,a)=>{a.toLowerCase()==="content-type"&&e.body instanceof FormData||o.setRequestHeader(a,s);}),o.upload.onprogress=s=>{e.onUploadProgress(ie(s));};let n=()=>o.abort();e.signal?.addEventListener("abort",n,{once:true}),o.onload=()=>{e.signal?.removeEventListener("abort",n),t(new Response(o.response.byteLength>0?o.response:null,{status:o.status,statusText:o.statusText,headers:ae(o.getAllResponseHeaders())}));},o.onerror=()=>{e.signal?.removeEventListener("abort",n),r(new TypeError("Network request failed"));},o.onabort=()=>{e.signal?.removeEventListener("abort",n),r(new DOMException("The operation was aborted.","AbortError"));},o.send(e.body??null);}),v=()=>typeof XMLHttpRequest<"u";var ce=async(e,t)=>{let{context:r,fetchImpl:o,client:n,requestOptions:s}=e,a=s?.onUploadProgress;return a&&v()?U({url:r.request.url,method:r.request.method,headers:r.request.headers,body:t??null,signal:r.request.signal,credentials:n.credentials,onUploadProgress:a}):o(r.request.url,{method:r.request.method,headers:r.request.headers,body:t,signal:r.request.signal,credentials:n.credentials})},M=async e=>{let{context:t,interceptors:r,requestOptions:o}=e;try{let n=await ce(e,A(t.request.body,t.request.headers));return ue({interceptors:r,context:t,response:n,requestOptions:o})}catch(n){if(E(n))throw n;t.error=h(0,{code:"NETWORK_ERROR",message:n instanceof Error?n.message:"Network error"});let s=await m(r,a=>a.onRequestError,t);if(s.type==="retry")return {kind:"retry",delayMs:s.delayMs};if(s.type==="short-circuit")return {kind:"result",result:s.result};throw f(g(0,t.error,new Headers))}},ue=async e=>{let{interceptors:t,response:r,requestOptions:o}=e,n={...e.context,response:r,data:await C(r,o?.parseAs)},s=r.ok?i=>i.onResponse:i=>i.onResponseError;r.ok||(n.error=h(r.status,n.data));let a=await m(t,s,n);if(a.type==="retry")return {kind:"retry",delayMs:a.delayMs};if(a.type==="short-circuit")return {kind:"result",result:a.result};let d=a.type==="continue"?a.context:n;return {kind:"result",result:r.ok?P(r.status,d.data,r.headers):g(r.status,d.error??h(r.status,d.data),r.headers)}};var pe=(e,t)=>e.replace(/:([A-Za-z0-9_]+)/g,(r,o)=>{let n=t?.[o];if(n===void 0)throw new Error(`tanstack-fetch: missing path param "${o}"`);return encodeURIComponent(String(n))}),de=(e,t)=>{t&&Object.entries(t).forEach(([r,o])=>{o!=null&&e.searchParams.set(r,String(o));});},D=(e,t)=>{if(!(t==="browser"||!e)&&!/^https?:\/\//i.test(e))throw new Error("tanstack-fetch: set an absolute baseUrl for SSR and Edge runtimes")},B=(e,t,r,o)=>{let n=pe(t.replace(/\{([A-Za-z0-9_]+)\}/g,":$1"),r),s=/^https?:\/\//i.test(n)?n:`${(e??"").replace(/\/$/,"")}/${n.replace(/^\//,"")}`,a=new URL(s);return de(a,o),a};var le=e=>new Headers(e),L=(...e)=>{let t=new Headers;return e.forEach(r=>{r&&new Headers(r).forEach((o,n)=>{t.set(n,o);});}),t},_=async e=>{if(!e)return new Headers;let t=typeof e=="function"?await e():e;return le(t)};var N=e=>new Promise(t=>setTimeout(t,e)),j=e=>{let t=e.filter(o=>!!o);if(t.length===0)return;if(t.length===1)return t[0];if(typeof AbortSignal.any=="function")return AbortSignal.any(t);let r=new AbortController;return t.forEach(o=>{if(o.aborted){r.abort(o.reason);return}o.addEventListener("abort",()=>r.abort(o.reason),{once:true});}),r.signal},X=e=>{if(!e||e<=0)return;let t=new AbortController,r=setTimeout(()=>t.abort(new Error("tanstack-fetch: request timed out")),e);return t.signal.addEventListener("abort",()=>clearTimeout(r),{once:true}),t.signal};var me=async e=>{if(e.incoming)return typeof e.incoming=="function"?e.incoming():e.incoming},he=async(e,t)=>{let{method:r,path:o,requestOptions:n,client:s}=e,a=s.source??"browser";D(s.baseUrl,a);let d=B(s.baseUrl,o,n?.params,n?.query),c=L(await _(s.headers),n?.headers),i=n?.timeoutMs??s.timeoutMs??3e4,u=j([n?.signal,X(i)]);return {request:{method:r,url:d,headers:c,body:n?.body,signal:u},incoming:await me(s),meta:{attempt:t,maxRetries:s.maxRetries??2,source:a,operation:n?.operation}}},z=async e=>{let t=x(e.interceptors,e.requestOptions?.interceptors),r=e.client.fetch??globalThis.fetch;if(!r)throw new Error("tanstack-fetch: fetch is not available");let o=0,n=e.client.maxRetries??2;for(;o<=n;){let s=await ye({args:e,interceptors:t,fetchImpl:r,attempt:o});if(s.kind==="result")return s.result;o+=1,s.delayMs&&await N(s.delayMs);}throw new Error("tanstack-fetch: exceeded retry budget")},ye=async e=>{let t=await he(e.args,e.attempt),r=await m(e.interceptors,o=>o.onRequest,t);return r.type==="short-circuit"?{kind:"result",result:r.result}:r.type==="retry"?{kind:"retry",delayMs:r.delayMs}:(r.type==="continue"&&(t=r.context),M({requestOptions:e.args.requestOptions,client:e.args.client,interceptors:e.interceptors,fetchImpl:e.fetchImpl,context:t}))};var Ee=(e,t,r)=>{if(r==null)return;let o=Array.isArray(r)?r:[r];for(let n of o)if(n!=null){if(n instanceof Blob){e.append(t,n);continue}e.append(t,String(n));}},$=(e,t)=>{for(let[r,o]of Object.entries(t))Ee(e,r,o);return e},W=(e={})=>$(new FormData,e),ge=e=>!!(e.file||e.files?.length||e.fields),Q=e=>{if(e.body!==void 0&&!ge(e))return e.body;if(e.body!==void 0&&!(e.body instanceof FormData))throw new Error("tanstack-fetch: upload `body` must be FormData when using `file`, `files`, or `fields`");let t=e.body instanceof FormData?e.body:W();e.fields&&$(t,e.fields);let r=e.fieldName??"file";if(e.file&&t.append(r,e.file),e.files)for(let o of e.files)t.append(r,o);return t};var Fe=(e={})=>{let t={throwOnError:true,...e},r=[...H(t),...(t.plugins??[]).map(o=>S[o]()),...t.interceptors??[]];return {clientOptions:t,interceptors:r}},Re=e=>{let{clientOptions:t,interceptors:r}=e,o=(c,i,u)=>{let p={...i,name:c,order:u?.order??i.order},l=r.findIndex(y=>y.name===c);if(l>=0){r.splice(l,1,p);return}r.push(p);},n=c=>{let i=r.findIndex(u=>u.name===c);i>=0&&r.splice(i,1);},s=(async(c,i,u)=>{let p=await z({method:c,path:i,requestOptions:u,client:t,interceptors:r}),l=u?.throwOnError??t.throwOnError??true;if(!p.ok&&l)throw f(p);return l&&p.ok?p.data:p}),a=c=>(u,p)=>s(c,u,p),d=((c,i)=>{let{method:u="POST",file:p,files:l,fields:y,fieldName:J,body:G,...K}=i??{};return s(u,c,{...K,body:Q({body:G,file:p,files:l,fields:y,fieldName:J}),onUploadProgress:i?.onUploadProgress})});return {use:o,eject:n,request:s,get:a("GET"),post:a("POST"),put:a("PUT"),patch:a("PATCH"),delete:a("DELETE"),upload:d}},V=e=>Re(Fe(e)),He=V;
|
|
2
|
+
export{He as createClient,V as createFetch,f as createFetchError,W as createFormData,E as isAbortError,te as isFetchError,k as unwrap,se as unwrapAsync};
|
package/dist/plugins.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var c=e=>({name:"auth",order:15,onRequest:async t=>{let r=await e.getToken();if(!r)return {action:"continue",context:t};let n=e.header??"authorization",o=e.scheme===void 0?"Bearer":e.scheme,f=o?`${o} ${r}`:r;return t.request.headers.set(n,f),{action:"continue",context:t}}});var u=new Set(["GET","HEAD","OPTIONS"]),m=new Set([502,503,504]);var s=()=>({name:"retry-idempotent",order:80,onResponseError:e=>u.has(e.request.method)&&e.meta.attempt<e.meta.maxRetries&&!!(e.error&&m.has(e.error.status))?{action:"retry",delayMs:200*(e.meta.attempt+1)}:{action:"continue",context:e}});var a=()=>({name:"sse-resume",order:20,onSseEvent:e=>e.event.event==="ping"||e.event.event==="heartbeat"?{action:"drop"}:{action:"continue",context:e},onSseReconnect:e=>(e.meta.lastEventId&&e.request.headers.set("last-event-id",e.meta.lastEventId),{action:"continue",context:e})});var p=()=>({name:"ssr-forward",order:10,onRequest:e=>e.meta.source==="browser"?{action:"skip"}:(e.incoming?.cookie&&e.request.headers.set("cookie",e.incoming.cookie),e.incoming?.authorization&&e.request.headers.set("authorization",e.incoming.authorization),e.incoming?.requestId&&e.request.headers.set("x-request-id",e.incoming.requestId),{action:"continue",context:e})});var y=(e,t)=>{let r=t[e];return r||(e>=500&&e<600&&t["5xx"]?t["5xx"]:e>=400&&e<500&&t["4xx"]?t["4xx"]:t.default)},d=e=>({name:"on-status",order:95,onResponseError:async t=>{let r=t.error?.status??t.response?.status;if(r===void 0||!t.error)return {action:"continue",context:t};let n=y(r,e);if(!n)return {action:"continue",context:t};let o=await n({status:r,error:t.error,context:t});return o?.action==="retry"?{action:"retry",delayMs:o.delayMs}:{action:"continue",context:t}}});var i=()=>({name:"trace",order:0,onRequest:e=>{let t=e.incoming?.requestId??globalThis.crypto.randomUUID();return e.request.headers.set("x-request-id",t),e.meta.requestId=t,{action:"continue",context:e}}});var I={trace:i,"ssr-forward":p,"retry-idempotent":s,"sse-resume":a};var N=e=>e.map(t=>I[t]());exports.createAuthInterceptor=c;exports.createRetryIdempotentInterceptor=s;exports.createSseResumeInterceptor=a;exports.createSsrForwardInterceptor=p;exports.createStatusInterceptor=d;exports.createTraceInterceptor=i;exports.pluginFactories=I;exports.resolvePlugins=N;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { A as AuthConfig, b as HttpInterceptor, g as StatusHandlers, P as PluginName } from './config.type-eG_cuxpu.cjs';
|
|
2
|
+
|
|
3
|
+
declare const createAuthInterceptor: (auth: AuthConfig) => HttpInterceptor;
|
|
4
|
+
|
|
5
|
+
declare const createRetryIdempotentInterceptor: () => HttpInterceptor;
|
|
6
|
+
|
|
7
|
+
declare const createSseResumeInterceptor: () => HttpInterceptor;
|
|
8
|
+
|
|
9
|
+
declare const createSsrForwardInterceptor: () => HttpInterceptor;
|
|
10
|
+
|
|
11
|
+
declare const createStatusInterceptor: (handlers: StatusHandlers) => HttpInterceptor;
|
|
12
|
+
|
|
13
|
+
declare const createTraceInterceptor: () => HttpInterceptor;
|
|
14
|
+
|
|
15
|
+
declare const pluginFactories: Record<PluginName, () => HttpInterceptor>;
|
|
16
|
+
|
|
17
|
+
declare const resolvePlugins: (names: PluginName[]) => HttpInterceptor[];
|
|
18
|
+
|
|
19
|
+
export { HttpInterceptor, PluginName, createAuthInterceptor, createRetryIdempotentInterceptor, createSseResumeInterceptor, createSsrForwardInterceptor, createStatusInterceptor, createTraceInterceptor, pluginFactories, resolvePlugins };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { A as AuthConfig, b as HttpInterceptor, g as StatusHandlers, P as PluginName } from './config.type-eG_cuxpu.js';
|
|
2
|
+
|
|
3
|
+
declare const createAuthInterceptor: (auth: AuthConfig) => HttpInterceptor;
|
|
4
|
+
|
|
5
|
+
declare const createRetryIdempotentInterceptor: () => HttpInterceptor;
|
|
6
|
+
|
|
7
|
+
declare const createSseResumeInterceptor: () => HttpInterceptor;
|
|
8
|
+
|
|
9
|
+
declare const createSsrForwardInterceptor: () => HttpInterceptor;
|
|
10
|
+
|
|
11
|
+
declare const createStatusInterceptor: (handlers: StatusHandlers) => HttpInterceptor;
|
|
12
|
+
|
|
13
|
+
declare const createTraceInterceptor: () => HttpInterceptor;
|
|
14
|
+
|
|
15
|
+
declare const pluginFactories: Record<PluginName, () => HttpInterceptor>;
|
|
16
|
+
|
|
17
|
+
declare const resolvePlugins: (names: PluginName[]) => HttpInterceptor[];
|
|
18
|
+
|
|
19
|
+
export { HttpInterceptor, PluginName, createAuthInterceptor, createRetryIdempotentInterceptor, createSseResumeInterceptor, createSsrForwardInterceptor, createStatusInterceptor, createTraceInterceptor, pluginFactories, resolvePlugins };
|
package/dist/plugins.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var c=e=>({name:"auth",order:15,onRequest:async t=>{let r=await e.getToken();if(!r)return {action:"continue",context:t};let n=e.header??"authorization",o=e.scheme===void 0?"Bearer":e.scheme,f=o?`${o} ${r}`:r;return t.request.headers.set(n,f),{action:"continue",context:t}}});var u=new Set(["GET","HEAD","OPTIONS"]),m=new Set([502,503,504]);var s=()=>({name:"retry-idempotent",order:80,onResponseError:e=>u.has(e.request.method)&&e.meta.attempt<e.meta.maxRetries&&!!(e.error&&m.has(e.error.status))?{action:"retry",delayMs:200*(e.meta.attempt+1)}:{action:"continue",context:e}});var a=()=>({name:"sse-resume",order:20,onSseEvent:e=>e.event.event==="ping"||e.event.event==="heartbeat"?{action:"drop"}:{action:"continue",context:e},onSseReconnect:e=>(e.meta.lastEventId&&e.request.headers.set("last-event-id",e.meta.lastEventId),{action:"continue",context:e})});var p=()=>({name:"ssr-forward",order:10,onRequest:e=>e.meta.source==="browser"?{action:"skip"}:(e.incoming?.cookie&&e.request.headers.set("cookie",e.incoming.cookie),e.incoming?.authorization&&e.request.headers.set("authorization",e.incoming.authorization),e.incoming?.requestId&&e.request.headers.set("x-request-id",e.incoming.requestId),{action:"continue",context:e})});var y=(e,t)=>{let r=t[e];return r||(e>=500&&e<600&&t["5xx"]?t["5xx"]:e>=400&&e<500&&t["4xx"]?t["4xx"]:t.default)},d=e=>({name:"on-status",order:95,onResponseError:async t=>{let r=t.error?.status??t.response?.status;if(r===void 0||!t.error)return {action:"continue",context:t};let n=y(r,e);if(!n)return {action:"continue",context:t};let o=await n({status:r,error:t.error,context:t});return o?.action==="retry"?{action:"retry",delayMs:o.delayMs}:{action:"continue",context:t}}});var i=()=>({name:"trace",order:0,onRequest:e=>{let t=e.incoming?.requestId??globalThis.crypto.randomUUID();return e.request.headers.set("x-request-id",t),e.meta.requestId=t,{action:"continue",context:e}}});var I={trace:i,"ssr-forward":p,"retry-idempotent":s,"sse-resume":a};var N=e=>e.map(t=>I[t]());export{c as createAuthInterceptor,s as createRetryIdempotentInterceptor,a as createSseResumeInterceptor,p as createSsrForwardInterceptor,d as createStatusInterceptor,i as createTraceInterceptor,I as pluginFactories,N as resolvePlugins};
|