This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
aurelia-authentication.js
74 lines (63 loc) · 2.77 KB
/
aurelia-authentication.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import {PLATFORM} from 'aurelia-pal';
import {HttpClient} from 'aurelia-fetch-client';
import {Config, Rest} from 'aurelia-api';
import {BaseConfig} from './baseConfig';
import {FetchConfig} from './fetchClientConfig';
import {logger} from './logger';
import {Container} from 'aurelia-dependency-injection';
// added for bundling
import {AuthFilterValueConverter} from './authFilterValueConverter'; // eslint-disable-line no-unused-vars
import {AuthenticatedFilterValueConverter} from './authenticatedFilterValueConverter'; // eslint-disable-line no-unused-vars
import {AuthenticatedValueConverter} from './authenticatedValueConverter'; // eslint-disable-line no-unused-vars
/**
* Configure the plugin.
*
* @export
* @param {FrameworkConfiguration} frameworkConfig The FrameworkConfiguration instance
* @param {{}|Function} config The Config instance
*
*/
export function configure(frameworkConfig: { container: Container, globalResources: (...resources: string[]) => any }, config: {}|Function) {
// ie9 polyfill
if (!PLATFORM.location.origin) {
PLATFORM.location.origin = PLATFORM.location.protocol + '//' + PLATFORM.location.hostname + (PLATFORM.location.port ? ':' + PLATFORM.location.port : '');
}
const baseConfig = frameworkConfig.container.get(BaseConfig);
if (typeof config === 'function') {
config(baseConfig);
} else if (typeof config === 'object') {
baseConfig.configure(config);
}
// after baseConfig was configured
for (let converter of baseConfig.globalValueConverters) {
frameworkConfig.globalResources(PLATFORM.moduleName(`./${converter}`));
logger.info(`Add globalResources value-converter: ${converter}`);
}
const fetchConfig = frameworkConfig.container.get(FetchConfig);
const clientConfig = frameworkConfig.container.get(Config);
// Array? Configure the provided endpoints.
if (Array.isArray(baseConfig.configureEndpoints)) {
baseConfig.configureEndpoints.forEach(endpointToPatch => {
fetchConfig.configure(endpointToPatch);
});
}
let client;
// Let's see if there's a configured named or default endpoint or a HttpClient.
if (baseConfig.endpoint !== null) {
if (typeof baseConfig.endpoint === 'string') {
const endpoint = clientConfig.getEndpoint(baseConfig.endpoint);
if (!endpoint) {
throw new Error(`There is no '${baseConfig.endpoint || 'default'}' endpoint registered.`);
}
client = endpoint;
} else if (baseConfig.endpoint instanceof HttpClient) {
client = new Rest(baseConfig.endpoint);
}
}
// No? Fine. Default to HttpClient. BC all the way.
if (!(client instanceof Rest)) {
client = new Rest(frameworkConfig.container.get(HttpClient));
}
// Set the client on the config, for use throughout the plugin.
baseConfig.client = client;
}