This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): Added plugin configuration class
- Loading branch information
1 parent
65419d4
commit d225ecc
Showing
1 changed file
with
85 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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,85 @@ | |||
import {HttpClient} from 'aurelia-fetch-client'; | |||
import {Rest} from './rest'; | |||
|
|||
export class Config { | |||
endpoints = {}; | |||
defaultEndpoint = null; | |||
|
|||
/** | |||
* Register a new endpoint. | |||
* | |||
* @param {string} name The name of the new endpoint. | |||
* @param {function|string} [configureMethod] Configure method or endpoint. | |||
* @param {{}} [defaults] Defaults for the HttpClient | |||
* | |||
* @see http://aurelia.io/docs.html#/aurelia/fetch-client/latest/doc/api/class/HttpClientConfiguration | |||
* @return {Config} | |||
*/ | |||
registerEndpoint(name, configureMethod, defaults) { | |||
let newClient = new HttpClient(); | |||
this.endpoints[name] = new Rest(newClient); | |||
|
|||
// Manual configure of client. | |||
if (typeof configureMethod === 'function') { | |||
newClient.configure(configureMethod); | |||
|
|||
return this; | |||
} | |||
|
|||
// Base url is self. | |||
if (typeof configureMethod !== 'string') { | |||
return this; | |||
} | |||
|
|||
// Base url is string. Configure. | |||
newClient.configure(configure => { | |||
configure.withBaseUrl(configureMethod); | |||
|
|||
// Set optional defaults. | |||
if (typeof defaults === 'object') { | |||
configure.withDefaults(defaults); | |||
} | |||
}); | |||
|
|||
return this; | |||
} | |||
|
|||
/** | |||
* Get a previously registered endpoint. Returns null when not found. | |||
* | |||
* @param {string} [name] Returns default endpoint when not set. | |||
* | |||
* @return {Rest|null} | |||
*/ | |||
getEndpoint(name) { | |||
if (typeof name === 'undefined') { | |||
return this.defaultEndpoint || null; | |||
} | |||
|
|||
return this.endpoints[name] || null; | |||
} | |||
|
|||
/** | |||
* Check if an endpoint has been registered. | |||
* | |||
* @param {string} name | |||
* | |||
* @return {boolean} | |||
*/ | |||
endpointExists(name) { | |||
return !!this.endpoints[name]; | |||
} | |||
|
|||
/** | |||
* Set a previously registered endpoint as the default. | |||
* | |||
* @param {string} name | |||
* | |||
* @return {Config} | |||
*/ | |||
setDefaultEndpoint(name) { | |||
this.defaultEndpoint = this.getEndpoint(name); | |||
|
|||
return this; | |||
} | |||
} |