@wix/be-http-binding
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -9,8 +9,6 @@ | ||
function http(method, path, requestMessage, responseMessage, methodOptions) { | ||
const invocation = method(path, requestMessage, responseMessage); | ||
function http(binding, requestMessage, responseMessage, methodOptions) { | ||
return { | ||
async invoke(message, options) { | ||
const result = await invocation.invokeWith(methodOptions.invoker || options.invoker, requestMessage.fromValue(message)); | ||
const result = await binding.invokeWith(methodOptions.invoker || options.invoker, requestMessage.fromValue(message)); | ||
@@ -31,3 +29,7 @@ return responseMessage.fromValue(result); | ||
function body(method) { | ||
return (path) => methodInvoker(method, (message) => ({uri: path, message})); | ||
return (path) => { | ||
const mapToPath = pathMapper(path); | ||
return methodInvoker(method, (message) => mapToPath(message)); | ||
}; | ||
} | ||
@@ -55,7 +57,29 @@ | ||
function uriMapper(path) { | ||
// const [first, ...rest] = path.split(/{(.+?)}/g); | ||
return (message) => `${path}?${valueToQuery(message)}`; | ||
} | ||
// console.log(first); | ||
function pathMapper(path) { | ||
const [prefix, ...rest] = path.split(/({.+?})/g); | ||
return (message) => `${path}?${valueToQuery(message)}`; | ||
return (message) => { | ||
const copy = {...message}; | ||
const uri = prefix + rest.map((placeholderOrValue) => { | ||
if (placeholderOrValue.startsWith('{')) { | ||
const fieldName = placeholderOrValue.substring(1, placeholderOrValue.length - 1); | ||
const value = message[fieldName]; | ||
delete copy[fieldName]; | ||
return value; | ||
} else { | ||
return placeholderOrValue; | ||
} | ||
}).join(''); | ||
return { | ||
uri, | ||
message: copy | ||
}; | ||
}; | ||
} | ||
@@ -62,0 +86,0 @@ |
@@ -1,2 +0,2 @@ | ||
const {http, get, post} = require('./http-binding'); | ||
const {http, get, post, put} = require('./http-binding'); | ||
const {messageBuilder} = require('./message-builder'); | ||
@@ -7,2 +7,3 @@ const {int32} = require('./well-known-types'); | ||
const querystring = require('querystring'); | ||
const UrlPattern = require('url-pattern'); | ||
@@ -18,12 +19,7 @@ describe('http-binding', () => { | ||
beforeEach(() => { | ||
invoker = testInvoker((request) => { | ||
const uri = url.parse(request.uri); | ||
const queryParams = querystring.parse(uri.query); | ||
return request.message ? Object.assign(queryParams, request.message) : queryParams; | ||
}); | ||
invoker = testInvoker(handleInvoke.bind(this, null)); | ||
}); | ||
it('should invoke a GET method', async() => { | ||
const givenMethod = http(get, '/pass', message, message, {invoker}); | ||
const givenMethod = http(get('/pass'), message, message, {invoker}); | ||
@@ -47,3 +43,3 @@ const result = await givenMethod.invoke({ | ||
it('should invoke a POST method', async() => { | ||
const givenMethod = http(post, '/pass', message, message, {invoker}); | ||
const givenMethod = http(post('/pass'), message, message, {invoker}); | ||
@@ -70,2 +66,26 @@ const result = await givenMethod.invoke({ | ||
it('should invoke a PUT method', async() => { | ||
const givenInvoker = testInvoker(handleInvoke.bind(this, new UrlPattern('/pass/:a'))); | ||
const givenMethod = http(put('/pass/{a}'), message, message, {invoker: givenInvoker}); | ||
const result = await givenMethod.invoke({ | ||
a: 1, | ||
b: 2 | ||
}); | ||
expect(result).to.deep.equal({ | ||
a: 1, | ||
b: 2 | ||
}); | ||
expect(givenInvoker.invocations()).to.have.length(1); | ||
expect(givenInvoker.invocations()[0]).to.deep.equal({ | ||
method: 'put', | ||
uri: '/pass/1', | ||
message: { | ||
b: 2 | ||
} | ||
}); | ||
}); | ||
function testInvoker(fn) { | ||
@@ -85,2 +105,10 @@ const invocations = []; | ||
} | ||
function handleInvoke(urlPattern, request) { | ||
const uri = url.parse(request.uri); | ||
const valuesFromPath = urlPattern ? urlPattern.match(uri.pathname): {}; | ||
const queryParams = uri.query ? querystring.parse(uri.query) : {}; | ||
return request.message ? Object.assign(queryParams, valuesFromPath, request.message) : Object.assign(queryParams, valuesFromPath); | ||
} | ||
}); |
{ | ||
"name": "@wix/be-http-binding", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"author": "Mantas Indrašius <mantasi@wix.com>", | ||
@@ -24,2 +24,3 @@ "publishConfig": { | ||
"protobufjs": "^6.8.8", | ||
"url-pattern": "^1.0.3", | ||
"wnpm-ci": "^6.2.55" | ||
@@ -26,0 +27,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31083
486
9