-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppConfig.ts
282 lines (251 loc) · 8.86 KB
/
AppConfig.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
import {
getEnvOrError,
EventTypeId,
ignoredErrorCodesHeader,
} from '@events-helsinki/components';
import type { CommonButtonProps } from 'hds-react';
import getConfig from 'next/config';
import { ROUTES } from '../../constants';
// Only holds publicRuntimeConfig
const { publicRuntimeConfig } = getConfig();
class AppConfig {
/**
* The base URL of the CMS.
*
* Example usage:
* The headless CMS adds it's own domain (and possibly also the root path)
* to every link that it returns through API. The cmsOrigin-getter is used
* in the link manipulation so that we can remove the CMS origin from the link
* inside the app.
* */
static get cmsOrigin() {
return getEnvOrError(publicRuntimeConfig.cmsOrigin, 'CMS_ORIGIN');
}
/**
* The endpoint for the Apollo federation Router.
* Can be used for example to configure the Apollo-client.
* */
static get federationGraphqlEndpoint() {
return getEnvOrError(
publicRuntimeConfig.federationRouter,
'FEDERATION_ROUTER_ENDPOINT'
);
}
/**
* The base URL of the LinkedEvents event-API.
*
* Example usage:
* The headless CMS returns the LinkedEvent URLs through the API.
* The linkedEventsEventEndpoint-getter is used
* in the link manipulation so that we can remove the LinkedEvents origin from the link
* inside the app.
* */
static get linkedEventsEventEndpoint() {
return getEnvOrError(
publicRuntimeConfig.linkedEvents,
'LINKEDEVENTS_EVENT_ENDPOINT'
);
}
/**
* The own origin of the app.
* Can be used e.g. to configure the Headless CMS React Components -library.
* */
static get origin() {
return getEnvOrError(
process.env.NEXT_PUBLIC_APP_ORIGIN,
'NEXT_PUBLIC_APP_ORIGIN'
);
}
/**
* The own hostname of the app.
*/
static get hostname() {
return new URL(this.origin).hostname;
}
/**
* The app uses multiple domains from the Headless CMS API.
* It serves posts / articles from 1 root and e.g the pages from another.
* The CMS needs to be configured so, that the URI of an object it serves
* contains the context. For example an URL to an article could contain
* a static path `/articles` in it's pathname, so it's easily recognizible as an article URL.
* The application can then use a PostQuery to fetch the related information from an external service.
*/
static get cmsArticlesContextPath() {
return process.env.NEXT_PUBLIC_CMS_ARTICLES_CONTEXT_PATH ?? '/articles';
}
/**
* The app uses multiple domains from the Headless CMS API.
* It serves posts / articles from 1 root and e.g the pages from another.
* The CMS needs to be configured so, that the URI of an object it serves
* contains the context. For example an URL to an page could contain
* a static path `/pages` in it's pathname, so it's easily recognizible as a page URL.
* The application can then use a PageQuery to fetch the related information from an external service.
*/
static get cmsPagesContextPath() {
return process.env.NEXT_PUBLIC_CMS_PAGES_CONTEXT_PATH ?? '/pages';
}
/** The supported LinkedEvent event types. */
static get supportedEventTypes() {
return [EventTypeId.General];
}
/**
* The generally used date format.
* Helsinki services are recommended to follow the Finnish date and time locale -
* even if the user is using some other language.
* Follow these guidelines when presenting date and time in your services.
* https://hds.hel.fi/foundation/guidelines/data-formats/
*/
static get dateFormat() {
return 'dd.MM.yyyy';
}
/**
* The generally used short date time format.
* Helsinki services are recommended to follow the Finnish date and time locale -
* even if the user is using some other language.
* Follow these guidelines when presenting date and time in your services.
* https://hds.hel.fi/foundation/guidelines/data-formats/
*/
static get shortDatetimeFormat() {
return 'dd.MM.yyyy HH:mm';
}
/**
* The generally used long date time format.
* Helsinki services are recommended to follow the Finnish date and time locale -
* even if the user is using some other language.
* Follow these guidelines when presenting date and time in your services.
* https://hds.hel.fi/foundation/guidelines/data-formats/
*/
static get datetimeFormat() {
return 'dd.MM.yyyy HH:mm:ss';
}
/** Should the application allow HTTP-connections? */
static get allowUnauthorizedRequests() {
return Boolean(
parseEnvValue(process.env.NEXT_PUBLIC_ALLOW_UNAUTHORIZED_REQUESTS)
);
}
/** A global debug switch for development purposes. */
static get debug() {
return Boolean(parseEnvValue(process.env.NEXT_PUBLIC_DEBUG));
}
/** A default HDS theme for the buttons. https://hds.hel.fi/foundation/design-tokens/colour. */
static get defaultButtonTheme(): CommonButtonProps['theme'] {
return 'default';
}
/** A primary variant for the buttons. https://hds.hel.fi/foundation/design-tokens/colour. */
static get defaultButtonVariant(): CommonButtonProps['variant'] {
return 'success';
}
static get matomoConfiguration() {
const matomoUrlBase = process.env.NEXT_PUBLIC_MATOMO_URL_BASE;
const matomoEnabled = process.env.NEXT_PUBLIC_MATOMO_ENABLED;
const matomoSiteId = process.env.NEXT_PUBLIC_MATOMO_SITE_ID;
const getMatomoUrlPath = (path: string) => `${matomoUrlBase}${path}`;
return {
disabled: !parseEnvValue(matomoEnabled),
urlBase: matomoUrlBase as string,
srcUrl: getMatomoUrlPath(
process.env.NEXT_PUBLIC_MATOMO_SRC_URL as string
),
trackerUrl: getMatomoUrlPath(
process.env.NEXT_PUBLIC_MATOMO_TRACKER_URL as string
),
siteId: Number(matomoSiteId),
};
}
static askemFeedbackConfiguration(locale: 'en' | 'fi' | 'sv') {
const askemApiKeyByLocale: Record<typeof locale, string | undefined> = {
fi: process.env.NEXT_PUBLIC_ASKEM_API_KEY_FI,
sv: process.env.NEXT_PUBLIC_ASKEM_API_KEY_SV,
en: process.env.NEXT_PUBLIC_ASKEM_API_KEY_EN,
};
const askemEnabled = process.env.NEXT_PUBLIC_ASKEM_ENABLED;
return {
disabled: !parseEnvValue(askemEnabled),
apiKey: askemApiKeyByLocale[locale] ?? '',
};
}
/**
* A default NextJS page revalidation time.
* https://nextjs.org/docs/pages/building-your-application/data-fetching/incremental-static-regeneration#on-demand-revalidation
*/
static get defaultRevalidate() {
const envValue = process.env.NEXT_PUBLIC_DEFAULT_ISR_REVALIDATE_SECONDS;
const value = envValue ? parseEnvValue(envValue) : 60;
if (typeof value !== 'number') {
throw Error(
'NEXT_PUBLIC_DEFAULT_ISR_REVALIDATE_SECONDS must be a value that can be parsed into a number'
);
}
// no revalidation
// https://nextjs.org/docs/pages/building-your-application/data-fetching/incremental-static-regeneration#on-demand-revalidation
if (value < 1) {
return false;
}
return value;
}
/** A feature flag for the similar events. */
static get showSimilarEvents() {
return Boolean(
parseEnvValue(process.env.NEXT_PUBLIC_SHOW_SIMILAR_EVENTS, true)
);
}
/** A feature flag that can be used to show the enrolment status in the card details. */
static get showEnrolmentStatusInCardDetails() {
return Boolean(
parseEnvValue(
process.env.NEXT_PUBLIC_SHOW_ENROLMENT_STATUS_IN_CARD_DETAILS,
false
)
);
}
/**
* A map of URL reqriting.
* Rewrite the URLs returned by the HEadless CMS
* so that they can be routed and used inside the app.
* */
static get URLRewriteMapping() {
return {
[AppConfig.linkedEventsEventEndpoint]: ROUTES.EVENTS.replace(
'/[eventId]',
''
),
[`${AppConfig.cmsOrigin}[/fi|/en|/sv]*${AppConfig.cmsArticlesContextPath}`]:
ROUTES.ARTICLES.replace('/[...slug]', ''),
[`${AppConfig.cmsOrigin}[/fi|/en|/sv]*${AppConfig.cmsPagesContextPath}`]:
ROUTES.PAGES.replace('/[...slug]', ''),
};
}
/**
* The response status codes that the error handler of
* the Apollo federation client should ignore.
* */
static get apolloErrorHandlerIgnoredStatusCodes() {
// Ignore HTTP410 - Not found, which is raised e.g.
// when an event has been deleted from the LinkedEvents
return [410];
}
/**
* The Apollo-client context headers.
* NOTE: The federation router needs to propagate all the defined headers
* if they should be used in a request to a GraphQL subgraph.
*/
static get apolloFederationContextHeaders() {
// Ignore the unpopulated mandatory event data in LinkedEvents
return { ...ignoredErrorCodesHeader };
}
}
function parseEnvValue(
value?: string,
defaultValue: boolean | string | number | null = null
) {
if (!value) {
return defaultValue;
}
try {
return JSON.parse(value);
} catch (e) {
return null;
}
}
export default AppConfig;