Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepare-server-get-component-parameters #432

Merged
merged 10 commits into from
Apr 4, 2017

Conversation

mattiaerre
Copy link
Member

@mattiaerre mattiaerre commented Apr 3, 2017

Description

  • add mergeObjects function to build the query string based on component and options parameters

  • fix tests

  • add test:unit as npm script

@mattiaerre
Copy link
Member Author

this PR should fix #431 please @gtrias can you have a look at is and see if this makes sense to you?

// cc @matteofigus @ivan-dimitrov-skyscanner

Copy link
Member

@matteofigus matteofigus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking this PR flips the coin so that component.parameters are used over options.parameters, which may be a common scenario for when using renderComponents with a single component, but still just one of the two ways to use parameters - which can actually be used together.

I would like a test for a request like

client.renderComponents([{
  name: 'my-component',
  parameters: { a:1, b: 2 }
}], { parameters: { b: 3, c: 4 }, callback);

That would test url would be my-component?a=1&b=2&c=4.

@mattiaerre
Copy link
Member Author

@matteofigus I've added a new function that merges 2 objects together and tests that should cover the scenario you've described; let me know what you think.

// @gtrias @ivan-dimitrov-skyscanner

@gtrias
Copy link
Member

gtrias commented Apr 4, 2017

@mattiaerre I'm going to test it right now. Thanks a lot for the quick fix ;)

@gtrias
Copy link
Member

gtrias commented Apr 4, 2017

Hey @mattiaerre how can I try your branch?
I'm trying to add "oc-client": "git+ssh://git@github.com:opentable/oc.git#prepare-server-get-component-parameters" and running rm -rf node_modules/oc-client && npm install.
But then it is installing all oc repository and nodejs is unable to find module oc-client:

Error: Cannot find module 'oc-client'

Is there a way to specify the client folder? AFAIK npm doesn't allow to do that npm/npm#2974


var qs = '';
if (component.parameters || options.parameters) {
qs = '/?' + querystring.stringify(mergeObjects(component.parameters, options.parameters));
Copy link
Member

@matteofigus matteofigus Apr 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about using _.extend instead of mergeObjects. We use it a lot everywhere so it would be more consistent.

var _ = require('underscore')
var p = _.extend(options.parameters, component.parameters)
// example
_.extend({b:3, c:4}, {a:1, b:2})
// => { b: 2, c: 4, a: 1 }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about that but looking at the package.json I couldn't see lodash as a dependency (https://github.com/opentable/oc/blob/master/client/package.json#L17-L21) also in ES6 this will be part of the language so I preferred not to add a dependency and go "vanilla". plus the component is a standalone one (and fully tested) so we can always swap its internal implementation.

describe('when there are parameters in both "options" and "component"', () => {
it('it should return a valid request for the component with the parameters set as URL query params', () => {
let options = { parameters: { p1: 'v1', p2: 'v 2' } };
let component = { name: 'hello-world', version: '1.0.0', parameters: { message: 'hello' } };
Copy link
Member

@matteofigus matteofigus Apr 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we include a common parameter to ensure our merge goes in the right way, such as { message: 'hello', p1: 'v3'} and be sure qs has ...&p1=v1

Copy link
Member Author

@mattiaerre mattiaerre Apr 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is covered at the mergeObjects level but I will add a specific test for this scenario. and in this case, p1=v3 as the component's parameters win over the option ones (as per your other example above).

@mattiaerre
Copy link
Member Author

over to you @matteofigus

@gtrias
Copy link
Member

gtrias commented Apr 4, 2017

@mattiaerre now it works my use case :). But I haven't tried all the scenarios btw...

@gtrias
Copy link
Member

gtrias commented Apr 4, 2017

I can confirm using global parameters works as well but they can be overridden by the component ones...

@matteofigus
Copy link
Member

Awesome stuff thanks @mattiaerre

@matteofigus matteofigus merged commit 5a4162a into master Apr 4, 2017
@matteofigus matteofigus deleted the prepare-server-get-component-parameters branch April 4, 2017 17:25
@matteofigus
Copy link
Member

Just published. You should be able to upgrade and test now @gtrias thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants