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 8c6833805b feat!(request-hook): add new request hook
This hook works only for endpoint with GET method.

* refac!(request-hook): add lazy prefix to request hook

BREAKING CHANGES: you need to rename all `useRequest` hooks to
`useLazyRequest`

* refac!(request-hook): add public request state

BREAKING CHANGES: User shouldn't see previous headers, variables and params.
It's only for hooks so it doesn't call request again. If you use these
state you should to remove it from your code.

* chore: update example

Closes #5
2020-11-05 23:26:56 +03:00
examples/movies feat!(request-hook): add new request hook 2020-11-05 23:26:56 +03:00
src feat!(request-hook): add new request hook 2020-11-05 23:26:56 +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
package-lock.json feat!(request-hook): add new request hook 2020-11-05 23:26:56 +03:00
package.json chore: bump version 2020-11-05 00:31:04 +03:00
README.md doc: fix example in readme 2020-11-05 22:16:19 +03:00
tsconfig.json feat!(client): add client fetch based 2020-11-05 00:23:57 +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 [movies, { data, loading }] = useRequest<MoviesResponse>(MoviesEndpoint);

    React.useEffect(
        () => {
            movies();
        },
        [movies]
    );

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