@@ -4,11 +4,14 @@ import fetch from './fetch';
44import getEnv from './get-env' ;
55import promiseCache from './promise-cache' ;
66import { DirectoryListing , File , Folder } from './graph-api-types' ;
7+ import * as defaultConfig from './config' ;
78
89const [ ROOT ] = getEnv ( 'ROOT' ) ;
910
1011/**
1112 * SiteConfig can be set per each domain.
13+ * Anything set here will override the default configuration;
14+ * If nothing is set here then the defaults will apply.
1215 */
1316export type SiteConfig = {
1417 /**
@@ -21,24 +24,30 @@ export type SiteConfig = {
2124 * }
2225 * ```
2326 */
24- customErrors ? : {
27+ customErrors : {
2528 [ index : number ] : string ;
2629 } ;
2730 /**
2831 * Enable directory listings.
2932 */
30- dirListing ? : boolean ;
33+ dirListing : boolean ;
3134 /**
3235 * Execute functions.
3336 */
34- functions ?: boolean ;
37+ functions : boolean ;
38+ } ;
39+
40+ const defaultSiteConfig : SiteConfig = {
41+ customErrors : { } ,
42+ dirListing : defaultConfig . ENABLE_DIR_LISTING ,
43+ functions : defaultConfig . ENABLE_FUNCTIONS ,
3544} ;
3645
3746const dirCache = new LRU < string , Promise < Array < File | Folder > > > ( {
3847 max : 1 ,
3948 maxAge : 60 * 1000 ,
4049} ) ;
41- const configCache = new LRU < string , Promise < SiteConfig | null > > ( {
50+ const configCache = new LRU < string , Promise < SiteConfig > > ( {
4251 max : 100 ,
4352 maxAge : 60 * 1000 ,
4453} ) ;
@@ -54,17 +63,23 @@ const getDirList = promiseCache<Array<File | Folder>>(dirCache, async () => {
5463 return res . value ;
5564} ) ;
5665
57- const getSiteConfig = promiseCache ( configCache , async ( host : string ) => {
58- const dir = await getDirList ( ) ;
59- const configFile = dir . find ( ( o : any ) => o . file && o . name === `${ host } .json` ) as File | undefined ;
66+ const getSiteConfig = promiseCache (
67+ configCache ,
68+ async ( host : string ) : Promise < SiteConfig > => {
69+ const dir = await getDirList ( ) ;
70+ const configFile = dir . find ( ( o : any ) => o . file && o . name === `${ host } .json` ) as File | undefined ;
6071
61- if ( ! configFile ) {
62- return null ;
63- }
72+ if ( ! configFile ) {
73+ return defaultSiteConfig ;
74+ }
6475
65- const res = await fetch ( configFile [ '@microsoft.graph.downloadUrl' ] ) ;
66- const body : SiteConfig = await res . json ( ) ;
76+ const res = await fetch ( configFile [ '@microsoft.graph.downloadUrl' ] ) ;
77+ const body = await res . json ( ) ;
6778
68- return body ;
69- } ) ;
79+ return {
80+ ...defaultSiteConfig ,
81+ ...body ,
82+ } ;
83+ }
84+ ) ;
7085export default getSiteConfig ;
0 commit comments