Skip to content

Commit 77cd78f

Browse files
initial commit
0 parents  commit 77cd78f

File tree

9 files changed

+260
-0
lines changed

9 files changed

+260
-0
lines changed

.babelrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"presets": [
3+
"latest",
4+
"stage-2",
5+
"react",
6+
],
7+
"plugins": [
8+
"add-module-exports"
9+
]
10+
}

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# For more information visit http://editorconfig.org/
2+
# Top-most EditorConfig file
3+
root = true
4+
5+
# Unix-style newlines with a newline ending every file.
6+
# Also 2 space indentation.
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
indent_style = space
12+
indent_size = 2

.gitignore

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

.npmignore

Whitespace-only changes.

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.9.1

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# React Router Fetch
2+
3+
Loops through matched routes from react router and calls a static fetch function on each handler. The static fetch function should return a promise. This will return a promise that will resolve/reject when all other promises are finished.
4+
5+
### usage
6+
7+
react router fetch understands the structure of render props for the `match` function in react router.
8+
9+
```js
10+
match({ routes, location: url }, (err, redirect, props) => {
11+
routeResovler(props, isInitial, [options])
12+
.then(() => {
13+
//do something when it's all done
14+
})
15+
```

package.json

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"name": "react-router-fetch",
3+
"version": "1.0.0",
4+
"description": "Calls fetch on matched route components and returns a promise",
5+
"main": "lib/index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/kellyrmilligan/react-router-fetch.git"
9+
},
10+
"scripts": {
11+
"build": "npm-run-all clean standard babel",
12+
"babel": "babel src -d . -D -s",
13+
"babel:watch": "babel src -d . -w -D -s",
14+
"clean": "rimraf ./lib",
15+
"dev": "npm-run-all clean standard --parallel babel:watch standard:watch",
16+
"prepublish": "npm run build",
17+
"standard": "standard --fix",
18+
"standard:watch": "onchange 'src/**/*.js' -- npm run standard",
19+
"test": "cross-env NODE_ENV=test standard && jest",
20+
"test:watch": "npm run test -- --watch"
21+
},
22+
"author": "Kelly Milligan",
23+
"license": "ISC",
24+
"jest": {
25+
"testPathDirs": [
26+
"src"
27+
],
28+
"moduleDirectories": [
29+
"node_modules",
30+
"src"
31+
],
32+
"moduleFileExtensions": [
33+
"js",
34+
"json"
35+
],
36+
"coverageDirectory": "coverage",
37+
"collectCoverage": true,
38+
"testRegex": "(/__tests__/.*|\\.(test))\\.js$",
39+
"coverageThreshold": {
40+
"global": {
41+
"branches": 75,
42+
"functions": 75,
43+
"lines": 75,
44+
"statements": 75
45+
}
46+
},
47+
"coveragePathIgnorePatterns": [
48+
"/node_modules/",
49+
"/fixtures/",
50+
"/script/"
51+
]
52+
},
53+
"standard": {
54+
"parser": "babel-eslint"
55+
},
56+
"dependencies": {
57+
},
58+
"peerDependencies": {
59+
"react-router": "^2.0.0 || ^3.0.0"
60+
},
61+
"devDependencies": {
62+
"babel-cli": "^6.11.4",
63+
"babel-core": "^6.6.4",
64+
"babel-eslint": "^6.1.2",
65+
"babel-jest": "^15.0.0",
66+
"babel-plugin-add-module-exports": "^0.2.1",
67+
"babel-preset-react": "^6.16.0",
68+
"babel-preset-latest": "^6.16.0",
69+
"babel-preset-stage-2": "^6.18.0",
70+
"babel-runtime": "^6.11.6",
71+
"cross-env": "^2.0.1",
72+
"jest": "^15.1.1",
73+
"nodemon": "^1.9.0",
74+
"npm-run-all": "^1.5.1",
75+
"onchange": "^3.0.2",
76+
"react-router": "^3.0.0",
77+
"rimraf": "^2.5.4",
78+
"source-map-support": "^0.4.0",
79+
"standard": "^8.5.0"
80+
}
81+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* global describe, it, expect, spyOn */
2+
3+
import routeResolver from 'lib/index'
4+
5+
describe('route-resolver', function () {
6+
it('can be imported', function () {
7+
expect(routeResolver).toBeTruthy()
8+
})
9+
it('will resolve right away if isInitial is true', function (done) {
10+
routeResolver({}, true)
11+
.then(() => {
12+
done()
13+
})
14+
})
15+
it('will call fetch on a route handler if it has one', function (done) {
16+
const handler = {
17+
fetch () {
18+
return new Promise((resolve, reject) => {
19+
setTimeout(resolve, 1000, { test: '1234' })
20+
})
21+
}
22+
}
23+
const handler2 = {
24+
fetch () {
25+
return new Promise((resolve, reject) => {
26+
setTimeout(resolve, 1000, { test: '1234' })
27+
})
28+
}
29+
}
30+
const props = {
31+
components: [
32+
handler,
33+
handler2
34+
],
35+
params: {},
36+
location: {
37+
query: {}
38+
}
39+
}
40+
spyOn(handler, 'fetch').and.callThrough()
41+
routeResolver(props)
42+
.then((response) => {
43+
expect(handler.fetch).toHaveBeenCalled()
44+
done()
45+
})
46+
})
47+
it('will resolve right away if no handlers have fetch', function (done) {
48+
const handler = {
49+
}
50+
const props = {
51+
components: [
52+
handler
53+
],
54+
params: {},
55+
location: {
56+
query: {}
57+
}
58+
}
59+
routeResolver(props)
60+
.then((promises) => {
61+
expect(promises.length).toBe(0)
62+
done()
63+
})
64+
})
65+
})

src/lib/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* processes the props matched from react router 2
3+
* and calls fetch
4+
*
5+
* @param {Object} props
6+
* @param {boolean} isInitial
7+
* @param {Object} options
8+
* @returns {Promise}
9+
*/
10+
function routeResovler (props, isInitial, options) {
11+
if (isInitial) {
12+
return Promise.resolve()
13+
}
14+
15+
const promises = props.components
16+
.filter(component => component.fetch)
17+
.map(component => component.fetch(props.params, props.location.query, options))
18+
19+
if (promises && promises.length > 0) {
20+
return Promise.all(promises)
21+
} else {
22+
return Promise.resolve(promises)
23+
}
24+
}
25+
26+
export default routeResovler

0 commit comments

Comments
 (0)