enum ROUTES {
REQUEST_OTP = 'REQUEST_OTP',
LOGIN = 'LOGIN',
}
export const urls: { [key: string]: string } = {
[ROUTES.REQUEST_OTP]: '/v1/auth/otp',
[ROUTES.LOGIN]: '/v1/auth/login',
};
export function getUrl(route: string) {
return BASE_URL + urls[route];
}
Is there a better way to write ROUTES enum rather than repeatedly writing string literals next to it?
There seems to be a repetition of code in the my code writing the string enums.
Trying out mapped types:
type ROUTE_KEY = 'REQUEST_OTP' | 'LOGIN';
export const urls: { [key: string]: string } = {
REQUEST_OTP: '/v1/auth/request-otp',
LOGIN: '/v1/auth/login/?version=v2',
};
export function getUrl(route: ROUTE_KEY) {
return BASE_URL + urls[route];
}
{ [key: ROUTE_KEY]: string }type ROUTE_KEY = keyof typeof urls;(withurlsbeing declaredas const)