effector / effector/farfetched

Question: How to handle different server responses in farfetched?

Open
#344 1 comment 2 reactions 0 assignees View on GitHub
scope:core
Dominant language
TypeScript
Stars
221
Forks
36
PR merge metrics
No merged PRs in 30d

Description

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.

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.