๐ @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