{"version":3,"sources":["../../src/express/utils.js"],"sourcesContent":["// Adapted from https://github.com/AlbertoFdzM/express-list-endpoints/blob/305535d43008b46f34e18b01947762e039af6d2d/src/index.js\n// and also incorporated changes from https://github.com/AlbertoFdzM/express-list-endpoints/pull/96\n\n/**\n * @typedef {Object} Route\n * @property {Object} methods\n * @property {string | string[]} path\n * @property {any[]} stack\n *\n * @typedef {Object} Endpoint\n * @property {string} path Path name\n * @property {string[]} methods Methods handled\n * @property {string[]} middlewares Mounted middlewares\n */\n\nconst regExpToParseExpressPathRegExp =\n  /^\\/\\^\\\\?\\/?(?:(:?[\\w\\\\.-]*(?:\\\\\\/:?[\\w\\\\.-]*)*)|(\\(\\?:\\\\?\\/?\\([^)]+\\)\\)))\\\\\\/.*/;\nconst regExpToReplaceExpressPathRegExpParams = /\\(\\?:\\\\?\\/?\\([^)]+\\)\\)/;\nconst regexpExpressParamRegexp = /\\(\\?:\\\\?\\\\?\\/?\\([^)]+\\)\\)/g;\nconst regexpExpressPathParamRegexp = /(:[^)]+)\\([^)]+\\)/g;\n\nconst EXPRESS_ROOT_PATH_REGEXP_VALUE = \"/^\\\\/?(?=\\\\/|$)/i\";\nconst STACK_ITEM_VALID_NAMES = [\"router\", \"bound dispatch\", \"mounted_app\"];\n\n/**\n * Returns all the verbs detected for the passed route\n * @param {Route} route\n */\nconst getRouteMethods = function (route) {\n  let methods = Object.keys(route.methods);\n\n  methods = methods.filter((method) => method !== \"_all\");\n  methods = methods.map((method) => method.toUpperCase());\n\n  return methods;\n};\n\n/**\n * Returns the names (or anonymous) of all the middlewares attached to the\n * passed route\n * @param {Route} route\n * @returns {string[]}\n */\nconst getRouteMiddlewares = function (route) {\n  return route.stack.map((item) => {\n    return item.handle.name || \"anonymous\";\n  });\n};\n\n/**\n * Returns true if found regexp related with express params\n * @param {string} expressPathRegExp\n * @returns {boolean}\n */\nconst hasParams = function (expressPathRegExp) {\n  return regexpExpressParamRegexp.test(expressPathRegExp);\n};\n\n/**\n * @param {Route} route Express route object to be parsed\n * @param {string} basePath The basePath the route is on\n * @return {Endpoint[]} Endpoints info\n */\nconst parseExpressRoute = function (route, basePath) {\n  const paths = [];\n\n  if (Array.isArray(route.path)) {\n    paths.push(...route.path);\n  } else {\n    paths.push(route.path);\n  }\n\n  /** @type {Endpoint[]} */\n  const endpoints = paths.map((path) => {\n    const completePath =\n      basePath && path === \"/\" ? basePath : `${basePath}${path}`;\n\n    /** @type {Endpoint} */\n    const endpoint = {\n      path: completePath.replace(regexpExpressPathParamRegexp, \"$1\"),\n      methods: getRouteMethods(route),\n      middlewares: getRouteMiddlewares(route),\n    };\n\n    return endpoint;\n  });\n\n  return endpoints;\n};\n\n/**\n * @param {RegExp} expressPathRegExp\n * @param {any[]} params\n * @returns {string}\n */\nexport const parseExpressPath = function (expressPathRegExp, params) {\n  let parsedRegExp = expressPathRegExp.toString();\n  let expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);\n  let paramIndex = 0;\n\n  while (hasParams(parsedRegExp)) {\n    const paramName = params[paramIndex].name;\n    const paramId = `:${paramName}`;\n\n    parsedRegExp = parsedRegExp.replace(\n      regExpToReplaceExpressPathRegExpParams,\n      (str) => {\n        // Express >= 4.20.0 uses a different RegExp for parameters: it\n        // captures the slash as part of the parameter. We need to check\n        // for this case and add the slash to the value that will replace\n        // the parameter in the path.\n        if (str.startsWith(\"(?:\\\\/\")) {\n          return `\\\\/${paramId}`;\n        }\n\n        return paramId;\n      },\n    );\n\n    paramIndex++;\n  }\n\n  if (parsedRegExp !== expressPathRegExp.toString()) {\n    expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);\n  }\n\n  const parsedPath = expressPathRegExpExec[1].replace(/\\\\\\//g, \"/\");\n\n  return parsedPath;\n};\n\n/**\n * @param {import('express').Express | import('express').Router | any} app\n * @param {string} [basePath]\n * @param {Endpoint[]} [endpoints]\n * @returns {Endpoint[]}\n */\nconst parseEndpoints = function (app, basePath, endpoints) {\n  const stack = app.stack || (app._router && app._router.stack);\n\n  endpoints = endpoints || [];\n  basePath = basePath || \"\";\n\n  if (!stack) {\n    if (endpoints.length) {\n      endpoints = addEndpoints(endpoints, [\n        {\n          path: basePath,\n          methods: [],\n          middlewares: [],\n        },\n      ]);\n    }\n  } else {\n    endpoints = parseStack(stack, basePath, endpoints);\n  }\n\n  return endpoints;\n};\n\n/**\n * Ensures the path of the new endpoints isn't yet in the array.\n * If the path is already in the array merges the endpoints with the existing\n * one, if not, it adds them to the array.\n *\n * @param {Endpoint[]} currentEndpoints Array of current endpoints\n * @param {Endpoint[]} endpointsToAdd New endpoints to be added to the array\n * @returns {Endpoint[]} Updated endpoints array\n */\nconst addEndpoints = function (currentEndpoints, endpointsToAdd) {\n  endpointsToAdd.forEach((newEndpoint) => {\n    const existingEndpoint = currentEndpoints.find(\n      (endpoint) => endpoint.path === newEndpoint.path,\n    );\n\n    if (existingEndpoint !== undefined) {\n      const newMethods = newEndpoint.methods.filter(\n        (method) => !existingEndpoint.methods.includes(method),\n      );\n\n      existingEndpoint.methods = existingEndpoint.methods.concat(newMethods);\n    } else {\n      currentEndpoints.push(newEndpoint);\n    }\n  });\n\n  return currentEndpoints;\n};\n\n/**\n * @param {any[]} stack\n * @param {string} basePath\n * @param {Endpoint[]} endpoints\n * @returns {Endpoint[]}\n */\nconst parseStack = function (stack, basePath, endpoints) {\n  stack.forEach((stackItem) => {\n    if (stackItem.route) {\n      const newEndpoints = parseExpressRoute(stackItem.route, basePath);\n\n      endpoints = addEndpoints(endpoints, newEndpoints);\n    } else if (STACK_ITEM_VALID_NAMES.includes(stackItem.name)) {\n      const isExpressPathRegexp = regExpToParseExpressPathRegExp.test(\n        stackItem.regexp,\n      );\n\n      let newBasePath = basePath;\n\n      if (isExpressPathRegexp) {\n        const parsedPath = parseExpressPath(stackItem.regexp, stackItem.keys);\n\n        newBasePath += `/${parsedPath}`;\n      } else if (\n        !stackItem.path &&\n        stackItem.regexp &&\n        stackItem.regexp.toString() !== EXPRESS_ROOT_PATH_REGEXP_VALUE\n      ) {\n        const regExpPath = ` RegExp(${stackItem.regexp}) `;\n\n        newBasePath += `/${regExpPath}`;\n      }\n\n      endpoints = parseEndpoints(stackItem.handle, newBasePath, endpoints);\n    }\n  });\n\n  return endpoints;\n};\n\nexport const getEndpoints = function (app, basePath) {\n  const endpoints = parseEndpoints(app);\n  return endpoints.flatMap((route) =>\n    route.methods\n      .filter((method) => ![\"HEAD\", \"OPTIONS\"].includes(method.toUpperCase()))\n      .map((method) => ({\n        method,\n        path: (basePath + route.path).replace(/\\/\\//g, \"/\"),\n      })),\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;AAeA,IAAMA,iCACJ;AACF,IAAMC,yCAAyC;AAC/C,IAAMC,2BAA2B;AACjC,IAAMC,+BAA+B;AAErC,IAAMC,iCAAiC;AACvC,IAAMC,yBAAyB;EAAC;EAAU;EAAkB;;AAM5D,IAAMC,kBAAkB,gCAAUC,OAAK;AACrC,MAAIC,UAAUC,OAAOC,KAAKH,MAAMC,OAAO;AAEvCA,YAAUA,QAAQG,OAAO,CAACC,WAAWA,WAAW,MAAA;AAChDJ,YAAUA,QAAQK,IAAI,CAACD,WAAWA,OAAOE,YAAW,CAAA;AAEpD,SAAON;AACT,GAPwB;AAexB,IAAMO,sBAAsB,gCAAUR,OAAK;AACzC,SAAOA,MAAMS,MAAMH,IAAI,CAACI,SAAAA;AACtB,WAAOA,KAAKC,OAAOC,QAAQ;EAC7B,CAAA;AACF,GAJ4B;AAW5B,IAAMC,YAAY,gCAAUC,mBAAiB;AAC3C,SAAOnB,yBAAyBoB,KAAKD,iBAAAA;AACvC,GAFkB;AASlB,IAAME,oBAAoB,gCAAUhB,OAAOiB,UAAQ;AACjD,QAAMC,QAAQ,CAAA;AAEd,MAAIC,MAAMC,QAAQpB,MAAMqB,IAAI,GAAG;AAC7BH,UAAMI,KAAI,GAAItB,MAAMqB,IAAI;EAC1B,OAAO;AACLH,UAAMI,KAAKt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AtC,kBAAYiB,eAAegB,UAAU7C,QAAQiD,aAAarC,SAAAA;IAC5D;EACF,CAAA;AAEA,SAAOA;AACT,GAhCmB;AAkCZ,IAAMuC,eAAe,gCAAUrB,KAAKxB,UAAQ;AACjD,QAAMM,YAAYiB,eAAeC,GAAAA;AACjC,SAAOlB,UAAUwC,QAAQ,CAAC/D,UACxBA,MAAMC,QACHG,OAAO,CAACC,WAAW,CAAC;IAAC;IAAQ;IAAWiD,SAASjD,OAAOE,YAAW,CAAA,CAAA,EACnED,IAAI,CAACD,YAAY;IAChBA;IACAgB,OAAOJ,WAAWjB,MAAMqB,MAAMK,QAAQ,SAAS,GAAA;EACjD,EAAA,CAAA;AAEN,GAV4B;","names":["regExpToParseExpressPathRegExp","regExpToReplaceExpressPathRegExpParams","regexpExpressParamRegexp","regexpExpressPathParamRegexp","EXPRESS_ROOT_PATH_REGEXP_VALUE","STACK_ITEM_VALID_NAMES","getRouteMethods","route","methods","Object","keys","filter","method","map","toUpperCase","getRouteMiddlewares","stack","item","handle","name","hasParams","expressPathRegExp","test","parseExpressRoute","basePath","paths","Array","isArray","path","push","endpoints","completePath","endpoint","replace","middlewares","parseExpressPath","params","parsedRegExp","toString","expressPathRegExpExec","exec","paramIndex","paramName","paramId","str","startsWith","parsedPath","parseEndpoints","app","_router","length","addEndpoints","parseStack","currentEndpoints","endpointsToAdd","forEach","newEndpoint","existingEndpoint","find","undefined","newMethods","includes","concat","stackItem","newEndpoints","isExpressPathRegexp","regexp","newBasePath","regExpPath","getEndpoints","flatMap"]}