```tsx import { commonParams } from "@/constants/params"; import { toTitleCase } from "./stringCase"; import { GetArrayElementType } from "@/interfaces/utilities"; export const replacePathParams = ( path: string, params: Record<string, string>, ) => { return Object.entries(params).reduce((acc, [key, value]) => { return acc.replace(`:${key}`, value); }, path); }; type PrefixedParams<T extends string> = `${T}${Capitalize<GetArrayElementType<typeof commonParams>>}`; type CommonParamsType<T> = T extends string ? PrefixedParams<T>[] : GetArrayElementType<typeof commonParams>[]; /** * Generates common parameters with an optional prefix. * If a prefix is provided, each common parameter is prefixed and capitalized. * * @param prefix - Optional prefix to prepend to each common parameter * @returns Array of common parameters, optionally prefixed */ export const getCommonParams = <T extends string | undefined>( prefix?: T, ): CommonParamsType<T> => { return commonParams.map((p) => { if (!prefix) return p; return `${prefix}${toTitleCase(p)}` as string; }) as CommonParamsType<T>; }; /** * Removes the specified prefix from each prefixed parameter. * Converts the resulting string to lowercase to match the original common parameters. * * @param prefix - The prefix to remove from each parameter * @param params - Array of prefixed parameters * @returns Array of common parameters without the prefix */ export const removePrefixFromCommonParams = <T extends string>( prefix: T, params: PrefixedParams<T>[], ): GetArrayElementType<typeof commonParams>[] => params.map( (p) => p.replace(prefix, "").toLowerCase() as GetArrayElementType< typeof commonParams >, ); ```