/
core.js
185 lines (144 loc) · 3.49 KB
/
core.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
179
180
181
182
183
184
185
/**
* The Caramel Core functionality of Rapid
*/
import axios from 'axios';
import defaultsDeep from 'lodash/defaultsDeep';
import Defaults from './defaults';
import Debugger from './../debug/debugger';
import Logger from './../debug/logger';
class Core {
constructor (config) {
config = config || {};
config = defaultsDeep(config, Defaults);
this.initialize(config);
}
/**
* Set any config overrides in this method when extending
*/
boot () {
}
/**
* Setup the all of properties.
* @param {Object} config
*/
initialize (config) {
this.config = config;
this.initializeRoutes();
this.boot();
this.resetURLParams();
this.fireSetters();
this.initializeAPI();
this.setCurrentRoute(this.config.defaultRoute);
this.initializeDebugger();
this.initializeLogger();
this.resetRequestData();
this.defineCustomRoutes();
}
/**
* Fire the setters. This will make sure the routes are generated properly.
* Consider if this is really even necessary
*/
fireSetters () {
['baseURL', 'modelName', 'routeDelimeter', 'caseSensitive'].forEach(setter => this[setter] = this.config[setter]);
}
/**
* Initialze the debugger if debug is set to true.
*/
initializeDebugger () {
this.debugger = this.config.debug ? new Debugger(this) : false;
}
/**
* Initialze the debugger if debug is set to true.
*/
initializeLogger () {
this.logger = this.config.debug ? Logger : false;
}
/**
* Initialize the API.
*/
initializeAPI () {
this.api = axios.create(defaultsDeep({ baseURL: this.config.baseURL.replace(/\/$/, '') }, this.config.apiConfig));
this.writeInterceptorsToAPI();
}
/**
* Initialize the routes.
*/
initializeRoutes () {
this.routes = this.config.routes;
}
/**
* Set up the custom routes if we have any
*/
defineCustomRoutes () {
this.customRoutes = [];
// if we have custom routes, set up a name:route mapping
if (this.config.customRoutes.length) {
this.config.customRoutes.forEach((route) => {
this.customRoutes[route.name] = route;
});
}
}
/**
* Set the interceptors to the api object
*/
writeInterceptorsToAPI () {
const { interceptors } = this.config;
const types = Object.keys(interceptors);
if (types.length) {
types.forEach((type) => {
interceptors[type].forEach((interceptor) => {
this.api.interceptors[type].use(interceptor);
});
});
}
}
/**
* Resets the request data
*/
resetRequestData () {
this.requestData = {
params: {},
options: {},
};
}
/**
* Setters and Getters
*/
set debug (val) {
if (this.config.debug) {
this.logger.warn('debug mode must explicitly be turned on via the constructor in config.debug');
}
}
get collection () {
this.setCurrentRoute('collection');
return this;
}
get model () {
this.setCurrentRoute('model');
return this;
}
get any () {
this.setCurrentRoute('any');
return this;
}
get interceptors () {
return this.config.interceptors;
}
set baseURL (url) {
this.config.baseURL = this.sanitizeUrl(url);
this.initializeAPI();
}
set modelName (val) {
this.config.modelName = val;
this.setRoutes();
}
set routeDelimeter (val) {
this.config.routeDelimeter = val;
this.setRoutes();
}
set caseSensitive (val) {
this.config.caseSensitive = val;
this.setRoutes();
}
}
export default Core;