UNPKG

4.59 kBTypeScriptView Raw
1/// <reference types="node" />
2/// <reference types="react" />
3/// <reference types="react-dom" />
4
5import React from 'react'
6import { ParsedUrlQuery } from 'querystring'
7import { IncomingMessage, ServerResponse } from 'http'
8
9import {
10 NextPageContext,
11 NextComponentType,
12 NextApiResponse,
13 NextApiRequest,
14 NextApiHandler,
15 // @ts-ignore This path is generated at build time and conflicts otherwise
16} from '../dist/next-server/lib/utils'
17
18import {
19 NextApiRequestCookies,
20 // @ts-ignore This path is generated at build time and conflicts otherwise
21} from '../dist/next-server/server/api-utils'
22
23// @ts-ignore This path is generated at build time and conflicts otherwise
24import next from '../dist/server/next'
25
26// Extend the React types with missing properties
27declare module 'react' {
28 // <html amp=""> support
29 interface HtmlHTMLAttributes<T> extends React.HTMLAttributes<T> {
30 amp?: string
31 }
32
33 // <link nonce=""> support
34 interface LinkHTMLAttributes<T> extends HTMLAttributes<T> {
35 nonce?: string
36 }
37
38 // <style jsx> and <style jsx global> support for styled-jsx
39 interface StyleHTMLAttributes<T> extends HTMLAttributes<T> {
40 jsx?: boolean
41 global?: boolean
42 }
43}
44
45export type Redirect =
46 | {
47 statusCode: 301 | 302 | 303 | 307 | 308
48 destination: string
49 basePath?: false
50 }
51 | {
52 permanent: boolean
53 destination: string
54 basePath?: false
55 }
56
57/**
58 * `Page` type, use it as a guide to create `pages`.
59 */
60export type NextPage<P = {}, IP = P> = NextComponentType<NextPageContext, IP, P>
61
62/**
63 * `Config` type, use it for export const config
64 */
65export type PageConfig = {
66 amp?: boolean | 'hybrid'
67 api?: {
68 /**
69 * The byte limit of the body. This is the number of bytes or any string
70 * format supported by `bytes`, for example `1000`, `'500kb'` or `'3mb'`.
71 */
72 bodyParser?: { sizeLimit?: number | string } | false
73 /**
74 * Flag to disable warning "API page resolved
75 * without sending a response", due to explicitly
76 * using an external API resolver, like express
77 */
78 externalResolver?: true
79 }
80 env?: Array<string>
81 unstable_runtimeJS?: false
82 unstable_JsPreload?: false
83}
84
85export {
86 NextPageContext,
87 NextComponentType,
88 NextApiResponse,
89 NextApiRequest,
90 NextApiHandler,
91}
92
93export type PreviewData = string | false | object | undefined
94
95export type GetStaticPropsContext<Q extends ParsedUrlQuery = ParsedUrlQuery> = {
96 params?: Q
97 preview?: boolean
98 previewData?: PreviewData
99 locale?: string
100 locales?: string[]
101 defaultLocale?: string
102}
103
104export type GetStaticPropsResult<P> =
105 | { props: P; revalidate?: number | boolean }
106 | { redirect: Redirect; revalidate?: number | boolean }
107 | { notFound: true }
108
109export type GetStaticProps<
110 P extends { [key: string]: any } = { [key: string]: any },
111 Q extends ParsedUrlQuery = ParsedUrlQuery
112> = (
113 context: GetStaticPropsContext<Q>
114) => Promise<GetStaticPropsResult<P>> | GetStaticPropsResult<P>
115
116export type InferGetStaticPropsType<T> = T extends GetStaticProps<infer P, any>
117 ? P
118 : T extends (
119 context?: GetStaticPropsContext<any>
120 ) => Promise<GetStaticPropsResult<infer P>> | GetStaticPropsResult<infer P>
121 ? P
122 : never
123
124export type GetStaticPathsContext = {
125 locales?: string[]
126 defaultLocale?: string
127}
128
129export type GetStaticPathsResult<P extends ParsedUrlQuery = ParsedUrlQuery> = {
130 paths: Array<string | { params: P; locale?: string }>
131 fallback: boolean | 'blocking'
132}
133
134export type GetStaticPaths<P extends ParsedUrlQuery = ParsedUrlQuery> = (
135 context: GetStaticPathsContext
136) => Promise<GetStaticPathsResult<P>> | GetStaticPathsResult<P>
137
138export type GetServerSidePropsContext<
139 Q extends ParsedUrlQuery = ParsedUrlQuery
140> = {
141 req: IncomingMessage & {
142 cookies: NextApiRequestCookies
143 }
144 res: ServerResponse
145 params?: Q
146 query: ParsedUrlQuery
147 preview?: boolean
148 previewData?: PreviewData
149 resolvedUrl: string
150 locale?: string
151 locales?: string[]
152 defaultLocale?: string
153}
154
155export type GetServerSidePropsResult<P> =
156 | { props: P }
157 | { redirect: Redirect }
158 | { notFound: true }
159
160export type GetServerSideProps<
161 P extends { [key: string]: any } = { [key: string]: any },
162 Q extends ParsedUrlQuery = ParsedUrlQuery
163> = (
164 context: GetServerSidePropsContext<Q>
165) => Promise<GetServerSidePropsResult<P>>
166
167export type InferGetServerSidePropsType<T> = T extends GetServerSideProps<
168 infer P,
169 any
170>
171 ? P
172 : T extends (
173 context?: GetServerSidePropsContext<any>
174 ) => Promise<GetServerSidePropsResult<infer P>>
175 ? P
176 : never
177
178export default next