Last active
January 11, 2023 22:33
-
-
Save ibakshay/3d1a30d51d27da7c441c2e22650a2673 to your computer and use it in GitHub Desktop.
Axios configuration at class level
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
import axios, { AxiosRequestConfig, Method } from 'axios'; | |
import { AxiosInstance } from 'axios/index'; | |
export class CumulusApiClient { | |
private axios: AxiosInstance; | |
constructor(private readonly apiUrl: string) { | |
this.axios = axios.create(); | |
} | |
async makeRequest<T>(verb: Method, body: T) { | |
const config: AxiosRequestConfig = { | |
url: this.apiUrl, | |
method: verb, | |
headers: {}, | |
}; | |
const response = await this.axios.get( | |
'https://jsonplaceholder.typicode.com/todos/1', | |
); | |
console.log(response.data); | |
return response.data; | |
} | |
} | |
--- | |
import { Controller, Get } from '@nestjs/common'; | |
import { CumulusService } from './cumulus.service'; | |
import { CumulusApiClient } from './cumulus-api-client'; | |
@Controller('cumulus') | |
export class CumulusController { | |
private apiClient: CumulusApiClient; | |
constructor(private readonly cumulusService: CumulusService) {} | |
@Get('/test') | |
async testApi() { | |
this.apiClient = new CumulusApiClient( | |
'https://jsonplaceholder.typicode.com/todos/1', | |
); | |
return this.apiClient.makeRequest('GET', undefined); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment