From b9c03395c01ebc3475362db060df405f1c051fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Vieira?= Date: Mon, 24 Sep 2018 18:31:50 +0100 Subject: [PATCH] Update to Next.js 7. --- types/next/app.d.ts | 13 ++++-- types/next/config.d.ts | 2 +- types/next/document.d.ts | 58 ++++++++++++++++--------- types/next/error.d.ts | 14 +++++- types/next/head.d.ts | 2 +- types/next/index.d.ts | 19 +++----- types/next/link.d.ts | 10 +++-- types/next/router.d.ts | 23 +++++----- types/next/test/next-document-tests.tsx | 52 ++++++++-------------- types/next/test/next-error-tests.tsx | 19 +++++++- types/next/test/next-tests.ts | 2 - 11 files changed, 124 insertions(+), 90 deletions(-) diff --git a/types/next/app.d.ts b/types/next/app.d.ts index eea2af428b..2d6e79faa1 100644 --- a/types/next/app.d.ts +++ b/types/next/app.d.ts @@ -19,6 +19,14 @@ export interface NextAppContext { ctx: NextContext; } +/** + * Initial Props returned from base App class. + * https://github.com/zeit/next.js/blob/7.0.0/lib/app.js#L16 + */ +export interface DefaultAppIProps { + pageProps: any; +} + /** * Props passed to the App component. * Component and pageProps are dynamic - cannot infer type. @@ -28,7 +36,6 @@ export interface NextAppContext { export interface AppProps { Component: NextComponentType>; router: RouterProps; - pageProps: any; } /** @@ -41,6 +48,6 @@ export interface AppProps { export type AppComponentType = NextComponentType; export class Container extends React.Component {} -export default class App

extends React.Component

{ - static getInitialProps(context: NextAppContext): Promise<{ pageProps: any }>; +export default class App

extends React.Component

{ + static getInitialProps(context: NextAppContext): Promise; } diff --git a/types/next/config.d.ts b/types/next/config.d.ts index 1b2e330bb0..8466b60a4f 100644 --- a/types/next/config.d.ts +++ b/types/next/config.d.ts @@ -1 +1 @@ -export default function(): {serverRuntimeConfig: any, publicRuntimeConfig: any}; +export default function(): { serverRuntimeConfig: any; publicRuntimeConfig: any }; diff --git a/types/next/document.d.ts b/types/next/document.d.ts index 8296a8f136..febd36eb4a 100644 --- a/types/next/document.d.ts +++ b/types/next/document.d.ts @@ -3,14 +3,9 @@ import { NextContext, NextComponentType } from "."; import { DefaultQuery } from "./router"; export interface RenderPageResponse { - buildManifest: { [key: string]: any }; - chunks: { - names: string[]; - filenames: string[]; - }; + buildManifest: Record; html?: string; - head: Array>; - errorHtml: string; + head?: Array>; } export interface PageProps { @@ -38,24 +33,42 @@ export interface NextDocumentContext exte } /** - * Props passed to the Document component. + * Initial Props returned from base Document class. + * https://github.com/zeit/next.js/blob/7.0.0/server/document.js#L16 */ -export interface DocumentProps { - __NEXT_DATA__?: any; - dev?: boolean; - chunks?: { - names: string[]; - filenames: string[]; - }; - html?: string; - head?: Array>; - errorHtml?: string; +export interface DefaultDocumentIProps extends RenderPageResponse { styles?: Array>; - [key: string]: any; +} + +/** + * Props passed to the Document component. + * https://github.com/zeit/next.js/blob/7.0.0/server/render.js#L165 + */ +export interface DocumentProps { + __NEXT_DATA__: { + props: any; + page: string; + pathname: string; + query: Q; + buildId: string; + assetPrefix?: string; + runtimeConfig?: any; + nextExport?: boolean; + err?: any; + }; + dev: boolean; + dir?: string; + staticMarkup: boolean; + buildManifest: Record; + devFiles: string[]; + files: string[]; + dynamicImports: string[]; + assetPrefix: string; } /** * Props supported by the Head component. + * https://github.com/zeit/next.js/blob/7.0.0/server/document.js#L42 */ export interface HeadProps { nonce?: string; @@ -64,6 +77,7 @@ export interface HeadProps { /** * Props supported by the NextScript component. + * https://github.com/zeit/next.js/blob/7.0.0/server/document.js#L141 */ export interface NextScriptProps { nonce?: string; @@ -86,6 +100,8 @@ export type DocumentComponentType = NextCompon export class Head extends React.Component {} export class Main extends React.Component {} export class NextScript extends React.Component {} -export default class Document

extends React.Component

{ - static getInitialProps(context: NextDocumentContext): DocumentProps; +export default class Document

extends React.Component< + P & DefaultDocumentIProps & DocumentProps +> { + static getInitialProps(context: NextDocumentContext): DefaultDocumentIProps; } diff --git a/types/next/error.d.ts b/types/next/error.d.ts index d05d25b9c8..09d7e31860 100644 --- a/types/next/error.d.ts +++ b/types/next/error.d.ts @@ -1,2 +1,14 @@ import * as React from "react"; -export default class extends React.Component<{ statusCode: number }> {} +import { NextContext } from "."; + +/** + * Initial props returned from base Error class. + * https://github.com/zeit/next.js/blob/7.0.0/lib/error.js#L7 + */ +export interface DefaultErrorIProps { + statusCode: number; +} + +export default class Error

extends React.Component

{ + static getInitialProps(context: NextContext): DefaultErrorIProps; +} diff --git a/types/next/head.d.ts b/types/next/head.d.ts index 24fab7a076..d46b371d11 100644 --- a/types/next/head.d.ts +++ b/types/next/head.d.ts @@ -1,7 +1,7 @@ import * as React from "react"; export function defaultHead(): JSX.Element[]; -export default class extends React.Component { +export default class Head extends React.Component { static canUseDOM: boolean; static peek(): Array>; static rewind(): Array>; diff --git a/types/next/index.d.ts b/types/next/index.d.ts index f4b7affedd..16f3fb7f02 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for next 6.1 +// Type definitions for next 7.0 // Project: https://github.com/zeit/next.js // Definitions by: Drew Hays // Brice BERNARD @@ -16,7 +16,7 @@ import * as url from "url"; import { Response as NodeResponse } from "node-fetch"; -import { SingletonRouter, DefaultQuery } from "./router"; +import { SingletonRouter, DefaultQuery, UrlLike } from "./router"; declare namespace next { // Deprecated @@ -24,12 +24,10 @@ declare namespace next { type ServerConfig = NextConfig; // End Deprecated - type UrlLike = url.UrlObject | url.Url; - /** * Context object used in methods like `getInitialProps()` - * https://github.com/zeit/next.js/blob/6.1.1/server/render.js#L77 - * https://github.com/zeit/next.js/blob/6.1.1/readme.md#fetching-data-and-component-lifecycle + * https://github.com/zeit/next.js/blob/7.0.0/server/render.js#L97 + * https://github.com/zeit/next.js/blob/7.0.0/README.md#fetching-data-and-component-lifecycle * * @template Q Query object schema. */ @@ -52,7 +50,7 @@ declare namespace next { /** * Next.js config schema. - * https://github.com/zeit/next.js/blob/6.1.1/server/config.js#L10 + * https://github.com/zeit/next.js/blob/7.0.0/server/config.js#L9 */ interface NextConfig { webpack?: any; @@ -74,7 +72,7 @@ declare namespace next { /** * Options passed to the Server constructor in Node.js. - * https://github.com/zeit/next.js/blob/6.1.1/server/index.js#L30 + * https://github.com/zeit/next.js/blob/7.0.0/server/index.js#L25 */ interface ServerOptions { dir?: string; @@ -89,7 +87,7 @@ declare namespace next { */ interface Server { // From constructor - // https://github.com/zeit/next.js/blob/6.1.1/server/index.js#L30 + // https://github.com/zeit/next.js/blob/7.0.0/server/index.js#L25 dir: string; dev: boolean; quiet: boolean; @@ -105,7 +103,6 @@ declare namespace next { distDir: string; hotReloader: any; buildId: string; - availableChunks: object; generateETags: boolean; runtimeConfig?: object; }; @@ -172,9 +169,7 @@ declare namespace next { ): Promise; isServeableUrl(path: string): boolean; readBuildId(): string; - handleBuildId(buildId: string, res: http.ServerResponse): boolean; getCompilationError(): Promise; - send404(res: http.ServerResponse): void; } /** diff --git a/types/next/link.d.ts b/types/next/link.d.ts index 36c2e33546..12e3ee5419 100644 --- a/types/next/link.d.ts +++ b/types/next/link.d.ts @@ -1,8 +1,12 @@ import * as url from "url"; import * as React from "react"; +import { UrlLike } from "./router"; -export type UrlLike = url.UrlObject | url.Url; -export interface LinkState { +// Deprecated +export type LinkState = LinkProps; +// End Deprecated + +export interface LinkProps { prefetch?: boolean; shallow?: boolean; scroll?: boolean; @@ -14,4 +18,4 @@ export interface LinkState { children: React.ReactElement; } -export default class extends React.Component {} +export default class Link extends React.Component {} diff --git a/types/next/router.d.ts b/types/next/router.d.ts index 2039b213f8..4232f356c9 100644 --- a/types/next/router.d.ts +++ b/types/next/router.d.ts @@ -1,14 +1,13 @@ import * as React from "react"; import * as url from "url"; -type UrlLike = url.UrlObject | url.Url; - -type EventName = 'routeChangeStart' - | 'routeChangeComplete' - | 'routeChangeError' - | 'beforeHistoryChange' - | 'hashChangeStart' - | 'hashChangeComplete'; +type EventName = + | "routeChangeStart" + | "routeChangeComplete" + | "routeChangeError" + | "beforeHistoryChange" + | "hashChangeStart" + | "hashChangeComplete"; interface RouteChangeError { cancelled: boolean; @@ -17,6 +16,8 @@ interface RouteChangeError { type EventHandler = (url: string) => any; type ErrorEventHandler = (err: RouteChangeError, url: string) => any; +export type UrlLike = url.UrlObject | url.Url; + export interface EventChangeOptions { shallow?: boolean; [key: string]: any; @@ -47,13 +48,13 @@ export interface RouterProps { push( url: string | UrlLike, as?: string | UrlLike, - options?: EventChangeOptions, + options?: EventChangeOptions ): Promise; reload(route: string): Promise; replace( url: string | UrlLike, as?: string | UrlLike, - options?: EventChangeOptions, + options?: EventChangeOptions ): Promise; // events (deprecated soonish) @@ -87,7 +88,7 @@ export interface WithRouterProps { // without defining props explicitly export function withRouter( // tslint:disable-next-line:no-unnecessary-generics - Component: React.ComponentType>, + Component: React.ComponentType> ): React.ComponentType; declare const Router: SingletonRouter; diff --git a/types/next/test/next-document-tests.tsx b/types/next/test/next-document-tests.tsx index 40f01adfef..c97201b6f4 100644 --- a/types/next/test/next-document-tests.tsx +++ b/types/next/test/next-document-tests.tsx @@ -1,47 +1,42 @@ -import Document, { DocumentProps, Enhancer, Head, Main, NextScript, NextDocumentContext, PageProps } from 'next/document'; +import Document, { Enhancer, Head, Main, NextScript, NextDocumentContext, PageProps } from 'next/document'; import * as React from "react"; -const basicResults = ( - - - - -

- - -); +interface WithUrlProps { + url: string; +} -const withNonce = ( - - - - -); - -class MyDoc extends Document { - static async getInitialProps({ renderPage }: NextDocumentContext) { +class MyDoc extends Document { + static getInitialProps({ req, renderPage }: NextDocumentContext) { // without callback const _page = renderPage(); // with callback const enhancer: Enhancer = (App) => (props) => (); - const { html, head, errorHtml, chunks, buildManifest } = renderPage(enhancer); + const { html, head, buildManifest } = renderPage(enhancer); - const style = {}; + const styles = [ +