package
KodeFox fetching library for REST API
npm install feetchGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
This is a fetching library for REST API. This library wraps react-fetching-library for fetching and runtypes for checking the type of the data from the server at runtime.
The way you would use feetch is similar to react-fetching-library but with a slight difference of props in feetch when using createClient, useQuery, and useMutation. There will be an explanation regarding that below.
Install react-fetching-library:
yarn add react-fetching-libraryAfter you install the pre-requisite libraries, you can install feetch using npm or yarn.
yarn add feetchIf you are using TypeScript, feetch is built using TypeScript and we shipped it along with the .d.ts file, so you do not have to install @types/feetch.
Creating a client is similar to how it is done in react-fetching-library. Feetch also provides options that exists in react-fetching-library.
import { createClient } from 'feetch';
const client = createClient(options);Feetch also provides an option to use fixtures data so the client will return the fixtures data instead of calling the API.
import { createClient } from 'feetch';
const client = createClient({
...options,
fixtures: [
{
method: 'GET',
endpoint: '/me',
responseBody: {
id: 1,
name: 'John Fixture',
},
},
],
});The usage of useQuery is also similar, but feetch requires an option called schema. The schema is needed by feetch to check whether the returned data schema from the API is still valid or not. This way, we can tell whether the schema changes or not during runtime.
If the schema of the returned data is different from the schema that we provide, useQuery will catch that as an error. So error will become true and errorObject will return the error message.
import { useQuery, runtypes } from 'feetch';
const fetchUsersList = {
method: 'GET',
endpoint: '/users',
};
let UserSchema = runtypes.Record({
id: runtypes.String,
name: runtypes.String,
});
export const UsersListContainer = () => {
const {
loading,
payload,
error,
errorObject,
query,
reset,
abort,
} = useQuery(fetchUsersList, { schema: UserSchema });
return
The usage of useMutation is also similar to useQuery. Feetch also requires a schema to use.
import { useMutation, runtypes } from 'feetch';
const addUserAction = (formValues) => ({
method: 'POST',
endpoint: '/users',
body: formValues,
});
let Schema = runtypes.Record({
id: runtypes.String,
status: runtypes.Union(runtypes.Literal('success'), runtypes.Literal('fail')),
});
export const AddUserFormContainer = () => {
const {
loading,
payload,
mutate,
error,
Estimated unpacked size across 8 packages.
638.8 kB
Includes dependencies.