|
1 | 1 | import type { components } from '@/data/umbraco/deliveryApiSchema'; |
2 | 2 |
|
3 | | -export type UmbracoContent = components['schemas']['IApiContentResponseModel']; |
4 | | -export type UmbracoContentCollection = |
| 3 | +const CONTENT_TYPE_MAP = { |
| 4 | + PageContentModel: 'page', |
| 5 | + PageContentResponseModel: 'page', |
| 6 | + SessionContentModel: 'session', |
| 7 | + SessionContentResponseModel: 'session', |
| 8 | + SessionsContentModel: 'sessions', |
| 9 | + SessionsContentResponseModel: 'sessions', |
| 10 | + SpeakerContentModel: 'speaker', |
| 11 | + SpeakerContentResponseModel: 'speaker', |
| 12 | + SpeakersContentModel: 'speakers', |
| 13 | + SpeakersContentResponseModel: 'speakers', |
| 14 | + SponsorsContentModel: 'sponsors', |
| 15 | + SponsorsContentResponseModel: 'sponsors', |
| 16 | + TrackContentModel: 'track', |
| 17 | + TrackContentResponseModel: 'track', |
| 18 | +} as const; |
| 19 | + |
| 20 | +type ContentTypeMap = { |
| 21 | + [K in keyof typeof CONTENT_TYPE_MAP]: (typeof CONTENT_TYPE_MAP)[K]; |
| 22 | +}; |
| 23 | + |
| 24 | +type NormalizeContentType<T extends string> = T extends keyof ContentTypeMap |
| 25 | + ? ContentTypeMap[T] |
| 26 | + : T; |
| 27 | + |
| 28 | +type NormalizeUmbracoType<T> = T extends (infer U)[] |
| 29 | + ? NormalizeUmbracoType<U>[] |
| 30 | + : T extends readonly (infer U)[] |
| 31 | + ? readonly NormalizeUmbracoType<U>[] |
| 32 | + : T extends object |
| 33 | + ? { |
| 34 | + [K in keyof T]: K extends 'contentType' |
| 35 | + ? T[K] extends string |
| 36 | + ? NormalizeContentType<T[K]> |
| 37 | + : T[K] |
| 38 | + : NormalizeUmbracoType<T[K]>; |
| 39 | + } |
| 40 | + : T; |
| 41 | + |
| 42 | +export type UmbracoContent = NormalizeUmbracoType< |
| 43 | + components['schemas']['IApiContentResponseModel'] |
| 44 | +>; |
| 45 | +export type RawUmbracoContent = |
| 46 | + components['schemas']['IApiContentResponseModel']; |
| 47 | +export type UmbracoContentCollection = NormalizeUmbracoType< |
| 48 | + components['schemas']['PagedIApiContentResponseModel'] |
| 49 | +>; |
| 50 | +export type RawUmbracoContentCollection = |
5 | 51 | components['schemas']['PagedIApiContentResponseModel']; |
6 | 52 |
|
7 | 53 | export type ContentTypes = UmbracoContent; |
8 | 54 | export type ContentTypeKeys = ContentTypes['contentType']; |
9 | 55 |
|
| 56 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 57 | + return typeof value === 'object' && value !== null; |
| 58 | +} |
| 59 | + |
| 60 | +function normalizeValue(value: unknown): unknown { |
| 61 | + if (Array.isArray(value)) { |
| 62 | + return value.map(normalizeValue); |
| 63 | + } |
| 64 | + |
| 65 | + if (!isRecord(value)) { |
| 66 | + return value; |
| 67 | + } |
| 68 | + |
| 69 | + return Object.fromEntries( |
| 70 | + Object.entries(value).map(([key, entry]) => { |
| 71 | + if (key === 'contentType' && typeof entry === 'string') { |
| 72 | + return [ |
| 73 | + key, |
| 74 | + CONTENT_TYPE_MAP[entry as keyof ContentTypeMap] ?? entry, |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | + return [key, normalizeValue(entry)]; |
| 79 | + }), |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +export function normalizeUmbracoContent( |
| 84 | + content: RawUmbracoContent, |
| 85 | +): UmbracoContent { |
| 86 | + return normalizeValue(content) as UmbracoContent; |
| 87 | +} |
| 88 | + |
| 89 | +export function normalizeUmbracoContentCollection( |
| 90 | + collection: RawUmbracoContentCollection, |
| 91 | +): UmbracoContentCollection { |
| 92 | + return normalizeValue(collection) as UmbracoContentCollection; |
| 93 | +} |
| 94 | + |
10 | 95 | export type UmbracoClientOptions = { |
11 | 96 | requestOptions?: { |
12 | 97 | next?: RequestInit['next']; |
|
0 commit comments