createRequester-VFTkDs_7.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { processOptions, validateOptions } from "./defaultOptionsValidator-DdN0wke7.js";
  2. const middlewareReducer = (middleware) => function(hook, defaultValue, ...args) {
  3. const bailEarly = hook === "onError";
  4. let value = defaultValue;
  5. for (let i = 0; i < middleware[hook].length; i++) {
  6. const handler = middleware[hook][i];
  7. if (value = handler(value, ...args), bailEarly && !value)
  8. break;
  9. }
  10. return value;
  11. };
  12. function createPubSub() {
  13. const subscribers = /* @__PURE__ */ Object.create(null);
  14. let nextId = 0;
  15. function subscribe(subscriber) {
  16. const id = nextId++;
  17. return subscribers[id] = subscriber, function() {
  18. delete subscribers[id];
  19. };
  20. }
  21. function publish(event) {
  22. for (const id in subscribers)
  23. subscribers[id](event);
  24. }
  25. return {
  26. publish,
  27. subscribe
  28. };
  29. }
  30. const channelNames = [
  31. "request",
  32. "response",
  33. "progress",
  34. "error",
  35. "abort"
  36. ], middlehooks = [
  37. "processOptions",
  38. "validateOptions",
  39. "interceptRequest",
  40. "finalizeOptions",
  41. "onRequest",
  42. "onResponse",
  43. "onError",
  44. "onReturn",
  45. "onHeaders"
  46. ];
  47. function createRequester(initMiddleware, httpRequest) {
  48. const loadedMiddleware = [], middleware = middlehooks.reduce(
  49. (ware, name) => (ware[name] = ware[name] || [], ware),
  50. {
  51. processOptions: [processOptions],
  52. validateOptions: [validateOptions]
  53. }
  54. );
  55. function request(opts) {
  56. const onResponse = (reqErr, res, ctx) => {
  57. let error = reqErr, response = res;
  58. if (!error)
  59. try {
  60. response = applyMiddleware("onResponse", res, ctx);
  61. } catch (err) {
  62. response = null, error = err;
  63. }
  64. error = error && applyMiddleware("onError", error, ctx), error ? channels.error.publish(error) : response && channels.response.publish(response);
  65. }, channels = channelNames.reduce((target, name) => (target[name] = createPubSub(), target), {}), applyMiddleware = middlewareReducer(middleware), options = applyMiddleware("processOptions", opts);
  66. applyMiddleware("validateOptions", options);
  67. const context = { options, channels, applyMiddleware };
  68. let ongoingRequest;
  69. const unsubscribe = channels.request.subscribe((ctx) => {
  70. ongoingRequest = httpRequest(ctx, (err, res) => onResponse(err, res, ctx));
  71. });
  72. channels.abort.subscribe(() => {
  73. unsubscribe(), ongoingRequest && ongoingRequest.abort();
  74. });
  75. const returnValue = applyMiddleware("onReturn", channels, context);
  76. return returnValue === channels && channels.request.publish(context), returnValue;
  77. }
  78. return request.use = function(newMiddleware) {
  79. if (!newMiddleware)
  80. throw new Error("Tried to add middleware that resolved to falsey value");
  81. if (typeof newMiddleware == "function")
  82. throw new Error(
  83. "Tried to add middleware that was a function. It probably expects you to pass options to it."
  84. );
  85. if (newMiddleware.onReturn && middleware.onReturn.length > 0)
  86. throw new Error(
  87. "Tried to add new middleware with `onReturn` handler, but another handler has already been registered for this event"
  88. );
  89. return middlehooks.forEach((key) => {
  90. newMiddleware[key] && middleware[key].push(newMiddleware[key]);
  91. }), loadedMiddleware.push(newMiddleware), request;
  92. }, request.clone = () => createRequester(loadedMiddleware, httpRequest), initMiddleware.forEach(request.use), request;
  93. }
  94. export {
  95. createRequester
  96. };
  97. //# sourceMappingURL=createRequester-VFTkDs_7.js.map