Skip to content

Commit 4744cd7

Browse files
author
payam naghdi
committed
Add new features to readme
1 parent 051fcb0 commit 4744cd7

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

Readme.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ type:
1010

1111
with vue-cli the config file will be generated automatically, and
1212
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
1324
# Manual Installation
1425
To install this module just run:
1526
```angular2html
@@ -153,6 +164,39 @@ Vue.prototype.$apiManager.setAuthorizationHeader(getAuthorizationToken)
153164
export default APIRoutes
154165
```
155166

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
156200
# Usage
157201
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
158202
function with this name
@@ -178,6 +222,3 @@ let response = await this.$apiManager.apiOne({
178222
# Return Value
179223
This module uses axios underneath, and the return value is the same as axios
180224

181-
# Todo
182-
183-
- Create centralized error management for the module

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@payamnaghdy/vue-api-manager",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"main": "index.js",
55
"keywords": [
66
"vue",

0 commit comments

Comments
 (0)