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
const express = require('express') | |
const bodyParser = require('body-parser') | |
const app = express() | |
const port = process.env.PORT || 3010 | |
app.use(bodyParser.json()) | |
app.get('/data', (req, res) => { | |
const data = [ | |
{ |
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
const get = require('lodash.get') | |
const set = require('lodash.set') | |
const cloneDeep = require('lodash.clonedeep') | |
const got = require('got') | |
module.exports = { | |
makeRequest | |
} | |
async function makeRequest (payload) { |
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 composeRequest (payload) { | |
const newPayload = cloneDeep(payload) | |
const { host, headers, method, endpoint } = get(payload, 'data.config', {}) | |
const requestOptions = { | |
uri: new URL(endpoint, host), | |
options: { | |
headers, | |
method | |
} | |
} |
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
test('composeRequest() should compose request options so they can be consumed by the GOT http customer module ', async function ({ deepEqual, end }) { | |
const config = { | |
endpoint: 'foo', | |
host: 'http://localhost:3333', | |
method: 'GET', | |
headers: { 'content-type': 'application/json' } | |
} | |
// composeRequest :: Payload -> Payload | |
const actual = composeRequest(deepFreeze(lift(config))) | |
const expected = { |
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 composeRequest (payload) { | |
const config = payload.data.config | |
const endpoint = config.endpoint | |
const base = config.host | |
const headers = config.headers | |
const method = config.method | |
const requestOptions = { | |
uri: new URL(endpoint, base), | |
options: { | |
headers, |
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
// ... | |
test('composeRequest() should compose request options so they can be consumed by the GOT http customer module ', async function ({ deepEqual, end }) { | |
const config = { | |
endpoint: 'foo', | |
host: 'http://localhost:3333', | |
method: 'GET', | |
headers: { 'content-type': 'application/json' } | |
} | |
// composeRequest :: Payload -> Payload |
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
app.get('/stores', (req, res) => { | |
const responseBody = pipe( | |
composeRequest, | |
makeRequest, | |
composeResponse | |
)(lift(config)) | |
// ... | |
}) |
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
pipe( | |
// ... | |
)(lift(req.body)) | |
function lift(config = {}, body = {}) { | |
return {event: {body}, data: {config}, result: {}} | |
} |
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
pipe( | |
// ... | |
)({ | |
event: { | |
body: req.body | |
}, | |
data: { config }, | |
result: {} | |
}) |
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
const express = require('express') | |
const bodyParser = require('body-parser') | |
const {pipe} = require('./tools') | |
const app = express() | |
const port = process.env.PORT || 3000 | |
app.use(bodyParser.json()) | |
app.get('/stores', (req, res) => { | |
const responseBody = pipe( |