Skip to content
← Public packages

@kentcdodds/audible

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

src/app/urls.ts

27 lines · 959 B · TypeScript
import { packageContext } from 'kody:runtime'

export function appPaths() {
	const appBasePath = packageContext?.appBasePath || '/packages/audible'
	const hostedUrl =
		packageContext?.hostedUrl || `https://kody.kody.run${appBasePath}`
	return { appBasePath, hostedUrl }
}

/** Build a mount-safe absolute URL for an in-app path. */
export function appUrl(path: string): string {
	const { appBasePath, hostedUrl } = appPaths()
	const relative = path.replace(/^\/+/, '')
	const mount = appBasePath.endsWith('/') ? appBasePath : `${appBasePath}/`
	return new URL(`${mount}${relative}`, hostedUrl).toString()
}

export function pathnameOf(request: Request, appBasePath: string): string {
	const url = new URL(request.url)
	let path = url.pathname
	if (appBasePath && path.startsWith(appBasePath)) {
		path = path.slice(appBasePath.length) || '/'
	}
	if (path === '') path = '/'
	if (path.length > 1 && path.endsWith('/')) path = path.slice(0, -1)
	return path
}