Files
supabase/apps/docs/lib/custom-content/getCustomContent.ts
Chris Chinchilla d158e7142a docs: Allow for customisable CLI profiles in ref docs (#40181)
* Allow for customisable CLI profiles in ref docs

* Prettier

* Type fix

* Prettier

* Changes from review

* Set default config

* Handle empty values
2025-11-06 08:51:15 +01:00

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]> extends keyof CustomContentTypes
? CustomContentToCamelCase<T[number]>
: never]: CustomContentTypes[key] | null
}
export const getCustomContent = <T extends CustomContent[]>(
contents: T
): CustomContentResult<T> => {
return Object.fromEntries(
contents.map((content) => [contentToCamelCase(content), customContentStaticObj[content]])
) as CustomContentResult<T>
}