UNPKG

tipi-services

Version:

Node.js library to access wrapping REST API of tipi backend services

44 lines (40 loc) 1.13 kB
const _ = require('lodash') const ValidateEndpoint = require('./ValidateEndpoint') module.exports = ({ endpoint, query, params, body, formData, headers, state , uponReceiving, status, expectedBody } = {}) => { if (endpoint.schema) { ValidateEndpoint({ query, params, body }, endpoint.schema) } let path = endpoint.interactionPath _.each(params, (value, key) => { path = path.replace(`:${key}`, value) }) const interaction = { uponReceiving, withRequest: { method: endpoint.method, path, headers: { Accept: 'application/json', } }, willRespondWith: { status, headers: { 'Content-Type': 'application/json; charset=utf-8' }, body: expectedBody } } if (state) interaction.state = state if (query) interaction.withRequest.query = query if (body) { interaction.withRequest.body = body } if(headers){ interaction.withRequest.headers = {...interaction.withRequest.headers,...headers} } return interaction }