UNPKG

5.12 kBTypeScriptView Raw
1// Type definitions for next 6.0
2// Project: https://github.com/zeit/next.js
3// Definitions by: Drew Hays <https://github.com/dru89>
4// Brice BERNARD <https://github.com/brikou>
5// James Hegedus <https://github.com/jthegedus>
6// Resi Respati <https://github.com/resir014>
7// Scott Jones <https://github.com/scottdj92>
8// Joao Vieira <https://github.com/joaovieira>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
10// TypeScript Version: 2.6
11
12/// <reference types="node" />
13
14import * as http from "http";
15import * as url from "url";
16
17import { Response as NodeResponse } from "node-fetch";
18
19declare namespace next {
20 /** Map object used in query strings. */
21 type QueryStringMapObject = Record<string, string | string[] | undefined>;
22
23 /**
24 * Context object used in methods like `getInitialProps()`
25 * <<https://github.com/zeit/next.js/issues/1651>>
26 */
27 interface NextContext {
28 /** path section of URL */
29 pathname: string;
30 /** query string section of URL parsed as an object */
31 query: QueryStringMapObject;
32 /** String of the actual path (including the query) shows in the browser */
33 asPath: string;
34 /** HTTP request object (server only) */
35 req?: http.IncomingMessage;
36 /** HTTP response object (server only) */
37 res?: http.ServerResponse;
38 /** Fetch Response object (client only) - from https://developer.mozilla.org/en-US/docs/Web/API/Response */
39 jsonPageRes?: NodeResponse;
40 /** Error object if any error is encountered during the rendering */
41 err?: Error;
42 /** Whether we're running on the server environment or not. */
43 isServer?: boolean;
44 }
45
46 type NextSFC<TProps = {}> = NextStatelessComponent<TProps>;
47 interface NextStatelessComponent<TProps = {}>
48 extends React.StatelessComponent<TProps> {
49 getInitialProps?: (ctx: NextContext) => Promise<TProps>;
50 }
51
52 type UrlLike = url.UrlObject | url.Url;
53
54 interface ServerConfig {
55 // known keys
56 webpack?: any;
57 webpackDevMiddleware?: any;
58 poweredByHeader?: boolean;
59 distDir?: string;
60 assetPrefix?: string;
61 configOrigin?: string;
62 useFileSystemPublicRoutes?: boolean;
63
64 // and since this is a config, it can take anything else, too.
65 [key: string]: any;
66 }
67
68 interface ServerOptions {
69 dir?: string;
70 dev?: boolean;
71 staticMarkup?: boolean;
72 quiet?: boolean;
73 conf?: ServerConfig;
74 }
75
76 interface Server {
77 setAssetPrefix: (cdnUrl: string) => void;
78 handleRequest(
79 req: http.IncomingMessage,
80 res: http.ServerResponse,
81 parsedUrl?: UrlLike
82 ): Promise<void>;
83 getRequestHandler(): (
84 req: http.IncomingMessage,
85 res: http.ServerResponse,
86 parsedUrl?: UrlLike
87 ) => Promise<void>;
88 prepare(): Promise<void>;
89 close(): Promise<void>;
90 defineRoutes(): Promise<void>;
91 start(): Promise<void>;
92 run(
93 req: http.IncomingMessage,
94 res: http.ServerResponse,
95 parsedUrl: UrlLike
96 ): Promise<void>;
97
98 render(
99 req: http.IncomingMessage,
100 res: http.ServerResponse,
101 pathname: string,
102 query?: QueryStringMapObject,
103 parsedUrl?: UrlLike
104 ): Promise<void>;
105 renderError(
106 err: any,
107 req: http.IncomingMessage,
108 res: http.ServerResponse,
109 pathname: string,
110 query?: QueryStringMapObject
111 ): Promise<void>;
112 render404(
113 req: http.IncomingMessage,
114 res: http.ServerResponse,
115 parsedUrl: UrlLike
116 ): Promise<void>;
117 renderToHTML(
118 req: http.IncomingMessage,
119 res: http.ServerResponse,
120 pathname: string,
121 query?: QueryStringMapObject
122 ): Promise<string>;
123 renderErrorToHTML(
124 err: any,
125 req: http.IncomingMessage,
126 res: http.ServerResponse,
127 pathname: string,
128 query?: QueryStringMapObject
129 ): Promise<string>;
130
131 serveStatic(
132 req: http.IncomingMessage,
133 res: http.ServerResponse,
134 path: string
135 ): Promise<void>;
136 isServeableUrl(path: string): boolean;
137 isInternalUrl(req: http.IncomingMessage): boolean;
138 readBuildId(): string;
139 handleBuildId(buildId: string, res: http.ServerResponse): boolean;
140 getCompilationError(
141 page: string,
142 req: http.IncomingMessage,
143 res: http.ServerResponse
144 ): Promise<any>;
145 handleBuildHash(
146 filename: string,
147 hash: string,
148 res: http.ServerResponse
149 ): void;
150 send404(res: http.ServerResponse): void;
151 }
152}
153
154declare function next(options?: next.ServerOptions): next.Server;
155export = next;