utils.d.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. import type { HtmlProps } from './html-context';
  4. import type { ComponentType } from 'react';
  5. import type { DomainLocale } from '../../server/config';
  6. import type { Env } from '@next/env';
  7. import type { IncomingMessage, ServerResponse } from 'http';
  8. import type { NextRouter } from './router/router';
  9. import type { ParsedUrlQuery } from 'querystring';
  10. import type { PreviewData } from 'next/types';
  11. import { COMPILER_NAMES } from './constants';
  12. export declare type NextComponentType<C extends BaseContext = NextPageContext, IP = {}, P = {}> = ComponentType<P> & {
  13. /**
  14. * Used for initial page load data population. Data returned from `getInitialProps` is serialized when server rendered.
  15. * Make sure to return plain `Object` without using `Date`, `Map`, `Set`.
  16. * @param ctx Context of `page`
  17. */
  18. getInitialProps?(context: C): IP | Promise<IP>;
  19. };
  20. export declare type DocumentType = NextComponentType<DocumentContext, DocumentInitialProps, DocumentProps>;
  21. export declare type AppType<P = {}> = NextComponentType<AppContextType, P, AppPropsType<any, P>>;
  22. export declare type AppTreeType = ComponentType<AppInitialProps & {
  23. [name: string]: any;
  24. }>;
  25. /**
  26. * Web vitals provided to _app.reportWebVitals by Core Web Vitals plugin developed by Google Chrome team.
  27. * https://nextjs.org/blog/next-9-4#integrated-web-vitals-reporting
  28. */
  29. export declare type NextWebVitalsMetric = {
  30. id: string;
  31. startTime: number;
  32. value: number;
  33. } & ({
  34. label: 'web-vital';
  35. name: 'FCP' | 'LCP' | 'CLS' | 'FID' | 'TTFB' | 'INP';
  36. } | {
  37. label: 'custom';
  38. name: 'Next.js-hydration' | 'Next.js-route-change-to-render' | 'Next.js-render';
  39. });
  40. export declare type Enhancer<C> = (Component: C) => C;
  41. export declare type ComponentsEnhancer = {
  42. enhanceApp?: Enhancer<AppType>;
  43. enhanceComponent?: Enhancer<NextComponentType>;
  44. } | Enhancer<NextComponentType>;
  45. export declare type RenderPageResult = {
  46. html: string;
  47. head?: Array<JSX.Element | null>;
  48. };
  49. export declare type RenderPage = (options?: ComponentsEnhancer) => DocumentInitialProps | Promise<DocumentInitialProps>;
  50. export declare type BaseContext = {
  51. res?: ServerResponse;
  52. [k: string]: any;
  53. };
  54. export 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. source?: typeof COMPILER_NAMES.server | typeof COMPILER_NAMES.edgeServer;
  70. };
  71. gsp?: boolean;
  72. gssp?: boolean;
  73. customServer?: boolean;
  74. gip?: boolean;
  75. appGip?: boolean;
  76. locale?: string;
  77. locales?: string[];
  78. defaultLocale?: string;
  79. domainLocales?: DomainLocale[];
  80. scriptLoader?: any[];
  81. isPreview?: boolean;
  82. notFoundSrcPage?: string;
  83. };
  84. /**
  85. * `Next` context
  86. */
  87. export interface NextPageContext {
  88. /**
  89. * Error object if encountered during rendering
  90. */
  91. err?: (Error & {
  92. statusCode?: number;
  93. }) | null;
  94. /**
  95. * `HTTP` request object.
  96. */
  97. req?: IncomingMessage;
  98. /**
  99. * `HTTP` response object.
  100. */
  101. res?: ServerResponse;
  102. /**
  103. * Path section of `URL`.
  104. */
  105. pathname: string;
  106. /**
  107. * Query string section of `URL` parsed as an object.
  108. */
  109. query: ParsedUrlQuery;
  110. /**
  111. * `String` of the actual path including query.
  112. */
  113. asPath?: string;
  114. /**
  115. * The currently active locale
  116. */
  117. locale?: string;
  118. /**
  119. * All configured locales
  120. */
  121. locales?: string[];
  122. /**
  123. * The configured default locale
  124. */
  125. defaultLocale?: string;
  126. /**
  127. * `Component` the tree of the App to use if needing to render separately
  128. */
  129. AppTree: AppTreeType;
  130. }
  131. export declare type AppContextType<R extends NextRouter = NextRouter> = {
  132. Component: NextComponentType<NextPageContext>;
  133. AppTree: AppTreeType;
  134. ctx: NextPageContext;
  135. router: R;
  136. };
  137. export declare type AppInitialProps<P = any> = {
  138. pageProps: P;
  139. };
  140. export declare type AppPropsType<R extends NextRouter = NextRouter, P = {}> = AppInitialProps<P> & {
  141. Component: NextComponentType<NextPageContext, any, any>;
  142. router: R;
  143. __N_SSG?: boolean;
  144. __N_SSP?: boolean;
  145. };
  146. export declare type DocumentContext = NextPageContext & {
  147. renderPage: RenderPage;
  148. defaultGetInitialProps(ctx: DocumentContext, options?: {
  149. nonce?: string;
  150. }): Promise<DocumentInitialProps>;
  151. };
  152. export declare type DocumentInitialProps = RenderPageResult & {
  153. styles?: React.ReactElement[] | React.ReactFragment | JSX.Element;
  154. };
  155. export declare type DocumentProps = DocumentInitialProps & HtmlProps;
  156. /**
  157. * Next `API` route request
  158. */
  159. export interface NextApiRequest extends IncomingMessage {
  160. /**
  161. * Object of `query` values from url
  162. */
  163. query: Partial<{
  164. [key: string]: string | string[];
  165. }>;
  166. /**
  167. * Object of `cookies` from header
  168. */
  169. cookies: Partial<{
  170. [key: string]: string;
  171. }>;
  172. body: any;
  173. env: Env;
  174. preview?: boolean;
  175. /**
  176. * Preview data set on the request, if any
  177. * */
  178. previewData?: PreviewData;
  179. }
  180. /**
  181. * Send body of response
  182. */
  183. declare type Send<T> = (body: T) => void;
  184. /**
  185. * Next `API` route response
  186. */
  187. export declare type NextApiResponse<T = any> = ServerResponse & {
  188. /**
  189. * Send data `any` data in response
  190. */
  191. send: Send<T>;
  192. /**
  193. * Send data `json` data in response
  194. */
  195. json: Send<T>;
  196. status: (statusCode: number) => NextApiResponse<T>;
  197. redirect(url: string): NextApiResponse<T>;
  198. redirect(status: number, url: string): NextApiResponse<T>;
  199. /**
  200. * Set preview data for Next.js' prerender mode
  201. */
  202. setPreviewData: (data: object | string, options?: {
  203. /**
  204. * Specifies the number (in seconds) for the preview session to last for.
  205. * The given number will be converted to an integer by rounding down.
  206. * By default, no maximum age is set and the preview session finishes
  207. * when the client shuts down (browser is closed).
  208. */
  209. maxAge?: number;
  210. /**
  211. * Specifies the path for the preview session to work under. By default,
  212. * the path is considered the "default path", i.e., any pages under "/".
  213. */
  214. path?: string;
  215. }) => NextApiResponse<T>;
  216. /**
  217. * Clear preview data for Next.js' prerender mode
  218. */
  219. clearPreviewData: (options?: {
  220. path?: string;
  221. }) => NextApiResponse<T>;
  222. /**
  223. * @deprecated `unstable_revalidate` has been renamed to `revalidate`
  224. */
  225. unstable_revalidate: () => void;
  226. revalidate: (urlPath: string, opts?: {
  227. unstable_onlyGenerated?: boolean;
  228. }) => Promise<void>;
  229. };
  230. /**
  231. * Next `API` route handler
  232. */
  233. export declare type NextApiHandler<T = any> = (req: NextApiRequest, res: NextApiResponse<T>) => unknown | Promise<unknown>;
  234. /**
  235. * Utils
  236. */
  237. export declare function execOnce<T extends (...args: any[]) => ReturnType<T>>(fn: T): T;
  238. export declare const isAbsoluteUrl: (url: string) => boolean;
  239. export declare function getLocationOrigin(): string;
  240. export declare function getURL(): string;
  241. export declare function getDisplayName<P>(Component: ComponentType<P>): string;
  242. export declare function isResSent(res: ServerResponse): boolean;
  243. export declare function normalizeRepeatedSlashes(url: string): string;
  244. export declare function loadGetInitialProps<C extends BaseContext, IP = {}, P = {}>(App: NextComponentType<C, IP, P>, ctx: C): Promise<IP>;
  245. declare let warnOnce: (_: string) => void;
  246. export { warnOnce };
  247. export declare const SP: boolean;
  248. export declare const ST: boolean;
  249. export declare class DecodeError extends Error {
  250. }
  251. export declare class NormalizeError extends Error {
  252. }
  253. export declare class PageNotFoundError extends Error {
  254. code: string;
  255. constructor(page: string);
  256. }
  257. export declare class MissingStaticPage extends Error {
  258. constructor(page: string, message: string);
  259. }
  260. export declare class MiddlewareNotFoundError extends Error {
  261. code: string;
  262. constructor();
  263. }
  264. export interface CacheFs {
  265. readFile(f: string): Promise<string>;
  266. readFileSync(f: string): string;
  267. writeFile(f: string, d: any): Promise<void>;
  268. mkdir(dir: string): Promise<void | string>;
  269. stat(f: string): Promise<{
  270. mtime: Date;
  271. }>;
  272. }