|
| 1 | +# react-fetch-mock |
| 2 | +[](https://travis-ci.org/WhatAKitty/react-fetch-mock) |
| 3 | +[](https://snyk.io/test/npm/react-fetch-mock) |
| 4 | + |
| 5 | +fetch mock for react |
| 6 | + |
| 7 | +[React-Native Version](https://github.com/WhatAKitty/react-native-fetch-mock) |
| 8 | + |
| 9 | +## Why FetchMock ? |
| 10 | +No fetch mock could be used easily for react. |
| 11 | +So, I create one by myself. |
| 12 | + |
| 13 | +## Roadmap |
| 14 | +- [x] Combined with Mock.js |
| 15 | +- [ ] Proxy for other api server |
| 16 | +- [ ] Support RAP system |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Import lib |
| 21 | +1. using npm/yarn |
| 22 | +``` |
| 23 | +yarn add react-fetch-mock --dev |
| 24 | +npm install react-fetch-mock --save-dev |
| 25 | +``` |
| 26 | + |
| 27 | +__ mocks__/index.js |
| 28 | +``` |
| 29 | +export default { |
| 30 | + '/api/path': ({ method, url, params, urlparams, headers }) => { |
| 31 | + const all = Mock.mock({ |
| 32 | + 'list|2': [{ |
| 33 | + 'id|+1': 1, |
| 34 | + 'name': '@first @last', |
| 35 | + 'age|18-54': 1, |
| 36 | + }] |
| 37 | + }).list; |
| 38 | + return all; // default status is 200 |
| 39 | + }, |
| 40 | + '/api/path/{id}': ({ method, url, params, urlparams, headers }) => { |
| 41 | + const all = Mock.mock({ |
| 42 | + 'list|2': [{ |
| 43 | + 'id|+1': 1, |
| 44 | + 'name': '@first @last', |
| 45 | + 'age|18-54': 1, |
| 46 | + 'urlid': urlparams.id, |
| 47 | + }] |
| 48 | + }).list; |
| 49 | + return all; |
| 50 | + }, |
| 51 | + 'POST /api/path': ({ method, url, params, urlparams, headers }) => { |
| 52 | + return { |
| 53 | + status: 201, |
| 54 | + }; |
| 55 | + }, |
| 56 | + 'PUT /api/path/${id}': ({ method, url, params, urlparams, headers }) => { |
| 57 | + return { |
| 58 | + status: 204, |
| 59 | + id: urlparams.id, |
| 60 | + }; |
| 61 | + }, |
| 62 | +} |
| 63 | +``` |
| 64 | +index.js |
| 65 | +``` |
| 66 | +import FetchMock from 'react-fetch-mock'; |
| 67 | +
|
| 68 | +if (__dev__) { |
| 69 | + // attention: mocks file should be under `src/` |
| 70 | + global.fetch = new FetchMock(require('path/to/mocks/directory')).fetch; |
| 71 | +} |
| 72 | +
|
| 73 | +// if __dev__ is true, it will back the data you defined in mock directory |
| 74 | +fetch('/api/path', options); |
| 75 | +fetch('/api/path', { |
| 76 | + method: 'POST', |
| 77 | + headers: { |
| 78 | + 'Content-Type': 'application/json', |
| 79 | + }, |
| 80 | + body: JSON.stringify({ |
| 81 | + name: 'John', |
| 82 | + }), |
| 83 | +}); |
| 84 | +fetch('/api/path/123', { |
| 85 | + method: 'PUT', |
| 86 | + headers: { |
| 87 | + 'Content-Type': 'application/json', |
| 88 | + }, |
| 89 | + body: JSON.stringify({ |
| 90 | + name: 'John2', |
| 91 | + }), |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +## Example Runing |
| 96 | + |
| 97 | +``` |
| 98 | +git clone git@github.com:WhatAKitty/react-fetch-mock.git |
| 99 | +cd react-fetch-mock/example/basic |
| 100 | +yarn install |
| 101 | +yarn start |
| 102 | +``` |
| 103 | + |
| 104 | +## LICENSE |
| 105 | + |
| 106 | +MIT |
0 commit comments