I have a backend api that sends back responses in two different scenarios.
When successful, it sends
{
lastConnectedAt: Date;
}
if the request was bad, it sends below object with 404 status code.
{
error: {
message: string;
};
}
In typescript, how should I type both scenarios?
export interface GenericErrorResponse {
error: {
message: string;
};
}
export interface LoginSuccessResponse {
lastConnectedAt: Date;
}
import { AxiosInstance } from "axios";
import { authApi } from "../apis/auth/authApi";
import { LoginSuccessResponse } from "../apis/auth/types";
class AuthService {
constructor(private api: AxiosInstance) {}
public requestLogin = async (email: string, password: string) => {
const { data } = await this.api.post<LoginSuccessResponse>("/api/login", { email, password });
return data;
};
}
export default new AuthService(authApi);
As you see, I typed the successful response. But how on the other side?