-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBody.ts
More file actions
342 lines (315 loc) · 9.08 KB
/
Body.ts
File metadata and controls
342 lines (315 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { z } from 'zod'
import { ChartLineColours } from '@/api/models/Chart'
import { HealthAlertTypes } from '../../Alerts'
import { Blocks, HeadlineNumber, TrendNumber } from './Blocks'
import { Chart } from './Chart'
import { GlobalFilterRow, TimeRangeSchema } from './GlobalFilter'
/**
* Body Discriminated Union Types
* Variations of each object that can returned in the top-level body associated by the type key
*/
export const WithText = z.object({
body: z.string(),
})
export const ChartRelatedLinks = z.array(
z.object({
type: z.literal('related_link'),
id: z.string(),
value: z.object({
link: z.string(),
link_display_text: z.string(),
}),
})
)
export type ChartRelatedLink = z.infer<typeof ChartRelatedLinks>
const SourceLink = z.object({
link_display_text: z.string().nullable().optional(),
page: z.string().nullable().optional(),
external_url: z.string().nullable().optional(),
})
export const WithWeatherHealthAlertCard = z.object({
title: z.string(),
sub_title: z.string(),
description: z.string().nullable().optional(),
alert_type: HealthAlertTypes,
source: SourceLink.optional(),
})
export const WithHeadlineNumbersRowCard = z.object({
columns: z.array(
z.object({
id: z.string(),
type: z.literal('column'),
value: z.object({
title: z.string(),
date_prefix: z.string(),
rows: Blocks,
}),
})
),
})
const chartCardValues = z.object({
title: z.string(),
chart: Chart,
body: z.string(),
related_links: ChartRelatedLinks.optional(),
tag_manager_event_id: z.string().nullable(),
x_axis: z.string().nullable(),
y_axis: z.string().nullable(),
x_axis_title: z.string().optional(),
y_axis_title: z.string().optional(),
y_axis_minimum_value: z.number().nullable().optional(),
y_axis_maximum_value: z.number().nullable().optional(),
show_timeseries_filter: z.boolean().optional(),
date_prefix: z.string(),
about: z.string(),
confidence_intervals: z.boolean().optional(),
confidence_colour: ChartLineColours.nullable().optional(),
confidence_intervals_description: z.string().optional(),
})
export const WithChartHeadlineAndTrendCard = z.object({
id: z.string(),
type: z.literal('chart_with_headline_and_trend_card'),
value: chartCardValues.extend({
headline_number_columns: Blocks,
}),
})
export const WithChartCard = z.object({
id: z.string(),
type: z.enum(['chart_card', 'headline_chart_card']),
value: chartCardValues,
})
const WithSimplifiedChartCardAndLink = z.object({
id: z.string(),
type: z.enum(['simplified_chart_with_link']),
value: chartCardValues
.extend({
sub_title: z.string(),
topic_page: z.string(),
})
.omit({ body: true, date_prefix: true, about: true }),
})
const WithChartCardWithDescription = z.object({
id: z.string(),
type: z.enum(['chart_with_description_card']),
value: chartCardValues
.extend({
sub_title: z.string().optional(),
topic_page: z.string(),
description: z.string(),
source: SourceLink,
show_tooltips: z.boolean().optional(),
})
.omit({ body: true, date_prefix: true, about: true }),
})
/** Chart card value shape for popular topics (no body/date_prefix/about; has topic_page). */
const popularTopicsChartCardValue = chartCardValues
.extend({
sub_title: z.string().optional(),
topic_page: z.string(),
})
.omit({ body: true, date_prefix: true, about: true })
/** Chart card with description and source link (left column of popular topics card). */
export const ChartCardWithDescriptionValue = popularTopicsChartCardValue.extend({
description: z.string(),
source: SourceLink,
show_tooltips: z.boolean().optional(),
})
/** Headline metric card: title, date_prefix, and exactly 2 headline/trend blocks. */
const HeadlineMetricCardValue = z.object({
title: z.string(),
date_prefix: z.string(),
headline_metrics: z.array(z.discriminatedUnion('type', [HeadlineNumber, TrendNumber])).length(2),
})
/** Left column item: chart with description or weather health alert. */
const PopularTopicsLeftColumnItem = z.discriminatedUnion('type', [
z.object({
type: z.literal('chart_card_with_description'),
value: ChartCardWithDescriptionValue,
id: z.string(),
}),
z.object({
type: z.literal('weather_health_alert_card'),
value: WithWeatherHealthAlertCard,
id: z.string(),
}),
])
/** Right column top row: chart card linking to topic page. */
const PopularTopicsRightColumnTopItem = z.object({
type: z.literal('chart_card'),
value: popularTopicsChartCardValue,
id: z.string(),
})
/** Right column bottom row: headline metric card. */
const PopularTopicsRightColumnBottomItem = z.object({
type: z.literal('headline_metric_card'),
value: HeadlineMetricCardValue,
id: z.string(),
})
/** Popular topics card value (left column + right top/bottom rows). */
export const PopularTopicsCardValue = z.object({
left_column: z.array(PopularTopicsLeftColumnItem).length(1),
right_column_top_row: z.array(PopularTopicsRightColumnTopItem).length(1),
right_column_bottom_row: z.array(PopularTopicsRightColumnBottomItem).length(2),
})
export const ChartCardSchemas = z.discriminatedUnion('type', [
WithChartHeadlineAndTrendCard,
WithChartCard,
WithSimplifiedChartCardAndLink,
WithChartCardWithDescription,
])
export const CardTypes = z.discriminatedUnion('type', [
z.object({
type: z.literal('text_card'),
value: WithText,
id: z.string(),
}),
z.object({
type: z.literal('headline_numbers_row_card'),
value: WithHeadlineNumbersRowCard,
id: z.string(),
}),
z.object({
type: z.literal('chart_row_card'),
value: z.object({
columns: z.array(z.union([WithChartHeadlineAndTrendCard, WithChartCard])),
}),
id: z.string(),
}),
z.object({
type: z.literal('filter_linked_map'),
value: z.object({
title_prefix: z.optional(z.string()),
about: z.optional(z.string()),
}),
id: z.string(),
}),
z.object({
type: z.literal('chart_card_section'),
value: z.object({
cards: z.array(z.union([WithSimplifiedChartCardAndLink, WithChartCardWithDescription])),
}),
id: z.string(),
}),
z.object({
type: z.literal('weather_health_alert_card'),
value: WithWeatherHealthAlertCard,
id: z.string(),
}),
z.object({
type: z.literal('global_filter_card'),
value: z.object({
time_range: TimeRangeSchema,
rows: GlobalFilterRow,
}),
id: z.string(),
}),
z.object({
type: z.literal('filter_linked_sub_plot_chart_template'),
value: z.object({
title_prefix: z.string(),
legend_title: z.string(),
target_threshold: z.number(),
target_threshold_label: z.string().optional(),
about: z.string().optional(),
}),
id: z.string(),
}),
z.object({
type: z.literal('filter_linked_time_series_chart_template'),
value: z.object({
title_prefix: z.string(),
legend_title: z.string(),
about: z.string().optional(),
}),
id: z.string(),
}),
z.object({
type: z.literal('popular_topics_card'),
value: PopularTopicsCardValue,
id: z.string(),
}),
])
export const Body = z.array(
z.object({
type: z.literal('section'),
id: z.string(),
value: z.object({
heading: z.string(),
content: z.array(CardTypes),
page_link: z.optional(z.nullable(z.string())),
}),
})
)
export type Body = z.infer<typeof Body>
export const CompositeBody = z.array(
z.discriminatedUnion('type', [
z.object({
type: z.literal('text'),
value: z.string(),
id: z.string(),
}),
z.object({
type: z.literal('code_block'),
value: z.object({
heading: z.string(),
content: z.array(
z.object({
type: z.literal('code_snippet'),
value: z.object({
language: z.string(),
code: z.string(),
}),
id: z.string(),
})
),
}),
id: z.string(),
}),
z.object({
type: z.literal('internal_button'),
value: z.object({
text: z.string(),
button_type: z.enum(['BULK_DOWNLOAD']),
endpoint: z.string(),
method: z.enum(['POST', 'GET']),
}),
id: z.string(),
}),
z.object({
type: z.literal('external_button'),
value: z.object({
text: z.string(),
url: z.string(),
button_type: z.string().toLowerCase(),
icon: z.string(),
}),
id: z.string(),
}),
z.object({
type: z.literal('internal_page_links'),
value: z.array(
z.object({
type: z.literal('page_link'),
value: z.object({
title: z.string(),
sub_title: z.string(),
page: z.string(),
}),
id: z.string(),
})
),
id: z.string(),
}),
z.object({
type: z.literal('wha_button'),
value: z.object({
text: z.string(),
button_type: z.string(),
geography_code: z.string().nullable().optional(),
}),
id: z.string(),
}),
])
)
export type CardTypes = z.infer<typeof CardTypes>
export type CompositeBody = z.infer<typeof CompositeBody>