UNPKG

next-http-proxy-middleware

Version:
2 lines (1 loc) 3.42 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var httpProxy=require("http-proxy");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var httpProxy__default=_interopDefaultLegacy(httpProxy),__assign=function(){return(__assign=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};function __rest(e,t){var r={};for(o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols)for(var n=0,o=Object.getOwnPropertySymbols(e);n<o.length;n++)t.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(e,o[n])&&(r[o[n]]=e[o[n]]);return r}function __awaiter(e,i,u,l){return new(u=u||Promise)(function(r,t){function n(e){try{a(l.next(e))}catch(e){t(e)}}function o(e){try{a(l.throw(e))}catch(e){t(e)}}function a(e){var t;e.done?r(e.value):((t=e.value)instanceof u?t:new u(function(e){e(t)})).then(n,o)}a((l=l.apply(e,i||[])).next())})}function __generator(r,n){var o,a,i,u={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},e={next:t(0),throw:t(1),return:t(2)};return"function"==typeof Symbol&&(e[Symbol.iterator]=function(){return this}),e;function t(t){return function(e){return function(t){if(o)throw new TypeError("Generator is already executing.");for(;u;)try{if(o=1,a&&(i=2&t[0]?a.return:t[0]?a.throw||((i=a.return)&&i.call(a),0):a.next)&&!(i=i.call(a,t[1])).done)return i;switch(a=0,(t=i?[2&t[0],i.value]:t)[0]){case 0:case 1:i=t;break;case 4:return u.label++,{value:t[1],done:!1};case 5:u.label++,a=t[1],t=[0];continue;case 7:t=u.ops.pop(),u.trys.pop();continue;default:if(!(i=0<(i=u.trys).length&&i[i.length-1])&&(6===t[0]||2===t[0])){u=0;continue}if(3===t[0]&&(!i||t[1]>i[0]&&t[1]<i[3])){u.label=t[1];break}if(6===t[0]&&u.label<i[1]){u.label=i[1],i=t;break}if(i&&u.label<i[2]){u.label=i[2],u.ops.push(t);break}i[2]&&u.ops.pop(),u.trys.pop();continue}t=n.call(r,u)}catch(e){t=[6,e],a=0}finally{o=i=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}([t,e])}}}var hasRequestBodyMethods=["HEAD","POST","PUT","DELETE","CONNECT","OPTIONS","PATCH"],rewritePath=function(e,t){if(Array.isArray(t))for(var r=0,n=t;r<n.length;r++){var o=n[r],a=o.patternStr,o=o.replaceStr;if((i=RegExp(a)).test(e))return e.replace(i,o)}else for(var a in console.warn("[next-http-proxy-middleware] Use array instead of object for `pathRewrite` value (related issue: https://github.com/stegano/next-http-proxy-middleware/issues/39)"),t){var i=RegExp(a),u=t[a];if(i.test(e))return e.replace(i,u)}return e},httpProxyMiddleware=function(i,u,l){return void 0===l&&(l={}),__awaiter(void 0,void 0,void 0,function(){return __generator(this,function(e){return[2,new Promise(function(e,t){var r=l.pathRewrite,n=l.onProxyInit,o=__rest(l,["pathRewrite","onProxyInit"]),a=httpProxy__default.default.createProxy();"function"==typeof n&&n(a),r&&(i.url=rewritePath(i.url,r)),0<=hasRequestBodyMethods.indexOf(i.method)&&"object"==typeof i.body&&(i.body=JSON.stringify(i.body)),a.once("proxyReq",function(e,t){0<=hasRequestBodyMethods.indexOf(t.method)&&"string"==typeof t.body&&(e.write(t.body),e.end())}).once("proxyRes",e).once("error",t).web(i,u,__assign({changeOrigin:!0},o))})]})})};exports.default=httpProxyMiddleware,exports.rewritePath=rewritePath;