brass-runtime 1.13.2 → 1.13.4

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 +1,453 @@
1
- import {s}from'../chunk-JAQUSSUV.js';import {da,q,b as b$1,g as g$1,l,f,o,ca}from'../chunk-LTHJNW5A.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 A={headers:b.make(t=>t.headers??{},t=>e=>({...e,headers:t}))};var I=t=>e=>b.over(A.headers,r=>({...r,...t}))(e),C=t=>e=>b.over(A.headers,r=>({...t,...r}))(e),g=(t,e)=>r=>b.over(A.headers,n=>n[t]?n:{...n,[t]:e})(r);var v=t=>typeof t=="object"&&t!==null&&"_tag"in t,J=t=>v(t)?t:typeof t=="object"&&t!==null&&t.name==="AbortError"?{_tag:"Abort"}:{_tag:"FetchError",message:String(t)},M=t=>da(e=>new Promise((r,n)=>{if(e.aborted)return n({_tag:"Abort"});let u=setTimeout(r,t),p=()=>{clearTimeout(u),n({_tag:"Abort"});};e.addEventListener("abort",p,{once:true});}),J);var lt=t=>e=>T(t(e)),T=t=>Object.assign((e=>t(e)),{with:e=>T(e(t))}),O=t=>t instanceof DOMException&&t.name==="AbortError"?{_tag:"Abort"}:typeof t=="object"&&t&&"_tag"in t?t:{_tag:"FetchError",message:String(t)},k=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}}},D=t=>e=>{let r=Object.keys(t).length?C(t)(e):e,n=k(e.init?.headers);return n&&Object.keys(n).length&&(r=C(n)(r)),r};function N(t={}){let e=t.baseUrl??"",r=t.headers??{},n=D(r);return u=>da(async p=>{let y=n(u),i;try{i=new URL(y.url,e);}catch{throw {_tag:"BadUrl",message:`URL inv\xE1lida: ${y.url}`}}let a=performance.now(),s$1=await fetch(i,{...y.init??{},method:y.method,headers:A.headers.get(y),body:y.body,signal:p}),c={};s$1.headers.forEach((d,m)=>c[m]=d);let o=s(s$1.body,O);return {status:s$1.status,statusText:s$1.statusText,headers:c,body:o,ms:Math.round(performance.now()-a)}},O)}function _(t={}){let e=t.baseUrl??"",r=t.headers??{},n=D(r);return T(p=>da(async y=>{let i=n(p),a;try{a=new URL(i.url,e);}catch{throw {_tag:"BadUrl",message:`URL inv\xE1lida: ${i.url}`}}let s=performance.now(),c=await fetch(a,{...i.init??{},method:i.method,headers:A.headers.get(i),body:i.body,signal:y}),o=await c.text(),d={};return c.headers.forEach((m,H)=>d[H]=m),{status:c.status,statusText:c.statusText,headers:d,bodyText:o,ms:Math.round(performance.now()-s)}},O))}var G=(t,e,r)=>Math.max(e,Math.min(r,t)),z=t=>t._tag==="FetchError",Z=t=>t===408||t===429||t===500||t===502||t===503||t===504,F=(t,e,r)=>{let n=e*Math.pow(2,t),u=G(n,0,r);return Math.floor(Math.random()*u)},$=t=>{let e=Object.keys(t).find(p=>p.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 u=Date.parse(r);if(Number.isFinite(u))return Math.max(0,u-Date.now())},U=t=>e=>(r=>{let n=u=>b$1(e(r),p=>{if(p._tag==="Abort"||p._tag==="BadUrl")return g$1(p);if(!(u<t.maxRetries&&(t.retryOnError??z)(p)))return g$1(p);let i=F(u,t.baseDelayMs,t.maxDelayMs);return l(M(i),()=>n(u+1))},p=>{if(!(u<t.maxRetries&&(t.retryOnStatus??Z)(p.status)))return f(p);let a=$(p.headers)??F(u,t.baseDelayMs,t.maxDelayMs);return l(M(a),()=>n(u+1))});return n(0)});var K=["GET","HEAD","OPTIONS"],Q=t=>t===408||t===429||t===500||t===502||t===503||t===504,V=t=>t._tag==="FetchError";var X=(t,e,r)=>Math.max(e,Math.min(r,t)),L=(t,e,r)=>{let n=Math.max(0,e),u=Math.max(0,r),p=n*Math.pow(2,t),y=X(p,0,u);return Math.floor(Math.random()*y)},Y=(t,e)=>{let r=Object.keys(t).find(n=>n.toLowerCase()===e.toLowerCase());return r?t[r]:void 0},tt=t=>{let e=Y(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())},j=t=>e=>{let r=t.retryOnMethods??K,n=t.retryOnStatus??Q,u=t.retryOnError??V,p=i=>r.includes(i.method),y=(i,a)=>p(i)?b$1(e(i),s=>{if(s._tag==="Abort"||s._tag==="BadUrl")return g$1(s);if(!(a<t.maxRetries&&u(s)))return g$1(s);let o=L(a,t.baseDelayMs,t.maxDelayMs);return l(M(o),()=>y(i,a+1))},s=>{if(!(a<t.maxRetries&&n(s.status)))return f(s);let d=tt(s.headers)??L(a,t.baseDelayMs,t.maxDelayMs);return l(M(d),()=>y(i,a+1))}):e(i);return i=>y(i,0)};var et=(t,e)=>{try{return new URL(e,t??"").toString()}catch{return (t??"")+e}},B=(t={})=>{let e=_(t),r=a=>o((s,c)=>ca(s,c))(a),n=a=>e(a),u=a=>{let{headers:s,...c}=a??{};return {headers:k(s),init:c}},p=a=>s=>a?I(a)(s):s;return {cfg:t,wire:e,withPromise:r,requestRaw:n,splitInit:u,applyInitHeaders:p,buildReq:(a,s,c,o)=>{let d=u(c),m={method:a,url:s,...o&&o.length>0?{body:o}:{},init:d.init};return p(d.headers)(m)},toResponse:(a,s)=>({status:a.status,statusText:a.statusText,headers:a.headers,body:s})}};function _t(t={}){let e=B(t),r=n=>{let u=o=>n(o),p=o=>e.withPromise(u(o));return {request:p,get:(o,d)=>p(e.buildReq("GET",o,d)),post:(o,d,m)=>p(e.buildReq("POST",o,m,d)),getText:(o,d)=>{let m=e.buildReq("GET",o,d);return e.withPromise(q(u(m),H=>e.toResponse(H,H.bodyText)))},getJson:(o,d)=>{let m=e.buildReq("GET",o,d),H=g("accept","application/json")(m);return e.withPromise(q(u(H),l=>e.toResponse(l,JSON.parse(l.bodyText))))},postJson:(o,d,m)=>{let H=e.buildReq("POST",o,m,JSON.stringify(d??{})),l=g("content-type","application/json")(g("accept","application/json")(H));return e.withPromise(q(u(l),P=>e.toResponse(P,JSON.parse(P.bodyText))))},with:o=>r(n.with(o)),withRetry:o=>r(n.with(j(o))),wire:n}};return r(e.wire)}function Ut(t={}){let e=B(t),r=(s,c,o)=>({request:s,urlFinal:et(e.cfg.baseUrl,s.url),startedAt:o,durationMs:c.ms}),n=s=>{let c=Date.now();return e.withPromise(q(e.requestRaw(s),o=>({wire:o,meta:r(s,o,c)})))};return {request:n,get:(s,c)=>{let o=e.buildReq("GET",s,c);return n(o)},getText:(s,c)=>{let o=e.buildReq("GET",s,c),d=Date.now();return e.withPromise(q(e.requestRaw(o),m=>({wire:m,response:e.toResponse(m,m.bodyText),meta:r(o,m,d)})))},getJson:(s,c)=>{let o=e.buildReq("GET",s,c),d=g("accept","application/json")(o),m=Date.now();return e.withPromise(q(e.requestRaw(d),H=>({wire:H,response:e.toResponse(H,JSON.parse(H.bodyText)),meta:r(d,H,m)})))},post:(s,c,o)=>{let d=e.buildReq("POST",s,o,c);return n(d)},postJson:(s,c,o)=>{let d=e.buildReq("POST",s,o,JSON.stringify(c??{})),m=g("content-type","application/json")(g("accept","application/json")(d)),H=Date.now();return e.withPromise(q(e.requestRaw(m),l=>({wire:l,response:e.toResponse(l,JSON.parse(l.bodyText)),meta:r(m,l,H)})))}}}function Lt(t={}){let e=N(t),r=n=>{let u=i=>o((a,s)=>ca(a,s))(i),p=i=>u(n(i)),y=(i,a)=>{let s={method:"GET",url:i,init:a},c=g("accept","*/*")(s);return p(c)};return {request:p,getStream:y,get:y,with:i=>r(i(n)),withRetry:i=>r(U(i)(n)),wire:n}};return r(e)}export{T as decorate,_t as httpClient,Lt as httpClientStream,Ut as httpClientWithMeta,_ as makeHttp,N as makeHttpStream,k as normalizeHeadersInit,lt as withMiddleware,U as withRetryStream};
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
+
3
+ var _chunk3IF374MGjs = require('../chunk-3IF374MG.js');
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+ var _chunkTGOMLZ65js = require('../chunk-TGOMLZ65.js');
14
+
15
+ // src/http/optics/lens.ts
16
+ var Lens = {
17
+ make(get, set) {
18
+ return { get, set };
19
+ },
20
+ over(ln, f) {
21
+ return (s) => ln.set(f(ln.get(s)))(s);
22
+ },
23
+ compose(ab, sa) {
24
+ return Lens.make(
25
+ (s) => ab.get(sa.get(s)),
26
+ (b) => (s) => sa.set(ab.set(b)(sa.get(s)))(s)
27
+ );
28
+ }
29
+ };
30
+
31
+ // src/http/optics/request.ts
32
+ var Request = {
33
+ headers: Lens.make(
34
+ (req) => _nullishCoalesce(req.headers, () => ( {})),
35
+ (headers) => (req) => ({ ...req, headers })
36
+ )
37
+ };
38
+ var mergeHeaders = (extra) => (req) => Lens.over(Request.headers, (h) => ({ ...h, ...extra }))(req);
39
+ var mergeHeadersUnder = (under) => (req) => Lens.over(Request.headers, (h) => ({ ...under, ...h }))(req);
40
+ var setHeaderIfMissing = (k, v) => (req) => Lens.over(Request.headers, (h) => h[k] ? h : { ...h, [k]: v })(req);
41
+
42
+ // src/http/sleep.ts
43
+ var isHttpError = (e) => typeof e === "object" && e !== null && "_tag" in e;
44
+ var normalizeHttpError = (e) => {
45
+ if (isHttpError(e)) return e;
46
+ if (typeof e === "object" && e !== null && e.name === "AbortError") {
47
+ return { _tag: "Abort" };
48
+ }
49
+ return { _tag: "FetchError", message: String(e) };
50
+ };
51
+ var sleepMs = (ms) => _chunkTGOMLZ65js.fromPromiseAbortable.call(void 0,
52
+ (signal) => new Promise((resolve, reject) => {
53
+ if (signal.aborted) return reject({ _tag: "Abort" });
54
+ const id = setTimeout(resolve, ms);
55
+ const onAbort = () => {
56
+ clearTimeout(id);
57
+ reject({ _tag: "Abort" });
58
+ };
59
+ signal.addEventListener("abort", onAbort, { once: true });
60
+ }),
61
+ normalizeHttpError
62
+ );
63
+
64
+ // src/http/client.ts
65
+ var withMiddleware = (mw) => (c) => decorate(mw(c));
66
+ var decorate = (run) => Object.assign(((req) => run(req)), {
67
+ with: (mw) => decorate(mw(run))
68
+ });
69
+ var normalizeHttpError2 = (e) => {
70
+ if (e instanceof DOMException && e.name === "AbortError") return { _tag: "Abort" };
71
+ if (typeof e === "object" && e && "_tag" in e) return e;
72
+ return { _tag: "FetchError", message: String(e) };
73
+ };
74
+ var normalizeHeadersInit = (h) => {
75
+ if (!h) return void 0;
76
+ if (typeof Headers !== "undefined" && h instanceof Headers) {
77
+ const out = {};
78
+ h.forEach((v, k) => out[k] = v);
79
+ return out;
80
+ }
81
+ if (Array.isArray(h)) return Object.fromEntries(h);
82
+ if (typeof h === "object") return { ...h };
83
+ return void 0;
84
+ };
85
+ var normalizeRequest = (defaultHeaders) => (req0) => {
86
+ let req = Object.keys(defaultHeaders).length ? mergeHeadersUnder(defaultHeaders)(req0) : req0;
87
+ const initHeaders = normalizeHeadersInit(_optionalChain([req0, 'access', _ => _.init, 'optionalAccess', _2 => _2.headers]));
88
+ if (initHeaders && Object.keys(initHeaders).length) {
89
+ req = mergeHeadersUnder(initHeaders)(req);
90
+ }
91
+ return req;
92
+ };
93
+ function makeHttpStream(cfg = {}) {
94
+ const baseUrl = _nullishCoalesce(cfg.baseUrl, () => ( ""));
95
+ const defaultHeaders = _nullishCoalesce(cfg.headers, () => ( {}));
96
+ const normalize = normalizeRequest(defaultHeaders);
97
+ return (req0) => _chunkTGOMLZ65js.fromPromiseAbortable.call(void 0,
98
+ async (signal) => {
99
+ const req = normalize(req0);
100
+ let url;
101
+ try {
102
+ url = new URL(req.url, baseUrl);
103
+ } catch (e2) {
104
+ throw { _tag: "BadUrl", message: `URL inv\xE1lida: ${req.url}` };
105
+ }
106
+ const started = performance.now();
107
+ const res = await fetch(url, {
108
+ ..._nullishCoalesce(req.init, () => ( {})),
109
+ method: req.method,
110
+ headers: Request.headers.get(req),
111
+ // 👈 optics: headers ya normalizados
112
+ body: req.body,
113
+ signal
114
+ });
115
+ const headers = {};
116
+ res.headers.forEach((v, k) => headers[k] = v);
117
+ const body = _chunk3IF374MGjs.streamFromReadableStream.call(void 0, res.body, normalizeHttpError2);
118
+ return {
119
+ status: res.status,
120
+ statusText: res.statusText,
121
+ headers,
122
+ body,
123
+ ms: Math.round(performance.now() - started)
124
+ };
125
+ },
126
+ normalizeHttpError2
127
+ );
128
+ }
129
+ function makeHttp(cfg = {}) {
130
+ const baseUrl = _nullishCoalesce(cfg.baseUrl, () => ( ""));
131
+ const defaultHeaders = _nullishCoalesce(cfg.headers, () => ( {}));
132
+ const normalize = normalizeRequest(defaultHeaders);
133
+ const run = (req0) => _chunkTGOMLZ65js.fromPromiseAbortable.call(void 0,
134
+ async (signal) => {
135
+ const req = normalize(req0);
136
+ let url;
137
+ try {
138
+ url = new URL(req.url, baseUrl);
139
+ } catch (e3) {
140
+ throw { _tag: "BadUrl", message: `URL inv\xE1lida: ${req.url}` };
141
+ }
142
+ const started = performance.now();
143
+ const res = await fetch(url, {
144
+ ..._nullishCoalesce(req.init, () => ( {})),
145
+ method: req.method,
146
+ headers: Request.headers.get(req),
147
+ // 👈 optics
148
+ body: req.body,
149
+ signal
150
+ });
151
+ const bodyText = await res.text();
152
+ const headers = {};
153
+ res.headers.forEach((v, k) => headers[k] = v);
154
+ return {
155
+ status: res.status,
156
+ statusText: res.statusText,
157
+ headers,
158
+ bodyText,
159
+ ms: Math.round(performance.now() - started)
160
+ };
161
+ },
162
+ normalizeHttpError2
163
+ );
164
+ return decorate(run);
165
+ }
166
+ var clamp = (n, min, max) => Math.max(min, Math.min(max, n));
167
+ var defaultRetryOnError = (e) => e._tag === "FetchError";
168
+ var defaultRetryOnStatus = (s) => s === 408 || s === 429 || s === 500 || s === 502 || s === 503 || s === 504;
169
+ var backoffDelayMs = (attempt, base, cap) => {
170
+ const exp = base * Math.pow(2, attempt);
171
+ const lim = clamp(exp, 0, cap);
172
+ return Math.floor(Math.random() * lim);
173
+ };
174
+ var retryAfterMs = (headers) => {
175
+ const key = Object.keys(headers).find((k) => k.toLowerCase() === "retry-after");
176
+ if (!key) return void 0;
177
+ const v = _optionalChain([headers, 'access', _3 => _3[key], 'optionalAccess', _4 => _4.trim, 'call', _5 => _5()]);
178
+ if (!v) return void 0;
179
+ const secs = Number(v);
180
+ if (Number.isFinite(secs)) return Math.max(0, Math.floor(secs * 1e3));
181
+ const t = Date.parse(v);
182
+ if (Number.isFinite(t)) return Math.max(0, t - Date.now());
183
+ return void 0;
184
+ };
185
+ var withRetryStream = (p) => (next) => ((req) => {
186
+ const loop = (attempt) => _chunkTGOMLZ65js.asyncFold.call(void 0,
187
+ next(req),
188
+ (e) => {
189
+ if (e._tag === "Abort" || e._tag === "BadUrl") return _chunkTGOMLZ65js.asyncFail.call(void 0, e);
190
+ const canRetry = attempt < p.maxRetries && (_nullishCoalesce(p.retryOnError, () => ( defaultRetryOnError)))(e);
191
+ if (!canRetry) return _chunkTGOMLZ65js.asyncFail.call(void 0, e);
192
+ const d = backoffDelayMs(attempt, p.baseDelayMs, p.maxDelayMs);
193
+ return _chunkTGOMLZ65js.asyncFlatMap.call(void 0, sleepMs(d), () => loop(attempt + 1));
194
+ },
195
+ (w) => {
196
+ const canRetry = attempt < p.maxRetries && (_nullishCoalesce(p.retryOnStatus, () => ( defaultRetryOnStatus)))(w.status);
197
+ if (!canRetry) return _chunkTGOMLZ65js.asyncSucceed.call(void 0, w);
198
+ const ra = retryAfterMs(w.headers);
199
+ const d = _nullishCoalesce(ra, () => ( backoffDelayMs(attempt, p.baseDelayMs, p.maxDelayMs)));
200
+ return _chunkTGOMLZ65js.asyncFlatMap.call(void 0, sleepMs(d), () => loop(attempt + 1));
201
+ }
202
+ );
203
+ return loop(0);
204
+ });
205
+
206
+ // src/http/retry/retry.ts
207
+ var defaultRetryableMethods = ["GET", "HEAD", "OPTIONS"];
208
+ var defaultRetryOnStatus2 = (s) => s === 408 || s === 429 || s === 500 || s === 502 || s === 503 || s === 504;
209
+ var defaultRetryOnError2 = (e) => e._tag === "FetchError";
210
+ var clamp2 = (n, min, max) => Math.max(min, Math.min(max, n));
211
+ var backoffDelayMs2 = (attempt, base, cap) => {
212
+ const b = Math.max(0, base);
213
+ const c = Math.max(0, cap);
214
+ const exp = b * Math.pow(2, attempt);
215
+ const lim = clamp2(exp, 0, c);
216
+ return Math.floor(Math.random() * lim);
217
+ };
218
+ var headerCI = (h, name) => {
219
+ const k = Object.keys(h).find((x) => x.toLowerCase() === name.toLowerCase());
220
+ return k ? h[k] : void 0;
221
+ };
222
+ var retryAfterMs2 = (headers) => {
223
+ const v = _optionalChain([headerCI, 'call', _6 => _6(headers, "retry-after"), 'optionalAccess', _7 => _7.trim, 'call', _8 => _8()]);
224
+ if (!v) return void 0;
225
+ const secs = Number(v);
226
+ if (Number.isFinite(secs)) return Math.max(0, Math.floor(secs * 1e3));
227
+ const t = Date.parse(v);
228
+ if (Number.isFinite(t)) return Math.max(0, t - Date.now());
229
+ return void 0;
230
+ };
231
+ var withRetry = (p) => (next) => {
232
+ const retryOnMethods = _nullishCoalesce(p.retryOnMethods, () => ( defaultRetryableMethods));
233
+ const retryOnStatus = _nullishCoalesce(p.retryOnStatus, () => ( defaultRetryOnStatus2));
234
+ const retryOnError = _nullishCoalesce(p.retryOnError, () => ( defaultRetryOnError2));
235
+ const isMethodRetryable = (req) => retryOnMethods.includes(req.method);
236
+ const loop = (req, attempt) => {
237
+ if (!isMethodRetryable(req)) return next(req);
238
+ return _chunkTGOMLZ65js.asyncFold.call(void 0,
239
+ next(req),
240
+ (e) => {
241
+ if (e._tag === "Abort" || e._tag === "BadUrl") return _chunkTGOMLZ65js.asyncFail.call(void 0, e);
242
+ const canRetry = attempt < p.maxRetries && retryOnError(e);
243
+ if (!canRetry) return _chunkTGOMLZ65js.asyncFail.call(void 0, e);
244
+ const d = backoffDelayMs2(attempt, p.baseDelayMs, p.maxDelayMs);
245
+ return _chunkTGOMLZ65js.asyncFlatMap.call(void 0, sleepMs(d), () => loop(req, attempt + 1));
246
+ },
247
+ (w) => {
248
+ const canRetry = attempt < p.maxRetries && retryOnStatus(w.status);
249
+ if (!canRetry) return _chunkTGOMLZ65js.asyncSucceed.call(void 0, w);
250
+ const ra = retryAfterMs2(w.headers);
251
+ const d = _nullishCoalesce(ra, () => ( backoffDelayMs2(attempt, p.baseDelayMs, p.maxDelayMs)));
252
+ return _chunkTGOMLZ65js.asyncFlatMap.call(void 0, sleepMs(d), () => loop(req, attempt + 1));
253
+ }
254
+ );
255
+ };
256
+ return (req) => loop(req, 0);
257
+ };
258
+
259
+ // src/http/httpClient.ts
260
+ var resolveFinalUrl = (baseUrl, url) => {
261
+ try {
262
+ return new URL(url, _nullishCoalesce(baseUrl, () => ( ""))).toString();
263
+ } catch (e4) {
264
+ return (_nullishCoalesce(baseUrl, () => ( ""))) + url;
265
+ }
266
+ };
267
+ var createHttpCore = (cfg = {}) => {
268
+ const wire = makeHttp(cfg);
269
+ const withPromise = (eff) => _chunkTGOMLZ65js.withAsyncPromise.call(void 0, (e, env) => _chunkTGOMLZ65js.toPromise.call(void 0, e, env))(eff);
270
+ const requestRaw = (req) => wire(req);
271
+ const splitInit = (init) => {
272
+ const { headers, ...rest } = _nullishCoalesce(init, () => ( {}));
273
+ return {
274
+ headers: normalizeHeadersInit(headers),
275
+ init: rest
276
+ };
277
+ };
278
+ const applyInitHeaders = (headers) => (req) => headers ? mergeHeaders(headers)(req) : req;
279
+ const buildReq = (method, url, init, body) => {
280
+ const s = splitInit(init);
281
+ const req = {
282
+ method,
283
+ url,
284
+ ...body && body.length > 0 ? { body } : {},
285
+ init: s.init
286
+ };
287
+ return applyInitHeaders(s.headers)(req);
288
+ };
289
+ const toResponse = (w, body) => ({
290
+ status: w.status,
291
+ statusText: w.statusText,
292
+ headers: w.headers,
293
+ body
294
+ });
295
+ return {
296
+ cfg,
297
+ wire,
298
+ withPromise,
299
+ requestRaw,
300
+ splitInit,
301
+ applyInitHeaders,
302
+ buildReq,
303
+ toResponse
304
+ };
305
+ };
306
+ function httpClient(cfg = {}) {
307
+ const core = createHttpCore(cfg);
308
+ const make = (wire) => {
309
+ const requestRaw = (req) => wire(req);
310
+ const request = (req) => core.withPromise(requestRaw(req));
311
+ const get = (url, init) => request(core.buildReq("GET", url, init));
312
+ const post = (url, body, init) => request(core.buildReq("POST", url, init, body));
313
+ const getText = (url, init) => {
314
+ const req = core.buildReq("GET", url, init);
315
+ return core.withPromise(_chunkTGOMLZ65js.mapTryAsync.call(void 0, requestRaw(req), (w) => core.toResponse(w, w.bodyText)));
316
+ };
317
+ const getJson = (url, init) => {
318
+ const base = core.buildReq("GET", url, init);
319
+ const req = setHeaderIfMissing("accept", "application/json")(base);
320
+ return core.withPromise(_chunkTGOMLZ65js.mapTryAsync.call(void 0, requestRaw(req), (w) => core.toResponse(w, JSON.parse(w.bodyText))));
321
+ };
322
+ const postJson = (url, bodyObj, init) => {
323
+ const base = core.buildReq("POST", url, init, JSON.stringify(_nullishCoalesce(bodyObj, () => ( {}))));
324
+ const req = setHeaderIfMissing("content-type", "application/json")(
325
+ setHeaderIfMissing("accept", "application/json")(base)
326
+ );
327
+ return core.withPromise(
328
+ _chunkTGOMLZ65js.mapTryAsync.call(void 0, requestRaw(req), (w) => core.toResponse(w, JSON.parse(w.bodyText)))
329
+ );
330
+ };
331
+ return {
332
+ request,
333
+ get,
334
+ post,
335
+ getText,
336
+ getJson,
337
+ postJson,
338
+ with: (mw) => make(wire.with(mw)),
339
+ withRetry: (p) => make(wire.with(withRetry(p))),
340
+ wire
341
+ };
342
+ };
343
+ return make(core.wire);
344
+ }
345
+ function httpClientWithMeta(cfg = {}) {
346
+ const core = createHttpCore(cfg);
347
+ const mkMeta = (req, w, startedAt) => ({
348
+ request: req,
349
+ urlFinal: resolveFinalUrl(core.cfg.baseUrl, req.url),
350
+ startedAt,
351
+ durationMs: w.ms
352
+ });
353
+ const request = (req) => {
354
+ const startedAt = Date.now();
355
+ return core.withPromise(
356
+ _chunkTGOMLZ65js.mapTryAsync.call(void 0, core.requestRaw(req), (w) => ({
357
+ wire: w,
358
+ meta: mkMeta(req, w, startedAt)
359
+ }))
360
+ );
361
+ };
362
+ const get = (url, init) => {
363
+ const req = core.buildReq("GET", url, init);
364
+ return request(req);
365
+ };
366
+ const post = (url, body, init) => {
367
+ const req = core.buildReq("POST", url, init, body);
368
+ return request(req);
369
+ };
370
+ const postJson = (url, bodyObj, init) => {
371
+ const base = core.buildReq("POST", url, init, JSON.stringify(_nullishCoalesce(bodyObj, () => ( {}))));
372
+ const req = setHeaderIfMissing("content-type", "application/json")(
373
+ setHeaderIfMissing("accept", "application/json")(base)
374
+ );
375
+ const startedAt = Date.now();
376
+ return core.withPromise(
377
+ _chunkTGOMLZ65js.mapTryAsync.call(void 0, core.requestRaw(req), (w) => ({
378
+ wire: w,
379
+ response: core.toResponse(w, JSON.parse(w.bodyText)),
380
+ meta: mkMeta(req, w, startedAt)
381
+ }))
382
+ );
383
+ };
384
+ const getText = (url, init) => {
385
+ const req = core.buildReq("GET", url, init);
386
+ const startedAt = Date.now();
387
+ return core.withPromise(
388
+ _chunkTGOMLZ65js.mapTryAsync.call(void 0, core.requestRaw(req), (w) => ({
389
+ wire: w,
390
+ response: core.toResponse(w, w.bodyText),
391
+ meta: mkMeta(req, w, startedAt)
392
+ }))
393
+ );
394
+ };
395
+ const getJson = (url, init) => {
396
+ const base = core.buildReq("GET", url, init);
397
+ const req = setHeaderIfMissing("accept", "application/json")(base);
398
+ const startedAt = Date.now();
399
+ return core.withPromise(
400
+ _chunkTGOMLZ65js.mapTryAsync.call(void 0, core.requestRaw(req), (w) => ({
401
+ wire: w,
402
+ response: core.toResponse(w, JSON.parse(w.bodyText)),
403
+ meta: mkMeta(req, w, startedAt)
404
+ }))
405
+ );
406
+ };
407
+ return {
408
+ request,
409
+ // => { wire, meta }
410
+ get,
411
+ // => { wire, meta }
412
+ getText,
413
+ // => { wire, response(text), meta }
414
+ getJson,
415
+ // => { wire, response(json), meta }
416
+ post,
417
+ // => { wire, meta }
418
+ postJson
419
+ // => { wire, meta } (y además setea headers via optics)
420
+ };
421
+ }
422
+ function httpClientStream(cfg = {}) {
423
+ const wire = makeHttpStream(cfg);
424
+ const make = (w) => {
425
+ const withPromise = (eff) => _chunkTGOMLZ65js.withAsyncPromise.call(void 0, (e, env) => _chunkTGOMLZ65js.toPromise.call(void 0, e, env))(eff);
426
+ const request = (req) => withPromise(w(req));
427
+ const getStream = (url, init) => {
428
+ const base = { method: "GET", url, init };
429
+ const req = setHeaderIfMissing("accept", "*/*")(base);
430
+ return request(req);
431
+ };
432
+ return {
433
+ request,
434
+ getStream,
435
+ get: getStream,
436
+ with: (mw) => make(mw(w)),
437
+ withRetry: (p) => make(withRetryStream(p)(w)),
438
+ wire: w
439
+ };
440
+ };
441
+ return make(wire);
442
+ }
443
+
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+ exports.decorate = decorate; exports.httpClient = httpClient; exports.httpClientStream = httpClientStream; exports.httpClientWithMeta = httpClientWithMeta; exports.makeHttp = makeHttp; exports.makeHttpStream = makeHttpStream; exports.normalizeHeadersInit = normalizeHeadersInit; exports.withMiddleware = withMiddleware; exports.withRetryStream = withRetryStream;