Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
refactor(core): migrate core module from UI-lib (DSP-1853) (#505)
* refactor(core): migrates core module from UI-lib * refactor(core): removes core module and moves the files inside to more relevant directories
- Loading branch information
Showing
5 changed files
with
723 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { KnoraApiConfig, KnoraApiConnection } from '@dasch-swiss/dsp-js'; | ||
|
||
// config for dsp-js-lib (@dasch-swiss/dsp-js) config object | ||
export const DspApiConfigToken = new InjectionToken<KnoraApiConfig>('DSP api configuration'); | ||
|
||
// connection config for dsp-js-lib (@dasch-swiss/dsp-js) connection | ||
export const DspApiConnectionToken = new InjectionToken<KnoraApiConnection>('DSP api connection instance'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { AppInitService } from './app-init.service'; | ||
|
||
describe('TestService', () => { | ||
let service: AppInitService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(AppInitService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should fetch the fully specified config file when method Init is called', async () => { | ||
|
||
const fetchSpy = spyOn(window, 'fetch').and.callFake( | ||
path => Promise.resolve(new Response(JSON.stringify({ | ||
apiProtocol: 'http', | ||
apiHost: '0.0.0.0', | ||
apiPort: 3333, | ||
apiPath: 'mypath', | ||
jsonWebToken: 'mytoken', | ||
logErrors: true | ||
}))) | ||
); | ||
|
||
await service.Init('config', { name: 'prod', production: true }); | ||
|
||
expect(service.dspApiConfig.apiProtocol).toEqual('http'); | ||
expect(service.dspApiConfig.apiHost).toEqual('0.0.0.0'); | ||
expect(service.dspApiConfig.apiPort).toEqual(3333); | ||
expect(service.dspApiConfig.apiPath).toEqual('mypath'); | ||
expect(service.dspApiConfig.jsonWebToken).toEqual('mytoken'); | ||
expect(service.dspApiConfig.logErrors).toEqual(true); | ||
|
||
expect(service.config['apiProtocol']).toEqual('http'); | ||
expect(service.config['apiHost']).toEqual('0.0.0.0'); | ||
expect(service.config['apiPort']).toEqual(3333); | ||
expect(service.config['apiPath']).toEqual('mypath'); | ||
expect(service.config['jsonWebToken']).toEqual('mytoken'); | ||
expect(service.config['logErrors']).toEqual(true); | ||
|
||
expect(fetchSpy).toHaveBeenCalledTimes(1); | ||
expect(fetchSpy).toHaveBeenCalledWith('config/config.prod.json'); | ||
|
||
}); | ||
|
||
it('should fetch the minimally specified config file when method Init is called', async () => { | ||
|
||
const fetchSpy = spyOn(window, 'fetch').and.callFake( | ||
path => Promise.resolve(new Response(JSON.stringify({ | ||
apiProtocol: 'http', | ||
apiHost: '0.0.0.0' | ||
}))) | ||
); | ||
|
||
await service.Init('config', { name: 'prod', production: true }); | ||
|
||
expect(service.dspApiConfig.apiProtocol).toEqual('http'); | ||
expect(service.dspApiConfig.apiHost).toEqual('0.0.0.0'); | ||
expect(service.dspApiConfig.apiPort).toEqual(null); | ||
expect(service.dspApiConfig.apiPath).toEqual(''); | ||
expect(service.dspApiConfig.jsonWebToken).toEqual(''); | ||
expect(service.dspApiConfig.logErrors).toEqual(false); | ||
|
||
expect(service.config['apiProtocol']).toEqual('http'); | ||
expect(service.config['apiHost']).toEqual('0.0.0.0'); | ||
expect(service.config['apiPort']).toEqual(null); | ||
expect(service.config['apiPath']).toEqual(''); | ||
expect(service.config['jsonWebToken']).toEqual(''); | ||
expect(service.config['logErrors']).toEqual(false); | ||
|
||
expect(fetchSpy).toHaveBeenCalledTimes(1); | ||
expect(fetchSpy).toHaveBeenCalledWith('config/config.prod.json'); | ||
|
||
}); | ||
|
||
it('should fetch the config file with additional options when method Init is called', async () => { | ||
|
||
const fetchSpy = spyOn(window, 'fetch').and.callFake( | ||
path => Promise.resolve(new Response(JSON.stringify({ | ||
apiProtocol: 'http', | ||
apiHost: '0.0.0.0', | ||
myOption: true | ||
}))) | ||
); | ||
|
||
await service.Init('config', { name: 'prod', production: true }); | ||
|
||
expect(service.dspApiConfig.apiProtocol).toEqual('http'); | ||
expect(service.dspApiConfig.apiHost).toEqual('0.0.0.0'); | ||
expect(service.dspApiConfig.apiPort).toEqual(null); | ||
expect(service.dspApiConfig.apiPath).toEqual(''); | ||
expect(service.dspApiConfig.jsonWebToken).toEqual(''); | ||
expect(service.dspApiConfig.logErrors).toEqual(false); | ||
|
||
expect(service.config['apiProtocol']).toEqual('http'); | ||
expect(service.config['apiHost']).toEqual('0.0.0.0'); | ||
expect(service.config['apiPort']).toEqual(null); | ||
expect(service.config['apiPath']).toEqual(''); | ||
expect(service.config['jsonWebToken']).toEqual(''); | ||
expect(service.config['logErrors']).toEqual(false); | ||
expect(service.config['myOption']).toEqual(true); | ||
|
||
expect(fetchSpy).toHaveBeenCalledTimes(1); | ||
expect(fetchSpy).toHaveBeenCalledWith('config/config.prod.json'); | ||
|
||
}); | ||
|
||
it('should throw an error if required members are missing on the config object', async () => { | ||
|
||
const fetchSpy = spyOn(window, 'fetch').and.callFake( | ||
path => Promise.resolve(new Response(JSON.stringify({}))) | ||
); | ||
|
||
await expectAsync(service.Init('config', { | ||
name: 'prod', | ||
production: true | ||
})) | ||
.toBeRejectedWith(new Error('config misses required members: apiProtocol and/or apiHost')); | ||
|
||
expect(fetchSpy).toHaveBeenCalledTimes(1); | ||
expect(fetchSpy).toHaveBeenCalledWith('config/config.prod.json'); | ||
|
||
}); | ||
|
||
it('should throw an error if "apiProtocol" is missing on the config object', async () => { | ||
|
||
const fetchSpy = spyOn(window, 'fetch').and.callFake( | ||
path => Promise.resolve(new Response(JSON.stringify({ | ||
apiHost: '0.0.0.0' | ||
}))) | ||
); | ||
|
||
await expectAsync(service.Init('config', { | ||
name: 'prod', | ||
production: true | ||
})) | ||
.toBeRejectedWith(new Error('config misses required members: apiProtocol and/or apiHost')); | ||
|
||
expect(fetchSpy).toHaveBeenCalledTimes(1); | ||
expect(fetchSpy).toHaveBeenCalledWith('config/config.prod.json'); | ||
|
||
}); | ||
|
||
it('should throw an error if "apiHost" is missing on the config object', async () => { | ||
|
||
const fetchSpy = spyOn(window, 'fetch').and.callFake( | ||
path => Promise.resolve(new Response(JSON.stringify({ | ||
apiProtocol: 'http' | ||
}))) | ||
); | ||
|
||
await expectAsync(service.Init('config', { | ||
name: 'prod', | ||
production: true | ||
})) | ||
.toBeRejectedWith(new Error('config misses required members: apiProtocol and/or apiHost')); | ||
|
||
expect(fetchSpy).toHaveBeenCalledTimes(1); | ||
expect(fetchSpy).toHaveBeenCalledWith('config/config.prod.json'); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { KnoraApiConfig } from '@dasch-swiss/dsp-js'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class AppInitService { | ||
|
||
dspApiConfig: KnoraApiConfig; | ||
|
||
config: object; | ||
|
||
constructor() { | ||
} | ||
|
||
/** | ||
* fetches and initialises the configuration. | ||
* | ||
* @param path path to the config file. | ||
* @param env environment to be used (dev or prod). | ||
*/ | ||
Init(path: string, env: { name: string; production: boolean }): Promise<void> { | ||
|
||
return new Promise<void>((resolve, reject) => { | ||
fetch(`${path}/config.${env.name}.json`).then( | ||
(response: Response) => response.json()).then(dspApiConfig => { | ||
|
||
// check for presence of apiProtocol and apiHost | ||
if (typeof dspApiConfig.apiProtocol !== 'string' || typeof dspApiConfig.apiHost !== 'string') { | ||
throw new Error('config misses required members: apiProtocol and/or apiHost'); | ||
} | ||
|
||
// make input type safe | ||
const apiPort = (typeof dspApiConfig.apiPort === 'number' ? dspApiConfig.apiPort : null); | ||
const apiPath = (typeof dspApiConfig.apiPath === 'string' ? dspApiConfig.apiPath : ''); | ||
const jsonWebToken = (typeof dspApiConfig.jsonWebToken === 'string' ? dspApiConfig.jsonWebToken : ''); | ||
const logErrors = (typeof dspApiConfig.logErrors === 'boolean' ? dspApiConfig.logErrors : false); | ||
|
||
// init dsp-api configuration | ||
this.dspApiConfig = new KnoraApiConfig( | ||
dspApiConfig.apiProtocol, | ||
dspApiConfig.apiHost, | ||
apiPort, | ||
apiPath, | ||
jsonWebToken, | ||
logErrors | ||
); | ||
|
||
// get all options from config | ||
this.config = dspApiConfig; | ||
|
||
// set sanitized standard config options | ||
this.config['apiProtocol'] = dspApiConfig.apiProtocol; | ||
this.config['apiHost'] = dspApiConfig.apiHost; | ||
this.config['apiPort'] = apiPort; | ||
this.config['apiPath'] = apiPath; | ||
this.config['jsonWebToken'] = jsonWebToken; | ||
this.config['logErrors'] = logErrors; | ||
|
||
resolve(); | ||
} | ||
).catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
} |
Oops, something went wrong.