Run callbacks with the property key.
@stagas/getter exports a tiny Getter helper that calls a callback every time
a property is read. It is useful for APIs where the property name is the input:
CSS variables, translation keys, route builders, token maps, and other compact
lookup-style interfaces.
npm install @stagas/getterimport { Getter } from '@stagas/getter'
const css = Getter(key => `var(--${key})`)
css.color
// "var(--color)"
css.background
// "var(--background)"You can also pass an existing target object when the proxy needs to preserve a specific prototype or object identity.
import { Getter } from '@stagas/getter'
const target = {}
const paths = Getter(key => `/api/${key}`, target)
paths.users
// "/api/users"Creates a proxy that calls cb with the accessed property key and returns the
callback result.
const value = Getter<T>(
cb: (key: string) => T,
target?: any,
)cb: Function called with the property key being accessed.target: Optional proxy target. Defaults to an empty object.
A proxy typed as Record<string, T>.
MIT