Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@danj/audible-auth

Locked Audible gateway using one owner-provisioned Kody login.

FollowFork

Files

seed.ts

36 lines · 1.4 KB · TypeScript
import { packageContext } from 'kody:runtime'
import { loadAccount, saveAccount, type AudibleRow, type FakeAccount } from './state.ts'

const rows = (value: unknown, name: string): AudibleRow[] => {
	if (!Array.isArray(value) || value.some((r) => !r || typeof r !== 'object' || typeof r.asin !== 'string')) throw new Error(`${name} must be an array of rows with an asin.`)
	return value as AudibleRow[]
}

/**
 * Mock-only: replace the fake account, or disconnect with `{ connected: false }`.
 * Refuses outside a local run, so it cannot write to a hosted bucket.
 */
export default async function seed(input: {
	connected?: boolean
	locale?: string
	library?: unknown
	wishlist?: unknown
	accountInformation?: unknown
}) {
	const id = String(packageContext?.packageId ?? '')
	if (!id.startsWith('local')) throw new Error(`The audible-auth fake seeds only local runs; packageId is ${JSON.stringify(id)}.`)
	if (input.connected === false) {
		await saveAccount(null)
		return { connected: false, bucket: id }
	}
	const account: FakeAccount = {
		locale: input.locale ?? 'ca',
		connectedAt: new Date().toISOString(),
		library: rows(input.library ?? [], 'library'),
		wishlist: rows(input.wishlist ?? [], 'wishlist'),
		accountInformation: input.accountInformation ?? {},
	}
	await saveAccount(account)
	const saved = await loadAccount()
	return { connected: true, bucket: id, library: saved?.library.length ?? 0, wishlist: saved?.wishlist.length ?? 0 }
}