Skip to content

Commit

Permalink
add app directory route support (#179)
Browse files Browse the repository at this point in the history
fixes #178.
  • Loading branch information
tatethurston authored Nov 4, 2023
1 parent acc173d commit fdc0255
Show file tree
Hide file tree
Showing 13 changed files with 2,472 additions and 2,630 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18.12
20.9.0
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.1.0

- Add experimental support for app directory [route handler](https://nextjs.org/docs/app/building-your-application/routing/route-handlers). See [#178](https://github.com/tatethurston/nextjs-routes/issues/178).

## 2.0.1

- Fix `GetServerSidePropsContext` and `GetServerSidePropsResult` types. Thanks @po4tion!
Expand Down
12 changes: 6 additions & 6 deletions examples/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
"start": "next start"
},
"dependencies": {
"next": "^13.4.12",
"next": "^14.0.1",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@types/node": "20.4.2",
"@types/react": "18.2.15",
"eslint": "8.45.0",
"eslint-config-next": "13.4.12",
"@types/node": "20.8.10",
"@types/react": "18.2.34",
"eslint": "8.53.0",
"eslint-config-next": "14.0.1",
"nextjs-routes": "workspace:*",
"typescript": "5.1.6"
"typescript": "5.2.2"
}
}
18 changes: 9 additions & 9 deletions examples/cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
"start": "next start"
},
"dependencies": {
"next": "^13.4.12",
"next": "^14.0.1",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^14.0.0",
"@types/jest": "^29.5.3",
"@types/node": "20.4.2",
"@types/react": "18.2.15",
"@types/jest": "^29.5.7",
"@types/node": "20.8.10",
"@types/react": "18.2.34",
"@types/testing-library__jest-dom": "^5.14.8",
"eslint": "8.45.0",
"eslint-config-next": "13.4.12",
"jest": "^29.6.1",
"jest-environment-jsdom": "^29.6.1",
"eslint": "8.53.0",
"eslint-config-next": "14.0.1",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"nextjs-routes": "workspace:*",
"typescript": "5.1.6"
"typescript": "5.2.2"
}
}
12 changes: 6 additions & 6 deletions examples/intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
"start": "next start"
},
"dependencies": {
"next": "^13.4.12",
"next": "^14.0.1",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@types/node": "20.4.2",
"@types/react": "18.2.15",
"eslint": "8.45.0",
"eslint-config-next": "13.4.12",
"@types/node": "20.8.10",
"@types/react": "18.2.34",
"eslint": "8.53.0",
"eslint-config-next": "14.0.1",
"nextjs-routes": "workspace:*",
"typescript": "5.1.6"
"typescript": "5.2.2"
}
}
18 changes: 9 additions & 9 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
"start": "next start"
},
"dependencies": {
"next": "^13.4.12",
"next": "^14.0.1",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^14.0.0",
"@types/jest": "^29.5.3",
"@types/node": "20.4.2",
"@types/react": "18.2.15",
"@types/jest": "^29.5.7",
"@types/node": "20.8.10",
"@types/react": "18.2.34",
"@types/testing-library__jest-dom": "^5.14.8",
"eslint": "8.45.0",
"eslint-config-next": "13.4.12",
"jest": "^29.6.1",
"jest-environment-jsdom": "^29.6.1",
"eslint": "8.53.0",
"eslint-config-next": "14.0.1",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"nextjs-routes": "workspace:*",
"typescript": "5.1.6"
"typescript": "5.2.2"
}
}
42 changes: 21 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,36 @@
"examples:each": "for e in ./examples/*; do (cd \"$e\" && $npm_config_command); done",
"examples:regen": "npm run examples:each --command='pnpm next build'",
"lint": "pnpm typecheck && prettier --check . && eslint . && pnpm prettier-package-json --list-different './{packages,examples}/*/package.json'",
"lint:fix": "prettier --write '**/*.{ts,cts,mts,tsx,md,json,yaml}' && prettier-package-json --write './{packages,exmaples}/**/package.json' && eslint --fix .",
"lint:fix": "prettier --write '**/*.{ts,cts,mts,tsx,md,json,yaml}' && prettier-package-json --write './{packages,exmaples}/*/package.json' && eslint --fix .",
"prepare": "husky install",
"test": "npm run e2e:setup && jest",
"test:ci": "npm run test --coverage",
"typecheck": "pnpm -r exec tsc --noEmit"
"test": "pnpm run e2e:setup && jest",
"test:ci": "pnpm run test --coverage",
"typecheck": "pnpm --recursive exec tsc --noEmit"
},
"sideEffects": false,
"devDependencies": {
"@babel/preset-env": "^7.22.9",
"@babel/preset-typescript": "^7.22.5",
"@swc/core": "^1.3.70",
"@types/jest": "^29.5.3",
"@types/node": "^20.4.2",
"@types/react": "^18.2.15",
"@types/webpack": "^5.28.1",
"@typescript-eslint/eslint-plugin": "^6.1.0",
"@typescript-eslint/parser": "^6.1.0",
"@babel/preset-env": "^7.23.2",
"@babel/preset-typescript": "^7.23.2",
"@swc/core": "^1.3.95",
"@types/jest": "^29.5.7",
"@types/node": "^20.8.10",
"@types/react": "^18.2.34",
"@types/webpack": "^5.28.4",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"babel-loader": "^9.1.3",
"codecov": "^3.8.3",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.8.0",
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"husky": "^8.0.3",
"jest": "^29.6.1",
"next": "^13.4.12",
"prettier": "^3.0.0",
"jest": "^29.7.0",
"next": "^14.0.1",
"prettier": "^3.0.3",
"prettier-package-json": "^2.8.0",
"swc-loader": "^0.2.3",
"typescript": "^5.1.6",
"webpack": "^5.88.2",
"typescript": "^5.2.2",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
},
"packageManager": "pnpm@7.17.0"
"packageManager": "pnpm@8.10.2"
}
8 changes: 4 additions & 4 deletions packages/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"start": "next start"
},
"dependencies": {
"next": "*",
"react": "*",
"react-dom": "*"
"next": "^14.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"nextjs-routes": "workspace:*",
"typescript": "*"
"typescript": "^5.2.2"
}
}
2 changes: 1 addition & 1 deletion packages/nextjs-routes/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nextjs-routes",
"version": "2.0.1",
"version": "2.1.0",
"description": "Type safe routing for Next.js",
"license": "MIT",
"author": "Tate <[email protected]>",
Expand Down
6 changes: 5 additions & 1 deletion packages/nextjs-routes/src/__snapshots__/core.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ declare module "nextjs-routes" {
export type Route =
| StaticRoute<"/">
| StaticRoute<"/bar">
| StaticRoute<"/foo">
| StaticRoute<"/bar">;
| StaticRoute<"/foobar">
| StaticRoute<"/barbaz">
| StaticRoute<"/baz">
| StaticRoute<"/foobaz">;
interface StaticRoute<Pathname> {
pathname: Pathname;
Expand Down
6 changes: 5 additions & 1 deletion packages/nextjs-routes/src/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ describe("route generation", () => {
.mockImplementationOnce(() => true);
findFilesMock.mockReturnValueOnce([
"app/page.ts",
"app/foo/page.tsx",
"app/bar/page.ts",
"app/foo/page.tsx",
"app/foobar/page.js",
"app/barbaz/page.jsx",
"app/baz/route.js",
"app/foobaz/route.ts",
]);
writeNextJSRoutes({});
expect(writeFileSyncMock.mock.calls).toMatchSnapshot();
Expand Down
10 changes: 6 additions & 4 deletions packages/nextjs-routes/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,13 @@ function commonProcessing(paths: string[], opts: Opts): string[] {
);
}

const APP_DIRECTORY_ROUTABLE = ["page", "route"];

export function getAppRoutes(files: string[], opts: Opts): string[] {
return (
commonProcessing(files, opts)
// app pages must be named 'page'
.filter((file) => parse(file).name === "page")
.filter((file) => APP_DIRECTORY_ROUTABLE.includes(parse(file).name))
.map((file) =>
// transform filepath to url path
file
Expand All @@ -361,17 +363,17 @@ export function getAppRoutes(files: string[], opts: Opts): string[] {
(segment) => !(segment.startsWith("(") && segment.endsWith(")")),
)
// remove page
.filter((file) => parse(file).name !== "page")
.filter((file) => !APP_DIRECTORY_ROUTABLE.includes(parse(file).name))
.join(sep),
)
// handle index page
.map((file) => (file === "" ? "/" : file))
);
}

export function getPageRoutes(files: string[], opts: Opts): string[] {
const NEXTJS_NON_ROUTABLE = ["/_app", "/_document", "/_error", "/middleware"];
const NEXTJS_NON_ROUTABLE = ["/_app", "/_document", "/_error", "/middleware"];

export function getPageRoutes(files: string[], opts: Opts): string[] {
return (
commonProcessing(files, opts)
// remove index if present (/foos/index.ts is the same as /foos.ts)
Expand Down
Loading

0 comments on commit fdc0255

Please sign in to comment.