Last active
March 29, 2017 02:32
-
-
Save imranismail/661be5b490cbd2c5bd449a76cc01dfdb to your computer and use it in GitHub Desktop.
Functional Example in JS
This file contains hidden or 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
| function new = () => ({ | |
| _struct: "Conn", | |
| host: null, | |
| path: null, | |
| port: null, | |
| scheme: null, | |
| params: {}, | |
| requestHeaders: [], | |
| responseHeaders: [], | |
| responseBody: {}, | |
| status: null | |
| }) | |
| function putHost = (conn, host) => ({...user, name: name}) | |
| function putRequestHeaders = (conn, ...headers) => ({...conn, requestHeaders: [...conn.requestHeaders, ...headers}) | |
| function putPath = (conn, path) => ({...conn, path: path}) | |
| function putPort = (conn, port) => ({...conn, port: port}) | |
| function putScheme = (conn, scheme) => ({...conn, scheme: scheme}) | |
| function putParams = (conn, params) => ({...conn, params: params}) | |
| function putStatus = (conn, status) => ({...conn, status: status}) | |
| function putResponseHeaders = (conn, ...headers) => ({...conn, responseHeaders: [...conn.responseHeaders, ...headers]}) | |
| function putResponseBody = (conn, body) => ({...conn, responseBody: body}) | |
| function getHost = (conn) => conn.host | |
| function getPath = (conn) => conn.path | |
| function getPort = (conn) => conn.port | |
| function getScheme = (conn) => conn.scheme | |
| function getParams = (conn) => conn.params | |
| function getStatus = (conn) => conn.status | |
| function getRequestHeaders = (conn) => conn.requestHeaders.reduce((acc, [key, val]) => ({...acc, [key]: val}), {}) | |
| function getResponseHeaders = (conn) => conn.responseHeaders.reduce((acc, [key, val]) => ({...acc, [key]: val}), {}) | |
| function getResponseBody = (conn) => conn.responseBody | |
| export default { | |
| new, | |
| putHost, | |
| putPath, | |
| putPort, | |
| putScheme, | |
| putParams, | |
| putRequestHeaders, | |
| putResponseHeaders, | |
| putResponseBody, | |
| putStatus, | |
| getHost, | |
| getPath, | |
| getPort, | |
| getScheme, | |
| getParams, | |
| getStatus, | |
| getRequestHeaders, | |
| getResponseHeaders, | |
| getResponseBody | |
| } |
This file contains hidden or 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
| import Conn from './conn.js' | |
| import User from './user.js' // some schema/model to help map to user from DB | |
| function show(conn) { | |
| render(conn, User.getBy({id: Conn.getParams(conn)["id"]}) | |
| } | |
| function render(conn, body) { | |
| conn = putStatus(conn, 200) | |
| conn = putResponseHeaders(conn, ["Content-Type", "application/json"]) | |
| return putResponseBody(conn, JSON.stringify(body)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment