1

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?

1 Answer 1

1

data will always be of type LoginSuccessResponse. A 404 error will make axios throw an error. That can have many reasons, you can narrow it down like this:

function isAxiosError(error: unknown): error is AxiosError {
  return (error as AxiosError).isAxiosError !== undefined
}

...
try {
  const { data } = await this.api.post<LoginSuccessResponse>('/api/login', {
    email,
    password,
  })
  return data
} catch (err) {
  if (isAxiosError(err) && err.response?.status == 404) {
    // handle 404 error
  } else {
    // Handle all other errors
  }
}
...

If you want requestLogin to return either data or an error you can "handle" the error by returning it (return err as GenericErrorResponse). Return type of your function would then be Promise<LoginSuccessResponse | GenericErrorResponse>.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.