generated from DTS-STN/next-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontact-us-pages-dynamic.ts
311 lines (304 loc) · 10.9 KB
/
contact-us-pages-dynamic.ts
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
import { cachified } from 'cachified'
import { lruCache as cache, defaultTtl as ttl } from '../../lib/cache-utils'
import { buildAemUri } from '../../lib/links'
interface GetSchContactUsDynamicV1 {
data: {
schPageV1List: {
items: Array<{
_path: string
scId: string
scTitleEn: string
scTitleFr: string
scPageNameEn: string
scPageNameFr: string
scDescriptionEn: {
plaintext: string
}
scDescriptionFr: {
plaintext: string
}
scShortTitleEn: null
scShortTitleFr: null
scBreadcrumbParentPages: Array<{
_path: string
scId: string
scPageNameEn: string
scPageNameFr: string
scTitleEn: string
scTitleFr: string
scShortTitleFr: null
scShortTitleEn: null
scOwner: Array<string>
scDateModifiedOverwrite: string
scAudience: null
}>
scContentType: Array<string>
scAudience: null
scOwner?: Array<string>
scFragments: Array<{
_path: string
scId: string
scTitleEn: string
scTitleFr: string
scItems: Array<{
_path: string
scId: string
scTitleEn: string
scTitleFr: string
schContactMethodLayout?: string
schIntroEn: {
markdown: string
}
schIntroFr: {
markdown: string
}
schDetails: Array<{
_path: string
scBackgroundColour?: boolean
scTitleEn: string
scTitleFr: string
scId: string
scItems: Array<{
_path: string
scId: string
scContentEn?: {
markdown?: string
}
scContentFr?: {
markdown?: string
}
scIconCSS?: string
scFragments?: Array<{
scId: string
scLinkTextEn?: string
scLinkTextFr?: string
scDestinationURLEn?: string
scDestinationURLFr?: string
scButtonType?: Array<string>
_path?: string
scRecipientEn?: string
scRecipientFr?: string
scProgramEn?: string
scProgramFr?: string
scPostalBoxEn?: string
scPostalBoxFr?: string
scPostalStationEn?: string
scPostalStationFr?: string
scCityEn?: string
scCityFr?: string
scProvTerrAbbr?: Array<string>
scPostalCode?: string
scCountry?: Array<string>
scProvTerrAbbrEnum?: string
scCountryEn?: string
scCountryFr?: string
}>
scRecipientEn?: string
scRecipientFr?: string
scPostalBoxEn?: string
scPostalBoxFr?: string
scPostalStationEn?: string
scPostalStationFr?: string
scCityEn?: string
scCityFr?: string
scProvTerrAbbr?: Array<string>
scPostalCode?: string
scCountry?: Array<string>
scProvTerrAbbrEnum?: string
scCountryEn?: string
scCountryFr?: string
}>
}>
}>
}>
}>
}
}
}
const getCachedContent = () => {
return cachified({
key: `content-dynamic-contact-us`,
cache,
getFreshValue: async (): Promise<GetSchContactUsDynamicV1 | null> => {
const targetUri = buildAemUri('getSchContactUsDynamicV2')
const response = await fetch(targetUri)
if (!response.ok) return null
return await response.json()
},
ttl,
})
}
/**
* Awaited: Recursively unwraps the "awaited type" of a type.
* ReturnType: Obtain the return type of a function type
*/
export type GetContactUsPageReturnType = Awaited<
ReturnType<typeof getContactUsPage>
>
export async function getContactUsPage(id: string) {
const response = await getCachedContent()
const queryData = response?.data.schPageV1List.items.find(
({ scId }) => scId === id,
)
// Fail fast if a non-existent page is queried
if (!queryData) return
const mappedContactPage = {
en: {
breadcrumb: queryData.scBreadcrumbParentPages.map((level) => {
return {
link: level.scPageNameEn,
text: level.scTitleEn,
}
}),
title: queryData.scTitleEn,
description: queryData.scDescriptionEn.plaintext,
...queryData.scFragments.map((body) => {
return {
id: body.scId,
subHeader: body.scTitleEn,
items: body.scItems.map((fragment) => {
return {
id: fragment.scId,
title: fragment.scTitleEn,
intro: fragment.schIntroEn.markdown,
layout: fragment.schContactMethodLayout,
details: fragment.schDetails.map((row) => {
return {
id: row.scId,
color: row.scBackgroundColour,
title: row.scTitleEn,
items: row.scItems.map((contentItem) => {
if (contentItem.scContentEn) {
//Return address nested in content
return {
content: contentItem.scContentEn.markdown,
icon: contentItem.scIconCSS,
...((contentItem.scFragments ?? []).map(
(destination) => {
if (!destination.scButtonType) {
return {
city: destination.scCityEn,
country: destination.scCountryEn,
id: destination.scId,
poBox: destination.scPostalBoxEn,
postal: destination.scPostalCode,
station: destination.scPostalStationEn,
program: destination.scProgramEn,
province: destination.scProvTerrAbbrEnum,
recipient: destination.scRecipientEn,
}
} else {
return {
id: destination.scId,
content: destination.scLinkTextEn,
link: destination.scDestinationURLEn,
button: destination.scButtonType,
}
}
},
)[0] ?? {}),
}
} else {
//Return address unnested
return {
city: contentItem.scCityEn,
country: contentItem.scCountryEn?.toUpperCase(),
id: contentItem.scId,
poBox: contentItem.scPostalBoxEn,
postal: contentItem.scPostalCode,
province: contentItem.scProvTerrAbbrEnum,
recipient: contentItem.scRecipientEn,
station: contentItem.scPostalStationEn,
}
}
}),
}
}),
}
}),
}
})[0],
id: queryData.scId,
pageName: queryData.scPageNameEn,
},
fr: {
breadcrumb: queryData.scBreadcrumbParentPages.map((level) => {
return {
link: level.scPageNameFr,
text: level.scTitleFr,
}
}),
title: queryData.scTitleFr,
description: queryData.scDescriptionFr.plaintext,
...queryData.scFragments.map((body) => {
return {
id: body.scId,
subHeader: body.scTitleFr,
items: body.scItems.map((fragment) => {
return {
id: fragment.scId,
title: fragment.scTitleFr,
intro: fragment.schIntroFr.markdown,
layout: fragment.schContactMethodLayout,
details: fragment.schDetails.map((row) => {
return {
id: row.scId,
title: row.scTitleFr,
color: row.scBackgroundColour,
items: row.scItems.map((contentItem) => {
if (contentItem.scContentFr) {
//Return address nested in content
return {
content: contentItem.scContentFr.markdown,
icon: contentItem.scIconCSS,
...((contentItem.scFragments ?? []).map(
(destination) => {
if (!destination.scButtonType) {
return {
city: destination.scCityFr,
country: destination.scCountryFr,
id: destination.scId,
poBox: destination.scPostalBoxFr,
postal: destination.scPostalCode,
station: destination.scPostalStationFr,
program: destination.scProgramFr,
province: destination.scProvTerrAbbrEnum,
recipient: destination.scRecipientFr,
}
} else {
return {
id: destination.scId,
content: destination.scLinkTextFr,
link: destination.scDestinationURLFr,
button: destination.scButtonType,
}
}
},
)[0] ?? {}),
}
} else {
//Return address unnested
return {
city: contentItem.scCityFr,
country: contentItem.scCountryFr?.toUpperCase(),
id: contentItem.scId,
poBox: contentItem.scPostalBoxFr,
postal: contentItem.scPostalCode,
province: contentItem.scProvTerrAbbrEnum,
recipient: contentItem.scRecipientFr,
station: contentItem.scPostalStationFr,
}
}
}),
}
}),
}
}),
}
})[0],
id: queryData.scId,
pageName: queryData.scPageNameFr,
},
}
return mappedContactPage
}