All Projects → dleitee → React Fetches

dleitee / React Fetches

🐙React Fetches a new way to make requests into your REST API's.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Fetches

Ngx Restangular
Restangular for Angular 2 and higher versions
Stars: ✭ 787 (+211.07%)
Mutual labels:  api, rest, fetch
React Refetch
A simple, declarative, and composable way to fetch data for React components
Stars: ✭ 3,418 (+1250.99%)
Mutual labels:  api, rest, fetch
Symfony Flex Backend
Symfony Flex REST API template project
Stars: ✭ 214 (-15.42%)
Mutual labels:  api, rest
Ray
a framework that helps you to deliver well-designed python APIs
Stars: ✭ 215 (-15.02%)
Mutual labels:  api, rest
Ccxt Rest
Open Source Unified REST API of 100+ Crypto Exchange Sites (18k+ docker pulls) - https://ccxt-rest.io/
Stars: ✭ 210 (-17%)
Mutual labels:  api, rest
Verb
Organize and send HTTP requests from Emacs
Stars: ✭ 205 (-18.97%)
Mutual labels:  api, rest
Express Api Es6 Starter
Build APIs with Express.js in no time using ES6/ES7/ESNext goodness.
Stars: ✭ 212 (-16.21%)
Mutual labels:  api, rest
Flaresolverr
Proxy server to bypass Cloudflare protection
Stars: ✭ 241 (-4.74%)
Mutual labels:  api, rest
Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: ✭ 191 (-24.51%)
Mutual labels:  api, rest
Horaires Ratp Api
Webservice pour les horaires et trafic RATP en temps réel
Stars: ✭ 232 (-8.3%)
Mutual labels:  api, rest
Flask Restplus
Fully featured framework for fast, easy and documented API development with Flask
Stars: ✭ 2,585 (+921.74%)
Mutual labels:  api, rest
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+1262.85%)
Mutual labels:  api, rest
Fastapi Gino Arq Uvicorn
High-performance Async REST API, in Python. FastAPI + GINO + Arq + Uvicorn (w/ Redis and PostgreSQL).
Stars: ✭ 204 (-19.37%)
Mutual labels:  api, rest
Jikan Rest
The REST API for Jikan
Stars: ✭ 200 (-20.95%)
Mutual labels:  api, rest
Magic
Create your .Net Core/Angular/Database CRUD Web apps by simply clicking a button
Stars: ✭ 214 (-15.42%)
Mutual labels:  api, rest
Hapi Openapi
Build design-driven apis with OpenAPI (formerly swagger) 2.0 and hapi.
Stars: ✭ 196 (-22.53%)
Mutual labels:  api, rest
Bookmarks.dev
Bookmarks and Code Snippets Manager for Developers & Co
Stars: ✭ 218 (-13.83%)
Mutual labels:  api, rest
Jiraps
PowerShell module to interact with Atlassian JIRA
Stars: ✭ 241 (-4.74%)
Mutual labels:  api, rest
Rest980
REST interface to control your iRobot Roomba 980 via local server on your lan.
Stars: ✭ 186 (-26.48%)
Mutual labels:  api, rest
Flexirest
Flexirest - The really flexible REST API client for Ruby
Stars: ✭ 188 (-25.69%)
Mutual labels:  api, rest

react-fetches

Greenkeeper badge codecov CircleCI

React Fetches is a simple and efficient way to make requests into your REST API's.

Table of Contents

Motivation

Me and my friends were tired to use a lot of boilerplate code to do our requests.

We used to build our projects with a set of libraries as listed below:

We needed to make a request, normalize the response, put it into a reducer, listen to the reducer, unnormalize the data came from reducer, show it on the view.

OMG!!!

So I created the react-fetches.

PS: Thank you for all of these libraries that helped us until now to build awesome projects.

Install

npm install --save fetches react-fetches

Basic Example

app.js

import React from 'react'
import { render } from 'react-dom'
import { createClient } from 'fetches'
import { Provider } from 'react-fetches'

import View from './view'

const client = createClient('https://your-api.com/api/v1/')

const Root = () => (
  <Provider client={client}>
    <View />
  </Provider>
)

render(<Root />, document.getElementById('root'))

view.js

