From bd2218b6f6f1449636dbd32a5458c65be4ccb54b Mon Sep 17 00:00:00 2001 From: James Hegedus Date: Mon, 12 Mar 2018 20:30:25 +1100 Subject: [PATCH 001/141] [WIP] Next.js Context Object type --- types/next/index.d.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 036e9f58ce..3ebcb9b395 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -6,11 +6,23 @@ // TypeScript Version: 2.6 /// +/// import * as http from "http"; import * as url from "url"; +import * as fetch from "isomorphic-unfetch"; declare namespace next { + interface NextContext { + pathname?: string; //path section of URL + query?: any; // meant to be an object - // query string section of URL parsed as an object + asPath?: string; // String of the actual path (including the query) shows in the browser + req?: http.IncomingMessage; //HTTP request object (server only) + res?: http.ServerResponse; //HTTP response object (server only) + jsonPageRes?: fetch.IsomorphicResponse; //Fetch Response object (client only) - from https://developer.mozilla.org/en-US/docs/Web/API/Response + err?: Error; //Error object if any error is encountered during the rendering + } + type UrlLike = url.UrlObject | url.Url; interface ServerConfig { From bcf98ec1f542feb91866a95818824960ec0c796f Mon Sep 17 00:00:00 2001 From: James Hegedus Date: Mon, 12 Mar 2018 20:38:23 +1100 Subject: [PATCH 002/141] add self to receive update notifications --- types/next/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 3ebcb9b395..a6cf91ddc6 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/zeit/next.js // Definitions by: Drew Hays // Brice BERNARD +// James Hegedus // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 From 4b0b64bd58aa00102f3f081a84fcc695378257c8 Mon Sep 17 00:00:00 2001 From: James Hegedus Date: Mon, 12 Mar 2018 20:43:21 +1100 Subject: [PATCH 003/141] fix some lint errors and answer qs from PR template --- types/next/index.d.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/types/next/index.d.ts b/types/next/index.d.ts index a6cf91ddc6..77cea7062d 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for next 2.4 +// Type definitions for next 5.0 // Project: https://github.com/zeit/next.js // Definitions by: Drew Hays // Brice BERNARD @@ -7,21 +7,21 @@ // TypeScript Version: 2.6 /// -/// import * as http from "http"; import * as url from "url"; import * as fetch from "isomorphic-unfetch"; declare namespace next { + // <> interface NextContext { - pathname?: string; //path section of URL + pathname?: string; // path section of URL query?: any; // meant to be an object - // query string section of URL parsed as an object asPath?: string; // String of the actual path (including the query) shows in the browser - req?: http.IncomingMessage; //HTTP request object (server only) - res?: http.ServerResponse; //HTTP response object (server only) - jsonPageRes?: fetch.IsomorphicResponse; //Fetch Response object (client only) - from https://developer.mozilla.org/en-US/docs/Web/API/Response - err?: Error; //Error object if any error is encountered during the rendering + req?: http.IncomingMessage; // HTTP request object (server only) + res?: http.ServerResponse; // HTTP response object (server only) + jsonPageRes?: fetch.IsomorphicResponse; // Fetch Response object (client only) - from https://developer.mozilla.org/en-US/docs/Web/API/Response + err?: Error; // Error object if any error is encountered during the rendering } type UrlLike = url.UrlObject | url.Url; From 34026b93c0468e1428770d964bae94e56c1eee8c Mon Sep 17 00:00:00 2001 From: Kiyotoshi Ichikawa Date: Thu, 5 Apr 2018 13:03:08 -0400 Subject: [PATCH 004/141] [browser-sync] Type definition for Option type is missing many valid properties. Also appending tests to cover newly added properties/interfaces. --- types/browser-sync/browser-sync-tests.ts | 276 +++++++++++++++++++++-- types/browser-sync/index.d.ts | 147 +++++++++--- 2 files changed, 379 insertions(+), 44 deletions(-) diff --git a/types/browser-sync/browser-sync-tests.ts b/types/browser-sync/browser-sync-tests.ts index bcd829367a..613f8aee25 100644 --- a/types/browser-sync/browser-sync-tests.ts +++ b/types/browser-sync/browser-sync-tests.ts @@ -8,11 +8,98 @@ import browserSync = require("browser-sync"); })(); browserSync({ - server: { - baseDir: "./" + ui: true +}); + +browserSync({ + ui: { + port: 9000, + weinre: { + port: 9001 + } } }); +browserSync({ + files: "app/css/style.css" +}); + +browserSync({ + files: [ + "app/css/style.css", + "app/js/*.js" + ] +}); + +browserSync({ + files: [ + "app/css/style.css", + "!app/js/*.js" + ] +}); + +browserSync({ + files: [ + "wp-content/themes/**/*.css", + { + match: ["wp-content/themes/**/*.css"], + fn: function (event, file) { + /** Custom event handler **/ + } + } + ] +}); + +browserSync({ + watchEvents: [ + "change", + "add", + "unlink", + "addDir", + "unlinkDir" + ] +}); + +browserSync({ + watch: true +}); + +browserSync({ + ignore: [ + "app/js/*.js" + ] +}); + +browserSync({ + single: true +}); + +browserSync({ + watchOptions: { + ignoreInitial: true, + ignored: '*.txt' + }, + files: ['./app'] +}); + +browserSync({ + files: [ + { + match: ["wp-content/themes/**/*.php"], + fn: function (event, file) { + /** Custom event handler **/ + }, + options: { + ignored: '*.txt' + } + } + ] +}); + +browserSync({ + server: "app" +}); + // multiple base directory browserSync({ server: { @@ -20,6 +107,39 @@ browserSync({ } }); +browserSync({ + server: true +}); + +browserSync({ + server: { + baseDir: "./" + } +}); + +browserSync({ + server: { + baseDir: "./", + index: "index.htm" + } +}); + +browserSync({ + server: { + baseDir: "./", + directory: true + } +}); + +browserSync({ + server: { + baseDir: "app", + serveStaticOptions: { + extensions: ["html"] + } + } +}); + browserSync({ proxy: "yourlocal.dev" }); @@ -27,8 +147,8 @@ browserSync({ browserSync({ proxy: { target: "http://yourlocal.dev", - proxyReq: function(proxyReq) { - console.log(proxyReq); + proxyReq: function (proxyReq) { + console.log(proxyReq); } } }); @@ -37,7 +157,7 @@ browserSync({ proxy: { target: "http://yourlocal.dev", proxyReq: [ - function(proxyReq) { + function (proxyReq) { console.log(proxyReq); } ] @@ -47,7 +167,7 @@ browserSync({ browserSync({ proxy: { target: "http://yourlocal.dev", - proxyRes: function(proxyRes, req, res) { + proxyRes: function (proxyRes, req, res) { console.log(proxyRes); } } @@ -57,13 +177,137 @@ browserSync({ proxy: { target: "http://yourlocal.dev", proxyRes: [ - function(proxyRes, req, res) { + function (proxyRes, req, res) { console.log(proxyRes); } ] } }); +browserSync({ + proxy: "https://yourlocal.dev", + https: { + key: "./path/to/the/key/file.key", + cert: "./path/to/the/cert/file.cer" + } +}); + +browserSync({ + port: 3000 +}); + +browserSync({ + proxy: "http://yourlocal.dev", + serveStatic: ['.', './app/css'] +}); + +browserSync({ + proxy: "http://yourlocal.dev", + serveStatic: [{ + route: '/assets', + dir: 'tmp' + }] +}); + +browserSync({ + proxy: "http://yourlocal.dev", + serveStatic: [{ + route: ['/assets', '/content'], + dir: 'tmp' + }] +}); + +browserSync({ + proxy: "http://yourlocal.dev", + serveStatic: [{ + route: '/assets', + dir: ['./tmp', './app'] + }] +}); + +browserSync({ + serveStatic: ['.', './app', './temp'], + serveStaticOptions: { + extensions: ['html'] // pretty urls + } +}); + +browserSync({ + https: true +}); + +browserSync({ + server: "./app", + https: true +}); + +browserSync({ + server: "./app", + https: { + key: "./path/to/the/key/file.key", + cert: "./path/to/the/cert/file.cer" + } +}); + +browserSync({ + httpModule: "http2" +}); + +browserSync({ + ghostMode: { + clicks: true, + scroll: true, + forms: { + inputs: true, + submit: true, + toggles: true + } + }, + proxy: "https://yourlocal.dev" +}); + +/** + * Not testing a bunch because they are either only string or boolean types. + * ....Also just got lazy and tired of writing the simple ones. + */ + +browserSync({ + snippetOptions: { + + // Ignore all HTML files within the templates folder + blacklist: [ + "templates/*.html" + ], + // Provide a custom Regex for inserting the snippet. + rule: { + match: /<\/body>/i, + fn: function (snippet, match) { + return snippet + match; + } + } + } +}); + +browserSync({ + rewriteRules: [ + { + match: /Browsersync/g, + fn: function (req, res, match) { + return 'kittenz'; + } + } + ] +}); + +browserSync({ + rewriteRules: [ + { + match: /(cats|kitten[sz]) are mediocre/g, + replace: "$1 are excellent" + } + ] +}); + var config = { server: { baseDir: "./" @@ -84,13 +328,13 @@ browserSync(config, function (err, bs) { browserSync.reload(); // single file -browserSync.reload( "styles.css" ); +browserSync.reload("styles.css"); // multiple files -browserSync.reload( ["styles.css", "ie.css"] ); +browserSync.reload(["styles.css", "ie.css"]); // streams support -browserSync.reload( { stream: true } ); +browserSync.reload({ stream: true }); browserSync.notify("Compiling, please wait!"); @@ -143,19 +387,19 @@ browser.exit(); browser.stream(); // -- "once" option. -browser.stream({once: true}); +browser.stream({ once: true }); // -- "match" option (string). -browser.stream({match: "**/*.js"}); +browser.stream({ match: "**/*.js" }); // -- "match" option (RegExp). -browser.stream({match: /\.js$/}); +browser.stream({ match: /\.js$/ }); // -- "match" option (function). -browser.stream({match: (testString) => true}); +browser.stream({ match: (testString) => true }); // -- "match" option (array). -browser.stream({match: ["**/*.js", /\.js$/, (testString) => true]}); +browser.stream({ match: ["**/*.js", /\.js$/, (testString) => true] }); // -- Both options. -browser.stream({once: true, match: ["**/*.js", /\.js$/, (testString) => true]}); +browser.stream({ once: true, match: ["**/*.js", /\.js$/, (testString) => true] }); diff --git a/types/browser-sync/index.d.ts b/types/browser-sync/index.d.ts index b22054c367..3b97fc8616 100644 --- a/types/browser-sync/index.d.ts +++ b/types/browser-sync/index.d.ts @@ -1,16 +1,21 @@ // Type definitions for browser-sync // Project: http://www.browsersync.io/ -// Definitions by: Asana , Joe Skeen +// Definitions by: Asana , +// Joe Skeen // Thomas "Thasmo" Deinhamer +// Kiyotoshi Ichikawa // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 /// /// +/// import * as chokidar from "chokidar"; import * as fs from "fs"; import * as http from "http"; import * as mm from "micromatch"; +import { ServeStaticOptions } from "serve-static"; declare namespace browserSync { interface Options { @@ -20,7 +25,7 @@ declare namespace browserSync { * * port - Default: 3001 * weinre.port - Default: 8080 - * Note: requires at least version 2.0.0 + * Note: Requires at least version 2.0.0. */ ui?: UIOptions | boolean; /** @@ -29,20 +34,40 @@ declare namespace browserSync { * patterns. * Default: false */ - files?: string | (string | FileCallback)[]; + files?: string | (string | FileCallback | object)[]; + /** + * Specify which file events to respond to. + * Available events: `add`, `change`, `unlink`, `addDir`, `unlinkDir` + */ + watchEvents?: string[]; + /** + * Watch files automatically. + */ + watch?: boolean; + /** + * Patterns for any watchers to ignore. + * Anything provided here will end up inside 'watchOptions.ignored'. + */ + ignore?: string[]; + /** + * Serve an index.html file for all non-asset routes. + * Useful when using client-routers. + */ + single?: boolean; /** * File watching options that get passed along to Chokidar. Check their docs for available options * Default: undefined - * Note: requires at least version 2.6.0 + * Note: Requires at least version 2.6.0. */ watchOptions?: chokidar.WatchOptions; /** * Use the built-in static server for basic HTML/JS/CSS websites. * Default: false */ - server?: ServerOptions; + server?: string | boolean | string[] | ServerOptions; /** * Proxy an EXISTING vhost. Browsersync will wrap your vhost with a proxy URL to view your site. + * Passing only a URL as a string equates to passing only target property of ProxyOptions type. * target - Default: undefined * ws - Default: undefined * middleware - Default: undefined @@ -50,25 +75,40 @@ declare namespace browserSync { * proxyRes - Default: undefined * proxyReq - Default: undefined */ - proxy?: string | boolean | ProxyOptions; + proxy?: string | ProxyOptions; /** * Use a specific port (instead of the one auto-detected by Browsersync) * Default: 3000 */ port?: number; + /** + * Functions or actual plugins used as middleware. + */ + middleware?: MiddlewareHandler | PerRouteMiddleware | (MiddlewareHandler | PerRouteMiddleware)[]; /** * Add additional directories from which static files should be served. * Should only be used in proxy or snippet mode. * Default: [] - * Note: requires at least version 2.8.0 + * Note: Requires at least version 2.8.0. */ - serveStatic?: string[]; + serveStatic?: (string | { route?: string | string[], dir?: string | string[]})[]; + /** + * Options that are passed to the serve-static middleware when you use the + * string[] syntax: eg: `serveStatic: ['./app']`. + * Please see [serve-static](https://github.com/expressjs/serve-static) for details. + */ + serveStaticOptions?: ServeStaticOptions; /** * Enable https for localhost development. - * Note - this is not needed for proxy option as it will be inferred from your target url. - * Note: requires at least version 1.3.0 + * Note: This may not be needed for proxy option as it will try to infer from your target url. + * Note: If privacy error is encountered please see HttpsOptions below, setting those will resolve. + * Note: Requires at least version 1.3.0. */ - https?: boolean; + https?: boolean | HttpsOptions; + /** + * Override http module to allow using 3rd party server modules (such as http2). + */ + httpModule?: string; /** * Clicks, Scrolls & Form inputs on any device will be mirrored to all others. * clicks - Default: true @@ -84,7 +124,7 @@ declare namespace browserSync { /** * Change the console logging prefix. Useful if you're creating your own project based on Browsersync * Default: BS - * Note: requires at least version 1.5.1 + * Note: Requires at least version 1.5.1. */ logPrefix?: string; /** @@ -100,19 +140,19 @@ declare namespace browserSync { /** * Log the snippet to the console when you're in snippet mode (no proxy/server) * Default: true - * Note: requires at least version 1.5.2 + * Note: Requires at least version 1.5.2. */ logSnippet?: boolean; /** * You can control how the snippet is injected onto each page via a custom regex + function. * You can also provide patterns for certain urls that should be ignored from the snippet injection. - * Note: requires at least version 2.0.0 + * Note: Requires at least version 2.0.0. */ snippetOptions?: SnippetOptions; /** * Add additional HTML rewriting rules. * Default: false - * Note: requires at least version 2.4.0 + * Note: Requires at least version 2.4.0. */ rewriteRules?: boolean | RewriteRules[]; /** @@ -139,7 +179,7 @@ declare namespace browserSync { /** * Add HTTP access control (CORS) headers to assets served by Browsersync. * Default: false - * Note: requires at least version 2.16.0 + * Note: Requires at least version 2.16.0. */ cors?: boolean; /** @@ -177,12 +217,12 @@ declare namespace browserSync { /** * Sync the scroll position of any element on the page. Add any amount of CSS selectors * Default: [] - * Note: requires at least version 2.9.0 + * Note: Requires at least version 2.9.0. */ scrollElements?: string[]; /** * Default: [] - * Note: requires at least version 2.9.0 + * Note: Requires at least version 2.9.0. * Sync the scroll position of any element on the page - where any scrolled element will cause * all others to match scroll position. This is helpful when a breakpoint alters which element * is actually scrolling @@ -197,13 +237,18 @@ declare namespace browserSync { /** * Restrict the frequency in which browser:reload events can be emitted to connected clients * Default: 0 - * Note: requires at least version 2.6.0 + * Note: Requires at least version 2.6.0. */ reloadDebounce?: number; + /** + * Emit only the first event during sequential time windows of a specified duration. + * Note: Requires at least version 2.13.0. + */ + reloadThrottle?: number; /** * User provided plugins * Default: [] - * Note: requires at least version 2.6.0 + * Note: Requires at least version 2.6.0. */ plugins?: any[]; /** @@ -224,6 +269,10 @@ declare namespace browserSync { * Override host detection if you know the correct IP to use */ host?: string; + /** + * Support environments where dynamic hostnames are not required (ie: electron). + */ + localOnly?: boolean; /** * Send file-change events to the browser * Default: true @@ -234,10 +283,16 @@ declare namespace browserSync { * Default: true */ timestamps?: boolean; + /** + * ¯\_(ツ)_/¯ + * Best guess, when ghostMode (or SocketIO?) is setup the events + * listed here will be emitted and able to hook into. + */ + clientEvents?: string[]; /** * Alter the script path for complete control over where the Browsersync Javascript is served * from. Whatever you return from this function will be used as the script path. - * Note: requires at least version 1.5.0 + * Note: Requires at least version 1.5.0. */ scriptPath?: (path: string) => string; /** @@ -248,10 +303,21 @@ declare namespace browserSync { * domain - Default: undefined * port - Default: undefined * clients.heartbeatTimeout - Default: 5000 - * Note: requires at least version 1.6.2 + * Note: Requires at least version 1.6.2. */ socket?: SocketOptions; - middleware?: MiddlewareHandler | PerRouteMiddleware | (MiddlewareHandler | PerRouteMiddleware)[]; + /** + * ¯\_(ツ)_/¯ + */ + tagNames?: TagNamesOptions; + /** + * ¯\_(ツ)_/¯ + */ + injectFileTypes?: string[]; + /** + * ¯\_(ツ)_/¯ + */ + excludeFileTypes?: string[]; } interface Hash { @@ -287,6 +353,7 @@ declare namespace browserSync { routes?: Hash; /** configure custom middleware */ middleware?: (MiddlewareHandler | PerRouteMiddleware)[]; + serveStaticOptions?: ServeStaticOptions } interface ProxyOptions { @@ -298,6 +365,11 @@ declare namespace browserSync { proxyReq?: ((res: http.ServerRequest) => any)[] | ((res: http.ServerRequest) => any); } + interface HttpsOptions { + key?: string; + cert?: string; + } + interface MiddlewareHandler { (req: http.IncomingMessage, res: http.ServerResponse, next: Function): any; } @@ -310,11 +382,17 @@ declare namespace browserSync { interface GhostOptions { clicks?: boolean; scroll?: boolean; - forms?: boolean; + forms?: boolean | { + submit?: boolean; + inputs?: boolean; + toggles?: boolean; + }; } interface SnippetOptions { - ignorePaths?: string; + async?: boolean, + whitelist?: string[], + blacklist?: string[], rule?: { match?: RegExp; fn?: (snippet: string, match: string) => any }; } @@ -327,9 +405,22 @@ declare namespace browserSync { clients?: { heartbeatTimeout?: number; }; } + interface TagNamesOptions { + less?: string; + scss?: string; + css?: string; + jpg?: string; + jpeg?: string; + png?: string; + svg?: string; + gif?: string; + js?: string; + } + interface RewriteRules { match: RegExp; - fn: (match: string) => string; + replace?: string; + fn?: (req: http.IncomingMessage, res: http.ServerResponse, match: string) => string; } interface StreamOptions { @@ -342,7 +433,7 @@ declare namespace browserSync { * Start the Browsersync service. This will launch a server, proxy or start the snippet mode * depending on your use-case. */ - (config?: Options, callback?: (err: Error, bs: Object) => any): BrowserSyncInstance; + (config?: Options, callback?: (err: Error, bs: object) => any): BrowserSyncInstance; /** * Create a Browsersync instance * @param name an identifier that can used for retrieval later @@ -367,7 +458,7 @@ declare namespace browserSync { * Start the Browsersync service. This will launch a server, proxy or start the snippet mode * depending on your use-case. */ - init(config?: Options, callback?: (err: Error, bs: Object) => any): BrowserSyncInstance; + init(config?: Options, callback?: (err: Error, bs: object) => any): BrowserSyncInstance; /** * Reload the browser * The reload method will inform all browsers about changed files and will either cause the browser From 55194a76e4854bca216f818098be7ebd5f5b8f67 Mon Sep 17 00:00:00 2001 From: Kiyotoshi Ichikawa Date: Wed, 25 Apr 2018 15:43:30 -0400 Subject: [PATCH 005/141] Updated types and appended some. The proxy.proxyRes property could be further expressed to take one or three parameters. Its previous representation was not correct according to the documentation and default configuration the package generates. Specified types for watchEvents, open, and logLevel options. Converted type Object references to object per recommended guidelines of DefinitelyTyped repo. Converted type Function references to arrow function. More SnippetOptions properties, defined FormsOptions under GhostOptions. Added and modified tests. --- types/browser-sync/browser-sync-tests.ts | 28 ++++++++++-- types/browser-sync/index.d.ts | 56 +++++++++++++++++------- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/types/browser-sync/browser-sync-tests.ts b/types/browser-sync/browser-sync-tests.ts index 613f8aee25..c63cb0c862 100644 --- a/types/browser-sync/browser-sync-tests.ts +++ b/types/browser-sync/browser-sync-tests.ts @@ -167,8 +167,8 @@ browserSync({ browserSync({ proxy: { target: "http://yourlocal.dev", - proxyRes: function (proxyRes, req, res) { - console.log(proxyRes); + proxyRes: function (proxyResponse, req, res) { + console.log(proxyResponse); } } }); @@ -177,8 +177,28 @@ browserSync({ proxy: { target: "http://yourlocal.dev", proxyRes: [ - function (proxyRes, req, res) { - console.log(proxyRes); + function (proxyResponse, req, res) { + console.log(proxyResponse); + } + ] + } +}); + +browserSync({ + proxy: { + target: "http://yourlocal.dev", + proxyRes: function (res) { + console.log(res); + } + } +}); + +browserSync({ + proxy: { + target: "http://yourlocal.dev", + proxyRes: [ + function (res) { + console.log(res); } ] } diff --git a/types/browser-sync/index.d.ts b/types/browser-sync/index.d.ts index 3b97fc8616..de0a457266 100644 --- a/types/browser-sync/index.d.ts +++ b/types/browser-sync/index.d.ts @@ -39,7 +39,7 @@ declare namespace browserSync { * Specify which file events to respond to. * Available events: `add`, `change`, `unlink`, `addDir`, `unlinkDir` */ - watchEvents?: string[]; + watchEvents?: WatchEvents | string[]; /** * Watch files automatically. */ @@ -72,7 +72,7 @@ declare namespace browserSync { * ws - Default: undefined * middleware - Default: undefined * reqHeaders - Default: undefined - * proxyRes - Default: undefined + * proxyRes - Default: undefined (http.ServerResponse if expecting single parameter) * proxyReq - Default: undefined */ proxy?: string | ProxyOptions; @@ -91,7 +91,7 @@ declare namespace browserSync { * Default: [] * Note: Requires at least version 2.8.0. */ - serveStatic?: (string | { route?: string | string[], dir?: string | string[]})[]; + serveStatic?: StaticOptions[] | string[]; /** * Options that are passed to the serve-static middleware when you use the * string[] syntax: eg: `serveStatic: ['./app']`. @@ -120,7 +120,7 @@ declare namespace browserSync { * Can be either "info", "debug", "warn", or "silent" * Default: info */ - logLevel?: string; + logLevel?: LogLevel; /** * Change the console logging prefix. Useful if you're creating your own project based on Browsersync * Default: BS @@ -170,7 +170,7 @@ declare namespace browserSync { * Decide which URL to open automatically when Browsersync starts. Defaults to "local" if none set. * Can be true, local, external, ui, ui-external, tunnel or false */ - open?: string | boolean; + open?: OpenOptions | boolean; /** * The browser(s) to open * Default: default @@ -320,6 +320,12 @@ declare namespace browserSync { excludeFileTypes?: string[]; } + type WatchEvents = "add" | "change" | "unlink" | "addDir" | "unlinkDir"; + + type LogLevel = "info" | "debug" | "warn" | "silent"; + + type OpenOptions = "local" | "external" | "ui" | "ui-external" | "tunnel"; + interface Hash { [path: string]: T; } @@ -353,16 +359,21 @@ declare namespace browserSync { routes?: Hash; /** configure custom middleware */ middleware?: (MiddlewareHandler | PerRouteMiddleware)[]; - serveStaticOptions?: ServeStaticOptions + serveStaticOptions?: ServeStaticOptions; } interface ProxyOptions { target?: string; middleware?: MiddlewareHandler; ws?: boolean; - reqHeaders?: (config: any) => Hash; - proxyRes?: ((res: http.ServerResponse, req: http.IncomingMessage, next: Function) => any)[] | ((res: http.ServerResponse, req: http.IncomingMessage, next: Function) => any); - proxyReq?: ((res: http.ServerRequest) => any)[] | ((res: http.ServerRequest) => any); + reqHeaders?: (config: object) => Hash; + proxyRes?: ProxyResponseMiddleware | ProxyResponseMiddleware[]; + proxyReq?: ((res: http.ServerRequest) => void)[] | ((res: http.ServerRequest) => void); + error?: (err: NodeJS.ErrnoException, req: http.IncomingMessage, res: http.ServerResponse) => void; + } + + interface ProxyResponseMiddleware { + (proxyRes: http.ServerResponse | http.IncomingMessage, res: http.ServerResponse, req: http.IncomingMessage): void; } interface HttpsOptions { @@ -370,11 +381,17 @@ declare namespace browserSync { cert?: string; } + interface StaticOptions { + route: string | string[], + dir: string | string[] + } + interface MiddlewareHandler { - (req: http.IncomingMessage, res: http.ServerResponse, next: Function): any; + (req: http.IncomingMessage, res: http.ServerResponse, next: () => void): any; } interface PerRouteMiddleware { + id?: string; route: string; handle: MiddlewareHandler; } @@ -382,18 +399,23 @@ declare namespace browserSync { interface GhostOptions { clicks?: boolean; scroll?: boolean; - forms?: boolean | { - submit?: boolean; - inputs?: boolean; - toggles?: boolean; - }; + forms?: FormsOptions | boolean; + } + + interface FormsOptions { + inputs: boolean, + submit: boolean, + toggles: boolean } interface SnippetOptions { - async?: boolean, + async?: boolean; whitelist?: string[], blacklist?: string[], - rule?: { match?: RegExp; fn?: (snippet: string, match: string) => any }; + rule?: { + match?: RegExp; + fn?: (snippet: string, match: string) => any + }; } interface SocketOptions { From 16fe1426ca2d73ae73299e0e6a9f76c3075a65ad Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Wed, 2 May 2018 00:29:28 +0700 Subject: [PATCH 006/141] [next] NextContext type adjustments * Add renderPage property to `NextContext` * `pathname`, `query`, and `asPath` is always defined * fixed object types, add proper JSDoc to context interface --- types/next/index.d.ts | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 77cea7062d..5ecfba20da 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -15,13 +15,28 @@ import * as fetch from "isomorphic-unfetch"; declare namespace next { // <> interface NextContext { - pathname?: string; // path section of URL - query?: any; // meant to be an object - // query string section of URL parsed as an object - asPath?: string; // String of the actual path (including the query) shows in the browser - req?: http.IncomingMessage; // HTTP request object (server only) - res?: http.ServerResponse; // HTTP response object (server only) - jsonPageRes?: fetch.IsomorphicResponse; // Fetch Response object (client only) - from https://developer.mozilla.org/en-US/docs/Web/API/Response - err?: Error; // Error object if any error is encountered during the rendering + /** path section of URL */ + pathname: string + /** query string section of URL parsed as an object */ + query: { + [key: string]: any + } + /** String of the actual path (including the query) shows in the browser */ + asPath: string + /** HTTP request object (server only) */ + req?: http.IncomingMessage + /** HTTP response object (server only) */ + res?: http.ServerResponse + /** Fetch Response object (client only) - from https://developer.mozilla.org/en-US/docs/Web/API/Response */ + jsonPageRes?: fetch.IsomorphicResponse + /** Error object if any error is encountered during the rendering */ + err?: Error + /** a callback that executes the actual React rendering logic (synchronously) */ + renderPage( + cb: (enhancer: () => JSX.Element) => React.ComponentType + ): { + [key: string]: any + } } type UrlLike = url.UrlObject | url.Url; From 6145af0a3acff432e4d24a5e90ec6125bce7f5b6 Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Wed, 2 May 2018 00:34:12 +0700 Subject: [PATCH 007/141] [next] renderPage callback should be optional --- types/next/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 5ecfba20da..7f7c58140e 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -33,7 +33,7 @@ declare namespace next { err?: Error /** a callback that executes the actual React rendering logic (synchronously) */ renderPage( - cb: (enhancer: () => JSX.Element) => React.ComponentType + cb?: (enhancer: () => JSX.Element) => React.ComponentType ): { [key: string]: any } From 07de2cfd39a8da31d5013d77180ed45a72637ff1 Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Sat, 5 May 2018 00:34:47 +0700 Subject: [PATCH 008/141] [next] target next@6.0.0 `next@6.0.0` is out, so let's change the target version to it --- types/next/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 7f7c58140e..3abb22a2a4 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for next 5.0 +// Type definitions for next 6.0 // Project: https://github.com/zeit/next.js // Definitions by: Drew Hays // Brice BERNARD From a47662329cfbdd2978f58a4591248069341c48df Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Fri, 11 May 2018 22:38:21 +0700 Subject: [PATCH 009/141] Add NextStatelessComponent interface --- types/next/index.d.ts | 6 ++++++ types/next/test/next-component-tests.tsx | 26 ++++++++++++++++++++++++ types/next/tsconfig.json | 3 ++- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 types/next/test/next-component-tests.tsx diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 3abb22a2a4..52c34542ba 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -3,6 +3,7 @@ // Definitions by: Drew Hays // Brice BERNARD // James Hegedus +// Resi Respati // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 @@ -39,6 +40,11 @@ declare namespace next { } } + type NextSFC

