Created
August 1, 2021 06:47
-
-
Save Goldziher/7231c47467ac48a14ffc0bc9154735c2 to your computer and use it in GitHub Desktop.
Example backend api client using axios
This file contains 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 { UserDTO } from '@shared/dto/user.dto'; | |
import axios, { AxiosInstance } from 'axios'; | |
class BackendClient { | |
private instance: AxiosInstance; | |
constructor() { | |
this.instance = axios.create({ | |
baseURL: process.env.BACKEND_API_URL, | |
headers: { | |
Authorization: process.env.BACKEND_API_TOKEN, | |
}, | |
}); | |
} | |
// ... | |
getUser = async (id: string): Promise<UserDTO> => { | |
const { data } = await this.instance.get<UserDTO>('/user/' + id); | |
return data; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment