Minimalistic REST API client for React inspired by Apollo.
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
Find a file
Dmitriy Pleshevskiy cf61435259
Merge pull request #16 from pleshevskiy/task-13
feat(request-hook): allow autofetching for...
2020-11-06 03:58:31 +02:00
examples/movies feat(request-hook): add on failure callback 2020-11-06 00:58:43 +03:00
src feat(request-hook): allow autofetching for... 2020-11-06 04:57:12 +03:00
tests fix: filter undefined variable values 2020-11-06 04:47:01 +03:00
.dockerignore Initial commit 2020-11-04 00:39:14 +03:00
.eslintignore Initial commit 2020-11-04 00:39:14 +03:00
.eslintrc.yml Initial commit 2020-11-04 00:39:14 +03:00
.gitignore Initial commit 2020-11-04 00:39:14 +03:00
jest.config.js fix: filter undefined variable values 2020-11-06 04:47:01 +03:00
package-lock.json fix: filter undefined variable values 2020-11-06 04:47:01 +03:00
package.json fix: filter undefined variable values 2020-11-06 04:47:01 +03:00
README.md doc: fix example in readme 2020-11-06 01:15:13 +03:00
tsconfig.json Merge pull request #15 from pleshevskiy/bug-14 2020-11-06 04:51:29 +03:00

react-rest-request

Minimalistic REST API client for React inspired by Apollo.

Installation

npm install react-rest-request --save

Usage

import React from 'react';
import ReactDOM from 'react-dom';
import { Client, Endpoint, Method, useRequest, RequestProvider } from 'react-rest-request';

const client = new Client({
    baseUrl: 'https://sampleapis.com/movies/api',
});

type Movie = Readonly<{
    id: number;
    title: string;
    posterURL: string;
    imdbId: string;
}>

const MoviesEndpoint: Endpoint = {
    method: Method.GET,
    url: '/action-adventure',
};

type MoviesResponse = Movie[];

function App() {
    const { data, loading } = useRequest<MoviesResponse>(MoviesEndpoint);

    return !data ? (
        <div>{ loading ? 'Loading...' : 'Something went wrong' }</div>
    ) : (
        <ul>
            {data.map(movie => (
                <li key={movie.id}>{movie.title}</li>
            ))}
        </ul>
    );
}

ReactDOM.render(
    <RequestProvider client={client}>
        <App />
    </RequestProvider>,
    document.getElementById('root'),
);