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

[TS] Allow to specify success/error ApiResponse #204

Closed
Andreyco opened this issue Mar 4, 2019 · 1 comment
Closed

[TS] Allow to specify success/error ApiResponse #204

Andreyco opened this issue Mar 4, 2019 · 1 comment

Comments

@Andreyco
Copy link

Andreyco commented Mar 4, 2019

Currenty, ApiResponse type is declared as:

type ApiResponse<T> = ApiErrorResponse<T> | ApiOkResponse<T>;

This means, both types for success and error is T

Allow end user to specify success/error types in following way:

// APISAUCE types
type ApiErrorResponse<T> = {
  ok: false,
  data: T
}

type ApiOkResponse < T > = {
  ok: true,
  data: T
}

type ApiResponse<O, E = O> = ApiErrorResponse<E> | ApiOkResponse<O>;

// Userland types
type Ok = {
  firstName: string
}

type Err = {
  message: string
}

// Example of success/error responses
// Success response
const responseOk: ApiResponse<Ok, Err> = {
  ok: true,
  data: {
    firstName: 'Andrej'
  }
} 

// Error response
const responseErr: ApiResponse<Ok, Err> = {
  ok: false,
  data: {
    message: 'xx'
  }
}

// Example of `get` request
const get = () => {
  return {} as ApiResponse<Ok, Err>
} 

const response = get()

if (response.ok === true) {
  console.log(response.data.firstName)
} else {
  console.log(response.data.message)
}
@ryanlntn
Copy link
Member

Addressed with #219

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

2 participants