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

Question: How to handle different server responses in farfetched? #344

Open
MiiZZo opened this issue Aug 11, 2023 · 1 comment
Open

Question: How to handle different server responses in farfetched? #344

MiiZZo opened this issue Aug 11, 2023 · 1 comment
Labels
scope:core type:enhancement New feature or request
Milestone

Comments

@MiiZZo
Copy link
Contributor

MiiZZo commented Aug 11, 2023

In this repository, the discussions section is disabled. I believe my question should be located there.
I would like to see some API like this but maybe farfetched has a different approach to solving the same case.

import { createJsonQuery } from '@farfetched/core'
import { runtypeContract } from '@farfetched/runtypes';
import { Record, Literal } from 'runtypes';

const NotFoundUserResponse = Record({
  result: Literal(false),
  error: Record({
    type: Literal('USER_NOT_FOUND'),
   })
});
const SuccessUserResponse = Record({
  data: User,
  result: Literal(true)
});

const query = createJsonQuery({
  // other options...
  contracts: {
    notFound: runtypeContract(NotFoundUserResponse),
    success: runtypeContract(SuccessUserResponse),
  }
});


sample({
  clock: query.finished.notFound,
  target: navigateUserToHomePageFx,
});

sample({
  clock: query.finished.success,
  // do something when everthing is ok
});

sample({
 clock: query.finished.unexpected,
 // do something when server response type is unexpected
});
@MiiZZo MiiZZo closed this as completed Aug 12, 2023
@igorkamyshev igorkamyshev reopened this Aug 17, 2023
@igorkamyshev
Copy link
Owner

Hello! Thanks for the idea 🙏 I'll think about it, let's stick it as a feature request for now.

@igorkamyshev igorkamyshev added type:enhancement New feature or request scope:core labels Aug 17, 2023
@igorkamyshev igorkamyshev added this to the v1.0 milestone Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope:core type:enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants