Created
March 28, 2025 15:23
-
-
Save bradley-pp/b6691b20fffd3da212fd3edfe0a4fc87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| provide: 'EXAMPLE_CLIENT', | |
| inject: [ConfigService, CACHE_MANAGER], | |
| useFactory: (configService: ConfigService, cacheManager: Cache) => { | |
| const client = axios.create({ | |
| baseURL: configService.get('EXAMPLE_API_BASE_URL'), | |
| }); | |
| client.interceptors.request.use(async (config) => { | |
| let accessToken = await cacheManager.get('example_access_token'); | |
| if (!accessToken) { | |
| const { data } = await axios.post( | |
| `${configService.get('EXAMPLE_API_BASE_URL')}/auth/token`, | |
| { | |
| id: configService.get('EXAMPLE_API_CLIENT_ID'), | |
| secret: configService.get('EXAMPLE_API_CLIENT_SECRET'), | |
| } | |
| ); | |
| accessToken = data.access_token; | |
| await cacheManager.set( | |
| 'example_access_token', | |
| accessToken, | |
| data.expired_in | |
| ); | |
| } | |
| config.headers['Authorization'] = `Bearer ${accessToken}`; | |
| return config; | |
| }); | |
| return client; | |
| }, | |
| }, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment