Appearance
π§© ΠΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅ middleware β
ts
const authMiddleware = {
name: 'auth',
before: async (config) => {
config.headers = {
...config.headers,
Authorization: 'Bearer your-token',
};
return config;
}
};
const api = new HttpClient()
.registerMiddleware(authMiddleware);
await api.get('/me');
Π£Π΄Π°Π»Π΅Π½ΠΈΠ΅ middleware β
ts
api.removeMiddleware('auth');
ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ middleware ΠΏΡΠΈ Π·Π°ΠΏΡΠΎΡΠ΅ β
ts
await api.get('/me', {
excludeMiddleware: ['auth'],
});
ΠΡΡ ΠΈΡΠ΅ΠΊΡΡΡΠ° middleware β
ΠΠ°ΠΆΠ΄ΡΠΉ middleware ΠΌΠΎΠΆΠ΅Ρ ΡΠ΅Π°Π»ΠΈΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ:
before(config)
β Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΠΏΠ΅ΡΠ΅Π΄ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ΠΌ Π·Π°ΠΏΡΠΎΡΠ°after(response)
β Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΠΏΠΎΡΠ»Π΅ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΠΎΡΠ²Π΅ΡΠ°
Middleware ΠΎΠ±ΡΠ°Π±Π°ΡΡΠ²Π°ΡΡΡΡ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΡΠ΅Π³ΠΈΡΡΡΠ°ΡΠΈΠΈ.