{"id":13989675,"url":"https://github.com/dleitee/react-fetches","last_synced_at":"2025-04-24T00:23:24.490Z","repository":{"id":43163626,"uuid":"134442655","full_name":"dleitee/react-fetches","owner":"dleitee","description":"🐙React Fetches a new way to make requests into your REST API's.","archived":false,"fork":false,"pushed_at":"2022-03-15T17:48:56.000Z","size":2522,"stargazers_count":248,"open_issues_count":22,"forks_count":11,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-30T05:11:21.480Z","etag":null,"topics":["api","fetch","react","rest"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dleitee.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-22T16:20:20.000Z","updated_at":"2025-03-07T02:37:40.000Z","dependencies_parsed_at":"2022-07-26T00:46:22.481Z","dependency_job_id":null,"html_url":"https://github.com/dleitee/react-fetches","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dleitee%2Freact-fetches","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dleitee%2Freact-fetches/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dleitee%2Freact-fetches/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dleitee%2Freact-fetches/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dleitee","download_url":"https://codeload.github.com/dleitee/react-fetches/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250536403,"owners_count":21446727,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","fetch","react","rest"],"created_at":"2024-08-09T13:01:56.674Z","updated_at":"2025-04-24T00:23:24.470Z","avatar_url":"https://github.com/dleitee.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# react-fetches\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/dleitee/react-fetches.svg?token=02c8adf78705fea22a73f5fb6ebb60e58cb82cd203254efc1dc5f141eb9bd461\u0026ts=1528216010503)](https://greenkeeper.io/)\n[![codecov](https://codecov.io/gh/dleitee/react-fetches/branch/master/graph/badge.svg?token=HGzOPgq5AL)](https://codecov.io/gh/dleitee/react-fetches)\n[![CircleCI](https://circleci.com/gh/dleitee/react-fetches/tree/master.svg?style=svg\u0026circle-token=317c7e90c40a084e9de799bfa3fd963a85c1acb7)](https://circleci.com/gh/dleitee/react-fetches/tree/master)\n\nReact Fetches is a simple and efficient way to make requests into your REST API's.\n\n## Table of Contents\n\n- [Motivation](#motivation)\n- [Install](#install)\n- [Basic Example](#basic-example)\n- [API Reference](#api-reference)\n   - [connect](#connectmaprequesttoprops-mapdispatchtopropscomponent)\n   - [mapRequestToProps](#maprequesttoprops--http-map--object)\n   - [mapDispatchToProps](#mapdispatchtoprops--http-dispatch--object)\n- [Inspirations](#inspirations)\n- [Support](#support)\n- [How to Contribute](#how-to-contribute)\n- [License](#license)\n\n## Motivation\n\nMe and my friends were tired to use a lot of boilerplate code to do our requests.\n\nWe used to build our projects with a set of libraries as listed below:\n\n- [Redux](https://github.com/reduxjs/redux)\n- [React Redux](https://github.com/reduxjs/react-redux)\n- [ImmutableJS](https://github.com/facebook/immutable-js)\n- [Normalizr](https://github.com/paularmstrong/normalizr)\n- [Reselect](https://github.com/reduxjs/reselect)\n- [redux-promise-middleware](https://github.com/pburtchaell/redux-promise-middleware)\n- [redux-thunk](https://github.com/reduxjs/redux-thunk)\n\nWe 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.\n\nOMG!!!\n\nSo I created the `react-fetches`.\n\n**PS: Thank you for all of these libraries that helped us until now to build awesome projects.**\n\n## Install\n\n```sh\nnpm install --save fetches react-fetches\n```\n\n## Basic Example\n\n**app.js**\n```es6\nimport React from 'react'\nimport { render } from 'react-dom'\nimport { createClient } from 'fetches'\nimport { Provider } from 'react-fetches'\n\nimport View from './view'\n\nconst client = createClient('https://your-api.com/api/v1/')\n\nconst Root = () =\u003e (\n  \u003cProvider client={client}\u003e\n    \u003cView /\u003e\n  \u003c/Provider\u003e\n)\n\nrender(\u003cRoot /\u003e, document.getElementById('root'))\n```\n\n**view.js**\n```es6\nimport React, { Component, Fragment } from 'react'\nimport { connect } from 'react-fetches'\n\nconst mapRequestsToProps = (http, map) =\u003e ({\n  userID: map(http.get('user'), (user) =\u003e user.id),\n  groups: http.get('groups'),\n})\n\nconst mapDispatchToProps = (http, dispatch) =\u003e ({\n  addGroup: dispatch(http.post('group'))\n})\n\nclass View extends Component {\n\n  constructor(props) {\n    super(props)\n    this.state = {\n      addedGroups: [],\n    }\n  }\n  \n  addGroup() {\n    this.props.addGroup({ name: 'Name of Group' }).then(({data}) =\u003e {\n      this.setState((prevState) =\u003e ({\n        addedGroups: [...prevState.addedGroupd, data]\n      }))\n    })\n  }\n\n  render() {\n    if (this.props.loading) {\n      return 'Loading...'\n    }\n    \n    const groups = [...this.props.groups, ...this.state.addedGroups]\n    \n    return (\n      \u003cFragment\u003e\n        \u003cul\u003e\n          {groups.map((group) =\u003e (\n            \u003cli key={group.id}\u003e{group.name}\u003c/li\u003e\n           ))}\n        \u003c/ul\u003e\n        \u003cbutton onClick={this.addGroup}\u003eAdd group\u003c/button\u003e\n      \u003c/Fragment\u003e\n    )\n  }\n\n}\n\nexport default connect(mapRequestsToProps, mapDispatchToProps)(View)\n```\n\n## API Reference\n\n### connect(mapRequestToProps, mapDispatchToProps)(Component)\n\nAdds some props, came from mapRequestToProps and mapDispatchToProps, into your component.\n\n - **mapRequestToProps** props\n    - **loading** - Boolean - *default: false* - identifies if a request is performing.\n    - **errors** - Object - *default: undefined* - identifies if occurred some error in requests.\n    - **responses** - Object - *default: undefined* - Response object from each one request.\n    - **\\\u003crequest-name\\\u003e** - Object - *default: undefined* - the response body of request.\n    \n      **Example:**\n```es6\nconst props = {\n  loading: false,\n  errors: {\n    exampleRequest: {\n      name: 'name is not defined',\n    },\n  }\n  responses: {\n    exampleRequest: Response,\n  },\n  exampleRequest: null,\n}\n```\n\n- **mapDispatchToProps** props\n    - **\\\u003cdispatch-name\\\u003e** - Function - the function to make your request, this function returns for you a promise.\n      - This function can be called with two parameters **data** and **map**\n    \n      **Example:**\n```es6\nconst props = {\n  exampleFunction: Function =\u003e Promise,\n}\n\nconst data = { name: 'param name' }\nexampleFunction(data, (response) =\u003e response.id)\n```\n\n### mapRequestToProps = (http, map) =\u003e Object\n\nShould be a function that receive two arguments **http** and **map**, and should return an object with the props.\n\n```es6\nconst mapRequestsToProps = (http, map) =\u003e ({\n  groups: http.get('groups'),\n  userID: map(http.get('user'), (user) =\u003e user.id),\n})\n```\n\n - **http** - an object with the HTTP methods as a function.\n     - **get(uri, [params, [options]])**\n       - **uri** - String, Array\\\u003cString\\\u003e - the complement of your main URI. \n       - **params** - Object - *optional* - URL query params.\n       - **options** - Object - *optional* - The same custom settings accepted by [fetch](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax)\n\n     - **post(uri, [params, [options]])**\n     - **put(uri, [params, [options]])**\n     - **patch(uri, [params, [options]])**\n     - **delete(uri, [params, [options]])**\n       - **uri** - String, Array\\\u003cString\\\u003e - the complement of your main URI. \n       - **data** - Object - *optional* - The body of request.\n       - **options** - Object - *optional* - The same custom settings accepted by [fetch](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax)\n\n  - **map(request, (response) =\u003e mappedObject)** - a function that permits you to map your responses as well as you want.\n\n\n### mapDispatchToProps = (http, dispatch) =\u003e Object\n\nShould be a function that receive two arguments **http** and **dispatch**, and should return an object with the props. \n\n**NOTE:** Here the **http** is a bit different of the **mapRequestToProps http**.\n\n```es6\nconst mapDispatchToProps = (http, dispatch) =\u003e ({\n  addGroup: dispatch(http.post('group'))\n})\n```\n\n - **http** - an object with the HTTP methods as a function.\n     - **get(uri)**\n     - **post(uri)**\n     - **put(uri)**\n     - **patch(uri)**\n     - **delete(uri)**\n       - **uri** - String, Array\\\u003cString\\\u003e - the complement of your main URI. \n       \n  - **dispatch(request, config)** - in mapDispatchToProps, each item always must call the dispatch function.\n     - **request** - the **http** request\n     - **config** - Object - *optional* - The same custom settings accepted by [fetch](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Syntax)\n     \n**NOTE:** The function returned as a prop can receive two parameters **data** and **map**\n - **data** - Object - *optional* - The query params for the get method and body of the request for the others.\n - **map = (response) =\u003e mappedObject** - a function that permits you to map your responses as well as you want.\n\n## Inspirations\n\n- [React Apollo](https://github.com/apollographql/react-apollo) - to make the responses and dispatches as props.\n- [React Redux](https://github.com/reduxjs/react-redux) - to name our functions.\n\n\n## Support\n\nReact 16+\n\nFetches is based on [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/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](https://github.com/github/fetch)\n\n## How to Contribute\n\n1. Fork it!\n1. Create your feature branch: `git checkout -b my-new-feature`\n1. Commit your changes: `git commit -m 'Add some feature'`\n1. Push to the branch: `git push origin my-new-feature`\n1. Submit a pull request :)\n\n## License\n\nMIT License\n\nCopyright (c) 2018 Daniel Leite de Oliveira\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdleitee%2Freact-fetches","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdleitee%2Freact-fetches","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdleitee%2Freact-fetches/lists"}