Skip to content

Commit

Permalink
Merge pull request #15 from bcgov/bugfix/configlifecycle
Browse files Browse the repository at this point in the history
Rewrite getConfig to attempt to reacquire config if missing
  • Loading branch information
kyle1morel authored Feb 21, 2024
2 parents 0316035 + 8318978 commit bdb3bec
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions frontend/src/services/configService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export default class ConfigService {
return new Promise((resolve, reject) => {
if (storageType.getItem(StorageKey.CONFIG) === null) {
axios
.get('/config')
.get('/config', {
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
})
.then(({ data }) => {
storageType.setItem(StorageKey.CONFIG, JSON.stringify(data));
resolve(new ConfigService());
Expand All @@ -54,11 +59,30 @@ export default class ConfigService {
*/
public getConfig(): any | undefined {
try {
const cfgString = storageType.getItem(StorageKey.CONFIG);
return cfgString ? JSON.parse(cfgString) : undefined;
let cfgString = storageType.getItem(StorageKey.CONFIG);
if (cfgString === null) {
// eslint-disable-next-line no-console
console.warn('Configuration missing. Attempting to reacquire...');
axios
.get('/config', {
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
})
.then(({ data }) => {
storageType.setItem(StorageKey.CONFIG, JSON.stringify(data));
cfgString = data;
})
.catch((err) => {
// eslint-disable-next-line no-console
console.error(`Failed to reacquire configuration: ${err}`);
});
}
return JSON.parse(cfgString as string);
} catch (err: unknown) {
// eslint-disable-next-line no-console
console.error(`Missing configuration: ${err}`);
console.error(`Unparseable configuration: ${err}`);
return undefined;
}
}
Expand Down

0 comments on commit bdb3bec

Please sign in to comment.