Skip to content

Commit

Permalink
Merge pull request #113 from JasonPaff/cli-filename-update
Browse files Browse the repository at this point in the history
Updated the CLI to allow for a filename option to override the default 'routeType.ts' filename
  • Loading branch information
ethanniser authored Dec 7, 2024
2 parents 176b88b + ead4092 commit 13afbf1
Show file tree
Hide file tree
Showing 27 changed files with 518 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"commit": false,
"access": "public",
"baseBranch": "main",
"ignore": ["pagesdir", "appdir"]
"ignore": ["pagesdir", "appdir", "custom-filename"]
}
5 changes: 5 additions & 0 deletions .changeset/little-mirrors-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"next-typesafe-url": minor
---

updates the cli to allow for a custom filename to be set under the --filename flag to replace the default of 'routeType.ts' - Thank you @JasonPaff !
3 changes: 3 additions & 0 deletions examples/custom-filename/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
35 changes: 35 additions & 0 deletions examples/custom-filename/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
34 changes: 34 additions & 0 deletions examples/custom-filename/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
18 changes: 18 additions & 0 deletions examples/custom-filename/_next-typesafe-url_.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This file is generated by next-typesafe-url
// Do not edit this file directly.

// @generated
// prettier-ignore
/* eslint-disable */

declare module "@@@next-typesafe-url" {
import type { InferRoute, StaticRoute } from "next-typesafe-url";

interface DynamicRouter {
"/[slug]/[...foo]": InferRoute<import("./src/app/[slug]/[...foo]/route-type").RouteType>;
}

interface StaticRouter {
"/": StaticRoute;
}
}
12 changes: 12 additions & 0 deletions examples/custom-filename/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const withMDX = require('@next/mdx')()


/** @type {import('next').NextConfig} */
const nextConfig = {
pageExtensions: ['js', 'jsx', 'mdx', 'ts', 'tsx'],
experimental: {
appDir: true,
},
}

module.exports = withMDX(nextConfig)
31 changes: 31 additions & 0 deletions examples/custom-filename/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "custom-filename",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "concurrently \"next-typesafe-url -w --filename route-type --pageExtensions js,jsx,mdx,ts,tsx\" \"next dev\"",
"build": "next-typesafe-url --filename route-type --pageExtensions js,jsx,mdx,ts,tsx && next build",
"start": "next start"
},
"dependencies": {
"@mdx-js/loader": "^3.0.1",
"@mdx-js/react": "^3.0.1",
"@next/mdx": "^14.2.4",
"@types/mdx": "^2.0.13",
"@types/node": "18.16.3",
"@types/react": "18.2.5",
"@types/react-dom": "18.2.3",
"autoprefixer": "10.4.14",
"concurrently": "^8.0.1",
"eslint": "8.39.0",
"eslint-config-next": "13.4.0",
"next": "13.4.12",
"next-typesafe-url": "workspace:*",
"postcss": "8.4.23",
"react": "18.2.0",
"react-dom": "18.2.0",
"tailwindcss": "3.3.2",
"typescript": "5.0.4",
"zod": "^3.20.2"
}
}
6 changes: 6 additions & 0 deletions examples/custom-filename/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
1 change: 1 addition & 0 deletions examples/custom-filename/public/next.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/custom-filename/public/vercel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions examples/custom-filename/src/app/[slug]/[...foo]/client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use client";

import { $path } from "next-typesafe-url";
import Link from "next/link";
import { useState } from "react";
import { useRouteParams, useSearchParams } from "next-typesafe-url/app";
import { Route } from "./route-type";
import { useParams } from "next/navigation";

export const Client = () => {
const [input, setInput] = useState("");
const nextParams = useParams();
console.log("nextParams", nextParams);

const params = useSearchParams(Route.searchParams);
const routeParams = useRouteParams(Route.routeParams);

return (
<>
<Link href={$path({ route: "/" })}>Back</Link>
<br />
<input value={input} onChange={(e) => setInput(e.target.value)} />
<Link
href={$path({
route: "/[slug]/[...foo]",
routeParams: {
slug: input === "" ? "default" : input,
foo: [123, 424, 343],
},
searchParams: {
location: "us",
userInfo: { name: "harry", age: 123 },
},
})}
>
hooks
</Link>
<br />
<h1>searchParams</h1>
<div>{`data: ${JSON.stringify(params)}`}</div>
<h1>routeParams</h1>
<div>{`data: ${JSON.stringify(routeParams)}`}</div>
</>
);
};
30 changes: 30 additions & 0 deletions examples/custom-filename/src/app/[slug]/[...foo]/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use client"; // Error components must be Client components

