@feelinglovelynow/svelte-kv
v1.0.6
Published
Ease SvelteKit & Cloudflare KV workflow, where you may call Cloudflare's API during local development and utilize the platform object when deployed
Downloads
16
Maintainers
Readme
🕉 @feelinglovelynow/svelte-kv
💎 Install
pnpm add @feelinglovelynow/svelte-kv
🙏 Description
- From SvelteKit documentation:
platform.env is only available in the final build and not in dev mode
- This package brings ease to SvelteKit & Cloudflare KV development allowing you to call Cloudflare's API during local development and utilize the platform object when deployed
💚 Add svelteKVOptions to your code base
import { PUBLIC_ENVIRONMENT } from '$env/static/public'
import { enumSvelteKVPoint } from '@feelinglovelynow/svelte-kv'
import { CLOUDFLARE_KV_API_TOKEN, CLOUDFLARE_ACCOUNT_ID, CLOUDFLARE_KV_NAMESPACE_ID } from '$env/static/private'
export default {
doJSONParse: true, // on .get() would you love a JSON.parse() to happen
doJSONStringify: true, // on .put() would you love a JSON.stringify() to happen
accountId: CLOUDFLARE_ACCOUNT_ID, // Found @ Cloudflare Dashboard > Workers & Pages > Overview
apiToken: CLOUDFLARE_KV_API_TOKEN, // Found @ Cloudflare Dashboard > Workers & Pages > Manage API Tokens
namespace: 'CACHE', // Found @ Cloudflare Dashboard > Workers & Pages > KV
namespaceId: CLOUDFLARE_KV_NAMESPACE_ID, // Found @ Cloudflare Dashboard > Workers & Pages > KV > Click namespace
point: PUBLIC_ENVIRONMENT === 'local' ? enumSvelteKVPoint.api : enumSvelteKVPoint.platform, // Would you love .get() and .put() to use api or platform - PUBLIC_ENVIRONMENT is defined via @feelinglovelynow/env-write
}
💛 Put Example
import type { RequestHandler } from './$types'
import { serverCatch } from '$lib/global/catch'
import { SvelteKV } from '@feelinglovelynow/svelte-kv'
import svelteKVOptions from '$lib/global/svelteKVOptions'
export const GET = (async ({ locals, platform }) => {
try {
const svelteKV = new SvelteKV({ ...svelteKVOptions, platform })
await svelteKV.put('key', [ 'value', 'hello', 'world' ])
} catch (e) {
return serverCatch(e) // from @feelinglovelynow/svelte-catch
}
}) satisfies RequestHandler
🧡 Get Example
import type { LayoutServerLoad } from './$types'
import { pageServerCatch } from '$lib/global/catch'
import { SvelteKV } from '@feelinglovelynow/svelte-kv'
import svelteKVOptions from '$lib/global/svelteKVOptions'
export const load = (async ({ platform }) => {
try {
const svelteKV = new SvelteKV({ ...svelteKVOptions, platform })
const [ sources, products ] = await Promise.all([
svelteKV.get('sources'),
svelteKV.get('products')
])
} catch (e) {
return pageServerCatch(e) // from @feelinglovelynow/svelte-catch
}
}) satisfies LayoutServerLoad
🎁 All Our Packages
- @feelinglovelynow/datetime-local: NPM ⋅ Github
- @feelinglovelynow/dgraph: NPM ⋅ Github
- @feelinglovelynow/env-write: NPM ⋅ Github
- @feelinglovelynow/get-form-entries: NPM ⋅ Github
- @feelinglovelynow/get-relative-time: NPM ⋅ Github
- @feelinglovelynow/global-style: NPM ⋅ Github
- @feelinglovelynow/jwt: NPM ⋅ Github
- @feelinglovelynow/loop-backwards: NPM ⋅ Github
- @feelinglovelynow/slug: NPM ⋅ Github
- @feelinglovelynow/svelte-catch: NPM ⋅ Github
- @feelinglovelynow/svelte-kv: NPM ⋅ Github
- @feelinglovelynow/svelte-loading-anchor: NPM ⋅ Github
- @feelinglovelynow/svelte-modal: NPM ⋅ Github
- @feelinglovelynow/svelte-turnstile: NPM ⋅ Github
- @feelinglovelynow/toast: NPM ⋅ Github