@prairielearn/express-list-endpoints 1.0.11 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -31,7 +31,7 @@ function hasParams(expressPathRegExp) {
|
|
|
31
31
|
/**
|
|
32
32
|
* @param route Express route object to be parsed
|
|
33
33
|
* @param basePath The basePath the route is on
|
|
34
|
-
* @
|
|
34
|
+
* @returns Endpoints info
|
|
35
35
|
*/
|
|
36
36
|
function parseExpressRoute(route, basePath) {
|
|
37
37
|
const paths = [];
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,MAAM,8BAA8B,GAClC,iFAAiF,CAAC;AACpF,MAAM,sCAAsC,GAAG,wBAAwB,CAAC;AACxE,MAAM,wBAAwB,GAAG,4BAA4B,CAAC;AAC9D,MAAM,4BAA4B,GAAG,oBAAoB,CAAC;AAE1D,MAAM,8BAA8B,GAAG,mBAAmB,CAAC;AAC3D,MAAM,sBAAsB,GAAG,CAAC,QAAQ,EAAE,gBAAgB,EAAE,aAAa,CAAC,CAAC;AAE3E;;GAEG;AACH,SAAS,eAAe,CAAC,KAAY;IACnC,IAAI,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAEzC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC;IACxD,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC;IAExD,OAAO,OAAO,CA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string[];\n stack: any[];\n}\n\nexport interface Endpoint {\n path: string;\n methods: string[];\n middlewares: string[];\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 */\nfunction getRouteMethods(route: 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 */\nfunction getRouteMiddlewares(route: Route): string[] {\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 */\nfunction hasParams(expressPathRegExp: string): boolean {\n return regexpExpressParamRegexp.test(expressPathRegExp);\n}\n\n/**\n * @param route Express route object to be parsed\n * @param basePath The basePath the route is on\n * @return Endpoints info\n */\nfunction parseExpressRoute(route: Route, basePath: string): Endpoint[] {\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 const endpoints: Endpoint[] = paths.map((path) => {\n const completePath = basePath && path === '/' ? basePath : `${basePath}${path}`;\n\n const endpoint: 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\nfunction parseExpressPath(expressPathRegExp: RegExp, params: any[]): string {\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(regExpToReplaceExpressPathRegExpParams, (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 paramIndex++;\n }\n\n if (parsedRegExp !== expressPathRegExp.toString()) {\n expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);\n }\n\n // TODO: can we do better than this?\n if (!expressPathRegExpExec) {\n throw new Error('Error parsing express path');\n }\n\n const parsedPath = expressPathRegExpExec[1].replace(/\\\\\\//g, '/');\n\n return parsedPath;\n}\n\nfunction parseEndpoints(\n app: Express | Router | any,\n basePath?: string,\n endpoints?: Endpoint[],\n): Endpoint[] {\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 currentEndpoints Array of current endpoints\n * @param endpointsToAdd New endpoints to be added to the array\n * @returns Updated endpoints array\n */\nfunction addEndpoints(currentEndpoints: Endpoint[], endpointsToAdd: Endpoint[]): Endpoint[] {\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\nfunction parseStack(stack: any[], basePath: string, endpoints: Endpoint[]): Endpoint[] {\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(stackItem.regexp);\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\n/**\n * Returns an array of strings with all the detected endpoints\n * @param app The express/router instance to get the endpoints from\n * @returns The array of endpoints\n */\nfunction expressListEndpoints(app: Express | Router | any): Endpoint[] {\n const endpoints = parseEndpoints(app);\n\n return endpoints;\n}\n\nexport default expressListEndpoints;\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,MAAM,8BAA8B,GAClC,iFAAiF,CAAC;AACpF,MAAM,sCAAsC,GAAG,wBAAwB,CAAC;AACxE,MAAM,wBAAwB,GAAG,4BAA4B,CAAC;AAC9D,MAAM,4BAA4B,GAAG,oBAAoB,CAAC;AAE1D,MAAM,8BAA8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AS,CAAC,MAAM,IAAI,CAAC;gBAEnD,WAAW,IAAI,IAAI,UAAU,EAAE,CAAC;YAClC,CAAC;YAED,SAAS,GAAG,cAAc,CAAC,SAAS,CAAC,MAAM,EAAE,WAAW,EAAE,SAAS,CAAC,CAAC;QACvE,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;GAIG;AACH,SAAS,oBAAoB,CAAC,GAA2B;IACvD,MAAM,SAAS,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC;IAEtC,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,eAAe,oBAAoB,CAAC","sourcesContent":["import type { Express, Router } from 'express';\n\ninterface Route {\n methods: object;\n path: string | string[];\n stack: any[];\n}\n\nexport interface Endpoint {\n path: string;\n methods: string[];\n middlewares: string[];\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 */\nfunction getRouteMethods(route: 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 */\nfunction getRouteMiddlewares(route: Route): string[] {\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 */\nfunction hasParams(expressPathRegExp: string): boolean {\n return regexpExpressParamRegexp.test(expressPathRegExp);\n}\n\n/**\n * @param route Express route object to be parsed\n * @param basePath The basePath the route is on\n * @returns Endpoints info\n */\nfunction parseExpressRoute(route: Route, basePath: string): Endpoint[] {\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 const endpoints: Endpoint[] = paths.map((path) => {\n const completePath = basePath && path === '/' ? basePath : `${basePath}${path}`;\n\n const endpoint: 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\nfunction parseExpressPath(expressPathRegExp: RegExp, params: any[]): string {\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(regExpToReplaceExpressPathRegExpParams, (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 paramIndex++;\n }\n\n if (parsedRegExp !== expressPathRegExp.toString()) {\n expressPathRegExpExec = regExpToParseExpressPathRegExp.exec(parsedRegExp);\n }\n\n // TODO: can we do better than this?\n if (!expressPathRegExpExec) {\n throw new Error('Error parsing express path');\n }\n\n const parsedPath = expressPathRegExpExec[1].replace(/\\\\\\//g, '/');\n\n return parsedPath;\n}\n\nfunction parseEndpoints(\n app: Express | Router | any,\n basePath?: string,\n endpoints?: Endpoint[],\n): Endpoint[] {\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 currentEndpoints Array of current endpoints\n * @param endpointsToAdd New endpoints to be added to the array\n * @returns Updated endpoints array\n */\nfunction addEndpoints(currentEndpoints: Endpoint[], endpointsToAdd: Endpoint[]): Endpoint[] {\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\nfunction parseStack(stack: any[], basePath: string, endpoints: Endpoint[]): Endpoint[] {\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(stackItem.regexp);\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\n/**\n * Returns an array of strings with all the detected endpoints\n * @param app The express/router instance to get the endpoints from\n * @returns The array of endpoints\n */\nfunction expressListEndpoints(app: Express | Router | any): Endpoint[] {\n const endpoints = parseEndpoints(app);\n\n return endpoints;\n}\n\nexport default expressListEndpoints;\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prairielearn/express-list-endpoints",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.13",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,11 +16,11 @@
|
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@prairielearn/tsconfig": "^0.0.0",
|
|
19
|
-
"@types/node": "^22.
|
|
19
|
+
"@types/node": "^22.17.0",
|
|
20
20
|
"@vitest/coverage-v8": "^3.2.4",
|
|
21
21
|
"express": "^4.21.2",
|
|
22
22
|
"tsx": "^4.20.3",
|
|
23
|
-
"typescript": "^5.
|
|
23
|
+
"typescript": "^5.9.2",
|
|
24
24
|
"vitest": "^3.2.4"
|
|
25
25
|
}
|
|
26
26
|
}
|
package/src/index.ts
CHANGED
|
@@ -53,7 +53,7 @@ function hasParams(expressPathRegExp: string): boolean {
|
|
|
53
53
|
/**
|
|
54
54
|
* @param route Express route object to be parsed
|
|
55
55
|
* @param basePath The basePath the route is on
|
|
56
|
-
* @
|
|
56
|
+
* @returns Endpoints info
|
|
57
57
|
*/
|
|
58
58
|
function parseExpressRoute(route: Route, basePath: string): Endpoint[] {
|
|
59
59
|
const paths = [];
|