import { useEffect } from "react";

export default function Error({
error,
reset,
}: {
error: Error;
reset: () => void;
}) {
useEffect(() => {
// Log the error to an error reporting service
console.error(error);
}, [error]);

return (
<div>
<h2>Something went wrong!</h2>
<button
onClick={
// Attempt to recover by trying to re-render the segment
() => reset()
}
>
Try again
</button>
</div>
);
}
25 changes: 25 additions & 0 deletions examples/custom-filename/src/app/[slug]/[...foo]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { withParamValidation } from "next-typesafe-url/app/hoc";
import { InferPagePropsType } from "next-typesafe-url";
import { Route, RouteType } from "./route-type";
import { Client } from "./client";

type PageProps = InferPagePropsType<RouteType>;

let count = 0;

const Page = ({ routeParams, searchParams }: PageProps) => {
console.log("render", count++);
console.log(JSON.stringify(routeParams));
console.log(JSON.stringify(searchParams));

return (
<>
<div>{`data: ${JSON.stringify(routeParams)}`}</div>
<br />
<div>{`data: ${JSON.stringify(searchParams)}`}</div>
<Client />
</>
);
};

export default withParamValidation(Page, Route);
20 changes: 20 additions & 0 deletions examples/custom-filename/src/app/[slug]/[...foo]/route-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { z } from "zod";
import { type DynamicRoute } from "next-typesafe-url";

export const Route = {
routeParams: z.object({
slug: z.string(),
foo: z.array(z.number()).or(z.number()),
}),
searchParams: z.object({
location: z.enum(["us", "eu"]).optional(),
userInfo: z
.object({
name: z.string(),
age: z.number(),
})
.optional(),
}),
} satisfies DynamicRoute;

export type RouteType = typeof Route;
24 changes: 24 additions & 0 deletions examples/custom-filename/src/app/[slug]/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { z } from "zod";
import { withLayoutParamValidation } from "next-typesafe-url/app/hoc";
import type { DynamicLayout, InferLayoutPropsType } from "next-typesafe-url";

const LayoutRoute = {
routeParams: z.object({
slug: z.string(),
}),
} satisfies DynamicLayout;
type LayoutType = typeof LayoutRoute;

type Props = InferLayoutPropsType<LayoutType>;
function Layout({ children, routeParams }: Props) {
return (
<div>
<h1>THIS IS A LAYOUT</h1>
<p>{JSON.stringify(routeParams)}</p>
<div className="border border-black">{children}</div>
<p>bottom</p>
</div>
);
}

export default withLayoutParamValidation(Layout, LayoutRoute);
Binary file added examples/custom-filename/src/app/favicon.ico
Binary file not shown.
3 changes: 3 additions & 0 deletions examples/custom-filename/src/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
21 changes: 21 additions & 0 deletions examples/custom-filename/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Inter } from "next/font/google";
import "./globals.css";

const inter = Inter({ subsets: ["latin"] });

export const metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
);
}
26 changes: 26 additions & 0 deletions examples/custom-filename/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use client";

import { $path, type RouterOutputs } from "next-typesafe-url";
import Link from "next/link";

type _ThisIsHelpful = RouterOutputs["/[slug]/[...foo]"]["routeParams"];

export default function Page() {
return (
<>
<div>test</div>
<Link
href={$path({
route: "/[slug]/[...foo]",
routeParams: { slug: "string", foo: [123, 424, 343] },
searchParams: {
location: "us",
userInfo: { name: "string", age: 123 },
},
})}
>
server component!
</Link>
</>
);
}
7 changes: 7 additions & 0 deletions examples/custom-filename/src/mdx-components.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { MDXComponents } from "mdx/types";

export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
};
}
18 changes: 18 additions & 0 deletions examples/custom-filename/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./src/pages/**/*.{js,ts,jsx,tsx,mdx}',
'./src/components/**/*.{js,ts,jsx,tsx,mdx}',
'./src/app/**/*.{js,ts,jsx,tsx,mdx}',
],
theme: {
extend: {
backgroundImage: {
'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))',
'gradient-conic':
'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))',
},
},
},
plugins: [],
}
Loading

0 comments on commit 13afbf1

Please sign in to comment.