You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Small library that creates a pre-configured instance of axios to make HTTP requests to REST resources. Written in Typescript. Heavily inspired by AngularJS' $resource.
3
+
Schema-based HTTP client powered by axios. Built with Typescript. Heavily inspired by AngularJS' `$resource`.
4
4
5
5
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
6
6
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
@@ -9,8 +9,7 @@ Small library that creates a pre-configured instance of axios to make HTTP reque
// create: (requestConfig) => axiosPromise // sends POST http://localhost:3000/entity1,
@@ -53,72 +52,67 @@ npm i axios-rest-resource axios
53
52
- Use your resource whenever you want to make an AJAX call
54
53
55
54
```ts
56
-
import { entity1Resource } from"api/entity1";
55
+
import { entity1Resource } from'api/entity1'
57
56
58
-
const resRead =entity1Resource.read();
57
+
const resRead =entity1Resource.read()
59
58
// sends GET http://localhost:3000/entity1
60
59
// resRead is a Promise of data received from the server
61
60
62
-
const resReadOne =entity1Resource.readOne({ params: { id } });
61
+
const resReadOne =entity1Resource.readOne({ params: { id } })
63
62
// for id = '123'
64
63
// sends GET http://localhost:3000/entity1/123
65
64
// resReadOne is a Promise of data received from the server
66
65
67
-
const resCreate =entity1Resource.create({ data });
66
+
const resCreate =entity1Resource.create({ data })
68
67
// for data = { field1: 'test' }
69
68
// sends POST http://localhost:3000/entity1 with body { field1: 'test' }
70
69
// resCreate is a Promise of data received from the server
71
70
72
-
const resUpdate =entity1Resource.update({ data, params: { id } });
71
+
const resUpdate =entity1Resource.update({ data, params: { id } })
73
72
// for data = { field1: 'test' } and id = '123'
74
73
// sends PUT http://localhost:3000/entity1/123 with body { field1: 'test' }
75
74
// resUpdate is a Promise of data received from the server
76
75
77
-
const resRemove =entity1Resource.remove({ params: { id } });
76
+
const resRemove =entity1Resource.remove({ params: { id } })
78
77
// for id = '123'
79
78
// sends DELETE http://localhost:3000/entity1/123
80
79
// resRemove is a Promise of data received from the server
81
80
```
82
81
83
82
## URL token substituion
84
83
85
-
axios-rest-resource applies [interceptorUrlFormatter](docs/api/README.md#interceptorurlformatter) interceptor by default. It handles {token} substituin in URLs.
84
+
axios-rest-resource applies [interceptorUrlFormatter](src/url-formatter.ts) interceptor by default. It handles {token} substitution in URLs.
// read: (requestConfig) => axiosPromise // sends GET http://localhost:3000/entity,
207
201
// readOne: (requestConfig) => axiosPromise // sends GET http://localhost:3000/entity/{id},
208
202
// }
209
203
```
210
204
211
-
## Adavanced usage
205
+
## In depth
212
206
213
-
You can pass a custom axios instance factory to ResourceBuilder. It's useful if you want to do something more with your axios instance like add an interceptor.
207
+
What does `ResourceBuilder`do exactly upon creation?
As you can see there's a lot you have to remember. Not to keep all those things in mind you can utilize [createAxiosResourceFactory](docs/api/README.md#createaxiosresourcefactory).
211
+
1. If your `axiosConfig` doesn't have `headers.Accept` property it sets it to 'application/json'.
212
+
1. It creates a new instance of axios passing `axiosConfig` to `axios.create`.
213
+
1. It adds `interceptorUrlFormatter` to request interceptors of the newly created instance of axios.
214
+
1. It exposes the newly created instance of axios for further modifications at `axiosInstance`.
215
+
216
+
Each instance of ResourceBuilder has its own `axiosInstance`. It's useful if you want to do something more with your axios instance like adding an interceptor.
// Creates an axios instance with appended resourceUrl and applied interceptorUrlFormatter. You can pass an additional array of request interceptors just like with ResourceBuilder. In fact ResourceBuilder uses this very function uner the hood.
0 commit comments