{"id":23440787,"url":"https://github.com/dabapps/redux-requests","last_synced_at":"2025-04-09T20:37:29.708Z","repository":{"id":33153220,"uuid":"146011556","full_name":"dabapps/redux-requests","owner":"dabapps","description":"Library for simple redux requests","archived":false,"fork":false,"pushed_at":"2023-07-12T13:14:46.000Z","size":663,"stargazers_count":0,"open_issues_count":19,"forks_count":0,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-04-06T11:47:12.773Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dabapps.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-08-24T15:55:39.000Z","updated_at":"2023-07-12T13:11:25.000Z","dependencies_parsed_at":"2024-01-07T16:21:33.283Z","dependency_job_id":null,"html_url":"https://github.com/dabapps/redux-requests","commit_stats":{"total_commits":137,"total_committers":9,"mean_commits":"15.222222222222221","dds":0.6788321167883211,"last_synced_commit":"0a632723a4127a5507bbd7c1077f49c08142738f"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabapps%2Fredux-requests","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabapps%2Fredux-requests/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabapps%2Fredux-requests/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabapps%2Fredux-requests/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dabapps","download_url":"https://codeload.github.com/dabapps/redux-requests/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248108847,"owners_count":21049212,"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":[],"created_at":"2024-12-23T16:18:41.825Z","updated_at":"2025-04-09T20:37:29.687Z","avatar_url":"https://github.com/dabapps.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Redux Requests\n\n[![Build Status](https://travis-ci.com/dabapps/redux-requests.svg?token=YbH3f6uroz5f5q8RxDdW\u0026branch=master)](https://travis-ci.com/dabapps/redux-requests)\n\nLibrary for simple redux requests\n\n## Disclaimer\n\nThis module is in its early stages and until it reaches its first major version it may be unstable, with potentially breaking changes with new minor version releases.\n\nPatch version changes will include both minor changes and patches.\n\n## Installation\n\nInstall via NPM:\n\n```shell\nnpm install @dabapps/redux-requests --save\n```\n\nIf you are using a version of npm that doesn't support package lock files, we'd recommend installing with the `--save-exact` flag to pin to a specific version in your package.json.\n\n## Getting Started\n\n### Prerequisites\n\nYou will need redux-thunk installed and applied as a middleware for actions to be correctly dispatched.\n\n### Creating request actions\n\nWhen defining a new request, you will need to first define an actionset, using `makeAsyncActionSet`.  This will give you a set of three actions that your reducers can then key off of.\n\n```typescript\nconst GET_USER = makeAsyncActionSet('GET_USER');\n\n/*\n{\n  REQUEST: 'GET_USER_REQUEST',\n  SUCCESS: 'GET_USER_SUCCESS',\n  FAILURE: 'GET_USER_FAILURE'\n}\n*/\n```\n\nLaunching an action is as simple as calling `request`, with the actionset as the first argument, then the URL, then the method, and then the data.\n\nThis returns a thunk action, and should be returned from an action creator, as below.\n\n```typescript\nconst getUser = (data) =\u003e {\n  return request(GET_USER, '/api/user/', 'GET', data);\n};\n```\n\n`request` also takes an additional argument - a dictionary with the following optional keys:\n\n* `tag` - for name-spacing requests if you plan to make multiple calls to the same point with different parameters.\n* `metaData` - for storing additional data about the request that will not be forwarded to the server.\n* `headers` - for allowing the setting of custom headers on the request.\n* `shouldRethrow` - a callback that takes the error object, and can return `true` if you want the Promise to fail instead of digest the error.\n\n```typescript\nconst getUser = (data) =\u003e {\n  return request(GET_USER, '/api/user/', 'GET', data, {\n    tag: 'users-list',\n    headers: {Authorization: TOKEN}\n  });\n};\n```\n\nIf you need to pass additional parameters to the Axios call, we supply an additional function `requestWithConfig`. Using this you can pass any additional option that Axios can handle.\n\n* `actionSet` - Async action set.\n* `axoisConfig` - Axios configuration object as described in [the axios documentation](https://github.com/axios/axios).\n* `options` - An options object, containing the optional parameters `shouldRethrow` and `tag`.\n* `extraMeta` - Additional meta data in the form of a Dict.\n\n```typescript\nconst getUser = (data) =\u003e {\n  return requestWithConfig(GET_USER, {\n      url: '/api/user/',\n      method: 'GET',\n      data: data,\n      headers: {Authorization: TOKEN}\n    }, {\n      tag: 'users-list'\n    },\n    {\n      userId: user.id,\n      userName: user.name\n    })\n}\n```\n\nOnce launched, individual actions for `REQUEST`, `SUCCESS` and `FAILURE` will be dispatched, as well as actions to control the `REQUEST_STATE`, which is consumed by `responsesReducer`, should you choose to use it.\n\nInternally, `request` uses a function called `requestFromFunction`, which instead wraps a callback that produces an Axios request.  You can use this in advance cases, if you need finer-grained control over how the request is made.\n\n```typescript\nrequest(GET_USER, () =\u003e axios({ /* some config */}), additionalConfig);\n```\n\n\n### Keeping track of request states and errors\n\nIf you do not plan to keep track of the state of requests yourself, we also provide a helper reducer, plus some additional actions for managing its state.\n\nMount `responsesReducer` in your store, with the type `ResponsesReducerState`.  This will keep track of all requests, accessible via the helper functions `isPending`, `hasFailed`, `hasSucceeded`, `anyPending`, and `getErrorData`.  An action called `resetRequestState` can be fired to clear out any stored data.\n\n\n```typescript\ninterface StoreState {\n  responses: ResponsesReducerState;\n}\n\nconst store = createStore(combineReducers({\n  responses: responsesReducer\n}));\n```\n\nTo access loading states and error messages with react-redux, you can use the various helper functions within your `mapStateToProps` function.\n\n```typescript\nfunction mapStateToProps (state: StoreState) {\n  return {\n    userIsLoading: isPending(state.responses, GET_USER),\n    anythingIsLoading: anyPending(state.responses, [UPDATE_USER, GET_USER]),\n    hasErrors: hasFailed(state.responses, GET_USER),\n    errors: getErrorData(state.responses, GET_USER)\n  };\n}\n```\n\n### Keeping track of request data\n\nTo store response data you will need to create a custom reducer that handles the various request states.\n\nHere you can handle transforming the responses, and clearing data, for example, upon request, or if a request fails.\n\n```typescript\nfunction user (state: User | null = null, action: AnyAction) {\n  switch (action.type) {\n    case GET_USER.SUCCESS:\n      return action.payload.data;\n    case GET_USER.REQUEST:\n    case GET_USER.FAILURE:\n      return null;\n    default:\n      return state;\n  }\n}\n```\n\n## Code of conduct\n\nFor guidelines regarding the code of conduct when contributing to this repository please review [https://www.dabapps.com/open-source/code-of-conduct/](https://www.dabapps.com/open-source/code-of-conduct/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabapps%2Fredux-requests","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdabapps%2Fredux-requests","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabapps%2Fredux-requests/lists"}