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.

starter-fetch/src/app/notes.ts

38 lines · 1018 B · TypeScript
import { packageStorage } from 'kody:runtime'

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

const KEY = 'starter-notes-v1'

function storage() {
	return packageStorage()
}

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

export async function addNote(title: string): Promise<Note> {
	const notes = await listNotes()
	const note: Note = {
		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 storage().put(KEY, notes)
	return note
}

export async function deleteNote(id: string): Promise<{ ok: boolean }> {
	const notes = await listNotes()
	const next = notes.filter((n) => n.id !== id)
	await storage().put(KEY, next)
	return { ok: next.length !== notes.length }
}