Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save bradley-pp/b6691b20fffd3da212fd3edfe0a4fc87 to your computer and use it in GitHub Desktop.

Select an option

Save bradley-pp/b6691b20fffd3da212fd3edfe0a4fc87 to your computer and use it in GitHub Desktop.
{
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