Skip to content

Commit 242706f

Browse files
committed
Initial commit
0 parents  commit 242706f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+19104
-0
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": [ "react-native" ]
3+
}

.eslintrc.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"parser": "babel-eslint",
3+
"env": {
4+
"browser": true
5+
},
6+
"extends": "eslint:recommended",
7+
"rules": {
8+
"indent": [
9+
"error",
10+
2
11+
],
12+
"linebreak-style": [
13+
"error",
14+
"unix"
15+
],
16+
"quotes": [
17+
"error",
18+
"single"
19+
],
20+
"semi": [
21+
"error",
22+
"always"
23+
]
24+
}
25+
}

.gitignore

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# nyc test coverage
18+
.nyc_output
19+
20+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21+
.grunt
22+
23+
# node-waf configuration
24+
.lock-wscript
25+
26+
# Compiled binary addons (http://nodejs.org/api/addons.html)
27+
build/Release
28+
29+
# Dependency directories
30+
node_modules
31+
jspm_packages
32+
33+
# Optional npm cache directory
34+
.npm
35+
36+
# Optional REPL history
37+
.node_repl_history
38+
39+
# outpu
40+
dist/
41+
42+
# MACOS
43+
.DS_Store

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
logs
3+
*.log
4+
__mocks__
5+
dist
6+
node_modules
7+
example

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- '4.8.2'
4+
- '6.10.2'
5+
- '7.8.0'

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 WhatAKitty
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# react-fetch-mock
2+
[![Build Status](https://travis-ci.org/WhatAKitty/react-fetch-mock.svg?branch=master)](https://travis-ci.org/WhatAKitty/react-fetch-mock)
3+
[![Known Vulnerabilities](https://snyk.io/test/npm/react-fetch-mock/badge.svg)](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

__mocks__/index.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { Mock } from '../';
2+
3+
export default {
4+
'/api/users': ({ params }) => {
5+
const all = [
6+
{
7+
name: 'John',
8+
age: 15,
9+
},
10+
{
11+
name: 'Lily',
12+
age: 16,
13+
}
14+
];
15+
let filtered;
16+
if ('undefined' !== typeof params) {
17+
filtered = all.filter(item => {
18+
let result = true;
19+
const keys = Object.keys(params);
20+
keys.forEach(key => {
21+
const param = params[key];
22+
23+
if (item[key] && item[key] !== param) {
24+
result = false;
25+
}
26+
});
27+
28+
return result;
29+
});
30+
} else {
31+
filtered = all;
32+
}
33+
return {
34+
status: 200,
35+
data: filtered,
36+
};
37+
},
38+
'/api/users/mockjs': ({ params }) => {
39+
const all = Mock.mock({
40+
'list|2': [{
41+
'id|+1': 1,
42+
'name': '@first @last',
43+
'age|18-54': 1,
44+
}]
45+
}).list;
46+
let filtered;
47+
if ('undefined' !== typeof params) {
48+
filtered = all.filter(item => {
49+
let result = true;
50+
const keys = Object.keys(params);
51+
keys.forEach(key => {
52+
const param = params[key];
53+
54+
if (item[key] && item[key] !== param) {
55+
result = false;
56+
}
57+
});
58+
59+
return result;
60+
});
61+
} else {
62+
filtered = all;
63+
}
64+
return {
65+
status: 200,
66+
data: filtered,
67+
};
68+
},
69+
'/api/users/{userId}': ({ urlparams }) => {
70+
return {
71+
status: 200,
72+
data: {
73+
userId: urlparams.userId,
74+
},
75+
};
76+
},
77+
'/api/users/pru/{userId}': ({ urlparams }) => {
78+
return {
79+
userId: urlparams.userId,
80+
};
81+
},
82+
'POST /api/users': () => {
83+
return {
84+
status: 201,
85+
};
86+
},
87+
'PUT /api/users/{userId}': ({ urlparams }) => {
88+
return {
89+
status: 204,
90+
data: {
91+
userId: urlparams.userId,
92+
},
93+
};
94+
}
95+
}

example/basic/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# See https://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
6+
# testing
7+
/coverage
8+
9+
# production
10+
/build
11+
12+
# misc
13+
.DS_Store
14+
.env.local
15+
.env.development.local
16+
.env.test.local
17+
.env.production.local
18+
19+
npm-debug.log*
20+
yarn-debug.log*
21+
yarn-error.log*

example/basic/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### Usage
2+
3+
```
4+
yarn install
5+
yarn start
6+
```

example/basic/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "basic",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"react": "^16.0.0",
7+
"react-dom": "^16.0.0",
8+
"react-fetch-mock": "file:../../",
9+
"react-scripts": "1.0.17"
10+
},
11+
"scripts": {
12+
"start": "react-scripts start",
13+
"build": "react-scripts build",
14+
"test": "react-scripts test --env=jsdom",
15+
"eject": "react-scripts eject"
16+
}
17+
}

example/basic/public/favicon.ico

3.78 KB
Binary file not shown.

0 commit comments

Comments
 (0)