Skip to content

Commit 62ee3ec

Browse files
author
DylanBulmer
committed
fix dynamic route presedence bug; update deps
1 parent 5ddc666 commit 62ee3ec

File tree

5 files changed

+1092
-587
lines changed

5 files changed

+1092
-587
lines changed

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "@dylanbulmer/openapi",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"main": "index.js",
5-
"author": "Dylan Bulmer <dylan@bulmersolutions.com>",
5+
"author": "Dylan Bulmer <dylan@dylanbulmer.com>",
66
"license": "MIT",
77
"scripts": {
88
"test": "jest --config jest.config.json --passWithNoTests --coverage",
@@ -15,21 +15,21 @@
1515
"postversion": "git push && git push --tags"
1616
},
1717
"dependencies": {
18-
"express": "^4.18.1"
18+
"express": "^4.18.2"
1919
},
2020
"devDependencies": {
21-
"@swc/cli": "^0.1.57",
22-
"@swc/core": "^1.2.224",
23-
"@swc/jest": "^0.2.22",
24-
"@types/express": "^4.17.13",
25-
"@types/jest": "^28.1.4",
21+
"@swc/cli": "^0.1.62",
22+
"@swc/core": "^1.3.50",
23+
"@swc/jest": "^0.2.26",
24+
"@types/express": "^4.17.17",
25+
"@types/jest": "^29.5.0",
2626
"@types/node": "^17.0.33",
2727
"@typescript-eslint/eslint-plugin": "^5.30.6",
2828
"@typescript-eslint/parser": "^5.30.6",
2929
"eslint": "^8.19.0",
3030
"eslint-config-prettier": "^8.5.0",
3131
"eslint-plugin-jest": "^26.5.3",
32-
"jest": "^28.1.2",
32+
"jest": "^29.5.0",
3333
"openapi-types": "^11.0.0",
3434
"prettier": "^2.7.1",
3535
"typescript": "^4.6.4"

src/__tests__/DynamicRoutes/DynamicRoutes.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ test("Dynamic routes", () => {
2121
const paths = doc.paths as OpenAPIV3_1.PathsObject;
2222
const pathKeys = Object.keys(paths);
2323
// check if dynamic route file exists
24-
expect(pathKeys).toContain("/{test}");
25-
expect(paths["/{test}"]?.get?.description).toBe("Testing dynamic routes");
24+
expect(pathKeys).toContain("/test");
25+
expect(paths["/test"]?.get?.description).toBe("Testing dynamic routes");
2626
// check if dynamic route folder exists
2727
expect(pathKeys).toContain("/{test2}");
2828
expect(paths["/{test2}"]?.get?.description).toBe(

src/index.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,20 @@ export const initialize = function initialize({ app, api, ui }: IOptions) {
7575
}
7676

7777
if (api.expose) {
78-
app.get(api.url || "/openapi", (req, res) => {
78+
app.get(api.url || "/apidocs", (_req, res) => {
7979
res.status(200).json(api.doc);
8080
});
8181
app.get(
82-
api.url ? `${api.url}/expanded` : "/openapi/expanded",
83-
(req, res) => {
82+
api.url ? `${api.url}/expanded` : "/apidocs/expanded",
83+
(_req, res) => {
8484
const readable = JSON.stringify(api.doc, undefined, 2);
8585
res.status(200).send(`<pre>${readable}</pre>`);
8686
},
8787
);
8888

8989
console.log(
90-
`OpenAPI spec is hosted at ${api.url || "/openapi"} and ${
91-
api.url || "/openapi"
90+
`OpenAPI spec is hosted at ${api.url || "/apidocs"} and ${
91+
api.url || "/apidocs"
9292
}/expanded`,
9393
);
9494
}
@@ -115,7 +115,25 @@ const getFiles = async function* getFiles(
115115
path: string;
116116
}> {
117117
// get dirents from folder location.
118-
const dirents = fs.readdirSync(folder, { withFileTypes: true });
118+
const dirents = fs
119+
.readdirSync(folder, { withFileTypes: true })
120+
// .sort((a, b) => {
121+
// // sort directories to top of array
122+
// const aIsDir = a.isDirectory() ? 1 : 0;
123+
// const bIsDir = b.isDirectory() ? 1 : 0;
124+
// if (bIsDir > aIsDir) return 1;
125+
// if (bIsDir == aIsDir) return 0;
126+
// else return -1;
127+
// })
128+
.sort((a, b) => {
129+
// sort dynamic routes to end
130+
const regex = /\[(\w+)\]/i;
131+
const aIsDynamic = regex.test(a.name) ? 1 : 0;
132+
const bIsDynamic = regex.test(b.name) ? 1 : 0;
133+
if (bIsDynamic < aIsDynamic) return 1;
134+
if (bIsDynamic == aIsDynamic) return 0;
135+
else return -1;
136+
});
119137
// for each dirent, determine if it's a folder or file.
120138
for (const dirent of dirents) {
121139
const res = path.resolve(folder, dirent.name);

0 commit comments

Comments
 (0)