diff --git a/types/next/.prettierrc b/types/next/.prettierrc new file mode 100644 index 0000000000..8682c4845c --- /dev/null +++ b/types/next/.prettierrc @@ -0,0 +1,5 @@ +{ + "parser": "typescript", + "tabWidth": 4, + "trailingComma": "all" +} diff --git a/types/next/document.d.ts b/types/next/document.d.ts index dfb34359e1..4696819626 100644 --- a/types/next/document.d.ts +++ b/types/next/document.d.ts @@ -1,4 +1,4 @@ -import * as React from 'react'; +import * as React from "react"; export interface DocumentProps { __NEXT_DATA__?: any; diff --git a/types/next/dynamic.d.ts b/types/next/dynamic.d.ts index 4271f89c96..62d53f1e72 100644 --- a/types/next/dynamic.d.ts +++ b/types/next/dynamic.d.ts @@ -1,16 +1,29 @@ -import * as React from 'react'; +import * as React from "react"; export interface DynamicOptions { loading?: React.ComponentType; ssr?: boolean; - modules?(props: TCProps & TLProps): { [key: string]: Promise> }; - render?(props: TCProps & TLProps, modules: { [key: string]: React.ComponentType }): void; + modules?( + props: TCProps & TLProps, + ): { [key: string]: Promise> }; + render?( + props: TCProps & TLProps, + modules: { [key: string]: React.ComponentType }, + ): void; } export class SameLoopPromise extends Promise { - constructor(executor: (resolve: (value?: T) => void, reject: (reason?: any) => void) => void); + constructor( + executor: ( + resolve: (value?: T) => void, + reject: (reason?: any) => void, + ) => void, + ); setResult(value: T): void; setError(value: any): void; runIfNeeded(): void; } -export default function(componentPromise: Promise>, options?: DynamicOptions): React.ComponentType; +export default function( + componentPromise: Promise>, + options?: DynamicOptions, +): React.ComponentType; diff --git a/types/next/error.d.ts b/types/next/error.d.ts index a6d8695947..d05d25b9c8 100644 --- a/types/next/error.d.ts +++ b/types/next/error.d.ts @@ -1,2 +1,2 @@ -import * as React from 'react'; -export default class extends React.Component<{statusCode: number}> {} +import * as React from "react"; +export default class extends React.Component<{ statusCode: number }> {} diff --git a/types/next/head.d.ts b/types/next/head.d.ts index fc50b7bcf9..24fab7a076 100644 --- a/types/next/head.d.ts +++ b/types/next/head.d.ts @@ -1,4 +1,4 @@ -import * as React from 'react'; +import * as React from "react"; export function defaultHead(): JSX.Element[]; export default class extends React.Component { diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 17f3848bca..4942e778c5 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -6,58 +6,109 @@ /// -import * as http from 'http'; -import * as url from 'url'; +import * as http from "http"; +import * as url from "url"; declare namespace next { - type UrlLike = url.UrlObject | url.Url; + type UrlLike = url.UrlObject | url.Url; - interface ServerConfig { - // known keys - webpack?: any; - webpackDevMiddleware?: any; - poweredByHeader?: boolean; - distDir?: string; - assetPrefix?: string; - configOrigin?: string; - useFileSystemPublicRoutes?: boolean; + interface ServerConfig { + // known keys + webpack?: any; + webpackDevMiddleware?: any; + poweredByHeader?: boolean; + distDir?: string; + assetPrefix?: string; + configOrigin?: string; + useFileSystemPublicRoutes?: boolean; - // and since this is a config, it can take anything else, too. - [key: string]: any; - } + // and since this is a config, it can take anything else, too. + [key: string]: any; + } - interface ServerOptions { - dir?: string; - dev?: boolean; - staticMarkup?: boolean; - quiet?: boolean; - conf?: ServerConfig; - } + interface ServerOptions { + dir?: string; + dev?: boolean; + staticMarkup?: boolean; + quiet?: boolean; + conf?: ServerConfig; + } - interface Server { - handleRequest(req: http.IncomingMessage, res: http.ServerResponse, parsedUrl?: UrlLike): Promise; - getRequestHandler(): (req: http.IncomingMessage, res: http.ServerResponse, parsedUrl?: UrlLike) => Promise; - prepare(): Promise; - close(): Promise; - defineRoutes(): Promise; - start(): Promise; - run(req: http.IncomingMessage, res: http.ServerResponse, parsedUrl: UrlLike): Promise; + interface Server { + handleRequest( + req: http.IncomingMessage, + res: http.ServerResponse, + parsedUrl?: UrlLike, + ): Promise; + getRequestHandler(): ( + req: http.IncomingMessage, + res: http.ServerResponse, + parsedUrl?: UrlLike, + ) => Promise; + prepare(): Promise; + close(): Promise; + defineRoutes(): Promise; + start(): Promise; + run( + req: http.IncomingMessage, + res: http.ServerResponse, + parsedUrl: UrlLike, + ): Promise; - render(req: http.IncomingMessage, res: http.ServerResponse, pathname: string, query?: {[key: string]: any}, parsedUrl?: UrlLike): Promise; - renderError(err: any, req: http.IncomingMessage, res: http.ServerResponse, pathname: string, query?: {[key: string]: any}): Promise; - render404(req: http.IncomingMessage, res: http.ServerResponse, parsedUrl: UrlLike): Promise; - renderToHTML(req: http.IncomingMessage, res: http.ServerResponse, pathname: string, query?: {[key: string]: any}): Promise; - renderErrorToHTML(err: any, req: http.IncomingMessage, res: http.ServerResponse, pathname: string, query?: {[key: string]: any}): Promise; + render( + req: http.IncomingMessage, + res: http.ServerResponse, + pathname: string, + query?: { [key: string]: any }, + parsedUrl?: UrlLike, + ): Promise; + renderError( + err: any, + req: http.IncomingMessage, + res: http.ServerResponse, + pathname: string, + query?: { [key: string]: any }, + ): Promise; + render404( + req: http.IncomingMessage, + res: http.ServerResponse, + parsedUrl: UrlLike, + ): Promise; + renderToHTML( + req: http.IncomingMessage, + res: http.ServerResponse, + pathname: string, + query?: { [key: string]: any }, + ): Promise; + renderErrorToHTML( + err: any, + req: http.IncomingMessage, + res: http.ServerResponse, + pathname: string, + query?: { [key: string]: any }, + ): Promise; - serveStatic(req: http.IncomingMessage, res: http.ServerResponse, path: string): Promise; - isServeableUrl(path: string): boolean; - isInternalUrl(req: http.IncomingMessage): boolean; - readBuildId(): string; - handleBuildId(buildId: string, res: http.ServerResponse): boolean; - getCompilationError(page: string, req: http.IncomingMessage, res: http.ServerResponse): Promise; - handleBuildHash(filename: string, hash: string, res: http.ServerResponse): void; - send404(res: http.ServerResponse): void; - } + serveStatic( + req: http.IncomingMessage, + res: http.ServerResponse, + path: string, + ): Promise; + isServeableUrl(path: string): boolean; + isInternalUrl(req: http.IncomingMessage): boolean; + readBuildId(): string; + handleBuildId(buildId: string, res: http.ServerResponse): boolean; + getCompilationError( + page: string, + req: http.IncomingMessage, + res: http.ServerResponse, + ): Promise; + handleBuildHash( + filename: string, + hash: string, + res: http.ServerResponse, + ): void; + send404(res: http.ServerResponse): void; + } } declare function next(options?: next.ServerOptions): next.Server; diff --git a/types/next/link.d.ts b/types/next/link.d.ts index d931dd550d..36c2e33546 100644 --- a/types/next/link.d.ts +++ b/types/next/link.d.ts @@ -1,5 +1,5 @@ -import * as url from 'url'; -import * as React from 'react'; +import * as url from "url"; +import * as React from "react"; export type UrlLike = url.UrlObject | url.Url; export interface LinkState { diff --git a/types/next/router.d.ts b/types/next/router.d.ts index 9f937ca6ba..90a2e3af55 100644 --- a/types/next/router.d.ts +++ b/types/next/router.d.ts @@ -1,5 +1,5 @@ -import * as React from 'react'; -import * as url from 'url'; +import * as React from "react"; +import * as url from "url"; type UrlLike = url.UrlObject | url.Url; @@ -14,7 +14,9 @@ export interface SingletonRouter { ready(cb: RouterCallback): void; // router properties - readonly components: { [key: string]: { Component: React.ComponentType, err: any } }; + readonly components: { + [key: string]: { Component: React.ComponentType; err: any }; + }; readonly pathname: string; readonly route: string; readonly asPath?: string; @@ -23,8 +25,16 @@ export interface SingletonRouter { // router methods reload(route: string): Promise; back(): void; - push(url: string|UrlLike, as?: string|UrlLike, options?: EventChangeOptions): Promise; - replace(url: string|UrlLike, as?: string|UrlLike, options?: EventChangeOptions): Promise; + push( + url: string | UrlLike, + as?: string | UrlLike, + options?: EventChangeOptions, + ): Promise; + replace( + url: string | UrlLike, + as?: string | UrlLike, + options?: EventChangeOptions, + ): Promise; prefetch(url: string): Promise>; // router events @@ -35,7 +45,9 @@ export interface SingletonRouter { onRouteChangeError?(error: any, url: string): void; } -export function withRouter(Component: React.ComponentType): React.ComponentType; +export function withRouter( + Component: React.ComponentType, +): React.ComponentType; export const Singleton: SingletonRouter; export default Singleton; diff --git a/types/next/test/next-document-tests.tsx b/types/next/test/next-document-tests.tsx index 4fbb25948b..0177d1d451 100644 --- a/types/next/test/next-document-tests.tsx +++ b/types/next/test/next-document-tests.tsx @@ -1,12 +1,12 @@ -import Document, * as document from 'next/document'; -import * as React from 'react'; +import Document, * as document from "next/document"; +import * as React from "react"; const results = ( - - - - - - - + + + + + + + ); diff --git a/types/next/test/next-dynamic-tests.tsx b/types/next/test/next-dynamic-tests.tsx index be10954de9..0b4cdde82d 100644 --- a/types/next/test/next-dynamic-tests.tsx +++ b/types/next/test/next-dynamic-tests.tsx @@ -1,23 +1,25 @@ -import dynamic, * as d from 'next/dynamic'; -import * as React from 'react'; +import dynamic, * as d from "next/dynamic"; +import * as React from "react"; // typically you'd use this with an esnext-style import() statement, but we'll make do without interface DynamicComponentProps { - foo: string; - bar: number; + foo: string; + bar: number; } async function getComponent() { - return ( - (props: DynamicComponentProps) =>
I'm an async component! {props.foo} {props.bar}
- ); + return (props: DynamicComponentProps) => ( +
+ I'm an async component! {props.foo} {props.bar} +
+ ); } interface LoadingComponentProps { - baz: boolean; + baz: boolean; } const DynamicComponent = dynamic(getComponent(), { - loading: (props: LoadingComponentProps) =>
Loading! {props.baz}
+ loading: (props: LoadingComponentProps) =>
Loading! {props.baz}
, }); -const jsx = (); +const jsx = ; diff --git a/types/next/test/next-error-tests.tsx b/types/next/test/next-error-tests.tsx index 38692ac6de..057eb95f23 100644 --- a/types/next/test/next-error-tests.tsx +++ b/types/next/test/next-error-tests.tsx @@ -1,6 +1,4 @@ -import * as React from 'react'; -import ErrorComponent from 'next/error'; +import * as React from "react"; +import ErrorComponent from "next/error"; -const result = ( - -); +const result = ; diff --git a/types/next/test/next-head-tests.tsx b/types/next/test/next-head-tests.tsx index c81333700c..735402ca9a 100644 --- a/types/next/test/next-head-tests.tsx +++ b/types/next/test/next-head-tests.tsx @@ -1,19 +1,11 @@ -import Head, * as head from 'next/head'; -import * as React from 'react'; +import Head, * as head from "next/head"; +import * as React from "react"; const elements: JSX.Element[] = head.defaultHead(); -const jsx = ( - - {elements} - -); +const jsx = {elements}; if (!Head.canUseDOM) { - Head.rewind().map( - x => [x.key, x.props, x.type] - ); + Head.rewind().map(x => [x.key, x.props, x.type]); } -Head.peek().map( - x => [x.key, x.props, x.type] -); +Head.peek().map(x => [x.key, x.props, x.type]); diff --git a/types/next/test/next-link-tests.tsx b/types/next/test/next-link-tests.tsx index 281c6aff7a..2a85fec1ad 100644 --- a/types/next/test/next-link-tests.tsx +++ b/types/next/test/next-link-tests.tsx @@ -1,13 +1,23 @@ -import Link from 'next/link'; -import * as React from 'react'; +import Link from "next/link"; +import * as React from "react"; const links = ( -
- { console.log("Handled error!", e); }} prefetch replace scroll shallow> - Gotta link to somewhere! - - - All props are optional! - -
+
+ { + console.log("Handled error!", e); + }} + prefetch + replace + scroll + shallow + > + Gotta link to somewhere! + + + All props are optional! + +
); diff --git a/types/next/test/next-router-tests.tsx b/types/next/test/next-router-tests.tsx index e713f127b7..fe82d6f0b2 100644 --- a/types/next/test/next-router-tests.tsx +++ b/types/next/test/next-router-tests.tsx @@ -1,28 +1,34 @@ -import Router, * as r from 'next/router'; -import * as React from 'react'; -import * as qs from 'querystring'; +import Router, * as r from "next/router"; +import * as React from "react"; +import * as qs from "querystring"; -Router.readyCallbacks.push(() => { console.log("I'll get called when the router initializes."); }); -Router.ready(() => { console.log("I'll get called immediately if the router initializes, or when it eventually does."); }); +Router.readyCallbacks.push(() => { + console.log("I'll get called when the router initializes."); +}); +Router.ready(() => { + console.log( + "I'll get called immediately if the router initializes, or when it eventually does.", + ); +}); // Access readonly properties of the router. Object.keys(Router.components).forEach(key => { - const c = Router.components[key]; - c.err.isAnAny; + const c = Router.components[key]; + c.err.isAnAny; - return ; + return ; }); function split(routeLike: string) { - routeLike.split('/').forEach(part => { - console.log("path part: ", part); - }); + routeLike.split("/").forEach(part => { + console.log("path part: ", part); + }); } if (Router.asPath) { - split(Router.asPath); - split(Router.asPath); + split(Router.asPath); + split(Router.asPath); } split(Router.pathname); @@ -31,25 +37,41 @@ const query = `?${qs.stringify(Router.query)}`; // Assign some callback methods. Router.onAppUpdated = (nextRoute: string) => console.log(nextRoute); -Router.onRouteChangeStart = (url: string) => console.log("Route is starting to change.", url); -Router.onBeforeHistoryChange = (as: string) => console.log("History hasn't changed yet.", as); -Router.onRouteChangeComplete = (url: string) => console.log("Route chaneg is complete.", url); -Router.onRouteChangeError = (err: any, url: string) => console.log("Route is starting to change.", url, err); +Router.onRouteChangeStart = (url: string) => + console.log("Route is starting to change.", url); +Router.onBeforeHistoryChange = (as: string) => + console.log("History hasn't changed yet.", as); +Router.onRouteChangeComplete = (url: string) => + console.log("Route chaneg is complete.", url); +Router.onRouteChangeError = (err: any, url: string) => + console.log("Route is starting to change.", url, err); // Call methods on the router itself. -Router.reload('/route').then(() => console.log('route was reloaded')); +Router.reload("/route").then(() => console.log("route was reloaded")); Router.back(); -Router.push('/route').then((success: boolean) => console.log('route push success: ', success)); -Router.push('/route', '/asRoute').then((success: boolean) => console.log('route push success: ', success)); -Router.push('/route', '/asRoute', {shallow: false}).then((success: boolean) => console.log('route push success: ', success)); +Router.push("/route").then((success: boolean) => + console.log("route push success: ", success), +); +Router.push("/route", "/asRoute").then((success: boolean) => + console.log("route push success: ", success), +); +Router.push("/route", "/asRoute", { shallow: false }).then((success: boolean) => + console.log("route push success: ", success), +); -Router.replace('/route').then((success: boolean) => console.log('route replace success: ', success)); -Router.replace('/route', '/asRoute').then((success: boolean) => console.log('route replace success: ', success)); -Router.replace('/route', '/asRoute', {shallow: false}).then((success: boolean) => console.log('route replace success: ', success)); +Router.replace("/route").then((success: boolean) => + console.log("route replace success: ", success), +); +Router.replace("/route", "/asRoute").then((success: boolean) => + console.log("route replace success: ", success), +); +Router.replace("/route", "/asRoute", { + shallow: false, +}).then((success: boolean) => console.log("route replace success: ", success)); -Router.prefetch('/route').then(Component => { - const element = (); +Router.prefetch("/route").then(Component => { + const element = ; }); r.withRouter(props =>
); diff --git a/types/next/test/next-tests.ts b/types/next/test/next-tests.ts index 7e9bb474e7..ddbc51ee33 100644 --- a/types/next/test/next-tests.ts +++ b/types/next/test/next-tests.ts @@ -1,56 +1,78 @@ -import createServer = require('next'); -import * as http from 'http'; -import * as url from 'url'; +import createServer = require("next"); +import * as http from "http"; +import * as url from "url"; const defaultServer: createServer.Server = createServer(); const server = createServer({ - dir: '..', - quiet: true, - conf: { - distDir: './dist', - useFileSystemPublicRoutes: false, - anotherProperty: { - key: true - } - }, + dir: "..", + quiet: true, + conf: { + distDir: "./dist", + useFileSystemPublicRoutes: false, + anotherProperty: { + key: true, + }, + }, }); const voidFunc = () => {}; -const stringFunc = (x: string) => x.split('\n'); +const stringFunc = (x: string) => x.split("\n"); server.prepare().then(voidFunc); server.close().then(voidFunc); server.defineRoutes().then(voidFunc); server.start().then(voidFunc); -const parsedUrl = url.parse('https://www.example.com'); +const parsedUrl = url.parse("https://www.example.com"); const handler = server.getRequestHandler(); function handle(req: http.IncomingMessage, res: http.ServerResponse) { - handler(req, res); - handler(req, res, parsedUrl).then(voidFunc); - server.run(req, res, parsedUrl).then(voidFunc); + handler(req, res); + handler(req, res, parsedUrl).then(voidFunc); + server.run(req, res, parsedUrl).then(voidFunc); - server.render(req, res, '/path/to/resource').then(voidFunc); - server.render(req, res, '/path/to/resource', {}, parsedUrl).then(voidFunc); - server.render(req, res, '/path/to/resource', { key: 'value' }, parsedUrl).then(voidFunc); - server.renderError(new Error(), req, res, '/path/to/resource').then(voidFunc); - server.renderError(new Error(), req, res, '/path/to/resource', { key: 'value' }).then(voidFunc); - server.renderError('this can be an error, too!', req, res, '/path/to/resource', { key: 'value' }).then(voidFunc); - server.render404(req, res, parsedUrl).then(voidFunc); + server.render(req, res, "/path/to/resource").then(voidFunc); + server.render(req, res, "/path/to/resource", {}, parsedUrl).then(voidFunc); + server + .render(req, res, "/path/to/resource", { key: "value" }, parsedUrl) + .then(voidFunc); + server + .renderError(new Error(), req, res, "/path/to/resource") + .then(voidFunc); + server + .renderError(new Error(), req, res, "/path/to/resource", { + key: "value", + }) + .then(voidFunc); + server + .renderError( + "this can be an error, too!", + req, + res, + "/path/to/resource", + { key: "value" }, + ) + .then(voidFunc); + server.render404(req, res, parsedUrl).then(voidFunc); - server.renderToHTML(req, res, '/path/to/resource', { foo: 'bar' }).then(x => x.split('\n')); - server.renderErrorToHTML(new Error(), req, res, '/path/to/resource', { foo: 'bar' }).then(x => x.split('\n')); + server + .renderToHTML(req, res, "/path/to/resource", { foo: "bar" }) + .then(x => x.split("\n")); + server + .renderErrorToHTML(new Error(), req, res, "/path/to/resource", { + foo: "bar", + }) + .then(x => x.split("\n")); - server.serveStatic(req, res, '/path/to/thing').then(voidFunc); + server.serveStatic(req, res, "/path/to/thing").then(voidFunc); - let b: boolean; - b = server.isServeableUrl('/path/to/thing'); - b = server.isInternalUrl(req); - b = server.handleBuildId('{buildId}', res); + let b: boolean; + b = server.isServeableUrl("/path/to/thing"); + b = server.isInternalUrl(req); + b = server.handleBuildId("{buildId}", res); - const s: string = server.readBuildId(); - server.getCompilationError('page', req, res).then(err => err.thisIsAnAny); - server.handleBuildHash('filename', 'hash', res); - server.send404(res); + const s: string = server.readBuildId(); + server.getCompilationError("page", req, res).then(err => err.thisIsAnAny); + server.handleBuildHash("filename", "hash", res); + server.send404(res); }