Skip to content

Kody is live

Watch the launch video — what Kody is, and why it exists.

← Public packages

@kentcdodds/package-app-kit

Design tokens, PWA install/update, About/version, cache helpers, and optional realtime notes sync for Kody package apps.

archive/fetch-demo/demo-notes.ts

37 lines · 1.0 KB · TypeScript
import { packageStorage } from 'kody:runtime'

export type DemoNote = {
	id: string
	title: string
	createdAt: string
}

const KEY = 'kit-demo-notes-v1'

export async function listDemoNotes(): Promise<DemoNote[]> {
	const raw = await packageStorage().get(KEY)
	if (!Array.isArray(raw)) return []
	return raw.filter(
		(n): n is DemoNote =>
			Boolean(n && typeof n === 'object' && typeof (n as DemoNote).id === 'string'),
	)
}

export async function addDemoNote(title: string): Promise<DemoNote> {
	const notes = await listDemoNotes()
	const note: DemoNote = {
		id: `n_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 7)}`,
		title: title.trim() || 'Untitled',
		createdAt: new Date().toISOString(),
	}
	notes.unshift(note)
	await packageStorage().set(KEY, notes)
	return note
}

export async function deleteDemoNote(id: string): Promise<{ ok: boolean }> {
	const notes = await listDemoNotes()
	const next = notes.filter((n) => n.id !== id)
	await packageStorage().set(KEY, next)
	return { ok: next.length !== notes.length }
}