import React, { Component, Fragment } from 'react'
import { connect } from 'react-fetches'

const mapRequestsToProps = (http, map) => ({
  userID: map(http.get('user'), (user) => user.id),
  groups: http.get('groups'),
})

const mapDispatchToProps = (http, dispatch) => ({
  addGroup: dispatch(http.post('group'))
})

class View extends Component {

  constructor(props) {
    super(props)
    this.state = {
      addedGroups: [],
    }
  }
  
  addGroup() {
    this.props.addGroup({ name: 'Name of Group' }).then(({data}) => {
      this.setState((prevState) => ({
        addedGroups: [...prevState.addedGroupd, data]
      }))
    })
  }

  render() {
    if (this.props.loading) {
      return 'Loading...'
    }
    
    const groups = [...this.props.groups, ...this.state.addedGroups]
    
    return (
      <Fragment>
        <ul>
          {groups.map((group) => (
            <li key={group.id}>{group.name}</li>
           ))}
        </ul>
        <button onClick={this.addGroup}>Add group</button>
      </Fragment>
    )
  }

}

export default connect(mapRequestsToProps, mapDispatchToProps)(View)

API Reference

connect(mapRequestToProps, mapDispatchToProps)(Component)

Adds some props, came from mapRequestToProps and mapDispatchToProps, into your component.

  • mapRequestToProps props
    • loading - Boolean - default: false - identifies if a request is performing.

    • errors - Object - default: undefined - identifies if occurred some error in requests.

    • responses - Object - default: undefined - Response object from each one request.

    • <request-name> - Object - default: undefined - the response body of request.

      Example:

const props = {
  loading: false,
  errors: {
    exampleRequest: {
      name: 'name is not defined',
    },
  }
  responses: {
    exampleRequest: Response,
  },
  exampleRequest: null,
}
  • mapDispatchToProps props
    • <dispatch-name> - Function - the function to make your request, this function returns for you a promise.

      • This function can be called with two parameters data and map

      Example:

const props = {
  exampleFunction: Function => Promise,
}

const data = { name: 'param name' }
exampleFunction(data, (response) => response.id)

mapRequestToProps = (http, map) => Object

Should be a function that receive two arguments http and map, and should return an object with the props.

const mapRequestsToProps = (http, map) => ({
  groups: http.get('groups'),
  userID: map(http.get('user'), (user) => user.id),
})
  • http - an object with the HTTP methods as a function.

    • get(uri, [params, [options]])

      • uri - String, Array<String> - the complement of your main URI.
      • params - Object - optional - URL query params.
      • options - Object - optional - The same custom settings accepted by fetch
    • post(uri, [params, [options]])

    • put(uri, [params, [options]])

    • patch(uri, [params, [options]])

    • delete(uri, [params, [options]])

      • uri - String, Array<String> - the complement of your main URI.
      • data - Object - optional - The body of request.
      • options - Object - optional - The same custom settings accepted by fetch
  • map(request, (response) => mappedObject) - a function that permits you to map your responses as well as you want.

mapDispatchToProps = (http, dispatch) => Object

Should be a function that receive two arguments http and dispatch, and should return an object with the props.

NOTE: Here the http is a bit different of the mapRequestToProps http.

const mapDispatchToProps = (http, dispatch) => ({
  addGroup: dispatch(http.post('group'))
})
  • http - an object with the HTTP methods as a function.

    • get(uri)
    • post(uri)
    • put(uri)
    • patch(uri)
    • delete(uri)
      • uri - String, Array<String> - the complement of your main URI.
  • dispatch(request, config) - in mapDispatchToProps, each item always must call the dispatch function.

    • request - the http request
    • config - Object - optional - The same custom settings accepted by fetch

NOTE: The function returned as a prop can receive two parameters data and map

  • data - Object - optional - The query params for the get method and body of the request for the others.
  • map = (response) => mappedObject - a function that permits you to map your responses as well as you want.

Inspirations

Support

React 16+

Fetches is based on Fetch API, which the most of modern browsers already are compatible, but if you need to be compatible with an older browser, you may use this polyfill

How to Contribute

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :)

License

MIT License

Copyright (c) 2018 Daniel Leite de Oliveira

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].