11import { ReactNode , createContext , use } from 'react' ;
22import { DocLinkCreator } from '../lib/shared/links' ;
3+ import { z } from 'zod' ;
34
45export enum Landscape {
56 Live = 'LIVE' ,
@@ -9,18 +10,16 @@ export enum Landscape {
910 Local = 'LOCAL' ,
1011}
1112
12- interface FrontendConfigContextProps {
13- backendUrl : string ;
14- gatewayUrl : string ;
15- landscape ?: Landscape ;
16- documentationBaseUrl : string ;
13+
14+
15+ interface FrontendConfigContextType extends FrontendConfig {
1716 links : DocLinkCreator ;
1817}
1918
2019export const FrontendConfigContext =
21- createContext < FrontendConfigContextProps | null > ( null ) ;
20+ createContext < FrontendConfigContextType | null > ( null ) ;
2221
23- const fetchPromise = fetch ( '/frontend-config.json' ) . then ( ( res ) => res . json ( ) ) ;
22+ const fetchPromise = fetch ( '/frontend-config.json' ) . then ( ( res ) => res . json ( ) ) . then ( ( data ) => validateAndCastFrontendConfig ( data ) ) ;
2423
2524interface FrontendConfigProviderProps {
2625 children : ReactNode ;
@@ -31,14 +30,10 @@ export function FrontendConfigProvider({
3130} : FrontendConfigProviderProps ) {
3231 const config = use ( fetchPromise ) ;
3332 const docLinks = new DocLinkCreator ( config . documentationBaseUrl ) ;
34- const value : FrontendConfigContextProps = {
33+ const value : FrontendConfigContextType = {
3534 links : docLinks ,
36- backendUrl : config . backendUrl ,
37- gatewayUrl : config . gatewayUrl ,
38- landscape : config . landscape ,
39- documentationBaseUrl : config . documentationBaseUrl ,
35+ ...config ,
4036 } ;
41-
4237 return (
4338 < FrontendConfigContext value = { value } > { children } </ FrontendConfigContext >
4439 ) ;
@@ -54,3 +49,27 @@ export const useFrontendConfig = () => {
5449 }
5550 return context ;
5651} ;
52+
53+ const OidcConfigSchema = z . object ( {
54+ clientId : z . string ( ) ,
55+ issuerUrl : z . string ( ) ,
56+ scopes : z . array ( z . string ( ) ) ,
57+ } ) ;
58+ export type OIDCConfig = z . infer < typeof OidcConfigSchema > ;
59+
60+ const FrontendConfigSchema = z . object ( {
61+ backendUrl : z . string ( ) ,
62+ gatewayUrl : z . string ( ) ,
63+ documentationBaseUrl : z . string ( ) ,
64+ oidcConfig : OidcConfigSchema ,
65+ landscape : z . optional ( z . nativeEnum ( Landscape ) ) ,
66+ } ) ;
67+ type FrontendConfig = z . infer < typeof FrontendConfigSchema > ;
68+
69+ function validateAndCastFrontendConfig ( config : unknown ) : FrontendConfig {
70+ try {
71+ return FrontendConfigSchema . parse ( config ) ;
72+ } catch ( error ) {
73+ throw new Error ( `Invalid frontend config: ${ error } ` ) ;
74+ }
75+ }
0 commit comments