10
10
11
11
with vue-cli the config file will be generated automatically, and
12
12
you can see each option description in the "Options description" section.
13
+
14
+ For Vue 2 you need to import the module:
15
+
16
+ ``` import apiManager from "./api-manager"; ```
17
+
18
+ then add the module to your vue instance
19
+ ``` Vue.prototype.$api = apiManager; ```
20
+
21
+ For Vue 3:
22
+
23
+ Just import the module in your component and use it
13
24
# Manual Installation
14
25
To install this module just run:
15
26
``` angular2html
@@ -153,6 +164,39 @@ Vue.prototype.$apiManager.setAuthorizationHeader(getAuthorizationToken)
153
164
export default APIRoutes
154
165
```
155
166
167
+
168
+ # Parse response and error:
169
+ ## Response
170
+ Imagine your api returns something like this:
171
+ ``` angular2html
172
+ {
173
+ body: {...}
174
+ }
175
+ ```
176
+
177
+ And the part you care about is in data(or any other preprocessing) you can write a function to get data and pass it to the module.
178
+
179
+ ``` angular2html
180
+ const parseResponse = response => {
181
+ return response.data.response;
182
+ };
183
+ ```
184
+ ``` apiManager.setResponseParser(parseResponse); ```
185
+
186
+ Then you can access the parsed data in return value like this:
187
+
188
+ ``` angular2html
189
+ let response = await this.$apiManager.someApiName();
190
+ console.log(response.parsedData)
191
+ ```
192
+ ## Error
193
+ You can also set an error parser in cases that error has body (in other cases automatically exception message will be set)
194
+ ``` angular2html
195
+ const parseError = response => {
196
+ return response.errorBody.message;
197
+ };
198
+ ```
199
+ then you can access error data in attribute ` parsedError ` in response
156
200
# Usage
157
201
Finally, in your component you can call the api (remember one of my apis had apiOne as the key so im going to call a
158
202
function with this name
@@ -178,6 +222,3 @@ let response = await this.$apiManager.apiOne({
178
222
# Return Value
179
223
This module uses axios underneath, and the return value is the same as axios
180
224
181
- # Todo
182
-
183
- - Create centralized error management for the module
0 commit comments