Skip to content

Instantly share code, notes, and snippets.

@Goldziher
Created August 1, 2021 06:47
Show Gist options
  • Save Goldziher/7231c47467ac48a14ffc0bc9154735c2 to your computer and use it in GitHub Desktop.
Save Goldziher/7231c47467ac48a14ffc0bc9154735c2 to your computer and use it in GitHub Desktop.
Example backend api client using axios
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