Skip to content

Commit 49965d6

Browse files
committed
Merge branch 'master' into release
2 parents 07e6a1f + 3b7c0d3 commit 49965d6

File tree

10 files changed

+705
-361
lines changed

10 files changed

+705
-361
lines changed

.mailmap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Jason Dobry <[email protected]> Jason Dobry <[email protected]>
1+
2+
Jason Dobry <[email protected]> Jason Dobry <[email protected]>

AUTHORS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
#
66
# The email address is not required for organizations.
77
#
8-
8+
Cory Robinson <[email protected]>
99
Dave Ramirez <[email protected]>
1010
Jason Dobry <[email protected]>

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
#### 1.0.0-alpha.1 - xx xx 2016
1+
#### 1.0.0-rc.1 - 26 August 2016
2+
3+
##### Backwards compatible changes
4+
- #13, #14 - Feature - Response handlers by @crobinson42
5+
- Made endpoints more configurable
6+
- Upgraded dependencies
7+
8+
#### 1.0.0-alpha.1 - 06 June 2016
29

310
Initial release

CONTRIBUTORS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
# Names are formatted as:
44
# Name <email address>
55
#
6+
Cory Robinson <[email protected]>
7+
Dave Ramirez <[email protected]>
68
Jason Dobry <[email protected]>

circle.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ machine:
77
version: 5.7.0
88
dependencies:
99
pre:
10-
- npm i -g npm codecov nyc
11-
- npm i js-data@^3.0.0-beta.6 express
10+
- npm i -g npm
11+
- npm i -g codecov nyc
12+
- npm i js-data@^3.0.0-rc.4 express
1213
test:
1314
post:
1415
- nyc report --reporter=lcov | codecov

conf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"templates": {
1515
"theme": "jsdata",
1616
"systemName": "js-data-express",
17-
"copyright": "js-data-express Copyright © 2014-2016 js-data-express project authors",
17+
"copyright": "js-data-express Copyright © 2016 js-data-express project authors",
1818
"outputSourceFiles": true,
1919
"linenums": true,
2020
"footer": "<div style=\"text-align:center\"><a href=\"/\">api.js-data.io</a>&nbsp;&#8226;&nbsp;<a href=\"http://js-data.io\">js-data.io</a></div>",

package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "js-data-express",
33
"description": "Generate Express.js-compatible route middleware for JSData models.",
4-
"version": "1.0.0-alpha.1",
4+
"version": "1.0.0-rc.1",
55
"homepage": "https://github.com/js-data/js-data-express",
66
"repository": {
77
"type": "git",
@@ -48,13 +48,12 @@
4848
"release": "npm test && npm run doc && repo-tools updates && repo-tools changelog && repo-tools authors"
4949
},
5050
"peerDependencies": {
51-
"js-data": "^3.0.0-beta.6"
51+
"js-data": "^3.0.0-rc.4"
5252
},
5353
"devDependencies": {
54-
"express": "4.13.4",
55-
"js-data-adapter": "0.7.3",
56-
"js-data-repo-tools": "0.5.2",
57-
"supertest": "1.2.0",
58-
"body-parser": "1.15.1"
54+
"express": "4.14.0",
55+
"js-data-repo-tools": "0.5.6",
56+
"supertest": "2.0.0",
57+
"body-parser": "1.15.2"
5958
}
6059
}

src/handlers.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { utils } from 'js-data'
2+
3+
const DEFAULTS = {
4+
create: {
5+
action (component, req) {
6+
return component.create(req.body, req.jsdataOpts)
7+
},
8+
statusCode: 201
9+
},
10+
createMany: {
11+
action (component, req) {
12+
return component.createMany(req.body, req.jsdataOpts)
13+
},
14+
statusCode: 201
15+
},
16+
destroy: {
17+
action (component, req) {
18+
return component.destroy(req.params.id, req.jsdataOpts)
19+
},
20+
statusCode: 204
21+
},
22+
destroyAll: {
23+
action (component, req) {
24+
return component.destroyAll(req.query, req.jsdataOpts)
25+
},
26+
statusCode: 204
27+
},
28+
find: {
29+
action (component, req) {
30+
return component.find(req.params.id, req.jsdataOpts)
31+
},
32+
statusCode: 200
33+
},
34+
findAll: {
35+
action (component, req) {
36+
return component.findAll(req.query, req.jsdataOpts)
37+
},
38+
statusCode: 200
39+
},
40+
update: {
41+
action (component, req) {
42+
return component.update(req.params.id, req.body, req.jsdataOpts)
43+
},
44+
statusCode: 200
45+
},
46+
updateAll: {
47+
action (component, req) {
48+
return component.updateAll(req.body, req.query, req.jsdataOpts)
49+
},
50+
statusCode: 200
51+
},
52+
updateMany: {
53+
action (component, req) {
54+
return component.updateMany(req.body, req.jsdataOpts)
55+
},
56+
statusCode: 200
57+
}
58+
}
59+
60+
export function makeRequestHandler (method, component, config = {}) {
61+
config[method] || (config[method] = {})
62+
const action = config[method].action || DEFAULTS[method].action
63+
64+
return (req, res, next) => {
65+
action(component, req)
66+
.then((result) => {
67+
req.jsdataResult = result
68+
next()
69+
})
70+
.catch(next)
71+
}
72+
}
73+
74+
export function makeResponseHandler (method, component, config = {}) {
75+
const methodConfig = config[method] || {}
76+
const statusCode = methodConfig.statusCode || DEFAULTS[method].statusCode
77+
let toJSON
78+
79+
// Pick the user's toJSON setting, in order of preference
80+
if (utils.isFunction(methodConfig.toJSON)) {
81+
toJSON = (component, result, opts) => methodConfig.toJSON(component, result, opts)
82+
} else if (methodConfig.toJSON === false) {
83+
toJSON = (component, result, opts) => result
84+
} else if (methodConfig.toJSON === true) {
85+
toJSON = (component, result, opts) => component.toJSON(result, opts)
86+
} else {
87+
if (utils.isFunction(config.toJSON)) {
88+
toJSON = (component, result, opts) => config.toJSON(component, result, opts)
89+
} else if (config.toJSON === false) {
90+
toJSON = (component, result, opts) => result
91+
} else {
92+
toJSON = (component, result, opts) => component.toJSON(result, opts)
93+
}
94+
}
95+
96+
return (req, res, next) => {
97+
const result = req.jsdataResult
98+
99+
res.status(statusCode)
100+
101+
try {
102+
if (result !== undefined) {
103+
res.send(toJSON(component, result, req.jsdataOpts))
104+
}
105+
} catch (err) {
106+
return next(err)
107+
}
108+
109+
res.end()
110+
}
111+
}

0 commit comments

Comments
 (0)