npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

feetch

v0.0.2

Published

KodeFox fetching library for REST API

Downloads

5

Readme

feetch · npm

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.

Pre-requisites

Install react-fetching-library:

yarn add react-fetching-library

Installation

After you install the pre-requisite libraries, you can install feetch using npm or yarn.

yarn add feetch

If 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.

Usage

createClient

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',
      },
    },
  ],
});

useQuery

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 (
    <UsersList
      loading={loading}
      error={error}
      users={payload}
      onReload={query}
    />
  );
};

useMutation

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,
    errorObject,
    reset,
    abort,
  } = useMutation(addUserAction, { schema: Schema });

  const handleSubmit = async (formValues) => {
    const { error: mutateError } = await mutate(formValues);

    if (mutateError) {
      // do something with the error
    }

    //success
  };

  return (
    <AddUserForm loading={loading} error={error} onSubmit={handleSubmit} />
  );
};