@cedvict/http-guardian 0.0.1-next.11 → 0.0.1-next.12

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/index.js CHANGED
@@ -223,7 +223,8 @@ function makeCacheKey(parts) {
223
223
  u: parts.url,
224
224
  h: headerObj,
225
225
  b: parts.body ?? null,
226
- i: parts.idempotencyKey ?? null
226
+ i: parts.idempotencyKey ?? null,
227
+ r: parts.responseType ?? "auto"
227
228
  });
228
229
  }
229
230
 
@@ -373,6 +374,8 @@ function createHttpClient(options) {
373
374
  const pipeline = composeGuards(baseGuards, terminal);
374
375
  async function request(method, path, ro) {
375
376
  const requestUrl = withQuery(joinUrl(draft.baseUrl, path), ro?.query);
377
+ const responseType = ro?.responseType ?? "auto";
378
+ const expectsBinary = isBinaryResponseType(responseType);
376
379
  const headers = createHeaders();
377
380
  if (draft.defaults?.headers) for (const [k, v] of Object.entries(draft.defaults.headers)) headers.set(k, v);
378
381
  if (ro?.headers) {
@@ -382,15 +385,15 @@ function createHttpClient(options) {
382
385
  let bodyForKey = void 0;
383
386
  if (ro?.json !== void 0) {
384
387
  if (!headers.has("content-type")) headers.set("content-type", "application/json");
385
- if (!headers.has("accept")) headers.set("accept", "application/json");
388
+ if (!headers.has("accept") && !expectsBinary) headers.set("accept", "application/json");
386
389
  body = JSON.stringify(ro.json);
387
390
  bodyForKey = ro.json;
388
391
  } else if (ro?.body !== void 0) {
389
392
  body = ro.body;
390
393
  bodyForKey = "[body]";
391
- if (!headers.has("accept")) headers.set("accept", "application/json");
394
+ if (!headers.has("accept") && !expectsBinary) headers.set("accept", "application/json");
392
395
  } else {
393
- if (!headers.has("accept")) headers.set("accept", "application/json");
396
+ if (!headers.has("accept") && !expectsBinary) headers.set("accept", "application/json");
394
397
  }
395
398
  const timeoutMs = ro?.timeoutMs ?? draft.defaults?.timeoutMs;
396
399
  const notifier = ro?.notifier ?? defaultNotifier;
@@ -409,6 +412,7 @@ function createHttpClient(options) {
409
412
  headers,
410
413
  ...method === "GET" ? {} : { body: bodyForKey },
411
414
  ...ro?.idempotencyKey !== void 0 ? { idempotencyKey: ro.idempotencyKey } : {},
415
+ responseType,
412
416
  ...cacheOpts?.key !== void 0 ? { keyOverride: cacheOpts.key } : {}
413
417
  }) : void 0;
414
418
  if (inflightKey) {
@@ -449,7 +453,7 @@ function createHttpClient(options) {
449
453
  ...timeoutMs !== void 0 ? { timeoutMs } : {},
450
454
  ...ro?.noAuth ? { noAuth: true } : {}
451
455
  });
452
- const raw = await decodeResponse(res);
456
+ const raw = await decodeResponse(res, responseType);
453
457
  const envelope = { kind: "network", url: res.url || requestUrl, status: res.status, headers: res.headers, raw };
454
458
  if (wantsCache && cacheOpts && res.ok) {
455
459
  const key = inflightKey;
@@ -475,7 +479,7 @@ function createHttpClient(options) {
475
479
  ...ro?.noAuth ? { noAuth: true } : {}
476
480
  });
477
481
  if (!res2.ok) return;
478
- const raw2 = await decodeResponse(res2);
482
+ const raw2 = await decodeResponse(res2, responseType);
479
483
  cache.set(key, {
480
484
  expiresAt: Date.now() + cacheOpts.ttlMs,
481
485
  value: raw2,
@@ -603,8 +607,25 @@ function makeCtx(args, redirect) {
603
607
  _fetch: args.resolvedFetch
604
608
  };
605
609
  }
606
- async function decodeResponse(res) {
610
+ function isBinaryResponseType(responseType) {
611
+ return responseType === "blob" || responseType === "arrayBuffer";
612
+ }
613
+ async function decodeResponse(res, responseType = "auto") {
607
614
  if (res.status === 204) return null;
615
+ if (res.ok && responseType === "blob") {
616
+ try {
617
+ return await res.blob();
618
+ } catch (e) {
619
+ throw new ParseError("Failed to read Blob response", void 0, e);
620
+ }
621
+ }
622
+ if (res.ok && responseType === "arrayBuffer") {
623
+ try {
624
+ return await res.arrayBuffer();
625
+ } catch (e) {
626
+ throw new ParseError("Failed to read ArrayBuffer response", void 0, e);
627
+ }
628
+ }
608
629
  const ct = res.headers.get("content-type") ?? "";
609
630
  if (ct.includes("application/json")) {
610
631
  try {
@@ -629,9 +650,10 @@ function asClientResult(envelope, requestUrl, opts, ro, notifier, shouldNotify,
629
650
  const raw = envelope.raw;
630
651
  const resHeaders = envelope.headers ?? new Headers();
631
652
  const finalUrl = envelope.url ?? requestUrl;
632
- const okByParser = opts.parser.isSuccess(raw, status);
653
+ const binaryResponse = isBinaryResponseType(ro?.responseType);
654
+ const okByParser = binaryResponse ? status >= 200 && status < 400 : opts.parser.isSuccess(raw, status);
633
655
  if (okByParser && status >= 200 && status < 400) {
634
- const dataRaw = opts.parser.getData(raw);
656
+ const dataRaw = binaryResponse ? raw : opts.parser.getData(raw);
635
657
  try {
636
658
  const data = ro?.dataSchema ? ro.dataSchema.parse(dataRaw) : dataRaw;
637
659
  return { ok: true, status, headers: resHeaders, data, raw, url: finalUrl };