Add .d.ts for next-server (#7133)

* Add .d.ts files

* Drop next declarations from index.d.ts

* Bring back number of errors

* Fix more errors

* Fix rewriteUrlForExport
This commit is contained in:
Tim Neutkens 2019-04-24 16:47:50 +02:00 committed by GitHub
parent 7a16379435
commit c9d599b698
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
41 changed files with 154 additions and 113 deletions

View file

@ -19,11 +19,13 @@
"lint-fix": "standard --fix && standard --fix --parser typescript-eslint-parser --plugin typescript packages/**/*.ts",
"prettier": "prettier --single-quote --no-semi --write examples/**/*.js && yarn lint-fix",
"typescript": "lerna run typescript",
"prepublish": "lerna run prepublish",
"prepublish": "lerna run prepublish && yarn run types-next-server && yarn run types-next",
"publish-canary": "lerna version prerelease --preid canary --force-publish && release --pre",
"publish-stable": "lerna version --force-publish",
"lint-staged": "lint-staged",
"next": "./packages/next/dist/bin/next"
"next": "./packages/next/dist/bin/next",
"types-next": "tsc --declaration --emitDeclarationOnly --declarationDir ./packages/next/dist --project ./packages/next",
"types-next-server": "tsc --declaration --emitDeclarationOnly --declarationDir ./packages/next-server/dist --project ./packages/next-server"
},
"pre-commit": "lint-staged",
"lint-staged": {

1
packages/next-server/amp.d.ts vendored Normal file
View file

@ -0,0 +1 @@
export * from './dist/lib/amp'

2
packages/next-server/config.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
import config from './dist/lib/runtime-config'
export default config

1
packages/next-server/constants.d.ts vendored Normal file
View file

@ -0,0 +1 @@
export * from './dist/lib/constants'

2
packages/next-server/dynamic.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from './dist/lib/dynamic'
export {default} from './dist/lib/dynamic'

2
packages/next-server/head.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from './dist/lib/head'
export {default} from './dist/lib/head'

2
packages/next-server/index.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
import Server, {ServerConstructor} from './dist/server/next-server'
export default function(options: ServerConstructor): Server

View file

@ -11,7 +11,7 @@ export const CONFIG_FILE = 'next.config.js'
export const BUILD_ID_FILE = 'BUILD_ID'
export const BLOCKED_PAGES = [
'/_document',
'/_app'
'/_app',
]
export const CLIENT_STATIC_FILES_PATH = 'static'
export const CLIENT_STATIC_FILES_RUNTIME = 'runtime'

View file

@ -125,9 +125,9 @@ function reduceComponents(headElements: Array<React.ReactElement<any>>, props: W
});
}
const Effect = withSideEffect<WithIsAmp>();
const Effect = withSideEffect();
export default function Head({ children }: { children: React.ReactNode }) {
function Head({ children }: { children: React.ReactNode }) {
return (
<AmpModeContext.Consumer>
{(ampMode) => (
@ -148,3 +148,5 @@ export default function Head({ children }: { children: React.ReactNode }) {
}
Head.rewind = Effect.rewind;
export default Head

View file

@ -0,0 +1,11 @@
export function rewriteUrlForNextExport(url: string): string {
const [pathname, hash] = url.split('#')
// tslint:disable-next-line
let [path, qs] = pathname.split('?')
path = path.replace(/\/$/, '')
// Append a trailing slash if this path does not have an extension
if (!/\.[^/]+\/?$/.test(path)) path += `/`
if (qs) path += '?' + qs
if (hash) path += '#' + hash
return path
}

View file

@ -4,6 +4,7 @@ import { ComponentType } from 'react';
import { parse } from 'url';
import mitt, {MittEmitter} from '../mitt';
import { formatWithValidation, getURL, loadGetInitialProps, IContext, IAppContext } from '../utils';
import {rewriteUrlForNextExport} from './rewrite-url-for-export'
function toRoute(path: string): string {
return path.replace(/\/$/, '') || '/'
@ -85,15 +86,7 @@ export default class Router implements IRouterInterface {
}
static _rewriteUrlForNextExport(url: string): string {
const [pathname, hash] = url.split('#')
// tslint:disable-next-line
let [path, qs] = pathname.split('?')
path = path.replace(/\/$/, '')
// Append a trailing slash if this path does not have an extension
if (!/\.[^/]+\/?$/.test(path)) path += `/`
if (qs) path += '?' + qs
if (hash) path += '#' + hash
return path
return rewriteUrlForNextExport(url)
}
onPopState = (e: PopStateEvent): void => {
@ -213,7 +206,7 @@ export default class Router implements IRouterInterface {
if (process.env.__NEXT_EXPORT_TRAILING_SLASH) {
// @ts-ignore this is temporarily global (attached to window)
if (__NEXT_DATA__.nextExport) {
as = Router._rewriteUrlForNextExport(as)
as = rewriteUrlForNextExport(as)
}
}

View file

@ -1,9 +1,9 @@
let runtimeConfig
let runtimeConfig: any
export default () => {
return runtimeConfig
}
export function setConfig (configValue) {
export function setConfig(configValue: any) {
runtimeConfig = configValue
}

View file

@ -7,20 +7,21 @@ type State = Array<React.ReactElement<any>> | undefined
type SideEffectProps = {
reduceComponentsToState: <T>(components: Array<React.ReactElement<any>>, props: T) => State,
handleStateChange?: (state: State) => void,
isAmp?: boolean,
}
export default function withSideEffect<MoreProps>() {
export default () => {
const mountedInstances: Set<any> = new Set()
let state: State
function emitChange(component: React.Component<SideEffectProps & MoreProps>) {
function emitChange(component: React.Component<SideEffectProps>) {
state = component.props.reduceComponentsToState([...mountedInstances], component.props)
if (component.props.handleStateChange) {
component.props.handleStateChange(state)
}
}
class SideEffect extends Component<SideEffectProps & MoreProps> {
return class extends Component<SideEffectProps> {
// Used when server rendering
static rewind() {
const recordedState = state
@ -52,6 +53,4 @@ export default function withSideEffect<MoreProps>() {
return null
}
}
return SideEffect
}

View file

@ -7,20 +7,30 @@
"files": [
"dist",
"index.js",
"index.d.ts",
"config.js",
"config.d.ts",
"constants.js",
"constants.d.ts",
"dynamic.js",
"dynamic.d.ts",
"head.js",
"head.d.ts",
"link.js",
"link.d.ts",
"router.js",
"router.d.ts",
"next-config.js",
"amp.js"
"next-config.d.ts",
"amp.js",
"amp.d.ts"
],
"scripts": {
"build": "taskr",
"release": "taskr release",
"prepublish": "npm run release",
"typescript": "tsc --noEmit"
"typescript": "tsc --noEmit --declaration",
"types": "tsc --declaration --emitDeclarationOnly --declarationDir dist"
},
"taskr": {
"requires": [

View file

@ -1,10 +1,10 @@
import os from 'os'
import findUp from 'find-up'
import { CONFIG_FILE } from 'next-server/constants'
import { CONFIG_FILE } from '../lib/constants'
const targets = ['server', 'serverless']
const defaultConfig = {
const defaultConfig: {[key: string]: any} = {
env: [],
webpack: null,
webpackDevMiddleware: null,
@ -19,30 +19,30 @@ const defaultConfig = {
poweredByHeader: true,
onDemandEntries: {
maxInactiveAge: 60 * 1000,
pagesBufferLength: 2
pagesBufferLength: 2,
},
experimental: {
cpus: Math.max(
1,
(Number(process.env.CIRCLE_NODE_TOTAL) ||
(os.cpus() || { length: 1 }).length) - 1
(os.cpus() || { length: 1 }).length) - 1,
),
ampBindInitData: false,
exportTrailingSlash: true,
terserLoader: false,
profiling: false,
flyingShuttle: false,
asyncToPromises: false
}
asyncToPromises: false,
},
}
function assignDefaults (userConfig) {
Object.keys(userConfig).forEach(key => {
function assignDefaults(userConfig: {[key: string]: any}) {
Object.keys(userConfig).forEach((key: string) => {
const maybeObject = userConfig[key]
if ((!!maybeObject) && (maybeObject.constructor === Object)) {
userConfig[key] = {
...(defaultConfig[key] || {}),
...userConfig[key]
...userConfig[key],
}
}
})
@ -50,25 +50,25 @@ function assignDefaults (userConfig) {
return { ...defaultConfig, ...userConfig }
}
function normalizeConfig (phase, config) {
function normalizeConfig(phase: string, config: any) {
if (typeof config === 'function') {
config = config(phase, { defaultConfig })
if (typeof config.then === 'function') {
throw new Error(
'> Promise returned in next config. https://err.sh/zeit/next.js/promise-in-next-config.md'
'> Promise returned in next config. https://err.sh/zeit/next.js/promise-in-next-config.md',
)
}
}
return config
}
export default function loadConfig (phase, dir, customConfig) {
export default function loadConfig(phase: string, dir: string, customConfig: any) {
if (customConfig) {
return assignDefaults({ configOrigin: 'server', ...customConfig })
}
const path = findUp.sync(CONFIG_FILE, {
cwd: dir
cwd: dir,
})
// If config file was found

View file

@ -2,23 +2,22 @@
// That's because, ^^^ package comes with very old version of path-to-regexp
// So, it'll give us issues when the app has used a newer version of path-to-regexp
// (When webpack resolving packages)
var pathToRegexp = require('path-to-regexp')
const pathToRegexp = require('path-to-regexp')
module.exports = function (options) {
options = options || {}
export default () => {
return (path: string) => {
const keys: any[] = []
const re = pathToRegexp(path, keys, {})
return function (path) {
var keys = []
var re = pathToRegexp(path, keys, options)
return function (pathname, params) {
var m = re.exec(pathname)
return (pathname: string|undefined, params?: any) => {
const m = re.exec(pathname)
if (!m) return false
params = params || {}
var key, param
for (var i = 0; i < keys.length; i++) {
let key
let param
for (let i = 0; i < keys.length; i++) {
key = keys[i]
param = m[i + 1]
if (!param) continue
@ -31,11 +30,12 @@ module.exports = function (options) {
}
}
function decodeParam (param) {
function decodeParam(param: string) {
try {
return decodeURIComponent(param)
} catch (_) {
const err = new Error('failed to decode param')
// @ts-ignore DECODE_FAILED is handled
err.code = 'DECODE_FAILED'
throw err
}

View file

@ -1,4 +1,4 @@
import {BUILD_MANIFEST, CLIENT_STATIC_FILES_PATH, REACT_LOADABLE_MANIFEST, SERVER_DIRECTORY} from 'next-server/constants';
import {BUILD_MANIFEST, CLIENT_STATIC_FILES_PATH, REACT_LOADABLE_MANIFEST, SERVER_DIRECTORY} from '../lib/constants';
import { join } from 'path';
import { requirePage } from './require';

View file

@ -9,19 +9,19 @@ import { sendHTML } from './send-html'
import { serveStatic } from './serve-static'
import Router, { route, Route } from './router'
import { isInternalUrl, isBlockedPage } from './utils'
import loadConfig from 'next-server/next-config'
import loadConfig from './config'
import {
PHASE_PRODUCTION_SERVER,
BUILD_ID_FILE,
CLIENT_STATIC_FILES_PATH,
CLIENT_STATIC_FILES_RUNTIME,
} from 'next-server/constants'
} from '../lib/constants'
import * as envConfig from '../lib/runtime-config'
import { loadComponents } from './load-components'
type NextConfig = any
type ServerConstructor = {
export type ServerConstructor = {
dir?: string
staticMarkup?: boolean
quiet?: boolean

View file

@ -6,6 +6,7 @@ import { IRouterInterface } from '../lib/router/router'
import mitt, { MittEmitter } from '../lib/mitt';
import { loadGetInitialProps, isResSent, getDisplayName, ComponentsEnhancer, RenderPage, IDocumentInitialProps, NextComponentType, DocumentType, AppType } from '../lib/utils'
import Head, { defaultHead } from '../lib/head'
// @ts-ignore types will be added later as it's an internal module
import Loadable from '../lib/loadable'
import { DataManagerContext } from '../lib/data-manager-context'
import { RequestContext } from '../lib/request-context'

View file

@ -1,5 +1,5 @@
import {join} from 'path'
import {PAGES_MANIFEST, SERVER_DIRECTORY} from 'next-server/constants'
import {PAGES_MANIFEST, SERVER_DIRECTORY} from '../lib/constants'
import { normalizePagePath } from './normalize-page-path'
export function pageNotFoundError(page: string): Error {

View file

@ -1,4 +1,4 @@
import { BLOCKED_PAGES } from 'next-server/constants'
import { BLOCKED_PAGES } from '../lib/constants'
const internalPrefixes = [
/^\/_next\//,

View file

@ -1,12 +1,15 @@
{
"compilerOptions": {
"strict": true,
"allowJs": true,
"noEmit": true,
"module": "esnext",
"target": "ES2017",
"esModuleInterop": true,
"moduleResolution": "node",
"jsx": "react"
}
},
"exclude": [
"./dist/**",
"./*.d.ts",
"./constants.d.ts"
]
}

2
packages/next/amp.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from 'next-server/amp'
export {default} from 'next-server/amp'

2
packages/next/app.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from './dist/pages/_app'
export {default} from './dist/pages/_app'

2
packages/next/babel.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from './dist/build/babel/preset'
export {default} from './dist/build/babel/preset'

2
packages/next/client.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from './dist/client/index'
export {default} from './dist/client/index'

View file

@ -3,7 +3,8 @@
import { resolve, parse } from 'url'
import React, { Component, Children } from 'react'
import PropTypes from 'prop-types'
import Router, { Router as _Router } from 'next/router'
import Router from 'next/router'
import { rewriteUrlForNextExport } from 'next-server/dist/lib/router/rewrite-url-for-export'
import { execOnce, formatWithValidation, getLocationOrigin } from 'next-server/dist/lib/utils'
function isLocal (href) {
@ -144,7 +145,7 @@ class Link extends Component {
typeof __NEXT_DATA__ !== 'undefined' &&
__NEXT_DATA__.nextExport
) {
props.href = _Router._rewriteUrlForNextExport(props.href)
props.href = rewriteUrlForNextExport(props.href)
}
}

View file

@ -101,9 +101,6 @@ export default SingletonRouter as ISingletonRouter
// Reexport the withRoute HOC
export { default as withRouter } from './with-router'
// Export the actual Router class, which is usually used inside the server
export { Router }
export function useRouter() {
return React.useContext(RouterContext)
}

2
packages/next/config.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from 'next-server/config'
export {default} from 'next-server/config'

2
packages/next/constants.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from 'next-server/constants'
export {default} from 'next-server/constants'

2
packages/next/data.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from './dist/lib/data'
export {default} from './dist/lib/data'

2
packages/next/document.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from './dist/pages/_document'
export {default} from './dist/pages/_document'

2
packages/next/dynamic.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from 'next-server/dynamic'
export {default} from 'next-server/dynamic'

2
packages/next/error.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from './dist/pages/_error'
export {default} from './dist/pages/_error'

2
packages/next/head.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from 'next-server/head'
export {default} from 'next-server/head'

2
packages/next/link.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from './dist/client/link'
export {default} from './dist/client/link'

View file

@ -10,18 +10,31 @@
"files": [
"dist",
"app.js",
"app.d.ts",
"babel.js",
"babel.d.ts",
"client.js",
"client.d.ts",
"config.js",
"config.d.ts",
"constants.js",
"constants.d.ts",
"data.js",
"data.d.ts",
"document.js",
"document.d.ts",
"dynamic.js",
"dynamic.d.ts",
"error.js",
"error.d.ts",
"head.js",
"head.d.ts",
"link.js",
"link.d.ts",
"router.js",
"amp.js"
"router.d.ts",
"amp.js",
"amp.d.ts"
],
"bin": {
"next": "./dist/bin/next"
@ -29,9 +42,8 @@
"scripts": {
"build": "taskr",
"release": "taskr release",
"prepublish": "npm run release && npm run types",
"typescript": "tsc --noEmit --declaration",
"types": "tsc --declaration --emitDeclarationOnly --declarationDir dist"
"prepublish": "npm run release",
"typescript": "tsc --noEmit --declaration"
},
"taskr": {
"requires": [

View file

@ -1,10 +1,12 @@
import React from 'react'
import React, {ErrorInfo} from 'react'
import PropTypes from 'prop-types'
import { execOnce, loadGetInitialProps, NextComponentType, IContext, IAppContext, IAppInitialProps, IAppProps } from 'next-server/dist/lib/utils'
import { Router, makePublicRouterInstance } from 'next/router'
import { makePublicRouterInstance } from '../client/router'
export { NextComponentType, IContext, IAppContext, IAppInitialProps, IAppProps }
type Router = import('next-server/lib/router/router').default
export type AppClientContext = IAppContext<Router>
export type AppProps = IAppProps<Router>
@ -26,9 +28,10 @@ export default class App extends React.Component<AppProps> {
}
// Kept here for backwards compatibility.
// When someone ended App they could call `super.componentDidCatch`. This is now deprecated.
componentDidCatch(err: Error) {
throw err
// When someone ended App they could call `super.componentDidCatch`.
// @deprecated This method is no longer needed. Errors are caught at the top level
componentDidCatch(error: Error, _errorInfo: ErrorInfo): void {
throw error
}
render() {
@ -51,7 +54,7 @@ export class Container extends React.Component {
this.scrollToHash()
}
scrollToHash() {
private scrollToHash() {
let { hash } = window.location
hash = hash && hash.substring(1)
if (!hash) return

2
packages/next/router.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export * from './dist/client/router'
export {default} from './dist/client/router'

View file

@ -6,5 +6,10 @@
"esModuleInterop": true,
"moduleResolution": "node",
"jsx": "react"
}
},
"exclude": [
"dist",
"./*.d.ts",
"./router.d.ts"
]
}

View file

@ -5,45 +5,6 @@ declare module 'webpack/lib/GraphHelpers';
declare module 'unfetch';
declare module 'styled-jsx/server';
declare module 'next/router' {
import * as all from 'next/client/router'
export = all
}
declare module 'next-server/head' {
import * as all from 'next-server/lib/head'
export = all
}
declare module 'next-server/dist/lib/data-manager-context' {
import * as all from 'next-server/lib/data-manager-context'
export = all
}
declare module 'next-server/dist/lib/router-context' {
import * as all from 'next-server/lib/router-context'
export = all
}
declare module 'next-server/dist/lib/router/router' {
import * as all from 'next-server/lib/router/router'
export = all
}
declare module 'next-server/dist/lib/request-context' {
import * as all from 'next-server/lib/request-context'
export = all
}
declare module 'next-server/dist/lib/utils' {
import * as all from 'next-server/lib/utils'
export = all
}
declare module 'next-server/dist/server/utils' {
import * as all from 'next-server/server/utils'
export = all
}
declare module 'next/dist/compiled/nanoid/index.js' {
function nanoid(size?: number): string;
@ -51,6 +12,11 @@ declare module 'next/dist/compiled/nanoid/index.js' {
export = nanoid;
}
declare module 'next/dist/compiled/unistore' {
import unistore from 'unistore'
export = unistore
}
declare module 'next/dist/compiled/resolve/index.js' {
import resolve from 'resolve'