brass-runtime 1.8.0 → 1.9.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.
- package/dist/http/index.d.mts +39 -20
- package/dist/http/index.d.ts +39 -20
- package/dist/http/index.js +1 -1
- package/dist/http/index.mjs +1 -1
- package/package.json +1 -1
package/dist/http/index.d.mts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { V as ZStream, A as Async, w as AsyncWithPromise } from '../stream-Byv3iEEs.mjs';
|
|
2
2
|
|
|
3
|
+
type RetryPolicy = {
|
|
4
|
+
maxRetries: number;
|
|
5
|
+
baseDelayMs: number;
|
|
6
|
+
maxDelayMs: number;
|
|
7
|
+
retryOnMethods?: HttpMethod[];
|
|
8
|
+
retryOnStatus?: (status: number) => boolean;
|
|
9
|
+
retryOnError?: (e: HttpError) => boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
3
12
|
type HttpError = {
|
|
4
13
|
_tag: "Abort";
|
|
5
14
|
} | {
|
|
@@ -37,29 +46,19 @@ type HttpWireResponseStream = {
|
|
|
37
46
|
ms: number;
|
|
38
47
|
};
|
|
39
48
|
type HttpClientStream = (req: HttpRequest) => Async<unknown, HttpError, HttpWireResponseStream>;
|
|
40
|
-
type HttpClient =
|
|
49
|
+
type HttpClient = HttpClientFn & {
|
|
50
|
+
with: (mw: HttpMiddleware) => HttpClient;
|
|
51
|
+
};
|
|
52
|
+
declare const withMiddleware: (mw: HttpMiddleware) => (c: HttpClient) => HttpClient;
|
|
53
|
+
declare const decorate: (run: HttpClientFn) => HttpClient;
|
|
54
|
+
type HttpClientFn = (req: HttpRequest) => Async<unknown, HttpError, HttpWireResponse>;
|
|
55
|
+
type HttpMiddleware = (next: HttpClientFn) => HttpClientFn;
|
|
56
|
+
declare const normalizeHeadersInit: (h: any) => Record<string, string> | undefined;
|
|
41
57
|
declare function makeHttpStream(cfg?: MakeHttpConfig): HttpClientStream;
|
|
42
58
|
declare function makeHttp(cfg?: MakeHttpConfig): HttpClient;
|
|
59
|
+
declare const withRetryStream: (p: RetryPolicy) => (next: HttpClientStream) => HttpClientStream;
|
|
43
60
|
|
|
44
61
|
type InitNoMethodBody = Omit<RequestInit, "method" | "body">;
|
|
45
|
-
declare function httpClient(cfg?: MakeHttpConfig): {
|
|
46
|
-
request: (req: HttpRequest) => AsyncWithPromise<unknown, HttpError, HttpWireResponse>;
|
|
47
|
-
get: (url: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, HttpWireResponse>;
|
|
48
|
-
getText: (url: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, {
|
|
49
|
-
status: number;
|
|
50
|
-
statusText: string;
|
|
51
|
-
headers: Record<string, string>;
|
|
52
|
-
body: string;
|
|
53
|
-
}>;
|
|
54
|
-
getJson: <A>(url: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, {
|
|
55
|
-
status: number;
|
|
56
|
-
statusText: string;
|
|
57
|
-
headers: Record<string, string>;
|
|
58
|
-
body: A;
|
|
59
|
-
}>;
|
|
60
|
-
post: (url: string, body?: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, HttpWireResponse>;
|
|
61
|
-
postJson: <A extends object>(url: string, bodyObj: A, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, HttpWireResponse>;
|
|
62
|
-
};
|
|
63
62
|
type HttpMeta = {
|
|
64
63
|
request: HttpRequest;
|
|
65
64
|
urlFinal: string;
|
|
@@ -81,6 +80,18 @@ type HttpResponseWithMeta<A> = {
|
|
|
81
80
|
response: HttpResponse<A>;
|
|
82
81
|
meta: HttpMeta;
|
|
83
82
|
};
|
|
83
|
+
type Dx = {
|
|
84
|
+
request: (req: HttpRequest) => any;
|
|
85
|
+
get: (url: string, init?: any) => any;
|
|
86
|
+
post: (url: string, body?: string, init?: any) => any;
|
|
87
|
+
getText: (url: string, init?: any) => any;
|
|
88
|
+
getJson: <A>(url: string, init?: any) => any;
|
|
89
|
+
postJson: <A>(url: string, body?: any, init?: any) => any;
|
|
90
|
+
with: (mw: HttpMiddleware) => Dx;
|
|
91
|
+
withRetry: (p: RetryPolicy) => Dx;
|
|
92
|
+
wire: HttpClient;
|
|
93
|
+
};
|
|
94
|
+
declare function httpClient(cfg?: MakeHttpConfig): Dx;
|
|
84
95
|
declare function httpClientWithMeta(cfg?: MakeHttpConfig): {
|
|
85
96
|
request: (req: HttpRequest) => AsyncWithPromise<unknown, HttpError, {
|
|
86
97
|
wire: HttpWireResponse;
|
|
@@ -121,5 +132,13 @@ declare function httpClientWithMeta(cfg?: MakeHttpConfig): {
|
|
|
121
132
|
meta: HttpMeta;
|
|
122
133
|
}>;
|
|
123
134
|
};
|
|
135
|
+
declare function httpClientStream(cfg?: MakeHttpConfig): {
|
|
136
|
+
request: (req: HttpRequest) => AsyncWithPromise<unknown, HttpError, HttpWireResponseStream>;
|
|
137
|
+
getStream: (url: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, HttpWireResponseStream>;
|
|
138
|
+
get: (url: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, HttpWireResponseStream>;
|
|
139
|
+
with: (mw: (n: HttpClientStream) => HttpClientStream) => /*elided*/ any;
|
|
140
|
+
withRetry: (p: RetryPolicy) => /*elided*/ any;
|
|
141
|
+
wire: HttpClientStream;
|
|
142
|
+
};
|
|
124
143
|
|
|
125
|
-
export { type HttpClient, type HttpClientStream, type HttpError, type HttpInit, type HttpMeta, type HttpMethod, type HttpRequest, type HttpResponse, type HttpResponseWithMeta, type HttpWireResponse, type HttpWireResponseStream, type HttpWireWithMeta, type MakeHttpConfig, httpClient, httpClientWithMeta, makeHttp, makeHttpStream };
|
|
144
|
+
export { type Dx, type HttpClient, type HttpClientFn, type HttpClientStream, type HttpError, type HttpInit, type HttpMeta, type HttpMethod, type HttpMiddleware, type HttpRequest, type HttpResponse, type HttpResponseWithMeta, type HttpWireResponse, type HttpWireResponseStream, type HttpWireWithMeta, type MakeHttpConfig, decorate, httpClient, httpClientStream, httpClientWithMeta, makeHttp, makeHttpStream, normalizeHeadersInit, withMiddleware, withRetryStream };
|
package/dist/http/index.d.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { V as ZStream, A as Async, w as AsyncWithPromise } from '../stream-Byv3iEEs.js';
|
|
2
2
|
|
|
3
|
+
type RetryPolicy = {
|
|
4
|
+
maxRetries: number;
|
|
5
|
+
baseDelayMs: number;
|
|
6
|
+
maxDelayMs: number;
|
|
7
|
+
retryOnMethods?: HttpMethod[];
|
|
8
|
+
retryOnStatus?: (status: number) => boolean;
|
|
9
|
+
retryOnError?: (e: HttpError) => boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
3
12
|
type HttpError = {
|
|
4
13
|
_tag: "Abort";
|
|
5
14
|
} | {
|
|
@@ -37,29 +46,19 @@ type HttpWireResponseStream = {
|
|
|
37
46
|
ms: number;
|
|
38
47
|
};
|
|
39
48
|
type HttpClientStream = (req: HttpRequest) => Async<unknown, HttpError, HttpWireResponseStream>;
|
|
40
|
-
type HttpClient =
|
|
49
|
+
type HttpClient = HttpClientFn & {
|
|
50
|
+
with: (mw: HttpMiddleware) => HttpClient;
|
|
51
|
+
};
|
|
52
|
+
declare const withMiddleware: (mw: HttpMiddleware) => (c: HttpClient) => HttpClient;
|
|
53
|
+
declare const decorate: (run: HttpClientFn) => HttpClient;
|
|
54
|
+
type HttpClientFn = (req: HttpRequest) => Async<unknown, HttpError, HttpWireResponse>;
|
|
55
|
+
type HttpMiddleware = (next: HttpClientFn) => HttpClientFn;
|
|
56
|
+
declare const normalizeHeadersInit: (h: any) => Record<string, string> | undefined;
|
|
41
57
|
declare function makeHttpStream(cfg?: MakeHttpConfig): HttpClientStream;
|
|
42
58
|
declare function makeHttp(cfg?: MakeHttpConfig): HttpClient;
|
|
59
|
+
declare const withRetryStream: (p: RetryPolicy) => (next: HttpClientStream) => HttpClientStream;
|
|
43
60
|
|
|
44
61
|
type InitNoMethodBody = Omit<RequestInit, "method" | "body">;
|
|
45
|
-
declare function httpClient(cfg?: MakeHttpConfig): {
|
|
46
|
-
request: (req: HttpRequest) => AsyncWithPromise<unknown, HttpError, HttpWireResponse>;
|
|
47
|
-
get: (url: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, HttpWireResponse>;
|
|
48
|
-
getText: (url: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, {
|
|
49
|
-
status: number;
|
|
50
|
-
statusText: string;
|
|
51
|
-
headers: Record<string, string>;
|
|
52
|
-
body: string;
|
|
53
|
-
}>;
|
|
54
|
-
getJson: <A>(url: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, {
|
|
55
|
-
status: number;
|
|
56
|
-
statusText: string;
|
|
57
|
-
headers: Record<string, string>;
|
|
58
|
-
body: A;
|
|
59
|
-
}>;
|
|
60
|
-
post: (url: string, body?: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, HttpWireResponse>;
|
|
61
|
-
postJson: <A extends object>(url: string, bodyObj: A, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, HttpWireResponse>;
|
|
62
|
-
};
|
|
63
62
|
type HttpMeta = {
|
|
64
63
|
request: HttpRequest;
|
|
65
64
|
urlFinal: string;
|
|
@@ -81,6 +80,18 @@ type HttpResponseWithMeta<A> = {
|
|
|
81
80
|
response: HttpResponse<A>;
|
|
82
81
|
meta: HttpMeta;
|
|
83
82
|
};
|
|
83
|
+
type Dx = {
|
|
84
|
+
request: (req: HttpRequest) => any;
|
|
85
|
+
get: (url: string, init?: any) => any;
|
|
86
|
+
post: (url: string, body?: string, init?: any) => any;
|
|
87
|
+
getText: (url: string, init?: any) => any;
|
|
88
|
+
getJson: <A>(url: string, init?: any) => any;
|
|
89
|
+
postJson: <A>(url: string, body?: any, init?: any) => any;
|
|
90
|
+
with: (mw: HttpMiddleware) => Dx;
|
|
91
|
+
withRetry: (p: RetryPolicy) => Dx;
|
|
92
|
+
wire: HttpClient;
|
|
93
|
+
};
|
|
94
|
+
declare function httpClient(cfg?: MakeHttpConfig): Dx;
|
|
84
95
|
declare function httpClientWithMeta(cfg?: MakeHttpConfig): {
|
|
85
96
|
request: (req: HttpRequest) => AsyncWithPromise<unknown, HttpError, {
|
|
86
97
|
wire: HttpWireResponse;
|
|
@@ -121,5 +132,13 @@ declare function httpClientWithMeta(cfg?: MakeHttpConfig): {
|
|
|
121
132
|
meta: HttpMeta;
|
|
122
133
|
}>;
|
|
123
134
|
};
|
|
135
|
+
declare function httpClientStream(cfg?: MakeHttpConfig): {
|
|
136
|
+
request: (req: HttpRequest) => AsyncWithPromise<unknown, HttpError, HttpWireResponseStream>;
|
|
137
|
+
getStream: (url: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, HttpWireResponseStream>;
|
|
138
|
+
get: (url: string, init?: InitNoMethodBody) => AsyncWithPromise<unknown, HttpError, HttpWireResponseStream>;
|
|
139
|
+
with: (mw: (n: HttpClientStream) => HttpClientStream) => /*elided*/ any;
|
|
140
|
+
withRetry: (p: RetryPolicy) => /*elided*/ any;
|
|
141
|
+
wire: HttpClientStream;
|
|
142
|
+
};
|
|
124
143
|
|
|
125
|
-
export { type HttpClient, type HttpClientStream, type HttpError, type HttpInit, type HttpMeta, type HttpMethod, type HttpRequest, type HttpResponse, type HttpResponseWithMeta, type HttpWireResponse, type HttpWireResponseStream, type HttpWireWithMeta, type MakeHttpConfig, httpClient, httpClientWithMeta, makeHttp, makeHttpStream };
|
|
144
|
+
export { type Dx, type HttpClient, type HttpClientFn, type HttpClientStream, type HttpError, type HttpInit, type HttpMeta, type HttpMethod, type HttpMiddleware, type HttpRequest, type HttpResponse, type HttpResponseWithMeta, type HttpWireResponse, type HttpWireResponseStream, type HttpWireWithMeta, type MakeHttpConfig, decorate, httpClient, httpClientStream, httpClientWithMeta, makeHttp, makeHttpStream, normalizeHeadersInit, withMiddleware, withRetryStream };
|
package/dist/http/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var chunk3NPW2C22_js=require('../chunk-3NPW2C22.js');var
|
|
1
|
+
'use strict';var chunk3NPW2C22_js=require('../chunk-3NPW2C22.js');var b={make(t,e){return {get:t,set:e}},over(t,e){return r=>t.set(e(t.get(r)))(r)},compose(t,e){return b.make(r=>t.get(e.get(r)),r=>n=>e.set(t.set(r)(e.get(n)))(n))}};var f={headers:b.make(t=>t.headers??{},t=>e=>({...e,headers:t}))};var k=t=>e=>b.over(f.headers,r=>({...r,...t}))(e),w=t=>e=>b.over(f.headers,r=>({...t,...r}))(e),M=(t,e)=>r=>b.over(f.headers,n=>n[t]?n:{...n,[t]:e})(r);var dt=t=>e=>T(t(e)),T=t=>Object.assign((e=>t(e)),{with:e=>T(e(t))}),S=t=>t instanceof DOMException&&t.name==="AbortError"?{_tag:"Abort"}:typeof t=="object"&&t&&"_tag"in t?t:{_tag:"FetchError",message:String(t)},C=t=>{if(t){if(typeof Headers<"u"&&t instanceof Headers){let e={};return t.forEach((r,n)=>e[n]=r),e}if(Array.isArray(t))return Object.fromEntries(t);if(typeof t=="object")return {...t}}},I=t=>e=>{let r=Object.keys(t).length?w(t)(e):e,n=C(e.init?.headers);return n&&Object.keys(n).length&&(r=w(n)(r)),r};function W(t={}){let e=t.baseUrl??"",r=t.headers??{},n=I(r);return c=>chunk3NPW2C22_js.L(async a=>{let y=n(c),i;try{i=new URL(y.url,e);}catch{throw {_tag:"BadUrl",message:`URL inv\xE1lida: ${y.url}`}}let p=performance.now(),s=await fetch(i,{...y.init??{},method:y.method,headers:f.headers.get(y),body:y.body,signal:a}),u={};s.headers.forEach((d,m)=>u[m]=d);let o=chunk3NPW2C22_js.da(s.body,S);return {status:s.status,statusText:s.statusText,headers:u,body:o,ms:Math.round(performance.now()-p)}},S)}function U(t={}){let e=t.baseUrl??"",r=t.headers??{},n=I(r);return T(a=>chunk3NPW2C22_js.L(async y=>{let i=n(a),p;try{p=new URL(i.url,e);}catch{throw {_tag:"BadUrl",message:`URL inv\xE1lida: ${i.url}`}}let s=performance.now(),u=await fetch(p,{...i.init??{},method:i.method,headers:f.headers.get(i),body:i.body,signal:y}),o=await u.text(),d={};return u.headers.forEach((m,H)=>d[H]=m),{status:u.status,statusText:u.statusText,headers:d,bodyText:o,ms:Math.round(performance.now()-s)}},S))}var j=(t,e,r)=>Math.max(e,Math.min(r,t)),J=t=>t._tag==="FetchError",G=t=>t===408||t===429||t===500||t===502||t===503||t===504,F=(t,e,r)=>{let n=e*Math.pow(2,t),c=j(n,0,r);return Math.floor(Math.random()*c)},D=t=>chunk3NPW2C22_js.L(e=>new Promise((r,n)=>{if(e.aborted)return n({_tag:"Abort"});let c=setTimeout(r,t);e.addEventListener("abort",()=>{clearTimeout(c),n({_tag:"Abort"});},{once:true});}),e=>typeof e=="object"&&e&&"_tag"in e?e:{_tag:"FetchError",message:String(e)}),z=t=>{let e=Object.keys(t).find(a=>a.toLowerCase()==="retry-after");if(!e)return;let r=t[e]?.trim();if(!r)return;let n=Number(r);if(Number.isFinite(n))return Math.max(0,Math.floor(n*1e3));let c=Date.parse(r);if(Number.isFinite(c))return Math.max(0,c-Date.now())},_=t=>e=>(r=>{let n=c=>chunk3NPW2C22_js.a(e(r),a=>{if(a._tag==="Abort"||a._tag==="BadUrl")return chunk3NPW2C22_js.f(a);if(!(c<t.maxRetries&&(t.retryOnError??J)(a)))return chunk3NPW2C22_js.f(a);let i=F(c,t.baseDelayMs,t.maxDelayMs);return chunk3NPW2C22_js.k(D(i),()=>n(c+1))},a=>{if(!(c<t.maxRetries&&(t.retryOnStatus??G)(a.status)))return chunk3NPW2C22_js.e(a);let p=z(a.headers)??F(c,t.baseDelayMs,t.maxDelayMs);return chunk3NPW2C22_js.k(D(p),()=>n(c+1))});return n(0)});var Z=["GET","HEAD","OPTIONS"],$=t=>t===408||t===429||t===500||t===502||t===503||t===504,K=t=>t._tag==="FetchError",Q=t=>typeof t=="object"&&t!==null&&"name"in t&&t.name==="AbortError"?{_tag:"Abort"}:typeof t=="object"&&t&&"_tag"in t?t:{_tag:"FetchError",message:String(t)},L=t=>chunk3NPW2C22_js.L(e=>new Promise((r,n)=>{let c=setTimeout(r,t),a=()=>{clearTimeout(c);let y=typeof globalThis.DOMException=="function"?new globalThis.DOMException("Aborted","AbortError"):{name:"AbortError"};n(y);};if(e.aborted)return a();e.addEventListener("abort",a,{once:true});}),Q),V=(t,e,r)=>Math.max(e,Math.min(r,t)),N=(t,e,r)=>{let n=Math.max(0,e),c=Math.max(0,r),a=n*Math.pow(2,t),y=V(a,0,c);return Math.floor(Math.random()*y)},X=(t,e)=>{let r=Object.keys(t).find(n=>n.toLowerCase()===e.toLowerCase());return r?t[r]:void 0},Y=t=>{let e=X(t,"retry-after")?.trim();if(!e)return;let r=Number(e);if(Number.isFinite(r))return Math.max(0,Math.floor(r*1e3));let n=Date.parse(e);if(Number.isFinite(n))return Math.max(0,n-Date.now())},v=t=>e=>{let r=t.retryOnMethods??Z,n=t.retryOnStatus??$,c=t.retryOnError??K,a=i=>r.includes(i.method),y=(i,p)=>a(i)?chunk3NPW2C22_js.a(e(i),s=>{if(s._tag==="Abort"||s._tag==="BadUrl")return chunk3NPW2C22_js.f(s);if(!(p<t.maxRetries&&c(s)))return chunk3NPW2C22_js.f(s);let o=N(p,t.baseDelayMs,t.maxDelayMs);return chunk3NPW2C22_js.k(L(o),()=>y(i,p+1))},s=>{if(!(p<t.maxRetries&&n(s.status)))return chunk3NPW2C22_js.e(s);let d=Y(s.headers)??N(p,t.baseDelayMs,t.maxDelayMs);return chunk3NPW2C22_js.k(L(d),()=>y(i,p+1))}):e(i);return i=>y(i,0)};var tt=(t,e)=>{try{return new URL(e,t??"").toString()}catch{return (t??"")+e}},B=(t={})=>{let e=U(t),r=p=>chunk3NPW2C22_js.n((s,u)=>chunk3NPW2C22_js.H(s,u))(p),n=p=>e(p),c=p=>{let{headers:s,...u}=p??{};return {headers:C(s),init:u}},a=p=>s=>p?k(p)(s):s;return {cfg:t,wire:e,withPromise:r,requestRaw:n,splitInit:c,applyInitHeaders:a,buildReq:(p,s,u,o)=>{let d=c(u),m={method:p,url:s,...o&&o.length>0?{body:o}:{},init:d.init};return a(d.headers)(m)},toResponse:(p,s)=>({status:p.status,statusText:p.statusText,headers:p.headers,body:s})}};function Ft(t={}){let e=B(t),r=n=>{let c=o=>n(o),a=o=>e.withPromise(c(o));return {request:a,get:(o,d)=>a(e.buildReq("GET",o,d)),post:(o,d,m)=>a(e.buildReq("POST",o,m,d)),getText:(o,d)=>{let m=e.buildReq("GET",o,d);return e.withPromise(chunk3NPW2C22_js.p(c(m),H=>e.toResponse(H,H.bodyText)))},getJson:(o,d)=>{let m=e.buildReq("GET",o,d),H=M("accept","application/json")(m);return e.withPromise(chunk3NPW2C22_js.p(c(H),O=>e.toResponse(O,JSON.parse(O.bodyText))))},postJson:(o,d,m)=>a(e.buildReq("POST",o,m,JSON.stringify(d??{}))),with:o=>r(n.with(o)),withRetry:o=>r(n.with(v(o))),wire:n}};return r(e.wire)}function Dt(t={}){let e=B(t),r=(s,u,o)=>({request:s,urlFinal:tt(e.cfg.baseUrl,s.url),startedAt:o,durationMs:u.ms}),n=s=>{let u=Date.now();return e.withPromise(chunk3NPW2C22_js.p(e.requestRaw(s),o=>({wire:o,meta:r(s,o,u)})))};return {request:n,get:(s,u)=>{let o=e.buildReq("GET",s,u);return n(o)},getText:(s,u)=>{let o=e.buildReq("GET",s,u),d=Date.now();return e.withPromise(chunk3NPW2C22_js.p(e.requestRaw(o),m=>({wire:m,response:e.toResponse(m,m.bodyText),meta:r(o,m,d)})))},getJson:(s,u)=>{let o=e.buildReq("GET",s,u),d=M("accept","application/json")(o),m=Date.now();return e.withPromise(chunk3NPW2C22_js.p(e.requestRaw(d),H=>({wire:H,response:e.toResponse(H,JSON.parse(H.bodyText)),meta:r(d,H,m)})))},post:(s,u,o)=>{let d=e.buildReq("POST",s,o,u);return n(d)},postJson:(s,u,o)=>{let d=e.buildReq("POST",s,o,JSON.stringify(u)),m=M("content-type","application/json")(M("accept","application/json")(d));return n(m)}}}function It(t={}){let e=W(t),r=n=>{let c=i=>chunk3NPW2C22_js.n((p,s)=>chunk3NPW2C22_js.H(p,s))(i),a=i=>c(n(i)),y=(i,p)=>{let s={method:"GET",url:i,init:p},u=M("accept","*/*")(s);return a(u)};return {request:a,getStream:y,get:y,with:i=>r(i(n)),withRetry:i=>r(_(i)(n)),wire:n}};return r(e)}exports.decorate=T;exports.httpClient=Ft;exports.httpClientStream=It;exports.httpClientWithMeta=Dt;exports.makeHttp=U;exports.makeHttpStream=W;exports.normalizeHeadersInit=C;exports.withMiddleware=dt;exports.withRetryStream=_;
|
package/dist/http/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {L,da,p,n,H}from'../chunk-SNH7DUUJ.mjs';var
|
|
1
|
+
import {L as L$1,da,a,p,n,f as f$1,k as k$1,e,H}from'../chunk-SNH7DUUJ.mjs';var b={make(t,e){return {get:t,set:e}},over(t,e){return r=>t.set(e(t.get(r)))(r)},compose(t,e){return b.make(r=>t.get(e.get(r)),r=>n=>e.set(t.set(r)(e.get(n)))(n))}};var f={headers:b.make(t=>t.headers??{},t=>e=>({...e,headers:t}))};var k=t=>e=>b.over(f.headers,r=>({...r,...t}))(e),w=t=>e=>b.over(f.headers,r=>({...t,...r}))(e),M=(t,e)=>r=>b.over(f.headers,n=>n[t]?n:{...n,[t]:e})(r);var dt=t=>e=>T(t(e)),T=t=>Object.assign((e=>t(e)),{with:e=>T(e(t))}),S=t=>t instanceof DOMException&&t.name==="AbortError"?{_tag:"Abort"}:typeof t=="object"&&t&&"_tag"in t?t:{_tag:"FetchError",message:String(t)},C=t=>{if(t){if(typeof Headers<"u"&&t instanceof Headers){let e={};return t.forEach((r,n)=>e[n]=r),e}if(Array.isArray(t))return Object.fromEntries(t);if(typeof t=="object")return {...t}}},I=t=>e=>{let r=Object.keys(t).length?w(t)(e):e,n=C(e.init?.headers);return n&&Object.keys(n).length&&(r=w(n)(r)),r};function W(t={}){let e=t.baseUrl??"",r=t.headers??{},n=I(r);return c=>L$1(async a=>{let y=n(c),i;try{i=new URL(y.url,e);}catch{throw {_tag:"BadUrl",message:`URL inv\xE1lida: ${y.url}`}}let p=performance.now(),s=await fetch(i,{...y.init??{},method:y.method,headers:f.headers.get(y),body:y.body,signal:a}),u={};s.headers.forEach((d,m)=>u[m]=d);let o=da(s.body,S);return {status:s.status,statusText:s.statusText,headers:u,body:o,ms:Math.round(performance.now()-p)}},S)}function U(t={}){let e=t.baseUrl??"",r=t.headers??{},n=I(r);return T(a=>L$1(async y=>{let i=n(a),p;try{p=new URL(i.url,e);}catch{throw {_tag:"BadUrl",message:`URL inv\xE1lida: ${i.url}`}}let s=performance.now(),u=await fetch(p,{...i.init??{},method:i.method,headers:f.headers.get(i),body:i.body,signal:y}),o=await u.text(),d={};return u.headers.forEach((m,H)=>d[H]=m),{status:u.status,statusText:u.statusText,headers:d,bodyText:o,ms:Math.round(performance.now()-s)}},S))}var j=(t,e,r)=>Math.max(e,Math.min(r,t)),J=t=>t._tag==="FetchError",G=t=>t===408||t===429||t===500||t===502||t===503||t===504,F=(t,e,r)=>{let n=e*Math.pow(2,t),c=j(n,0,r);return Math.floor(Math.random()*c)},D=t=>L$1(e=>new Promise((r,n)=>{if(e.aborted)return n({_tag:"Abort"});let c=setTimeout(r,t);e.addEventListener("abort",()=>{clearTimeout(c),n({_tag:"Abort"});},{once:true});}),e=>typeof e=="object"&&e&&"_tag"in e?e:{_tag:"FetchError",message:String(e)}),z=t=>{let e=Object.keys(t).find(a=>a.toLowerCase()==="retry-after");if(!e)return;let r=t[e]?.trim();if(!r)return;let n=Number(r);if(Number.isFinite(n))return Math.max(0,Math.floor(n*1e3));let c=Date.parse(r);if(Number.isFinite(c))return Math.max(0,c-Date.now())},_=t=>e$1=>(r=>{let n=c=>a(e$1(r),a=>{if(a._tag==="Abort"||a._tag==="BadUrl")return f$1(a);if(!(c<t.maxRetries&&(t.retryOnError??J)(a)))return f$1(a);let i=F(c,t.baseDelayMs,t.maxDelayMs);return k$1(D(i),()=>n(c+1))},a=>{if(!(c<t.maxRetries&&(t.retryOnStatus??G)(a.status)))return e(a);let p=z(a.headers)??F(c,t.baseDelayMs,t.maxDelayMs);return k$1(D(p),()=>n(c+1))});return n(0)});var Z=["GET","HEAD","OPTIONS"],$=t=>t===408||t===429||t===500||t===502||t===503||t===504,K=t=>t._tag==="FetchError",Q=t=>typeof t=="object"&&t!==null&&"name"in t&&t.name==="AbortError"?{_tag:"Abort"}:typeof t=="object"&&t&&"_tag"in t?t:{_tag:"FetchError",message:String(t)},L=t=>L$1(e=>new Promise((r,n)=>{let c=setTimeout(r,t),a=()=>{clearTimeout(c);let y=typeof globalThis.DOMException=="function"?new globalThis.DOMException("Aborted","AbortError"):{name:"AbortError"};n(y);};if(e.aborted)return a();e.addEventListener("abort",a,{once:true});}),Q),V=(t,e,r)=>Math.max(e,Math.min(r,t)),N=(t,e,r)=>{let n=Math.max(0,e),c=Math.max(0,r),a=n*Math.pow(2,t),y=V(a,0,c);return Math.floor(Math.random()*y)},X=(t,e)=>{let r=Object.keys(t).find(n=>n.toLowerCase()===e.toLowerCase());return r?t[r]:void 0},Y=t=>{let e=X(t,"retry-after")?.trim();if(!e)return;let r=Number(e);if(Number.isFinite(r))return Math.max(0,Math.floor(r*1e3));let n=Date.parse(e);if(Number.isFinite(n))return Math.max(0,n-Date.now())},v=t=>e$1=>{let r=t.retryOnMethods??Z,n=t.retryOnStatus??$,c=t.retryOnError??K,a$1=i=>r.includes(i.method),y=(i,p)=>a$1(i)?a(e$1(i),s=>{if(s._tag==="Abort"||s._tag==="BadUrl")return f$1(s);if(!(p<t.maxRetries&&c(s)))return f$1(s);let o=N(p,t.baseDelayMs,t.maxDelayMs);return k$1(L(o),()=>y(i,p+1))},s=>{if(!(p<t.maxRetries&&n(s.status)))return e(s);let d=Y(s.headers)??N(p,t.baseDelayMs,t.maxDelayMs);return k$1(L(d),()=>y(i,p+1))}):e$1(i);return i=>y(i,0)};var tt=(t,e)=>{try{return new URL(e,t??"").toString()}catch{return (t??"")+e}},B=(t={})=>{let e=U(t),r=p=>n((s,u)=>H(s,u))(p),n$1=p=>e(p),c=p=>{let{headers:s,...u}=p??{};return {headers:C(s),init:u}},a=p=>s=>p?k(p)(s):s;return {cfg:t,wire:e,withPromise:r,requestRaw:n$1,splitInit:c,applyInitHeaders:a,buildReq:(p,s,u,o)=>{let d=c(u),m={method:p,url:s,...o&&o.length>0?{body:o}:{},init:d.init};return a(d.headers)(m)},toResponse:(p,s)=>({status:p.status,statusText:p.statusText,headers:p.headers,body:s})}};function Ft(t={}){let e=B(t),r=n=>{let c=o=>n(o),a=o=>e.withPromise(c(o));return {request:a,get:(o,d)=>a(e.buildReq("GET",o,d)),post:(o,d,m)=>a(e.buildReq("POST",o,m,d)),getText:(o,d)=>{let m=e.buildReq("GET",o,d);return e.withPromise(p(c(m),H=>e.toResponse(H,H.bodyText)))},getJson:(o,d)=>{let m=e.buildReq("GET",o,d),H=M("accept","application/json")(m);return e.withPromise(p(c(H),O=>e.toResponse(O,JSON.parse(O.bodyText))))},postJson:(o,d,m)=>a(e.buildReq("POST",o,m,JSON.stringify(d??{}))),with:o=>r(n.with(o)),withRetry:o=>r(n.with(v(o))),wire:n}};return r(e.wire)}function Dt(t={}){let e=B(t),r=(s,u,o)=>({request:s,urlFinal:tt(e.cfg.baseUrl,s.url),startedAt:o,durationMs:u.ms}),n=s=>{let u=Date.now();return e.withPromise(p(e.requestRaw(s),o=>({wire:o,meta:r(s,o,u)})))};return {request:n,get:(s,u)=>{let o=e.buildReq("GET",s,u);return n(o)},getText:(s,u)=>{let o=e.buildReq("GET",s,u),d=Date.now();return e.withPromise(p(e.requestRaw(o),m=>({wire:m,response:e.toResponse(m,m.bodyText),meta:r(o,m,d)})))},getJson:(s,u)=>{let o=e.buildReq("GET",s,u),d=M("accept","application/json")(o),m=Date.now();return e.withPromise(p(e.requestRaw(d),H=>({wire:H,response:e.toResponse(H,JSON.parse(H.bodyText)),meta:r(d,H,m)})))},post:(s,u,o)=>{let d=e.buildReq("POST",s,o,u);return n(d)},postJson:(s,u,o)=>{let d=e.buildReq("POST",s,o,JSON.stringify(u)),m=M("content-type","application/json")(M("accept","application/json")(d));return n(m)}}}function It(t={}){let e=W(t),r=n$1=>{let c=i=>n((p,s)=>H(p,s))(i),a=i=>c(n$1(i)),y=(i,p)=>{let s={method:"GET",url:i,init:p},u=M("accept","*/*")(s);return a(u)};return {request:a,getStream:y,get:y,with:i=>r(i(n$1)),withRetry:i=>r(_(i)(n$1)),wire:n$1}};return r(e)}export{T as decorate,Ft as httpClient,It as httpClientStream,Dt as httpClientWithMeta,U as makeHttp,W as makeHttpStream,C as normalizeHeadersInit,dt as withMiddleware,_ as withRetryStream};
|