= NextStatelessComponent

; + interface NextStatelessComponent

extends React.StatelessComponent

{ + getInitialProps?: (ctx: NextContext) => Promise; + } + type UrlLike = url.UrlObject | url.Url; interface ServerConfig { diff --git a/types/next/test/next-component-tests.tsx b/types/next/test/next-component-tests.tsx new file mode 100644 index 0000000000..33ecb6bc93 --- /dev/null +++ b/types/next/test/next-component-tests.tsx @@ -0,0 +1,26 @@ +import * as React from "react"; +import { NextStatelessComponent } from "next"; + +interface NextComponentProps { + example: string +} + +class ClassNext extends React.Component { + async getInitialProps() { + return { example: 'example' } + } + + render() { + return ( +

I'm a stateless component! {this.props.example}
+ ) + } +} + +const StatelessNext: NextStatelessComponent = ({ example }) => ( +
I'm a stateless component! {example}
+) + +StatelessNext.getInitialProps = async () => { + return { example: 'example' } +} diff --git a/types/next/tsconfig.json b/types/next/tsconfig.json index 48d3f0c8be..d22768e4a1 100644 --- a/types/next/tsconfig.json +++ b/types/next/tsconfig.json @@ -33,6 +33,7 @@ "test/next-document-tests.tsx", "test/next-link-tests.tsx", "test/next-dynamic-tests.tsx", - "test/next-router-tests.tsx" + "test/next-router-tests.tsx", + "test/next-component-tests.tsx" ] } From ba8d541851f904fba222022c449683fe5c3d6cff Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Fri, 11 May 2018 22:42:03 +0700 Subject: [PATCH 010/141] [next] Use NodeResponse because of unfetch.IsomorphicResponse --- types/next/index.d.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 52c34542ba..d83b787b4a 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -11,7 +11,8 @@ import * as http from "http"; import * as url from "url"; -import * as fetch from "isomorphic-unfetch"; + +import { Response as NodeResponse } from "node-fetch"; declare namespace next { // <> @@ -29,7 +30,7 @@ declare namespace next { /** HTTP response object (server only) */ res?: http.ServerResponse /** Fetch Response object (client only) - from https://developer.mozilla.org/en-US/docs/Web/API/Response */ - jsonPageRes?: fetch.IsomorphicResponse + jsonPageRes?: NodeResponse /** Error object if any error is encountered during the rendering */ err?: Error /** a callback that executes the actual React rendering logic (synchronously) */ From 4dff7d985840fbe92101a8d5d3d8b5ccca9eb5f8 Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Fri, 11 May 2018 22:51:14 +0700 Subject: [PATCH 011/141] [next] Added typings for `next/app` --- types/next/app.d.ts | 10 ++++++++++ types/next/test/next-app-tests.tsx | 27 +++++++++++++++++++++++++++ types/next/tsconfig.json | 1 + 3 files changed, 38 insertions(+) create mode 100644 types/next/app.d.ts create mode 100644 types/next/test/next-app-tests.tsx diff --git a/types/next/app.d.ts b/types/next/app.d.ts new file mode 100644 index 0000000000..6d10d77af8 --- /dev/null +++ b/types/next/app.d.ts @@ -0,0 +1,10 @@ +import * as React from "react"; + +export interface AppComponentProps { + Component: React.ComponentType; + pageProps: any +} + +export class Container extends React.Component {} + +export default class App

extends React.Component

{} diff --git a/types/next/test/next-app-tests.tsx b/types/next/test/next-app-tests.tsx new file mode 100644 index 0000000000..3e7c56f87c --- /dev/null +++ b/types/next/test/next-app-tests.tsx @@ -0,0 +1,27 @@ +import * as React from "react"; +import App, { Container } from "next/app"; + +interface NextComponentProps { + example: string; +} + +class TestApp extends App { + static async getInitialProps({ Component, router, ctx }: any) { + let pageProps = {}; + + if (Component.getInitialProps) { + pageProps = await Component.getInitialProps(ctx); + } + + return { pageProps }; + } + + render() { + const { Component, pageProps } = this.props; + return ( + + + + ); + } +} diff --git a/types/next/tsconfig.json b/types/next/tsconfig.json index d22768e4a1..36aef39491 100644 --- a/types/next/tsconfig.json +++ b/types/next/tsconfig.json @@ -28,6 +28,7 @@ "router.d.ts", "config.d.ts", "test/next-tests.ts", + "test/next-app-tests.tsx", "test/next-error-tests.tsx", "test/next-head-tests.tsx", "test/next-document-tests.tsx", From 3c4579913defe15ac6bd51858f88dc17e7477d3f Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Fri, 11 May 2018 23:03:41 +0700 Subject: [PATCH 012/141] [next] run dtslint --- types/next/index.d.ts | 21 ++++++++++++--------- types/next/test/next-component-tests.tsx | 12 ++++++------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/types/next/index.d.ts b/types/next/index.d.ts index d83b787b4a..939dc27bdb 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -15,30 +15,33 @@ import * as url from "url"; import { Response as NodeResponse } from "node-fetch"; declare namespace next { - // <> + /** + * Context object used in methods like `getInitialProps()` + * <> + */ interface NextContext { /** path section of URL */ - pathname: string + pathname: string; /** query string section of URL parsed as an object */ query: { [key: string]: any - } + }; /** String of the actual path (including the query) shows in the browser */ - asPath: string + asPath: string; /** HTTP request object (server only) */ - req?: http.IncomingMessage + req?: http.IncomingMessage; /** HTTP response object (server only) */ - res?: http.ServerResponse + res?: http.ServerResponse; /** Fetch Response object (client only) - from https://developer.mozilla.org/en-US/docs/Web/API/Response */ - jsonPageRes?: NodeResponse + jsonPageRes?: NodeResponse; /** Error object if any error is encountered during the rendering */ - err?: Error + err?: Error; /** a callback that executes the actual React rendering logic (synchronously) */ renderPage( cb?: (enhancer: () => JSX.Element) => React.ComponentType ): { [key: string]: any - } + }; } type NextSFC

= NextStatelessComponent

; diff --git a/types/next/test/next-component-tests.tsx b/types/next/test/next-component-tests.tsx index 33ecb6bc93..a6e870f8e2 100644 --- a/types/next/test/next-component-tests.tsx +++ b/types/next/test/next-component-tests.tsx @@ -2,25 +2,25 @@ import * as React from "react"; import { NextStatelessComponent } from "next"; interface NextComponentProps { - example: string + example: string; } class ClassNext extends React.Component { async getInitialProps() { - return { example: 'example' } + return { example: 'example' }; } render() { return (

I'm a stateless component! {this.props.example}
- ) + ); } } const StatelessNext: NextStatelessComponent = ({ example }) => (
I'm a stateless component! {example}
-) +); StatelessNext.getInitialProps = async () => { - return { example: 'example' } -} + return { example: 'example' }; +}; From 3c5384acca044e06c4e214cf5b113b139a95cb7c Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Fri, 11 May 2018 23:12:07 +0700 Subject: [PATCH 013/141] [next] Added AppComponentContext type --- types/next/app.d.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/types/next/app.d.ts b/types/next/app.d.ts index 6d10d77af8..512c5f996f 100644 --- a/types/next/app.d.ts +++ b/types/next/app.d.ts @@ -1,10 +1,17 @@ import * as React from "react"; +import { NextContext } from "next"; export interface AppComponentProps { Component: React.ComponentType; pageProps: any } +export interface AppComponentContext { + Component: any; + router: any; // TODO: could be SingletonRouter? + ctx: NextContext; +} + export class Container extends React.Component {} export default class App

extends React.Component

{} From f3d3de950904a3e46137c31a222e1b2d4b4d4841 Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Fri, 11 May 2018 23:16:42 +0700 Subject: [PATCH 014/141] [next] include app.d.ts in bundle --- types/next/tsconfig.json | 1 + 1 file changed, 1 insertion(+) diff --git a/types/next/tsconfig.json b/types/next/tsconfig.json index 36aef39491..125953fcd6 100644 --- a/types/next/tsconfig.json +++ b/types/next/tsconfig.json @@ -20,6 +20,7 @@ }, "files": [ "index.d.ts", + "app.d.ts", "document.d.ts", "dynamic.d.ts", "error.d.ts", From 92db478980b2221b31f995a0658191902ba9bb4f Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Fri, 11 May 2018 23:17:40 +0700 Subject: [PATCH 015/141] [next] re-run dtslint --- types/next/app.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/next/app.d.ts b/types/next/app.d.ts index 512c5f996f..0296440ddb 100644 --- a/types/next/app.d.ts +++ b/types/next/app.d.ts @@ -1,9 +1,9 @@ import * as React from "react"; -import { NextContext } from "next"; +import { NextContext } from "."; export interface AppComponentProps { Component: React.ComponentType; - pageProps: any + pageProps: any; } export interface AppComponentContext { From 28b1367d70342534ff1bcd2ecedcc27b0da2f6d5 Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Sat, 12 May 2018 11:51:42 +0700 Subject: [PATCH 016/141] [next] fix tests (getInitialProps should be static) --- types/next/test/next-component-tests.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/next/test/next-component-tests.tsx b/types/next/test/next-component-tests.tsx index a6e870f8e2..fefde092ea 100644 --- a/types/next/test/next-component-tests.tsx +++ b/types/next/test/next-component-tests.tsx @@ -6,7 +6,7 @@ interface NextComponentProps { } class ClassNext extends React.Component { - async getInitialProps() { + static async getInitialProps() { return { example: 'example' }; } From 9d68e22a0c62fbd58869ab901ad09badf8eb6dcf Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Wed, 16 May 2018 21:57:28 +0700 Subject: [PATCH 017/141] [next] separate context objects for Document + fix tests --- types/next/document.d.ts | 13 +++++++++ types/next/index.d.ts | 6 ---- types/next/test/next-document-tests.tsx | 38 +++++++++++++++++++++---- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/types/next/document.d.ts b/types/next/document.d.ts index 4696819626..01344de7c8 100644 --- a/types/next/document.d.ts +++ b/types/next/document.d.ts @@ -1,4 +1,5 @@ import * as React from "react"; +import { NextContext } from "."; export interface DocumentProps { __NEXT_DATA__?: any; @@ -9,6 +10,18 @@ export interface DocumentProps { [key: string]: any; } +/** + * Context object used inside `Document` + */ +export interface NextDocumentContext extends NextContext { + /** A callback that executes the actual React rendering logic (synchronously) */ + renderPage( + cb?: (enhancer: () => JSX.Element) => React.ComponentType + ): { + [key: string]: any + }; +} + export class Head extends React.Component {} export class Main extends React.Component {} export class NextScript extends React.Component {} diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 939dc27bdb..6679420ba3 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -36,12 +36,6 @@ declare namespace next { jsonPageRes?: NodeResponse; /** Error object if any error is encountered during the rendering */ err?: Error; - /** a callback that executes the actual React rendering logic (synchronously) */ - renderPage( - cb?: (enhancer: () => JSX.Element) => React.ComponentType - ): { - [key: string]: any - }; } type NextSFC

= NextStatelessComponent

; diff --git a/types/next/test/next-document-tests.tsx b/types/next/test/next-document-tests.tsx index 0177d1d451..2b3257cd25 100644 --- a/types/next/test/next-document-tests.tsx +++ b/types/next/test/next-document-tests.tsx @@ -1,12 +1,40 @@ -import Document, * as document from "next/document"; +import Document, { Head, Main, NextScript, NextDocumentContext } from 'next/document'; import * as React from "react"; const results = ( - + - - - + +

+ ); + +const Wrapper: React.SFC = ({ children }) => {children}; + +export default class MyDocument extends Document { + static async getInitialProps({ renderPage }: NextDocumentContext) { + // Without callback + const page = renderPage(); + // With callback + const differentPage = renderPage(App => props => ); + const style = {}; + return { ...page, style }; + } + + render() { + return ( + + + My page +