11/*
22Copyright 2016 OpenMarket Ltd
3- Copyright 2019 - 2021 The Matrix.org Foundation C.I.C.
3+ Copyright 2019 - 2022 The Matrix.org Foundation C.I.C.
44
55Licensed under the Apache License, Version 2.0 (the "License");
66you may not use this file except in compliance with the License.
@@ -15,61 +15,62 @@ See the License for the specific language governing permissions and
1515limitations under the License.
1616*/
1717
18- export interface ISsoRedirectOptions {
19- immediate ?: boolean ;
20- on_welcome_page ?: boolean ; // eslint-disable-line camelcase
21- }
22-
23- /* eslint-disable camelcase */
24- export interface ConfigOptions {
25- [ key : string ] : any ;
26-
27- logout_redirect_url ?: string ;
18+ import { Optional } from "matrix-events-sdk" ;
2819
29- // sso_immediate_redirect is deprecated in favour of sso_redirect_options.immediate
30- sso_immediate_redirect ?: boolean ;
31- sso_redirect_options ?: ISsoRedirectOptions ;
32-
33- custom_translations_url ?: string ;
34- }
35- /* eslint-enable camelcase*/
20+ import { SnakedObject } from "./utils/SnakedObject" ;
21+ import { IConfigOptions , ISsoRedirectOptions } from "./IConfigOptions" ;
22+ import { KeysOfStrictType } from "./@types/common" ;
3623
37- export const DEFAULTS : ConfigOptions = {
38- // Brand name of the app
24+ // see element-web config.md for docs, or the IConfigOptions interface for dev docs
25+ export const DEFAULTS : Partial < IConfigOptions > = {
3926 brand : "Element" ,
40- // URL to a page we show in an iframe to configure integrations
4127 integrations_ui_url : "https://scalar.vector.im/" ,
42- // Base URL to the REST interface of the integrations server
4328 integrations_rest_url : "https://scalar.vector.im/api" ,
44- // Where to send bug reports. If not specified, bugs cannot be sent.
4529 bug_report_endpoint_url : null ,
46- // Jitsi conference options
4730 jitsi : {
48- // Default conference domain
49- preferredDomain : "meet.element.io" ,
31+ preferred_domain : "meet.element.io" ,
5032 } ,
51- desktopBuilds : {
33+ desktop_builds : {
5234 available : true ,
5335 logo : require ( "../res/img/element-desktop-logo.svg" ) . default ,
5436 url : "https://element.io/get-started" ,
5537 } ,
5638} ;
5739
5840export default class SdkConfig {
59- private static instance : ConfigOptions ;
41+ private static instance : IConfigOptions ;
42+ private static fallback : SnakedObject < IConfigOptions > ;
6043
61- private static setInstance ( i : ConfigOptions ) {
44+ private static setInstance ( i : IConfigOptions ) {
6245 SdkConfig . instance = i ;
46+ SdkConfig . fallback = new SnakedObject ( i ) ;
6347
6448 // For debugging purposes
6549 window . mxReactSdkConfig = i ;
6650 }
6751
68- public static get ( ) {
69- return SdkConfig . instance || { } ;
52+ public static get ( ) : IConfigOptions ;
53+ public static get < K extends keyof IConfigOptions > ( key : K , altCaseName ?: string ) : IConfigOptions [ K ] ;
54+ public static get < K extends keyof IConfigOptions = never > (
55+ key ?: K , altCaseName ?: string ,
56+ ) : IConfigOptions | IConfigOptions [ K ] {
57+ if ( key === undefined ) return SdkConfig . instance || { } ;
58+ return SdkConfig . fallback . get ( key , altCaseName ) ;
59+ }
60+
61+ public static getObject < K extends KeysOfStrictType < IConfigOptions , object > > (
62+ key : K , altCaseName ?: string ,
63+ ) : Optional < SnakedObject < IConfigOptions [ K ] > > {
64+ const val = SdkConfig . get ( key , altCaseName ) ;
65+ if ( val !== null && val !== undefined ) {
66+ return new SnakedObject ( val ) ;
67+ }
68+
69+ // return the same type for sensitive callers (some want `undefined` specifically)
70+ return val === undefined ? undefined : null ;
7071 }
7172
72- public static put ( cfg : ConfigOptions ) {
73+ public static put ( cfg : IConfigOptions ) {
7374 const defaultKeys = Object . keys ( DEFAULTS ) ;
7475 for ( let i = 0 ; i < defaultKeys . length ; ++ i ) {
7576 if ( cfg [ defaultKeys [ i ] ] === undefined ) {
@@ -83,14 +84,14 @@ export default class SdkConfig {
8384 SdkConfig . setInstance ( { } ) ;
8485 }
8586
86- public static add ( cfg : ConfigOptions ) {
87+ public static add ( cfg : IConfigOptions ) {
8788 const liveConfig = SdkConfig . get ( ) ;
8889 const newConfig = Object . assign ( { } , liveConfig , cfg ) ;
8990 SdkConfig . put ( newConfig ) ;
9091 }
9192}
9293
93- export function parseSsoRedirectOptions ( config : ConfigOptions ) : ISsoRedirectOptions {
94+ export function parseSsoRedirectOptions ( config : IConfigOptions ) : ISsoRedirectOptions {
9495 // Ignore deprecated options if the config is using new ones
9596 if ( config . sso_redirect_options ) return config . sso_redirect_options ;
9697
0 commit comments