{"id":22733203,"url":"https://github.com/fantasywind/redux-middleware-fetch","last_synced_at":"2025-04-14T01:53:49.310Z","repository":{"id":47415594,"uuid":"65630937","full_name":"fantasywind/redux-middleware-fetch","owner":"fantasywind","description":"Redux Whatwg Fetch Middleware","archived":false,"fork":false,"pushed_at":"2023-01-11T22:33:11.000Z","size":370,"stargazers_count":8,"open_issues_count":11,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T15:55:34.552Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fantasywind.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-13T18:21:30.000Z","updated_at":"2023-07-11T08:48:52.000Z","dependencies_parsed_at":"2023-02-09T08:45:17.152Z","dependency_job_id":null,"html_url":"https://github.com/fantasywind/redux-middleware-fetch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fantasywind%2Fredux-middleware-fetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fantasywind%2Fredux-middleware-fetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fantasywind%2Fredux-middleware-fetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fantasywind%2Fredux-middleware-fetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fantasywind","download_url":"https://codeload.github.com/fantasywind/redux-middleware-fetch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248794794,"owners_count":21162655,"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-10T20:13:16.493Z","updated_at":"2025-04-14T01:53:49.286Z","avatar_url":"https://github.com/fantasywind.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# redux-middleware-fetch\nRedux Whatwg Fetch Middleware\n\n## Usage\n\n### Bind Middleware\n\n```javascript\n// ...\nimport thunk from 'redux-thunk';\nimport {\n  createStore,\n  applyMiddleware,\n} from 'redux';\nimport fetchMiddleware from 'redux-middleware-fetch';\n\nconst store = createStore(reducer, defaultState, applyMiddleware(\n  thunk,\n  fetchMiddleware,\n));\n\n// ...\n```\n\n### Call on action\n\n```javascript\nimport { API_REQUEST } from 'redux-middleware-fetch';\nimport { replace } from 'react-router-redux';\n\nexport const PRODUCT_FETCHED = Symbol('PRODUCT_FETCHED');\nexport const PRODUCT_CREATED = Symbol('PRODUCT_CREATED');\nexport const PRODUCT_CREATE_FAILED = Symbol('PRODUCT_CREATE_FAILED');\nexport const PRODUCT_CREATING = Symbol('PRODUCT_CREATING');\n\nexport const fetchProducts = () =\u003e ({\n  [API_REQUEST]: {\n    types: [\n      PRODUCT_FETCHED,\n    ],\n    entrypoint: '/products',\n    auth: true,\n    method: 'GET',\n  },\n});\n\n// Thunkify Action\nexport const createProducts = (product) =\u003e ({\n  dispatch =\u003e dispatch({\n    [API_REQUEST]: {\n      types: [\n        PRODUCT_CREATED,\n        PRODUCT_CREATE_FAILED,\n        PRODUCT_CREATING,\n      ],\n      entrypoint: '/products',\n      auth: true,\n      json: true,\n      body: product\n      method: 'POST',\n      onSuccess: () =\u003e dispatch(replace('/products')),\n    },\n  });\n});\n```\n\n### Customize Storage\n\nIn default, redux-middleware-fetch use localStorage for storage accessToken (in key __accessToken__). You can change any other storage system implemented __getItem__ and __setItem__ functions for it.\n\n```\nimport {\n  SimpleStorage,\n  setStorage,\n} from 'redux-middleware-fetch';\n\nconst storage = new SimpleStorage();\n\nsetStorage(storage);\n```\n\n## Options\n\n- **types[Array]**: Array of dispatch action, support every types (eg. String, Symbol), action will be called on [REQUEST_SUCCESS, (REQUEST_FAILED), (REQUEST_SENT)].\n- **entrypoint[String]**: API resource url, fetch will add host declared in global ```API_HOST``` constant automatically.\n- **auth[Boolean]**: If true, JWT Token will add to fetch request in headers ```Authorization``` value. Token should be storage in ```localStorage.accessToken```.\n- **json[Boolean]**: If true, body will be JSON stringified text. (Don't use json, urlEncoded and formData at same time)\n- **formData[Boolean]**: If true, body will append to FormData by assigned keys. (Don't use json, urlEncoded and formData at same time)\n- **urlEncoded[Boolean]**: If true, body will be x-www-form-urlencoded and stringify by querystring module. (Don't use json, urlEncoded and formData at same time)\n- **body[Object]**: Key-value paired post data.\n- **method[String]**: HTTP Method, default: GET.\n- **onSuccess[Function]**: Success callback function, you can do something without store dispatch (eg. dispatch redirect react-router-redux action).\n- **onFailed[Function]**: Failed callback function.\n- **headers[Object]**: Custom headers in object.\n\n## Event Listener\n\n```\nimport {\n  requestListener,\n  EVENT_REQUESTED,\n  EVENT_REQUEST_SUCCESSED,\n  EVENT_REQUEST_FAILED,\n} from 'redux-middleware-fetch';\n\nrequestListener.on(EVENT_REQUESTED, (reqOptions) =\u003e {\n  // do something on request sent\n});\n\nrequestListener.on(EVENT_REQUEST_SUCCESSED, (response) =\u003e {\n  // do something on request successed\n});\n\nrequestListener.on(EVENT_REQUEST_FAILED, (response) =\u003e {\n  // do something on request failed\n});\n```\n\n## To-Do\n\n- [ ] Mocks Service / API Simulator\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffantasywind%2Fredux-middleware-fetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffantasywind%2Fredux-middleware-fetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffantasywind%2Fredux-middleware-fetch/lists"}