fetch-http-message
Version:
Generate an http message string using the fetch API
3 lines (2 loc) • 1.75 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).fetchHttpMessage=t()}(this,(function(){"use strict";function e(e,t){return null!=t&&"undefined"!=typeof Symbol&&t[Symbol.hasInstance]?!!t[Symbol.hasInstance](e):e instanceof t}var t="undefined"!=typeof Headers,n="undefined"!=typeof Blob,o="undefined"!=typeof Buffer,r="undefined"!=typeof URLSearchParams;function i(e){return"object"==(void 0===e?"undefined":(t=e)&&"undefined"!=typeof Symbol&&t.constructor===Symbol?"symbol":typeof t);var t}return function(f,d){if(void 0===f)throw new Error("Input is expected");var a;void 0===d&&(d={}),i(f)?a=f.url:(a=f,f={});var s=d.method||f.method||"GET";s=s.toUpperCase();var u=["".concat(s," ").concat(a," HTTP/1.1")],c=d.headers||f.headers;if(void 0!==c)if(t&&e(c,Headers)){var l=!0,p=!1,h=void 0;try{for(var y,b=c.entries()[Symbol.iterator]();!(l=(y=b.next()).done);l=!0){var v=y.value;u.push("".concat(v[0],": ").concat(v[1]))}}catch(e){p=!0,h=e}finally{try{l||null==b.return||b.return()}finally{if(p)throw h}}}else for(var m in c)u.push("".concat(m,": ").concat(c[m]));var S,w,T,g=d.body;if(void 0!==g){if(~["GET","HEAD"].indexOf(s))throw new Error("Option body not valid with method ".concat(s));if(n&&e(g,Blob))u.push(""),u.push((S=g,w=URL.createObjectURL(S),(T=new XMLHttpRequest).open("GET",w,!1),T.send(),URL.revokeObjectURL(w),T.responseText));else{if(!("string"==typeof g||e(g,String)||o&&e(g,Buffer)||r&&e(g,URLSearchParams)))throw new Error("Option body should be convertible to a string");u.push(""),u.push(g.toString())}}return u.join("\r\n")}}));
//# sourceMappingURL=fetch-http-message.min.cjs.map