Fix Utility Types (#11077)

This commit is contained in:
Aaron Reisman 2020-04-06 08:59:44 -07:00 committed by GitHub
parent 7a61fb7fdf
commit 8f4e265f76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 16 deletions

View file

@ -134,11 +134,6 @@ async function collectPluginMeta(
}
}
type SeparatedPlugins = {
appMiddlewarePlugins: PluginMetaData[]
documentMiddlewarePlugins: PluginMetaData[]
}
// clean package name so it can be used as variable
export const getPluginId = (pkg: string): string => {
pkg = pkg.replace(/\W/g, '')
@ -254,6 +249,4 @@ async function _collectPlugins(
// only execute it once between server/client configs
// since the plugins need to match
export const collectPlugins = execOnce(
_collectPlugins
) as typeof _collectPlugins
export const collectPlugins = execOnce(_collectPlugins)

View file

@ -239,17 +239,19 @@ export type NextApiHandler<T = any> = (
/**
* Utils
*/
export function execOnce(this: any, fn: (...args: any) => any) {
export function execOnce<T extends (...args: any[]) => ReturnType<T>>(
fn: T
): T {
let used = false
let result: any = null
let result: ReturnType<T>
return (...args: any) => {
return ((...args: any[]) => {
if (!used) {
used = true
result = fn.apply(this, args)
result = fn(...args)
}
return result
}
}) as T
}
export function getLocationOrigin() {
@ -263,7 +265,7 @@ export function getURL() {
return href.substring(origin.length)
}
export function getDisplayName(Component: ComponentType<any>) {
export function getDisplayName<P>(Component: ComponentType<P>) {
return typeof Component === 'string'
? Component
: Component.displayName || Component.name || 'Unknown'
@ -296,7 +298,7 @@ export async function loadGetInitialProps<
pageProps: await loadGetInitialProps(ctx.Component, ctx.ctx),
}
}
return {} as any
return {} as IP
}
const props = await App.getInitialProps(ctx)
@ -356,7 +358,7 @@ export function formatWithValidation(
}
}
return format(url as any, options)
return format(url as URL, options)
}
export const SP = typeof performance !== 'undefined'