Files
supabase/apps/docs/lib/custom-content/getCustomContent.ts

36 lines
1.3 KiB
TypeScript

import type { CustomContentTypes } from './CustomContent.types'
import customContentRaw from './custom-content.json'
const customContentStaticObj = customContentRaw as Omit<typeof customContentRaw, '$schema'>
export type CustomContent = keyof typeof customContentStaticObj
type SnakeToCamelCase<S extends string> = S extends `${infer First}_${infer Rest}`
? `${First}${SnakeToCamelCase<Capitalize<Rest>>}`
: S
type CustomContentToCamelCase<S extends CustomContent> = S extends `${infer P}:${infer R}`
? `${SnakeToCamelCase<P>}${Capitalize<SnakeToCamelCase<R>>}`
: SnakeToCamelCase<S>
function contentToCamelCase(feature: CustomContent) {
return feature
.replace(/:/g, '_')
.split('_')
.map((word, index) => (index === 0 ? word : word[0].toUpperCase() + word.slice(1)))
.join('') as CustomContentToCamelCase<typeof feature>
}
type CustomContentResult<T extends CustomContent[]> = {
[key in CustomContentToCamelCase<T[number]>]:
| CustomContentTypes[CustomContentToCamelCase<T[number]>]
| null
}
export const getCustomContent = <T extends CustomContent[]>(
contents: T
): CustomContentResult<T> => {
return Object.fromEntries(
contents.map((content) => [contentToCamelCase(content), customContentStaticObj[content]])
) as CustomContentResult<T>
}