Skip to content

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

@MiiZZo

Description

@MiiZZo

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
});

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions