55
66// prettier-ignore
77declare module "nextjs-routes" {
8+ import type {
9+ GetServerSidePropsContext as NextGetServerSidePropsContext ,
10+ GetServerSidePropsResult as NextGetServerSidePropsResult
11+ } from "nextjs" ;
12+
813 export type Route =
914 | StaticRoute < "/404" >
1015 | StaticRoute < "/account/api-key" >
@@ -107,6 +112,33 @@ declare module "nextjs-routes" {
107112 * route({ pathname: "/foos/[foo]", query: { foo: "bar" }}) will produce "/foos/bar".
108113 */
109114 export declare function route ( r : Route ) : string ;
115+
116+ /**
117+ * Nearly identical to GetServerSidePropsContext from next, but further narrows
118+ * types based on nextjs-route's route data.
119+ */
120+ export type GetServerSidePropsContext <
121+ Pathname extends Route [ "pathname" ] = Route [ "pathname" ] ,
122+ Preview extends NextGetServerSidePropsContext [ "previewData" ] = NextGetServerSidePropsContext [ "previewData" ]
123+ > = Omit < NextGetServerSidePropsContext , 'params' | 'query' | 'defaultLocale' | 'locale' | 'locales' > & {
124+ params : Extract < Route , { pathname : Pathname } > [ "query" ] ;
125+ query : Query ;
126+ defaultLocale ?: undefined ;
127+ locale ?: Locale ;
128+ locales ?: undefined ;
129+ } ;
130+
131+ /**
132+ * Nearly identical to GetServerSideProps from next, but further narrows
133+ * types based on nextjs-route's route data.
134+ */
135+ export type GetServerSideProps <
136+ Props extends { [ key : string ] : any } = { [ key : string ] : any } ,
137+ Pathname extends Route [ "pathname" ] = Route [ "pathname" ] ,
138+ Preview extends NextGetServerSideProps [ "previewData" ] = NextGetServerSideProps [ "previewData" ]
139+ > = (
140+ context : GetServerSidePropsContext < Pathname , Preview >
141+ ) => Promise < NextGetServerSidePropsResult < Props > >
110142}
111143
112144// prettier-ignore
@@ -121,13 +153,12 @@ declare module "next/link" {
121153 } from "react" ;
122154 export * from "next/dist/client/link" ;
123155
124- type Query = { query ?: { [ key : string ] : string | string [ ] | undefined } } ;
125156 type StaticRoute = Exclude < Route , { query : any } > [ "pathname" ] ;
126157
127158 export interface LinkProps
128159 extends Omit < NextLinkProps , "href" | "locale" > ,
129160 AnchorHTMLAttributes < HTMLAnchorElement > {
130- href : Route | StaticRoute | Query ;
161+ href : Route | StaticRoute | Omit < Route , "pathname" >
131162 locale ?: false ;
132163 }
133164
@@ -155,7 +186,6 @@ declare module "next/router" {
155186
156187 type NextTransitionOptions = NonNullable < Parameters < Router [ "push" ] > [ 2 ] > ;
157188 type StaticRoute = Exclude < Route , { query : any } > [ "pathname" ] ;
158- type Query = { query ?: { [ key : string ] : string | string [ ] | undefined } } ;
159189
160190 interface TransitionOptions extends Omit < NextTransitionOptions , "locale" > {
161191 locale ?: false ;
@@ -177,12 +207,12 @@ declare module "next/router" {
177207 locale ?: Locale ;
178208 locales ?: undefined ;
179209 push (
180- url : Route | StaticRoute | Query ,
210+ url : Route | StaticRoute | Omit < Route , "pathname" > ,
181211 as ?: string ,
182212 options ?: TransitionOptions
183213 ) : Promise < boolean > ;
184214 replace (
185- url : Route | StaticRoute | Query ,
215+ url : Route | StaticRoute | Omit < Route , "pathname" > ,
186216 as ?: string ,
187217 options ?: TransitionOptions
188218 ) : Promise < boolean > ;
0 commit comments