import { packageStorage } from 'kody:runtime' import { DEFAULT_API_VERSION_KEY, DEFAULT_SHOP_KEY, } from './lib/client.ts' import { DEFAULT_API_VERSION, normalizeShop, resolveApiVersion, } from './lib/shop.ts' export type ShopifyConfig = { defaultShop: string | null defaultApiVersion: string note: string } async function readString(key: string): Promise { const store = packageStorage() const raw = await store.get(key) return typeof raw === 'string' && raw.trim() ? raw.trim() : null } /** * Read fork-local defaults from `packageStorage()`. * * Live `@kody/shopify` storage is the platform package bucket, not yours. * Fork first (or `packages.invoke` your copy) before relying on these defaults. * * @example * import { getConfig } from 'kody:@kody/shopify/config' * const config = await getConfig() */ export async function getConfig(): Promise { const defaultShop = await readString(DEFAULT_SHOP_KEY) const storedVersion = await readString(DEFAULT_API_VERSION_KEY) return { defaultShop: defaultShop ? normalizeShop(defaultShop) : null, defaultApiVersion: resolveApiVersion( storedVersion || DEFAULT_API_VERSION, ), note: 'These defaults live in this package\'s packageStorage. Fork or invoke your own copy before setting them.', } } /** * Persist a default shop handle for later calls that omit `shop`. */ export async function setDefaultShop(params: { shop: string }) { const shop = normalizeShop(params.shop) const store = packageStorage() await store.set(DEFAULT_SHOP_KEY, shop) return { defaultShop: shop } } /** * Persist a default Admin API version (`YYYY-MM`). */ export async function setDefaultApiVersion(params: { apiVersion: string }) { const apiVersion = resolveApiVersion(params.apiVersion) const store = packageStorage() await store.set(DEFAULT_API_VERSION_KEY, apiVersion) return { defaultApiVersion: apiVersion } } /** * Clear stored shop and API version defaults. */ export async function clearConfig() { const store = packageStorage() await store.set(DEFAULT_SHOP_KEY, '') await store.set(DEFAULT_API_VERSION_KEY, '') return { cleared: true } } /** Default export: read config. */ export default getConfig