8000 chore: rename proxy and .hub to `.data/hub` · nuxt-hub/core@5d73b42 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Commit 5d73b42

Browse files
committed
chore: rename proxy and .hub to .data/hub
1 parent ec7e13f commit 5d73b42

File tree

12 files changed

+123
-65
lines changed

12 files changed

+123
-65
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dist
22
node_modules
3-
.hub
3+
.data
44
.output
55
.nuxt

.gitignore

Lines changed: 0 additions & 3 deleti 6DB6 ons
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ dist
2424
.build-*
2525
.netlify
2626

27-
# NuxtHUb
28-
.hub
29-
3027
# Env
3128
.env
3229

docs/content/docs/1.getting-started/2.installation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bun add --dev @nuxthub/core
5050

5151
::
5252

53-
3. Add it to your `modules` section in your `nuxt.config`:
53+
3. Add `@nuxthub/core` to your `modules` section in your `nuxt.config`:
5454

5555
```ts [nuxt.config.ts]
5656
export default defineNuxtConfig({
@@ -61,7 +61,7 @@ export default defineNuxtConfig({
6161
That's it! You can now use the NuxtHub module in your Nuxt project.
6262

6363
::callout
64-
The module will create a `.hub` directory in your project root, which contains the necessary configuration files and resources for the module to work. It will also add it to the `.gitignore` file to avoid committing it to your repository.
64+
The module will create a `.data/hub` directory in your project root, which contains the necessary configuration files and resources for the module to work. It will also add it to the `.gitignore` file to avoid committing it to your repository.
6565
::
6666

6767
## Options

docs/content/docs/2.storage/2.kv.md

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,86 @@ title: KV
33
description: How to use key-value data storage with NuxtHub.
44
---
55

6-
NuxtHub KV is a layer to [Cloudflare Workers KV](https://developers.cloudflare.com/kv){target=_blank}, a global, low-latency, key-value data storage.
7-
8-
<!-- TODO: config, binding ? -->
9-
10-
Once properly configured, NuxtHub module exposes a server composable to the application.
6+
NuxtHub KV is a layer on top of [Cloudflare Workers KV](https://developers.cloudflare.com/kv), a global, low-latency, key-value data storage.
117

128
## `hubKV()`
139

14-
Server composable that returns a [Storage](https://unstorage.unjs.io/getting-started/usage#interface){target=_blank}.
10+
Server method that returns an [unstorage instance](https://unstorage.unjs.io/guide#interface) with `keys()`, `get()`, `set()` and `del()` aliases.
1511

16-
### `getKeys()`
12+
### `keys()`
1713

18-
Retrieves all keys from the storage.
14+
Retrieves all keys from the KV storage (alias of `getKeys()`).
1915

20-
```ts[/api/kv/index.get.ts]
21-
export default eventHandler(async () => {
22-
return await hubKV().getKeys()
23-
})
16+
```ts
17+
const keys = await hubKV().keys()
18+
/*
19+
[
20+
'react',
21+
'react:gatsby',
22+
'react:next',
23+
'vue',
24+
'vue:nuxt',
25+
'vue:quasar'
26+
]
2427
```
2528
26-
### `getItem()`
29+
To get the keys starting with a specific prefix, you can pass the prefix as an argument.
2730
28-
Retrieves an item from the storage.
31+
```ts
32+
const vueKeys = await hubKV().keys('vue')
33+
/*
34+
[
35+
'vue:nuxt',
36+
'vue:quasar'
37+
]
38+
*/
39+
```
2940

30-
```ts[/api/kv/[key\\].get.ts]
31-
export default eventHandler(async () => {
32-
const { key } = getRouterParams(event)
41+
### `get()`
3342

34-
return await hubKV().getItem(key)
35-
})
43+
Retrieves an item from the Key-Value storage (alias of `getItem()`).
44+
45+
```ts
46+
const vue = await hubKV().get('vue')
47+
/*
48+
{
49+
year: 2014
50+
}
51+
*/
3652
```
3753

38-
### `setItem()`
54+
### `set()`
55+
56+
Puts an item in the storage (alias of `setItem()`)
3957

40-
Puts an item in the storage.
58+
```ts
59+
await hubKV().set('vue', { year: 2014 })
60+
```
4161

42-
```ts[/api/kv/index.put.ts]
43-
export default eventHandler(async () => {
44-
const { key, value } = await readBody(event)
62+
You can delimit the key with a `:` to create a namespace:
4563

46-
return await hubKV().setItem(key, value)
47-
})
64+
```ts
65+
await hubKV().set('vue:nuxt', { year: 2016 })
4866
```
4967

50-
### `deleteItem()`
68+
### `has()`
69+
70+
Checks if an item exists in the storage (alias of `hasItem()`)
5171

