Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CoCo_Code_Op2
next.js
提交
c48340cd
N
next.js
项目概览
CoCo_Code_Op2
/
next.js
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
next.js
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
c48340cd
编写于
4月 15, 2019
作者:
L
Luis Fernando Alvarez D
提交者:
GitHub
4月 15, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move next/router to Typescript (#7019)
Added types to next/router
上级
8b136a6f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
81 addition
and
38 deletion
+81
-38
packages/next/client/router.ts
packages/next/client/router.ts
+62
-38
packages/next/types/index.d.ts
packages/next/types/index.d.ts
+19
-0
未找到文件。
packages/next/client/router.
j
s
→
packages/next/client/router.
t
s
浏览文件 @
c48340cd
/* global window */
import
React
from
'
react
'
import
_Router
from
'
next-server/dist/lib/router/router
'
import
Router
,
{
IRouterInterface
}
from
'
next-server/dist/lib/router/router
'
import
{
RouterContext
}
from
'
next-server/dist/lib/router-context
'
import
{
RequestContext
}
from
'
next-server/dist/lib/request-context
'
const
SingletonRouter
=
{
type
ClassArguments
<
T
>
=
T
extends
new
(...
args
:
infer
U
)
=>
any
?
U
:
any
type
RouterArgs
=
ClassArguments
<
typeof
Router
>
interface
ISingletonRouterBase
{
router
:
Router
|
null
readyCallbacks
:
Array
<
()
=>
any
>
ready
(
cb
:
()
=>
any
):
void
}
export
interface
IPublicRouterInstance
extends
IRouterInterface
,
Pick
<
Router
,
|
'
components
'
|
'
push
'
|
'
replace
'
|
'
reload
'
|
'
back
'
|
'
prefetch
'
|
'
beforePopState
'
>
{
events
:
typeof
Router
[
'
events
'
]
}
export
interface
ISingletonRouter
extends
ISingletonRouterBase
,
IPublicRouterInstance
{}
const
SingletonRouter
:
ISingletonRouterBase
=
{
router
:
null
,
// holds the actual router instance
readyCallbacks
:
[],
ready
(
cb
)
{
ready
(
cb
:
()
=>
void
)
{
if
(
this
.
router
)
return
cb
()
if
(
typeof
window
!==
'
undefined
'
)
{
this
.
readyCallbacks
.
push
(
cb
)
}
}
}
,
}
// const x = SingletonRouter as IRealRouter
// Create public properties and methods of the router in the SingletonRouter
const
urlPropertyFields
=
[
'
pathname
'
,
'
route
'
,
'
query
'
,
'
asPath
'
]
const
propertyFields
=
[
'
components
'
]
...
...
@@ -23,9 +41,9 @@ const coreMethodFields = ['push', 'replace', 'reload', 'back', 'prefetch', 'befo
// Events is a static property on the router, the router doesn't have to be initialized to use it
Object
.
defineProperty
(
SingletonRouter
,
'
events
'
,
{
get
()
{
return
_
Router
.
events
}
get
()
{
return
Router
.
events
}
,
})
propertyFields
.
concat
(
urlPropertyFields
).
forEach
((
field
)
=>
{
...
...
@@ -34,29 +52,33 @@ propertyFields.concat(urlPropertyFields).forEach((field) => {
// The value might get changed as we change routes and this is the
// proper way to access it
Object
.
defineProperty
(
SingletonRouter
,
field
,
{
get
()
{
throwIfNoRouter
()
return
SingletonRouter
.
router
[
field
]
}
get
()
{
const
router
=
getRouter
()
as
any
return
router
[
field
]
as
string
}
,
})
})
coreMethodFields
.
forEach
((
field
)
=>
{
SingletonRouter
[
field
]
=
(...
args
)
=>
{
throwIfNoRouter
()
return
SingletonRouter
.
router
[
field
](...
args
)
// We don't really know the types here, so we add them later instead
(
SingletonRouter
as
any
)[
field
]
=
(...
args
:
any
[])
=>
{
const
router
=
getRouter
()
as
any
return
router
[
field
](...
args
)
}
})
routerEvents
.
forEach
((
event
)
=>
{
SingletonRouter
.
ready
(()
=>
{
_
Router
.
events
.
on
(
event
,
(...
args
)
=>
{
Router
.
events
.
on
(
event
,
(...
args
)
=>
{
const
eventField
=
`on
${
event
.
charAt
(
0
).
toUpperCase
()}${
event
.
substring
(
1
)}
`
if
(
SingletonRouter
[
eventField
])
{
const
singletonRouter
=
SingletonRouter
as
any
if
(
singletonRouter
[
eventField
])
{
try
{
S
ingletonRouter
[
eventField
](...
args
)
s
ingletonRouter
[
eventField
](...
args
)
}
catch
(
err
)
{
// tslint:disable-next-line:no-console
console
.
error
(
`Error when running the Router event:
${
eventField
}
`
)
// tslint:disable-next-line:no-console
console
.
error
(
`
${
err
.
message
}
\n
${
err
.
stack
}
`
)
}
}
...
...
@@ -64,25 +86,29 @@ routerEvents.forEach((event) => {
})
})
function
throwIfNoRouter
()
{
function
getRouter
()
{
if
(
!
SingletonRouter
.
router
)
{
const
message
=
'
No router instance found.
\n
'
+
'
You should only use "next/router" inside the client side of your app.
\n
'
throw
new
Error
(
message
)
}
return
SingletonRouter
.
router
}
// Export the SingletonRouter and this is the public API.
export
default
SingletonRouter
export
default
SingletonRouter
as
ISingletonRouter
// Reexport the withRoute HOC
export
{
default
as
withRouter
}
from
'
./with-router
'
export
function
useRouter
()
{
// Export the actual Router class, which is usually used inside the server
export
{
Router
}
export
function
useRouter
()
{
return
React
.
useContext
(
RouterContext
)
}
export
function
useRequest
()
{
export
function
useRequest
()
{
return
React
.
useContext
(
RequestContext
)
}
...
...
@@ -93,32 +119,30 @@ export function useRequest () {
// Create a router and assign it as the singleton instance.
// This is used in client side when we are initilizing the app.
// This should **not** use inside the server.
export
const
createRouter
=
function
(...
args
)
{
SingletonRouter
.
router
=
new
_
Router
(...
args
)
SingletonRouter
.
readyCallbacks
.
forEach
(
cb
=>
cb
())
export
const
createRouter
=
(...
args
:
RouterArgs
)
=>
{
SingletonRouter
.
router
=
new
Router
(...
args
)
SingletonRouter
.
readyCallbacks
.
forEach
(
(
cb
)
=>
cb
())
SingletonRouter
.
readyCallbacks
=
[]
return
SingletonRouter
.
router
}
// Export the actual Router class, which is usually used inside the server
export
const
Router
=
_Router
// This function is used to create the `withRouter` router instance
export
function
makePublicRouterInstance
(
router
)
{
const
instance
=
{}
export
function
makePublicRouterInstance
(
router
:
Router
):
IPublicRouterInstance
{
const
_router
=
router
as
any
const
instance
=
{}
as
any
for
(
const
property
of
urlPropertyFields
)
{
if
(
typeof
router
[
property
]
===
'
object
'
)
{
instance
[
property
]
=
{
...
router
[
property
]
}
// makes sure query is not stateful
if
(
typeof
_
router
[
property
]
===
'
object
'
)
{
instance
[
property
]
=
{
...
_
router
[
property
]
}
// makes sure query is not stateful
continue
}
instance
[
property
]
=
router
[
property
]
instance
[
property
]
=
_
router
[
property
]
}
// Events is a static property on the router, the router doesn't have to be initialized to use it
instance
.
events
=
_
Router
.
events
instance
.
events
=
Router
.
events
propertyFields
.
forEach
((
field
)
=>
{
// Here we need to use Object.defineProperty because, we need to return
...
...
@@ -126,15 +150,15 @@ export function makePublicRouterInstance (router) {
// The value might get changed as we change routes and this is the
// proper way to access it
Object
.
defineProperty
(
instance
,
field
,
{
get
()
{
return
router
[
field
]
}
get
()
{
return
_
router
[
field
]
}
,
})
})
coreMethodFields
.
forEach
((
field
)
=>
{
instance
[
field
]
=
(...
args
)
=>
{
return
router
[
field
](...
args
)
instance
[
field
]
=
(...
args
:
any
[]
)
=>
{
return
_
router
[
field
](...
args
)
}
})
...
...
packages/next/types/index.d.ts
浏览文件 @
c48340cd
...
...
@@ -4,6 +4,11 @@ declare module 'next-server/constants';
declare
module
'
webpack/lib/GraphHelpers
'
;
declare
module
'
unfetch
'
declare
module
'
next/router
'
{
import
*
as
all
from
'
next/client/router
'
export
=
all
}
declare
module
'
next-server/dist/lib/data-manager-context
'
{
import
*
as
all
from
'
next-server/lib/data-manager-context
'
export
=
all
...
...
@@ -14,6 +19,20 @@ declare module 'next-server/dist/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
'
{
export
function
loadGetInitialProps
(
Component
:
any
,
ctx
:
any
):
Promise
<
any
>
export
function
execOnce
(
fn
:
any
):
(...
args
:
any
[])
=>
void
}
declare
module
'
next/dist/compiled/nanoid/index.js
'
{
function
nanoid
(
size
?:
number
):
string
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录