@edgedev/create-edge-site 1.0.11 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/nuxt.config.ts +10 -1
- package/package.json +2 -1
- package/server/api/hello.ts +2 -2
- package/server/api/kv/[key].ts +17 -0
- package/server/utils/getKV.js +65 -0
- package/wrangler.toml +3 -0
package/nuxt.config.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import process from 'node:process'
|
|
1
2
|
import { defineNuxtConfig } from 'nuxt/config'
|
|
2
3
|
|
|
3
4
|
export default defineNuxtConfig({
|
|
@@ -28,7 +29,7 @@ export default defineNuxtConfig({
|
|
|
28
29
|
css: [
|
|
29
30
|
'~/assets/css/global.css', // ✅ Keep global styles only
|
|
30
31
|
],
|
|
31
|
-
modules: ['@nuxtjs/tailwindcss', '@vee-validate/nuxt'],
|
|
32
|
+
modules: ['@nuxtjs/tailwindcss', '@vee-validate/nuxt', 'nitro-cloudflare-dev'],
|
|
32
33
|
vite: {
|
|
33
34
|
define: {
|
|
34
35
|
'process.env.DEBUG': false,
|
|
@@ -41,4 +42,12 @@ export default defineNuxtConfig({
|
|
|
41
42
|
},
|
|
42
43
|
},
|
|
43
44
|
devtools: { enabled: false },
|
|
45
|
+
runtimeConfig: {
|
|
46
|
+
// These values are only available on the server
|
|
47
|
+
cfAccountId: process.env.NUXT_CF_ACCOUNT_ID,
|
|
48
|
+
cfKVNamespaceId: process.env.NUXT_CF_KV_NAMESPACE_ID,
|
|
49
|
+
cfApiToken: process.env.NUXT_CF_API_TOKEN,
|
|
50
|
+
|
|
51
|
+
public: {}, // nothing here unless you want it client-exposed
|
|
52
|
+
},
|
|
44
53
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edgedev/create-edge-site",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.13",
|
|
4
4
|
"description": "Create Edge Starter Site",
|
|
5
5
|
"bin": {
|
|
6
6
|
"create-edge-site": "./bin/cli.js"
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@antfu/eslint-config": "^4.11.0",
|
|
33
33
|
"eslint": "^9",
|
|
34
|
+
"nitro-cloudflare-dev": "^0.2.2",
|
|
34
35
|
"wrangler": "^4.14.4"
|
|
35
36
|
}
|
|
36
37
|
}
|
package/server/api/hello.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export default defineEventHandler(() => {
|
|
2
|
-
return { message:
|
|
3
|
-
})
|
|
2
|
+
return { message: 'Hello from World' }
|
|
3
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getKVValue } from '~/server/utils/getKV'
|
|
2
|
+
|
|
3
|
+
export default defineEventHandler(async (event) => {
|
|
4
|
+
const { key } = getRouterParams(event)
|
|
5
|
+
|
|
6
|
+
const context = event.context
|
|
7
|
+
const value = await getKVValue(key, context)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
if (!value) {
|
|
11
|
+
return sendError(event, createError({ statusCode: 404, statusMessage: 'Not found'}))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const jsonValue = JSON.parse(value)
|
|
15
|
+
|
|
16
|
+
return jsonValue
|
|
17
|
+
})
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { useRuntimeConfig } from '#imports'
|
|
2
|
+
|
|
3
|
+
export async function getKVValue(key, context) {
|
|
4
|
+
const config = useRuntimeConfig()
|
|
5
|
+
const isDev = import.meta.dev
|
|
6
|
+
const MY_KV = context?.cloudflare?.env?.MY_KV
|
|
7
|
+
|
|
8
|
+
const fetchFromAPI = async () => {
|
|
9
|
+
const { cfAccountId, cfKVNamespaceId, cfApiToken } = config
|
|
10
|
+
const url = `https://api.cloudflare.com/client/v4/accounts/${cfAccountId}/storage/kv/namespaces/${cfKVNamespaceId}/values/${key}`
|
|
11
|
+
const res = await fetch(url, {
|
|
12
|
+
headers: {
|
|
13
|
+
Authorization: `Bearer ${cfApiToken}`,
|
|
14
|
+
},
|
|
15
|
+
})
|
|
16
|
+
if (!res.ok) {
|
|
17
|
+
console.warn('❌ Remote fetch failed')
|
|
18
|
+
return null
|
|
19
|
+
}
|
|
20
|
+
return await res.text()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Dev only: sync from API to local KV
|
|
24
|
+
if (isDev && typeof MY_KV !== 'undefined') {
|
|
25
|
+
console.log('🛠 Dev mode – syncing from API to local KV')
|
|
26
|
+
const remoteValue = await fetchFromAPI()
|
|
27
|
+
if (remoteValue !== null) {
|
|
28
|
+
try {
|
|
29
|
+
await MY_KV.put(key, remoteValue)
|
|
30
|
+
console.log('📥 Synced to local KV')
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
console.warn('⚠️ Failed to write to local KV:', e)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Always try local KV first if available
|
|
39
|
+
if (typeof MY_KV !== 'undefined') {
|
|
40
|
+
const localValue = await MY_KV.get(key)
|
|
41
|
+
if (localValue !== null) {
|
|
42
|
+
console.log('✅ Retrieved from local KV')
|
|
43
|
+
return localValue
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (isDev) {
|
|
47
|
+
console.log('🔍 Not found in KV, trying API fallback (dev only)')
|
|
48
|
+
return await fetchFromAPI()
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
console.warn('🚫 Not found in KV and API fallback is disabled in production')
|
|
52
|
+
return null
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// No KV available
|
|
57
|
+
if (isDev) {
|
|
58
|
+
console.log('🌐 No KV available, trying API fallback (dev only)')
|
|
59
|
+
return await fetchFromAPI()
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
console.warn('🚫 No KV and no API fallback in production')
|
|
63
|
+
return null
|
|
64
|
+
}
|
|
65
|
+
}
|
package/wrangler.toml
ADDED