UNPKG

fetch-http-message

Version:

Generate an http message string using the fetch API

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