52-
Deletes an item from the storage.
72+
```ts
73+
const hasAngular = await hubKV().has('angular')
74+
```
5375

54-
```ts[/api/kv/[key\\].delete.ts]
55-
export default eventHandler(async (event) => {
56-
const { key } = getRouterParams(event)
76+
### `del()`
5777

58-
await hubKV().removeItem(key)
78+
Delete an item from the storage (alias of `removeItem()`)
5979

60-
return { key }
61-
})
80+
```ts
81+
await hubKV().del('react')
6282
```
83+
84+
### `...()`
85+
86+
::callout
87+
You can use any other method from [unstorage](https://unstorage.unjs.io/guide#interface) as well.
88+
::

playground/server/api/kv/index.get.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
export default eventHandler(async () => {
22
// List entries for the current user
3-
const storage = hubKV()
3+
const kv = hubKV()
44

5-
const keys = await storage.getKeys()
5+
const keys = await kv.keys()
66
// const items = await storage.getItems(keys)
77
const items = await Promise.all(keys.map(async (key) => {
8-
const value = await storage.getItem(key)
8+
const value = await kv.get(key)
99
return { key, value }
1010
}))
1111
return items

playground/server/api/test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
export default eventHandler(async () => {
2-
const db = hubDatabase()
2+
const kv = hubKV()
3+
4+
await kv.set('vue', { year: 2014 })
5+
await kv.set('vue:nuxt', { year: 2016 })
6+
await kv.set('vue:quasar', { version: 2015 })
7+
await kv.set('react', { version: 2013 })
8+
await kv.set('react:next', { version: 2016 })
9+
await kv.set('react:gatsby', { version: 2015 })
10+
11+
return kv.keys()
12+
// const db = hubDatabase()
313
// return useProjectKV(projectUrl).getKeys()
414
// return await db.prepare('SELECT * from todos').all()
515
// return await db.prepare("SELECT * from todos").first()

src/module.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ export default defineNuxtModule<ModuleOptions>({
136136
logger.info(`Remote storage available: ${Object.keys(manifest.storage).filter(k => manifest.storage[k]).map(k => `\`${k}\``).join(', ')} `)
137137
return
138138
} else {
139-
log.info('Using local data from `.hub/`')
139+
log.info('Using local data from `.data/hub`')
140140
}
141141

142142
// Local development without remote connection
143-
// Create the .hub/ directory
144-
const hubDir = join(rootDir, './.hub')
143+
// Create the .data/hub/ directory
144+
const hubDir = join(rootDir, './.data/hub')
145145
try {
146-
await mkdir(hubDir)
146+
await mkdir(hubDir, { recursive: true })
147147
} catch (e: any) {
148148
if (e.errno === -17) {
149149
// File already exists
@@ -155,8 +155,8 @@ export default defineNuxtModule<ModuleOptions>({
155155
// Add it to .gitignore
156156
const gitignorePath = join(workspaceDir , '.gitignore')
157157
const gitignore = await readFile(gitignorePath, 'utf-8').catch(() => '')
158-
if (!gitignore.includes('.hub')) {
159-
await writeFile(gitignorePath, `${gitignore ? gitignore + '\n' : gitignore}.hub`, 'utf-8')
158+
if (!gitignore.includes('.data')) {
159+
await writeFile(gitignorePath, `${gitignore ? gitignore + '\n' : gitignore}.data`, 'utf-8')
160160
}
161161

162162
// Generate the wrangler.toml file
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { AnalyticsEngineDataPoint } from '@cloudflare/workers-types/experimental'
22
import { eventHandler, readValidatedBody } from 'h3'
33
import { z } from 'zod'
4-
import { useAnalytics } from '../../../utils/analytics'
4+
import { hubAnalytics } from '../../../utils/analytics'
55

66
export default eventHandler(async (event) => {
77
const { data } = await readValidatedBody(event, z.object({
88
data: z.custom<AnalyticsEngineDataPoint>()
99
}).parse)
1010

11-
await useAnalytics().put(data)
11+
await hubAnalytics().put(data)
1212

1313
return true
1414
})

src/runtime/server/utils/analytics.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ function _useDataset() {
2121
throw createError(`Missing Cloudflare ${name} binding (Analytics Engine)`)
2222
}
2323

24-
export function useAnalytics() {
24+
export function hubAnalytics() {
2525
const hub = useRuntimeConfig().hub
2626
if (import.meta.dev && hub.projectUrl) {
27-
return useProxyAnalytics(hub.projectUrl, hub.projectSecretKey || hub.userToken)
27+
return proxyHubAnalytics(hub.projectUrl, hub.projectSecretKey || hub.userToken)
2828
}
2929
const dataset = _useDataset()
3030

@@ -36,7 +36,7 @@ export function useAnalytics() {
3636
}
3737
}
3838

39-
export function useProxyAnalytics(projectUrl: string, secretKey?: string) {
39+
export function proxyHubAnalytics(projectUrl: string, secretKey?: string) {
4040
const analyticsAPI = ofetch.create({
4141
baseURL: joinURL(projectUrl, '/api/_hub/analytics'),
4242
headers: {

src/runtime/server/utils/blob.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function _useBucket() {
5555
export function hubBlob() {
5656
const hub = useRuntimeConfig().hub
5757
if (import.meta.dev && hub.projectUrl) {
58-
return useProxyBlob(hub.projectUrl, hub.projectSecretKey || hub.userToken)
58+
return proxyHubBlob(hub.projectUrl, hub.projectSecretKey || hub.userToken)
5959
}
6060
const bucket = _useBucket()
6161

@@ -132,7 +132,7 @@ export function hubBlob() {
132132
}
133133
}
134134

135-
export function useProxyBlob(projectUrl: string, secretKey?: string) {
135+
export function proxyHubBlob(projectUrl: string, secretKey?: string) {
136136
const blobAPI = ofetch.create({
137137
baseURL: joinURL(projectUrl, '/api/_hub/blob'),
138138
headers: {

src/runtime/server/utils/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function hubDatabase(): D1Database {
1313
}
1414
const hub = useRuntimeConfig().hub
1515
if (import.meta.dev && hub.proje B41A ctUrl) {
16-
_db = useProxyDatabase(hub.projectUrl, hub.projectSecretKey || hub.userToken)
16+
_db = proxyHubDatabase(hub.projectUrl, hub.projectSecretKey || hub.userToken)
1717
return _db
1818
}
1919
// @ts-ignore
@@ -25,7 +25,7 @@ export function hubDatabase(): D1Database {
2525
throw createError('Missing Cloudflare DB binding (D1)')
2626
}
2727

28-
export function useProxyDatabase(projectUrl: string, secretKey?: string): D1Database {
28+
export function proxyHubDatabase(projectUrl: string, secretKey?: string): D1Database {
2929
const d1API = ofetch.create({
3030
baseURL: joinURL(projectUrl, '/api/_hub/database'),
3131
method: 'POST',

src/runtime/server/utils/kv.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,61 @@ import { joinURL } from 'ufo'
66
import { createError } from 'h3'
77
import { useRuntimeConfig } from '#imports'
88

9-
let _kv: Storage
9+
interface KV extends Storage {
10+
keys: Storage['getKeys']
11+
get: Storage['getItem']
12+
set: Storage['setItem']
13+
has: Storage['hasItem']
14+
del: Storage['removeItem']
15+
}
16+
17+
let _kv: KV
1018

11-
export function hubKV(): Storage {
19+
export function hubKV(): KV {
1220
if (_kv) {
1321
return _kv
1422
}
1523
const hub = useRuntimeConfig().hub
1624
if (import.meta.dev && hub.projectUrl) {
17-
return useProxyKV(hub.projectUrl, hub.projectSecretKey || hub.userToken)
25+
return proxyHubKV(hub.projectUrl, hub.projectSecretKey || hub.userToken)
1826
}
1927
// @ts-ignore
2028
const binding = process.env.KV || globalThis.__env__?.KV || globalThis.KV
2129
if (binding) {
22-
_kv = createStorage({
30+
const storage = createStorage({
2331
driver: cloudflareKVBindingDriver({
2432
binding
2533
})
2634
})
35+
_kv = {
36+
keys: storage.getKeys,
37+
get: storage.getItem,
38+
set: storage.setItem,
39+
has: storage.hasItem,
40+
del: storage.removeItem,
41+
...storage,
42+
}
2743
return _kv
2844
}
2945
throw createError('Missing Cloudflare KV binding (KV)')
3046
}
3147

32-
export function useProxyKV(projectUrl: string, secretKey?: string): Storage {
33-
return createStorage({
48+
export function proxyHubKV(projectUrl: string, secretKey?: string): KV {
49+
const storage = createStorage({
3450
driver: httpDriver({
3551
base: joinURL(projectUrl, '/api/_hub/kv/'),
3652
headers: {
3753
Authorization: `Bearer ${secretKey}`
3854
}
3955
})
4056
})
57+
58+
return {
59+
keys: storage.getKeys,
60+
get: storage.getItem,
61+
set: storage.setItem,
62+
has: storage.hasItem,
63+
del: storage.removeItem,
64+
...storage,
65+
}
4166
}

0 commit comments

Comments
 (0)
0