Skip to content

Commit 067a93f

Browse files
committed
feat: add use-request resolver
1 parent e1647be commit 067a93f

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Resolver } from 'unplugin-auto-import/types'
2+
let hooks: string[] | undefined
3+
4+
export type VueHooksPlusResolverOptions = {
5+
/**
6+
* prefix for name of components
7+
*
8+
* @default ''
9+
*/
10+
prefix?: string
11+
}
12+
13+
function queryMetaData() {
14+
try {
15+
hooks = ['useRequest']
16+
} catch (e) {
17+
console.error(e)
18+
throw new Error(
19+
'[@vue-hooks-plus/use-request:plugins] failed to load @vue-hooks-plus/use-worker, have you installed it?',
20+
)
21+
}
22+
}
23+
24+
function resolveHooks(name: string, options: VueHooksPlusResolverOptions) {
25+
if (!hooks) return
26+
27+
const { prefix } = options
28+
if (prefix) {
29+
if (!name.startsWith(prefix)) return
30+
name = name.substring(prefix.length)
31+
}
32+
if (!hooks.includes(name)) return
33+
34+
return {
35+
name,
36+
from: '@vue-hooks-plus/use-request',
37+
}
38+
}
39+
40+
export default function VueHooksPlusUseRequestResolver(
41+
options: VueHooksPlusResolverOptions = {},
42+
): Resolver {
43+
return name => {
44+
if (!hooks) {
45+
queryMetaData()
46+
}
47+
return resolveHooks(name, options)
48+
}
49+
}

0 commit comments

Comments
 (0)