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 21
/
config.js
178 lines (152 loc) · 4.6 KB
/
config.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {HttpClient} from 'aurelia-fetch-client';
import {Rest} from './rest';
/**
* Represents the options to use when constructing a `Rest` instance.
*/
interface RestOptions {
/**
* `true` to use the traditional URI template standard (RFC6570) when building
* query strings from criteria objects, `false` otherwise. Default is `false`.
* NOTE: maps to `useTraditionalUriTemplates` parameter on `Rest` constructor.
*
* @type {boolean}
*/
useTraditionalUriTemplates?: boolean;
}
/**
* Config class. Configures and stores endpoints
*/
export class Config {
/**
* Collection of configures endpoints
*
* @param {{}} Key: endpoint name; value: Rest client
*/
endpoints: {[key: string]: Rest} = {};
/**
* Current default endpoint if set
*
* @param {Rest} defaultEndpoint The Rest client
*/
defaultEndpoint: Rest;
/**
* Current default baseUrl if set
*
* @param {string} defaultBaseUrl The Rest client
*/
defaultBaseUrl: string;
/**
* Register a new endpoint.
*
* @param {string} name The name of the new endpoint.
* @param {Function|string} [configureMethod] Endpoint url or configure method for client.configure().
* @param {{}} [defaults] New defaults for the HttpClient
* @param {RestOptions} [restOptions] Options to pass when constructing the Rest instance.
*
* @see http://aurelia.io/docs.html#/aurelia/fetch-client/latest/doc/api/class/HttpClientConfiguration
* @return {Config} this Fluent interface
* @chainable
*/
registerEndpoint(name: string, configureMethod?: string|Function, defaults?: {}, restOptions?: RestOptions): Config {
let newClient = new HttpClient();
let useTraditionalUriTemplates;
if (restOptions !== undefined) {
useTraditionalUriTemplates = restOptions.useTraditionalUriTemplates;
}
this.endpoints[name] = new Rest(newClient, name, useTraditionalUriTemplates);
// set custom defaults to Rest
if (defaults !== undefined) {
this.endpoints[name].defaults = defaults;
}
// Manual configure of client.
if (typeof configureMethod === 'function') {
newClient.configure(configureMethod);
return this;
}
// Base url is self / current host.
if (typeof configureMethod !== 'string' && !this.defaultBaseUrl) {
return this;
}
if (this.defaultBaseUrl && typeof configureMethod !== 'string' && typeof configureMethod !== 'function') {
newClient.configure(configure => {
configure.withBaseUrl(this.defaultBaseUrl);
});
return this;
}
// Base url is string. Configure.
newClient.configure(configure => {
configure.withBaseUrl(configureMethod);
});
return this;
}
/**
* Get a previously registered endpoint. Returns null when not found.
*
* @param {string} [name] The endpoint name. Returns default endpoint when not set.
*
* @return {Rest|null}
*/
getEndpoint(name: string): Rest {
if (!name) {
return this.defaultEndpoint || null;
}
return this.endpoints[name] || null;
}
/**
* Check if an endpoint has been registered.
*
* @param {string} name The endpoint name
*
* @return {boolean}
*/
endpointExists(name: string): boolean {
return !!this.endpoints[name];
}
/**
* Set a previously registered endpoint as the default.
*
* @param {string} name The endpoint name
*
* @return {Config} this Fluent interface
* @chainable
*/
setDefaultEndpoint(name: string): Config {
this.defaultEndpoint = this.getEndpoint(name);
return this;
}
/**
* Set a base url for all endpoints
*
* @param {string} baseUrl The url for endpoints to append
*
* @return {Config} this Fluent interface
* @chainable
*/
setDefaultBaseUrl(baseUrl: string): Config {
this.defaultBaseUrl = baseUrl;
return this;
}
/**
* Configure with an object
*
* @param {{}} config The configuration object
*
* @return {Config} this Fluent interface
* @chainable
*/
configure(config: {defaultEndpoint: string, defaultBaseUrl: string, endpoints: Array<{name: string, endpoint: string, config: {}, default: boolean}>}): Config {
if (config.defaultBaseUrl) {
this.defaultBaseUrl = config.defaultBaseUrl;
}
config.endpoints.forEach(endpoint => {
this.registerEndpoint(endpoint.name, endpoint.endpoint, endpoint.config);
if (endpoint.default) {
this.setDefaultEndpoint(endpoint.name);
}
});
if (config.defaultEndpoint) {
this.setDefaultEndpoint(config.defaultEndpoint);
}
return this;
}
}