-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a package with JSON response middleware (#745)
This just handles some of the boilerplate involved with setting up a JSON endpoint in React Server.
- Loading branch information
Showing
6 changed files
with
78 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": ["react-server"], | ||
"plugins": ["add-module-exports"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# JSON Response middleware for React Server | ||
|
||
Use this middleware to return a JSON-serialized object from a React Server page. | ||
|
||
```javascript | ||
import JsonResponseMiddleware from "react-server-middleware-json-response" | ||
|
||
export default class DataEndpoint { | ||
static middleware() { | ||
return [JsonResponseMiddleware]; | ||
} | ||
|
||
getResponseData() { | ||
return { result: "OK!" }; | ||
} | ||
} | ||
``` |
14 changes: 14 additions & 0 deletions
14
packages/react-server-middleware-json-response/index.src.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export default class JsonResponseMiddleware { | ||
|
||
setConfigValues() { | ||
return { isRawResponse: true }; | ||
} | ||
|
||
getContentType(next) { | ||
return next() || 'application/json'; | ||
} | ||
|
||
getResponseData(next) { | ||
return next().then(data => JSON.stringify(data)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/react-server-middleware-json-response/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "react-server-middleware-json-response", | ||
"version": "0.4.10", | ||
"description": "JSON response middleware for React Server", | ||
"main": "index.js", | ||
"scripts": { | ||
"prepublish": "npm run build", | ||
"build": "babel index.src.js --out-file index.js", | ||
"clean": "rm index.js", | ||
"test": "eslint src" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/redfin/react-server.git" | ||
}, | ||
"keywords": [ | ||
"react", | ||
"server", | ||
"json", | ||
"middleware" | ||
], | ||
"author": "Bo Borgerson", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/redfin/react-server/issues" | ||
}, | ||
"homepage": "https://github.com/redfin/react-server#readme", | ||
"devDependencies": { | ||
"babel-cli": "^6.18.0", | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-preset-react-server": "^0.4.10", | ||
"eslint": "^3.8.1", | ||
"eslint-plugin-react": "^6.4.1", | ||
"babel-eslint": "^7.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import Q from "q"; | ||
import _ from "lodash"; | ||
import JsonResponseMiddleware from "react-server-middleware-json-response" | ||
|
||
const BIG = n => _.range(+n).reduce((m, v) => (m['element_'+v] = v, m), {}) | ||
|
||
export default class DelayDataPage { | ||
setConfigValues() { | ||
return { isRawResponse: true }; | ||
} | ||
getContentType() { | ||
return 'application/json'; | ||
|
||
static middleware() { | ||
return [JsonResponseMiddleware]; | ||
} | ||
|
||
getResponseData() { | ||
const { ms, val, big } = this.getRequest().getQuery(); | ||
return Q.delay(ms||0) | ||
.then(() => val||JSON.stringify(big?BIG(big):{'ok':true})); | ||
.then(() => val?JSON.parse(val):(big?BIG(big):{'ok':true})); | ||
} | ||
} |