UNPKG

7.55 kBTypeScriptView Raw
1/// <reference types="node" />
2import { IncomingMessage, ServerResponse } from 'http';
3import { ParsedUrlQuery } from 'querystring';
4import { ComponentType } from 'react';
5import { UrlObject } from 'url';
6import { NextRouter } from './router/router';
7import { Env } from '@next/env';
8import { BuildManifest } from '../server/get-page-files';
9import { DomainLocales } from '../server/config';
10import { PreviewData } from 'next/types';
11/**
12 * Types used by both next and next-server
13 */
14export declare type NextComponentType<C extends BaseContext = NextPageContext, IP = {}, P = {}> = ComponentType<P> & {
15 /**
16 * Used for initial page load data population. Data returned from `getInitialProps` is serialized when server rendered.
17 * Make sure to return plain `Object` without using `Date`, `Map`, `Set`.
18 * @param ctx Context of `page`
19 */
20 getInitialProps?(context: C): IP | Promise<IP>;
21};
22export declare type DocumentType = NextComponentType<DocumentContext, DocumentInitialProps, DocumentProps> & {
23 renderDocument(Document: DocumentType, props: DocumentProps): React.ReactElement;
24};
25export declare type AppType = NextComponentType<AppContextType, AppInitialProps, AppPropsType>;
26export declare type AppTreeType = ComponentType<AppInitialProps & {
27 [name: string]: any;
28}>;
29/**
30 * Web vitals provided to _app.reportWebVitals by Core Web Vitals plugin developed by Google Chrome team.
31 * https://nextjs.org/blog/next-9-4#integrated-web-vitals-reporting
32 */
33export declare type NextWebVitalsMetric = {
34 id: string;
35 label: string;
36 name: string;
37 startTime: number;
38 value: number;
39};
40export declare type Enhancer<C> = (Component: C) => C;
41export declare type ComponentsEnhancer = {
42 enhanceApp?: Enhancer<AppType>;
43 enhanceComponent?: Enhancer<NextComponentType>;
44} | Enhancer<NextComponentType>;
45export declare type RenderPageResult = {
46 html: string;
47 head?: Array<JSX.Element | null>;
48};
49export declare type RenderPage = (options?: ComponentsEnhancer) => RenderPageResult | Promise<RenderPageResult>;
50export declare type BaseContext = {
51 res?: ServerResponse;
52 [k: string]: any;
53};
54export declare type NEXT_DATA = {
55 props: Record<string, any>;
56 page: string;
57 query: ParsedUrlQuery;
58 buildId: string;
59 assetPrefix?: string;
60 runtimeConfig?: {
61 [key: string]: any;
62 };
63 nextExport?: boolean;
64 autoExport?: boolean;
65 isFallback?: boolean;
66 dynamicIds?: (string | number)[];
67 err?: Error & {
68 statusCode?: number;
69 };
70 gsp?: boolean;
71 gssp?: boolean;
72 customServer?: boolean;
73 gip?: boolean;
74 appGip?: boolean;
75 locale?: string;
76 locales?: string[];
77 defaultLocale?: string;
78 domainLocales?: DomainLocales;
79 scriptLoader?: any[];
80 isPreview?: boolean;
81};
82/**
83 * `Next` context
84 */
85export interface NextPageContext {
86 /**
87 * Error object if encountered during rendering
88 */
89 err?: (Error & {
90 statusCode?: number;
91 }) | null;
92 /**
93 * `HTTP` request object.
94 */
95 req?: IncomingMessage;
96 /**
97 * `HTTP` response object.
98 */
99 res?: ServerResponse;
100 /**
101 * Path section of `URL`.
102 */
103 pathname: string;
104 /**
105 * Query string section of `URL` parsed as an object.
106 */
107 query: ParsedUrlQuery;
108 /**
109 * `String` of the actual path including query.
110 */
111 asPath?: string;
112 /**
113 * The currently active locale
114 */
115 locale?: string;
116 /**
117 * All configured locales
118 */
119 locales?: string[];
120 /**
121 * The configured default locale
122 */
123 defaultLocale?: string;
124 /**
125 * `Component` the tree of the App to use if needing to render separately
126 */
127 AppTree: AppTreeType;
128}
129export declare type AppContextType<R extends NextRouter = NextRouter> = {
130 Component: NextComponentType<NextPageContext>;
131 AppTree: AppTreeType;
132 ctx: NextPageContext;
133 router: R;
134};
135export declare type AppInitialProps = {
136 pageProps: any;
137};
138export declare type AppPropsType<R extends NextRouter = NextRouter, P = {}> = AppInitialProps & {
139 Component: NextComponentType<NextPageContext, any, P>;
140 router: R;
141 __N_SSG?: boolean;
142 __N_SSP?: boolean;
143};
144export declare type DocumentContext = NextPageContext & {
145 renderPage: RenderPage;
146};
147export declare type DocumentInitialProps = RenderPageResult & {
148 styles?: React.ReactElement[] | React.ReactFragment;
149};
150export declare type DocumentProps = DocumentInitialProps & {
151 __NEXT_DATA__: NEXT_DATA;
152 dangerousAsPath: string;
153 docComponentsRendered: {
154 Html?: boolean;
155 Main?: boolean;
156 Head?: boolean;
157 NextScript?: boolean;
158 };
159 buildManifest: BuildManifest;
160 ampPath: string;
161 inAmpMode: boolean;
162 hybridAmp: boolean;
163 isDevelopment: boolean;
164 dynamicImports: string[];
165 assetPrefix?: string;
166 canonicalBase: string;
167 headTags: any[];
168 unstable_runtimeJS?: false;
169 unstable_JsPreload?: false;
170 devOnlyCacheBusterQueryString: string;
171 scriptLoader: {
172 afterInteractive?: string[];
173 beforeInteractive?: any[];
174 };
175 locale?: string;
176 disableOptimizedLoading?: boolean;
177};
178/**
179 * Next `API` route request
180 */
181export interface NextApiRequest extends IncomingMessage {
182 /**
183 * Object of `query` values from url
184 */
185 query: {
186 [key: string]: string | string[];
187 };
188 /**
189 * Object of `cookies` from header
190 */
191 cookies: {
192 [key: string]: string;
193 };
194 body: any;
195 env: Env;
196 preview?: boolean;
197 /**
198 * Preview data set on the request, if any
199 * */
200 previewData?: PreviewData;
201}
202/**
203 * Send body of response
204 */
205declare type Send<T> = (body: T) => void;
206/**
207 * Next `API` route response
208 */
209export declare type NextApiResponse<T = any> = ServerResponse & {
210 /**
211 * Send data `any` data in response
212 */
213 send: Send<T>;
214 /**
215 * Send data `json` data in response
216 */
217 json: Send<T>;
218 status: (statusCode: number) => NextApiResponse<T>;
219 redirect(url: string): NextApiResponse<T>;
220 redirect(status: number, url: string): NextApiResponse<T>;
221 /**
222 * Set preview data for Next.js' prerender mode
223 */
224 setPreviewData: (data: object | string, options?: {
225 /**
226 * Specifies the number (in seconds) for the preview session to last for.
227 * The given number will be converted to an integer by rounding down.
228 * By default, no maximum age is set and the preview session finishes
229 * when the client shuts down (browser is closed).
230 */
231 maxAge?: number;
232 }) => NextApiResponse<T>;
233 clearPreviewData: () => NextApiResponse<T>;
234};
235/**
236 * Next `API` route handler
237 */
238export declare type NextApiHandler<T = any> = (req: NextApiRequest, res: NextApiResponse<T>) => void | Promise<void>;
239/**
240 * Utils
241 */
242export declare function execOnce<T extends (...args: any[]) => ReturnType<T>>(fn: T): T;
243export declare function getLocationOrigin(): string;
244export declare function getURL(): string;
245export declare function getDisplayName<P>(Component: ComponentType<P>): string;
246export declare function isResSent(res: ServerResponse): boolean;
247export declare function loadGetInitialProps<C extends BaseContext, IP = {}, P = {}>(App: NextComponentType<C, IP, P>, ctx: C): Promise<IP>;
248export declare const urlObjectKeys: string[];
249export declare function formatWithValidation(url: UrlObject): string;
250export declare const SP: boolean;
251export declare const ST: boolean;
252export {};