Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ClientResponse typing #106

Open
ColinFrick opened this issue Sep 3, 2024 · 0 comments
Open

Improve ClientResponse typing #106

ColinFrick opened this issue Sep 3, 2024 · 0 comments

Comments

@ColinFrick
Copy link

Currently the Client Response is typed as:

export default class ClientResponse<T> {
  public statusCode: number;
  public response: T;
  public exception: Error;

  wasSuccessful() {
    return this.statusCode >= 200 && this.statusCode < 300;
  }
}

The thing is if a API function returns a value it is always a successful response, because in cause of an error the Error is thrown (ergo the function does not return normally).

try {
    const response = await client.retrieveRefreshTokens(user);
    if (response.wasSuccessful()) { // Always true
        const token = response.response.refreshToken; // response.response: RefreshTokenResponse
        const error = response.exception.message; // No compile error, but will throw a `exception is undefined` error
    }
} catch (e) {
    console.log(e); // e is a ClientResponse without response
}

So both response and exception properties should be marked as optional:

export default class ClientResponse<T> {
  public statusCode: number;
  public response?: T;
  public exception?: Error;

  wasSuccessful() {
    return this.statusCode >= 200 && this.statusCode < 300;
  }
}

But because either the function returns a response or throws an exception they should be the same type:

class ClientResponse<T> {
    public statusCode: number;
    public response: T;
}

class ErrorResponse<T> {
    public statusCode: number;
    public exception: Error;
}

wasSuccessful can be dropped in this case, because the function either returns something (wasSuccessful = true) or it throws an error (wasSuccessful = false).

As an alternative functions could never throw, but return either ClientResponse<T> or ErrorResponse:

type ClientResponse<T> = {
    statusCode: number;
    response: T;
    wasSuccessful: true;
}

type ErrorResponse = {
    statusCode: number;
    exception: Error;
    wasSuccessful: false;
}

type ClientResponse<T> = Response<T> | ErrorResponse;

I used types instead of classes because of type-hinting:

const response = await client.retrieveRefreshTokens(userId);
if (response.wasSuccessful) {
    const token = response.response.refreshToken; // response.response: RefreshTokenResponse
} else {
    throw new Error(response.exception.message); // response.exception: Error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant