Socket
Socket
Sign inDemoInstall

@wix/be-http-binding

Package Overview
Dependencies
Maintainers
4
Versions
129
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wix/be-http-binding - npm Package Compare versions

Comparing version 1.0.4 to 1.0.5

69

lib/message-builder.js

@@ -8,16 +8,8 @@

return {
field(name, type, tag) {
return messageBuilder({
...context,
[name]: {
type, tag
}
});
field(name, type, modifier) {
return defineField(context, name, type, modifier);
},
repeated(name, type, tag) {
return messageBuilder({
...context,
[name]: {
type, tag, repeated: true
}
repeated(name, type, modifier) {
return defineField(context, name, type, modifier, {
repeated: true
});

@@ -31,2 +23,15 @@ },

function defineField(context, name, type, modifier, props) {
const resolvedModifier = modifierFrom(modifier);
const modifierState = resolvedModifier.define ? resolvedModifier.define({name, type}) : undefined;
return messageBuilder({
...context,
[name]: {
type, modifier: resolvedModifier, modifierState,
...props
}
});
}
function message(context) {

@@ -36,11 +41,25 @@ return {

const result = {};
const valueModifiers = [];
Object.keys(context).forEach((field) => {
if (context[field].repeated) {
result[field] = value[field].map((value) => context[field].type.fromValue(value));
const {repeated, modifier, type} = context[field];
if (repeated) {
result[field] = (Array.isArray(value[field]) ? value[field] : [value[field]]).map((value) => type.fromValue(value));
} else {
result[field] = context[field].type.fromValue(value[field]);
result[field] = type.fromValue(value[field]);
}
if (modifier && modifier.value) {
valueModifiers.push({
field,
modifier
});
}
});
valueModifiers.forEach(({field, modifier}) => {
result[field] = modifier.value(result);
});
return result;

@@ -50,1 +69,19 @@ }

}
function modifierFrom(modifier) {
if (typeof(modifier) === 'number') {
return assignTag(modifier);
}
return modifier;
}
function assignTag(tag) {
return {
define() {
return {
tag
};
}
}
}
const {messageBuilder} = require('./message-builder');
const {string} = require('./well-known-types');
const {string, int32} = require('./well-known-types');
const {expect} = require('chai');
const _ = require('lodash');

@@ -20,3 +21,3 @@ describe('message-builder', () => {

it('should read a simple repeated message', () => {
it('should read a simple message with repeated field', () => {
const message = messageBuilder()

@@ -32,2 +33,36 @@ .repeated('names', string, 1)

});
it('should single field as repeated', () => {
const message = messageBuilder()
.repeated('names', string, 1)
.build();
expect(message.fromValue({
names: 'John'
})).to.deep.equal({
names: ['John']
});
});
it('should add field modifier', () => {
const message = messageBuilder()
.repeated('values', int32, 1)
.field('sum', int32, sumOf('values'))
.build();
expect(message.fromValue({
values: [1, 2]
})).to.deep.equal({
values: [1, 2],
sum: 3
});
});
function sumOf(fieldName) {
return {
value(message) {
return _.sum(message[fieldName]);
}
};
}
});
{
"name": "@wix/be-http-binding",
"version": "1.0.4",
"version": "1.0.5",
"author": "Mantas Indrašius <mantasi@wix.com>",

@@ -19,2 +19,3 @@ "publishConfig": {

"chai": "^4.2.0",
"lodash": "^4.17.11",
"mocha": "^5.2.0",

@@ -21,0 +22,0 @@ "mocha-env-reporter": "^3.0.0",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc