Skip to content
← Public packages

@kentcdodds/audible

Personal Audible library app — browser re-auth, library, wishlist, NAS archive stub.

src/locales.ts

121 lines · 2.5 KB · TypeScript
/** Audible marketplace templates (country_code → domain + marketplace id). */

export type LocaleInfo = {
	countryCode: string
	domain: string
	marketPlaceId: string
	apiHost: string
}

const TEMPLATES: Record<string, Omit<LocaleInfo, 'apiHost'>> = {
	us: {
		countryCode: 'us',
		domain: 'com',
		marketPlaceId: 'AF2M0KC94RCEA',
	},
	uk: {
		countryCode: 'uk',
		domain: 'co.uk',
		marketPlaceId: 'A2I9A3Q2GNFNGQ',
	},
	de: {
		countryCode: 'de',
		domain: 'de',
		marketPlaceId: 'AN7V1F1VY261K',
	},
	fr: {
		countryCode: 'fr',
		domain: 'fr',
		marketPlaceId: 'A2728XDNODOQ8T',
	},
	ca: {
		countryCode: 'ca',
		domain: 'ca',
		marketPlaceId: 'A2CQZ5RBY40XE',
	},
	it: {
		countryCode: 'it',
		domain: 'it',
		marketPlaceId: 'A2N7FU2W2BU2ZC',
	},
	au: {
		countryCode: 'au',
		domain: 'com.au',
		marketPlaceId: 'AN7EY7DTAW63G',
	},
	in: {
		countryCode: 'in',
		domain: 'in',
		marketPlaceId: 'AJO3FBRUE6J4S',
	},
	jp: {
		countryCode: 'jp',
		domain: 'co.jp',
		marketPlaceId: 'A1QAP3MOU4173J',
	},
	es: {
		countryCode: 'es',
		domain: 'es',
		marketPlaceId: 'ALMIKO4SZCSAR',
	},
	br: {
		countryCode: 'br',
		domain: 'com.br',
		marketPlaceId: 'A10J1VAYUDTYRN',
	},
}

/** Resolve locale from country code (default US). Accepts aliases like `en-US`, `com`, `united_states`. */
export function resolveLocale(input?: string | null): LocaleInfo {
	const raw = (input ?? 'us').trim().toLowerCase()
	const aliases: Record<string, string> = {
		'en-us': 'us',
		'en_us': 'us',
		com: 'us',
		united_states: 'us',
		'en-gb': 'uk',
		'en_gb': 'uk',
		'co.uk': 'uk',
		united_kingdom: 'uk',
		gb: 'uk',
		germany: 'de',
		france: 'fr',
		canada: 'ca',
		italy: 'it',
		australia: 'au',
		india: 'in',
		japan: 'jp',
		spain: 'es',
		brazil: 'br',
	}
	const code = aliases[raw] ?? raw
	const template = TEMPLATES[code]
	if (!template) {
		throw new Error(
			`Unknown Audible locale "${input}". Use one of: ${Object.keys(TEMPLATES).join(', ')}.`,
		)
	}
	return {
		...template,
		apiHost: `api.audible.${template.domain}`,
	}
}

/** Hostnames packages should list for secret host approval. */
export const AUDIBLE_API_HOSTS = Object.values(TEMPLATES).map(
	(t) => `api.audible.${t.domain}`,
)

/** Country codes supported by resolveLocale. */
export function listLocaleCodes(): string[] {
	return Object.keys(TEMPLATES)
}

/** Amazon + Audible API hosts for register/refresh/library (secret host approval). */
export const AMAZON_API_HOSTS = Object.values(TEMPLATES).map(
	(t) => `api.amazon.${t.domain}`,
)

export const AUTH_RELATED_HOSTS = [
	...new Set([...AUDIBLE_API_HOSTS, ...AMAZON_API_HOSTS]),
]