"use strict";var ServerResponseCode;Object.defineProperty(exports,"__esModule",{value:!0}),exports.ServerResponseCode=void 0,function(e){e[e.SUCCESS=0]="SUCCESS",e[e.SESSION_EXPIRE_CODE=34]="SESSION_EXPIRE_CODE",e[e.PASSPORT_SESSION_EXPIRE_CODE=4401]="PASSPORT_SESSION_EXPIRE_CODE",e[e.NOT_LOGIN=4]="NOT_LOGIN",e[e.COMPILE_ERROR=2200]="COMPILE_ERROR"}(ServerResponseCode=exports.ServerResponseCode||(exports.ServerResponseCode={}));class Middleware{static urlParams(e){return e.url&&e.url.includes("/:")&&(e.url=e.url.replace(/\/:(\w+)/g,(r,s)=>{var t,o,a;const d=(null===(t=e.data)||void 0===t?void 0:t[s])||(null===(o=e.params)||void 0===o?void 0:o[s]);return null===(a=e.data)||void 0===a||delete a[s],"/"+d})),e}static queryParams(e){return"get"===e.method&&e.data&&(e.params=e.data,delete e.data),e}static isSessionExpired(e){return r=>{var s,t;return([ServerResponseCode.SESSION_EXPIRE_CODE,ServerResponseCode.PASSPORT_SESSION_EXPIRE_CODE].includes(r.data.code)||r.data.code===ServerResponseCode.NOT_LOGIN&&(null===(s=null==e?void 0:e.hooks)||void 0===s?void 0:s.checkLoginStatus()))&&(null===(t=null==e?void 0:e.hooks)||void 0===t||t.sessionExpired()),r}}static formatResponse(e){return r=>{if(e.responseHeader)return r;const s=e.responseCode?r.data:r.data.data;return r.headers.tt_stable&&Object.assign(s,{stable:r.headers.tt_stable}),e.logId?(Object.assign(s,{logId:r.headers["x-tt-logid"]}),s):s}}static parseCookie(e){var r;const s=(null===(r=null==e?void 0:e.headers)||void 0===r?void 0:r["set-cookie"])||[],t=/(.+?)=([^;]+)/;return e&&(e.setCookie=s.reduce((e,r)=>{const s=t.exec(r);return s&&s[1]&&s[2]&&(e[s[1]]=s[2]),e},{})),e}}Middleware.logger={request:e=>r=>(e("info","request","start",r),r),response:e=>r=>(e("info","request","finish",r.status,r.headers,r.data),r)},exports.default=Middleware;