5
5
6
6
// prettier-ignore
7
7
declare module "nextjs-routes" {
8
+ import type {
9
+ GetServerSidePropsContext as NextGetServerSidePropsContext ,
10
+ GetServerSidePropsResult as NextGetServerSidePropsResult
11
+ } from "nextjs" ;
12
+
8
13
export type Route =
9
14
| StaticRoute < "/404" >
10
15
| StaticRoute < "/account/api-key" >
@@ -107,6 +112,33 @@ declare module "nextjs-routes" {
107
112
* route({ pathname: "/foos/[foo]", query: { foo: "bar" }}) will produce "/foos/bar".
108
113
*/
109
114
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 > >
110
142
}
111
143
112
144
// prettier-ignore
@@ -121,13 +153,12 @@ declare module "next/link" {
121
153
} from "react" ;
122
154
export * from "next/dist/client/link" ;
123
155
124
- type Query = { query ?: { [ key : string ] : string | string [ ] | undefined } } ;
125
156
type StaticRoute = Exclude < Route , { query : any } > [ "pathname" ] ;
126
157
127
158
export interface LinkProps
128
159
extends Omit < NextLinkProps , "href" | "locale" > ,
129
160
AnchorHTMLAttributes < HTMLAnchorElement > {
130
- href : Route | StaticRoute | Query ;
161
+ href : Route | StaticRoute | Omit < Route , "pathname" >
131
162
locale ?: false ;
132
163
}
133
164
@@ -155,7 +186,6 @@ declare module "next/router" {
155
186
156
187
type NextTransitionOptions = NonNullable < Parameters < Router [ "push" ] > [ 2 ] > ;
157
188
type StaticRoute = Exclude < Route , { query : any } > [ "pathname" ] ;
158
- type Query = { query ?: { [ key : string ] : string | string [ ] | undefined } } ;
159
189
160
190
interface TransitionOptions extends Omit < NextTransitionOptions , "locale" > {
161
191
locale ?: false ;
@@ -177,12 +207,12 @@ declare module "next/router" {
177
207
locale ?: Locale ;
178
208
locales ?: undefined ;
179
209
push (
180
- url : Route | StaticRoute | Query ,
210
+ url : Route | StaticRoute | Omit < Route , "pathname" > ,
181
211
as ?: string ,
182
212
options ?: TransitionOptions
183
213
) : Promise < boolean > ;
184
214
replace (
185
- url : Route | StaticRoute | Query ,
215
+ url : Route | StaticRoute | Omit < Route , "pathname" > ,
186
216
as ?: string ,
187
217
options ?: TransitionOptions
188
218
) : Promise < boolean > ;
0 commit comments