Skip to content

Instantly share code, notes, and snippets.

@jsonbytes
Created December 4, 2023 13:37
Show Gist options
  • Save jsonbytes/d4a8b59a7de68507dab42a9544564f26 to your computer and use it in GitHub Desktop.
Save jsonbytes/d4a8b59a7de68507dab42a9544564f26 to your computer and use it in GitHub Desktop.
General purpose API Client
import axios, {
AxiosError,
AxiosInstance,
AxiosRequestConfig,
AxiosResponse,
InternalAxiosRequestConfig,
} from "axios";
import { API_BASE_URI } from "../config/apiConfig";
import { ErrorResponse, FetchResponse } from "@sapience/common";
class AxiosInterceptors {
private axiosInstance: AxiosInstance;
constructor(axiosInstance: AxiosInstance) {
this.axiosInstance = axiosInstance;
this.setupInterceptors();
}
private onRequest = (
config: InternalAxiosRequestConfig
): InternalAxiosRequestConfig => {
return config;
};
private onRequestError = (error: AxiosError): Promise<AxiosError> => {
return Promise.reject(error);
};
private onResponse = (response: AxiosResponse): AxiosResponse => {
return response;
};
private onResponseError = (
error: AxiosError
): Promise<AxiosError<ErrorResponse>> => {
if (axios.isAxiosError(error)) {
const serverError = error as AxiosError<ErrorResponse>;
console.log(`error: ${serverError.response?.data.error.message}`);
return Promise.reject(serverError.response?.data.error);
}
return Promise.reject(error);
};
private setupInterceptors() {
this.axiosInstance.interceptors.request.use(
this.onRequest,
this.onRequestError
);
this.axiosInstance.interceptors.response.use(
this.onResponse,
this.onResponseError
);
}
getAxiosInstance(): AxiosInstance {
return this.axiosInstance;
}
}
class APIClient<T> {
private axiosInstance: AxiosInstance;
endpoint: string;
constructor(endpoint: string) {
this.endpoint = endpoint;
const axiosInterceptors = new AxiosInterceptors(
axios.create({ baseURL: API_BASE_URI })
);
this.axiosInstance = axiosInterceptors.getAxiosInstance();
}
create = async <U>(data?: T, config?: AxiosRequestConfig) => {
console.log(`API client: create`);
return this.axiosInstance
.post<U>(this.endpoint, data, config)
.then((response) => response.data);
};
getAll = async (config?: AxiosRequestConfig) => {
console.log(`API client: getAll`);
return this.axiosInstance
.get<FetchResponse<T>>(this.endpoint, config)
.then((response) => response.data);
};
get = async (config?: AxiosRequestConfig) => {
console.log(`API client: get`);
return this.axiosInstance
.get<T>(this.endpoint, config)
.then((response) => response.data);
};
update = async <U>(data?: T, config?: AxiosRequestConfig) => {
console.log(`API client: update`);
return this.axiosInstance
.patch<U>(this.endpoint, data, config)
.then((response) => response.data);
};
delete = async (config?: AxiosRequestConfig) => {
console.log(`API client: delete`);
return this.axiosInstance
.delete(this.endpoint, config)
.then((response) => response.data);
};
}
export default APIClient;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment