File tree Expand file tree Collapse file tree 1 file changed +11
-18
lines changed Expand file tree Collapse file tree 1 file changed +11
-18
lines changed Original file line number Diff line number Diff line change 1
1
import type { Storage } from 'unstorage'
2
2
import { createStorage } from 'unstorage'
3
- import fsDriver from 'unstorage/drivers/fs'
4
3
import httpDriver from 'unstorage/drivers/http'
5
4
import cloudflareKVBindingDriver from 'unstorage/drivers/cloudflare-kv-binding'
6
- import { join } from 'pathe'
7
5
import { joinURL } from 'ufo'
8
6
9
7
let _kv : Storage
10
8
11
9
export function useKV ( ) {
12
10
if ( ! _kv ) {
13
- // TODO: same as database
14
- if ( process . env . KV ) {
15
- // kv in production
16
- _kv = createStorage ( {
17
- driver : cloudflareKVBindingDriver ( {
18
- binding : process . env . KV
19
- } )
20
- } )
21
- } else if ( import . meta. dev && process . env . NUXT_HUB_URL ) {
11
+ if ( import . meta. dev && process . env . NUXT_HUB_URL ) {
22
12
console . log ( 'Using KV remote namespace...' )
23
13
// Use https://unstorage.unjs.io/drivers/http
24
14
_kv = createStorage ( {
@@ -29,14 +19,17 @@ export function useKV () {
29
19
}
30
20
} )
31
21
} )
32
- } else if ( import . meta. dev ) {
33
- // local kv in development
34
- console . log ( 'Using KV local namespace...' )
35
- _kv = createStorage ( {
36
- driver : fsDriver ( { base : join ( process . cwd ( ) , './.hub/kv' ) } )
37
- } )
38
22
} else {
39
- throw new Error ( 'No KV configured for production' )
23
+ const binding = process . env . KV || globalThis . __env__ ?. KV || globalThis . KV
24
+ if ( binding ) {
25
+ _kv = createStorage ( {
26
+ driver : cloudflareKVBindingDriver ( {
27
+ binding
28
+ } )
29
+ } )
30
+ } else {
31
+ throw createError ( 'Missing Cloudflare binding KV' )
32
+ }
40
33
}
41
34
}
42
35
You can’t perform that action at this time.
0 commit comments