Skip to content

Commit 28d3f7a

Browse files
committed
Update README
1 parent c2a0dfe commit 28d3f7a

File tree

1 file changed

+97
-5
lines changed

1 file changed

+97
-5
lines changed

README.md

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,113 @@ export class MyCustomService {
3838
}
3939
```
4040

41-
### Custom Config
41+
### Custom Axios config
4242

4343
```typescript
4444
@Module({
4545
imports: [
4646
AxiosModule.register({
47-
baseURL: "https://example.com",
48-
headers: {
49-
"X-My-Header": "Is a value!",
47+
config: {
48+
baseURL: "https://example.com",
49+
headers: {
50+
"X-My-Header": "Is a value!",
51+
},
5052
},
5153
}),
5254
],
5355
})
5456
export class AppModule {}
5557
```
5658

59+
#### Register Axios interceptors
60+
61+
[Axios interceptors](https://axios-http.com/docs/interceptors) can be
62+
configured on Axios clients to intercept requests or responses before they are
63+
handled by `then` or `catch`.
64+
65+
```typescript
66+
import { create } from "axios";
67+
68+
const client = create();
69+
70+
client.interceptors.request.use(
71+
(request) => {
72+
return request;
73+
},
74+
(err) => {
75+
return err;
76+
},
77+
{},
78+
);
79+
80+
client.interceptors.response.use(
81+
(response) => {
82+
return response;
83+
},
84+
(err) => {
85+
return err;
86+
},
87+
{},
88+
);
89+
```
90+
91+
However, because NestJS abstracts away the construction of services, including
92+
this library's Axios wrapper which makes registering Axios interceptors
93+
difficult via the usual approach.
94+
95+
To resolve this, this module provides an additional way to configure
96+
interceptors via the module registration configuration:
97+
98+
```typescript
99+
@Module({
100+
imports: [
101+
AxiosModule.register({
102+
interceptors: {
103+
request: {
104+
onFulfilled: (requestConfig) => {
105+
return requestConfig;
106+
},
107+
onRejected: (err) => {
108+
return err;
109+
},
110+
options: {},
111+
},
112+
113+
response: {
114+
onFulfilled: (response) => {
115+
return response;
116+
},
117+
onRejected: (err) => {
118+
return err;
119+
},
120+
options: {},
121+
},
122+
},
123+
}),
124+
],
125+
})
126+
export class AppModule {}
127+
```
128+
129+
Interceptors can still be modified later via the Axios reference on the
130+
service:
131+
132+
```typescript
133+
import { HttpService } from "nestjs-axios-promise";
134+
135+
@Injectable()
136+
export class CatsService {
137+
public constructor(private readonly httpService: HttpService) {}
138+
139+
public doThing() {
140+
this.httpService.axios.interceptors.request.use((requestConfig) => {
141+
return requestConfig;
142+
});
143+
this.httpService.axios.interceptors.request.eject();
144+
}
145+
}
146+
```
147+
57148
### Why
58149

59150
Because I find our way using a promise
@@ -79,7 +170,8 @@ export class CatsService {
79170
}
80171
```
81172

82-
MUCH more preferrable to the nestjs way of using an observable. They're casting their observable to a promise anyways... :facepalm:
173+
MUCH more preferable to the NestJS way of using an observable. They're casting
174+
their observable to a promise anyways... :facepalm:
83175

84176
```ts
85177
import { catchError, firstValueFrom } from "rxjs";

0 commit comments

Comments
 (0)