Wrapper utilities for interaction with REST APIs using Axios.
- Install the package:
npm i @ronas-it/axios-api-client
- Create API service instance:
import{ApiService}from'@ronas-it/axios-api-client';import{configuration}from'./configuration';exportconstapiService=newApiService(configuration.apiURL);import{ApiService,AuthConfiguration,RefreshTokenInterceptorOptions,tokenInterceptor,onResponseRefreshTokenInterceptor,onRequestRefreshTokenInterceptor,}from'@ronas-it/axios-api-client';constconfiguration: AuthConfiguration={apiURL: 'https://api.your-app.dev',auth: {refreshTokenRoute: '/auth/refresh',unauthorizedRoutes: ['/auth/forgot-password','/auth/restore-password'],logoutRoute: '/logout',},};constapiService=newApiService(configuration.apiURL);constoptions: RefreshTokenOptions={configuration: configuration.auth,getIsAuthenticated: ()=>{/* get isAuthenticated state here */},runTokenRefreshRequest: async()=>{const{ token, ttl }=awaitapiService.get('/refresh');/* do something with token and ttl */returntoken;},onError: ()=>apiService.post('/logout'),};apiService.useInterceptors({request: [[tokenInterceptor({getToken: ()=>{/* get token here */},}),],],});apiService.useInterceptors({request: [[onRequestRefreshToken(options)]],response: [[null,onResponseRefreshToken(options)],[null,unauthorizedInterceptor({publicEndpoints: configuration.auth.unauthorizedRoutes,onError: ()=>apiService.post('/logout'),}),],],});