transport.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.normalizeHeaders = normalizeHeaders;
  4. exports.createFetchWithInit = createFetchWithInit;
  5. /**
  6. * Normalizes HeadersInit to a plain Record<string, string> for manipulation.
  7. * Handles Headers objects, arrays of tuples, and plain objects.
  8. */
  9. function normalizeHeaders(headers) {
  10. if (!headers)
  11. return {};
  12. if (headers instanceof Headers) {
  13. return Object.fromEntries(headers.entries());
  14. }
  15. if (Array.isArray(headers)) {
  16. return Object.fromEntries(headers);
  17. }
  18. return { ...headers };
  19. }
  20. /**
  21. * Creates a fetch function that includes base RequestInit options.
  22. * This ensures requests inherit settings like credentials, mode, headers, etc. from the base init.
  23. *
  24. * @param baseFetch - The base fetch function to wrap (defaults to global fetch)
  25. * @param baseInit - The base RequestInit to merge with each request
  26. * @returns A wrapped fetch function that merges base options with call-specific options
  27. */
  28. function createFetchWithInit(baseFetch = fetch, baseInit) {
  29. if (!baseInit) {
  30. return baseFetch;
  31. }
  32. // Return a wrapped fetch that merges base RequestInit with call-specific init
  33. return async (url, init) => {
  34. const mergedInit = {
  35. ...baseInit,
  36. ...init,
  37. // Headers need special handling - merge instead of replace
  38. headers: init?.headers ? { ...normalizeHeaders(baseInit.headers), ...normalizeHeaders(init.headers) } : baseInit.headers
  39. };
  40. return baseFetch(url, mergedInit);
  41. };
  42. }
  43. //# sourceMappingURL=transport.js.map