Repository: tatethurston/nextjs-routes
Pull request #82 (2022-10-18 17:27:11)

v0.1.5


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fa0a810..af22f60 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,15 @@
 # Changelog
 
+## 0.1.5
+
+- Export `Locale` from `nextjs-routes`.
+
+  ```ts
+  import { Locale } from "nextjs-routes";
+  ```
+
+  Thanks [@Asamsig](https://github.com/Asamsig) for the contribution!
+
 ## 0.1.4
 
 - `nextjs-routes` now generates route types for [Nextjs i18n configuration](https://nextjs.org/docs/advanced-features/i18n-routing). Eg the following next config:
diff --git a/README.md b/README.md
index 5c47514..a620d97 100644
--- a/README.md
+++ b/README.md
@@ -217,6 +217,12 @@ module.exports = withRoutes({
 
 Will type `Link` and `useRouter`'s `locale` as `'de-DE' | 'en-FR' | 'en-US'`. All other i18n properties (`defaultLocale`, `domainLocales` and `locales`) are also typed.
 
+If you want to use the generated `Locale` type, you can import it from `nextjs-routes`:
+
+```ts
+import { Locale } from "nextjs-routes";
+```
+
 ## Configuration
 
 You can pass the following options to `withRoutes` in your `next.config.js`:
diff --git a/examples/intl/nextjs-routes.d.ts b/examples/intl/nextjs-routes.d.ts
index 52906fc..b03a536 100644
--- a/examples/intl/nextjs-routes.d.ts
+++ b/examples/intl/nextjs-routes.d.ts
@@ -18,8 +18,8 @@ declare module "nextjs-routes" {
 
   export type Locale = 
       | "en-US"
-      | "fr"
-      | "nl-NL";
+    | "fr"
+    | "nl-NL";
   
   /**
    * A typesafe utility function for generating paths in your application.
@@ -40,9 +40,7 @@ declare module "next/link" {
 
   export interface LinkProps extends Omit<NextLinkProps, "href" | "locale"> {
     href: RouteOrQuery;
-    locale?: 
-      | Locale
-      | false;
+    locale?: Locale | false;
   }
 
   declare function Link(
@@ -70,9 +68,7 @@ declare module "next/router" {
   type NextTransitionOptions = NonNullable<Parameters<Router["push"]>[2]>;
 
   interface TransitionOptions extends Omit<NextTransitionOptions, 'locale'> {
-    locale?: 
-      | Locale
-      | false;
+    locale?: Locale | false;
   };
 
   type RouteOrQuery = 
diff --git a/public.package.json b/public.package.json
index 5c48031..cc908fb 100644
--- a/public.package.json
+++ b/public.package.json
@@ -1,6 +1,6 @@
 {
   "name": "nextjs-routes",
-  "version": "0.1.4",
+  "version": "0.1.5",
   "description": "Type safe routing for Next.js",
   "license": "MIT",
   "author": "Tate <tatethurston@gmail.com>",
diff --git a/src/core.ts b/src/core.ts
index 2522d7b..b584570 100644
--- a/src/core.ts
+++ b/src/core.ts
@@ -99,9 +99,6 @@ function generate(routes: Route[], config: NextJSRoutesOptions): string {
   };
   const unknownQueryParamsType =
     "{ [key: string]: string | string[] | undefined }";
-  const locales = `\n      | ${i18n.locales
-    .map((x) => `"${x}"`)
-    .join("\n      | ")}`;
   return `\
 // THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
 // This file will be automatically regenerated when your Next.js server is running.
@@ -133,7 +130,11 @@ declare module "nextjs-routes" {
 
   export type RoutedQuery<P extends Route["pathname"]> = QueryForPathname[P];
 
-  export type Locale = ${!i18n.locales.length ? "undefined" : locales};
+  export type Locale = ${
+    !i18n.locales.length
+      ? "undefined"
+      : `\n      | ${i18n.locales.map((x) => `"${x}"`).join("\n    | ")}`
+  };
   
   /**
    * A typesafe utility function for generating paths in your application.
@@ -154,9 +155,7 @@ declare module "next/link" {
 
   export interface LinkProps extends Omit<NextLinkProps, "href" | "locale"> {
     href: RouteOrQuery;
-    locale?: ${
-      !i18n.locales.length ? "false" : `\n      | Locale\n      | false`
-    };
+    locale?: ${!i18n.locales.length ? "false" : `Locale | false`};
   }
 
   declare function Link(
@@ -184,9 +183,7 @@ declare module "next/router" {
   type NextTransitionOptions = NonNullable<Parameters<Router["push"]>[2]>;
 
   interface TransitionOptions extends Omit<NextTransitionOptions, 'locale'> {
-    locale?: ${
-      !i18n.locales.length ? "false" : `\n      | Locale\n      | false`
-    };
+    locale?: ${!i18n.locales.length ? "false" : `Locale | false`};
   };
 
   type RouteOrQuery = 
@@ -204,7 +201,7 @@ declare module "next/router" {
     domainLocales${
       i18n.domains?.length ? `: ${print(i18n.domains, 4)}` : "?: undefined"
     };
-    locale${(!i18n.locales.length ? "?: " : ": ") + "Locale"};
+    locale${!i18n.locales.length ? "?:" : ":"} Locale;
     locales${
       i18n.locales.length ? `: ${print(i18n.locales, 4)}` : "?: undefined"
     };


Test patch:
diff --git a/e2e/nextjs-routes.d.ts b/e2e/nextjs-routes.d.ts
index bfa3748..e846386 100644
--- a/e2e/nextjs-routes.d.ts
+++ b/e2e/nextjs-routes.d.ts
@@ -16,6 +16,8 @@ declare module "nextjs-routes" {
 
   export type RoutedQuery<P extends Route["pathname"]> = QueryForPathname[P];
 
+  export type Locale = undefined;
+  
   /**
    * A typesafe utility function for generating paths in your application.
    *
@@ -55,7 +57,7 @@ declare module "next/link" {
 
 // prettier-ignore
 declare module "next/router" {
-  import type { Route, RoutedQuery } from "nextjs-routes";
+  import type { Locale, Route, RoutedQuery } from "nextjs-routes";
   import type { NextRouter as Router } from "next/dist/client/router";
   export * from "next/dist/client/router";
   export { default } from "next/dist/client/router";
@@ -77,7 +79,7 @@ declare module "next/router" {
     > {
     defaultLocale?: undefined;
     domainLocales?: undefined;
-    locale?: undefined;
+    locale?: Locale;
     locales?: undefined;
     pathname: P;
     push(
diff --git a/e2e/package-lock.json b/e2e/package-lock.json
index 1507af6..9041ebf 100644
--- a/e2e/package-lock.json
+++ b/e2e/package-lock.json
@@ -20,7 +20,7 @@
     },
     "../dist": {
       "name": "nextjs-routes",
-      "version": "0.1.3",
+      "version": "0.1.5",
       "dev": true,
       "license": "MIT",
       "dependencies": {
diff --git a/src/__snapshots__/core.test.ts.snap b/src/__snapshots__/core.test.ts.snap
index 203b5f3..1dc246e 100644
--- a/src/__snapshots__/core.test.ts.snap
+++ b/src/__snapshots__/core.test.ts.snap
@@ -23,8 +23,8 @@ declare module "nextjs-routes" {
 
   export type Locale = 
       | "en-US"
-      | "fr"
-      | "nl-NL";
+    | "fr"
+    | "nl-NL";
   
   /**
    * A typesafe utility function for generating paths in your application.
@@ -45,9 +45,7 @@ declare module "next/link" {
 
   export interface LinkProps extends Omit<NextLinkProps, "href" | "locale"> {
     href: RouteOrQuery;
-    locale?: 
-      | Locale
-      | false;
+    locale?: Locale | false;
   }
 
   declare function Link(
@@ -75,9 +73,7 @@ declare module "next/router" {
   type NextTransitionOptions = NonNullable<Parameters<Router["push"]>[2]>;
 
   interface TransitionOptions extends Omit<NextTransitionOptions, 'locale'> {
-    locale?: 
-      | Locale
-      | false;
+    locale?: Locale | false;
   };
 
   type RouteOrQuery =