matchpattern - HTTP response example

node v7.10.1
version: master
endpointsharetweet
const { match, when } = require('matchpattern') const response = { status: 200, body: 'hello world' } const result = match(response, [ when `{ status: 200, body: contents }`( ({ contents }) => contents ), when `{ status: httpStatus, body: _ }` ( ({ httpStatus }) => { throw new Error(`HTTP ${httpStatus}`) } ), when `_` ( () => { throw new Error('Malformed response object') } ), ]) console.log(result)
'response' is matched with first case, cause 'status' property is equal to number 200. 'contents' just extracts 'body' property to case resolve function.
Loading…

no comments

    sign in to comment