Skip to content

Resolve ${var} values in middleware.json - #149

Merged
bajtos merged 1 commit into
strongloop:masterfrom
hacksparrow:feature/declarative-loopback-rest
Aug 25, 2015
Merged

bajtos merged 1 commit into
strongloop:masterfrom
hacksparrow:feature/declarative-loopback-rest

Conversation

@hacksparrow

Copy link
Copy Markdown
Member

Declaratively load loopback.rest using middleware.json:

{
  "routes": {
    "loopback#rest": {
      "paths": ["${restApiRoot}"]
    }
  }
}

Connect to #75

@bajtos

bajtos commented Aug 13, 2015

Copy link
Copy Markdown
Member

Hi @hacksparrow, thank you for the pull request.

Here are my responses to your comments in #75

are we not supporting const yet?

Not yet, all code must run on Node v0.10, which does not support ES2016 features.

is there a reason why loopback#rest takes an array of paths while loopback-explorer takes a single mountPath?

loopback-boot allows users to mount the same middleware at multiple paths, this is convenient for example when configuring the same authorization middleware for multiple endpoints. That's way it's technically possible to configure loopback#rest for multiple endpoints, even though it's rather impractical IMO.

As for loopback-explorer, it does not use the mechanism for middleware registration, thus it provides it's own configuration option mountPath that expects a single string only.

Is it more clear now?

@hacksparrow

Copy link
Copy Markdown
Member Author

All clear, @bajtos. Thanks!

@bajtos

bajtos commented Aug 13, 2015

Copy link
Copy Markdown
Member

Now regarding the pull request: I would strongly prefer to support ${var} syntax for all middleware and all configuration options, not only for loopback#rest's paths. See how $!../path is implemented for inspiration:

// Match values starting with `$!./` or `$!../`
var MIDDLEWARE_PATH_PARAM_REGEX = /^\$!(\.\/|\.\.\/)/;
function resolveMiddlewareParams(rootDir, params) {
return cloneDeep(params, function resolvePathParam(value) {
if (typeof value === 'string' && MIDDLEWARE_PATH_PARAM_REGEX.test(value)) {
return path.resolve(rootDir, value.slice(2));
} else {
return undefined; // no change
}
});
}

Nice to have: optimise the implementation so that it either does not use lodash's cloneDeep, or at least loads only cloneDeep, not the whole lodash module. This is important because executor runs in the browser and the lodash module adds too much code to the browserified bundle.

Also please add a unit test to verify the implementation and prevent regressions in the future, see

describe('config with relative paths in params', function() {
var RELATIVE_PATH_PARAMS = [
'$!./here',
'$!../there'
];
var absolutePathParams;
beforeEach(function resolveRelativePathParams() {
absolutePathParams = RELATIVE_PATH_PARAMS.map(function(p) {
return appdir.resolve(p.slice(2));
});
});
it('converts paths in top-level array items', function() {
givenMiddlewareEntrySync({ params: RELATIVE_PATH_PARAMS });
var instructions = boot.compile(appdir.PATH);
expectFirstMiddlewareParams(instructions)
.to.eql(absolutePathParams);
});
it('converts paths in top-level object properties', function() {
givenMiddlewareEntrySync({ params: {
path: RELATIVE_PATH_PARAMS[0],
}});
var instructions = boot.compile(appdir.PATH);
expectFirstMiddlewareParams(instructions)
.to.eql({ path: absolutePathParams[0] });
});
it('converts path value when params is a string', function() {
givenMiddlewareEntrySync({ params: RELATIVE_PATH_PARAMS[0] });
var instructions = boot.compile(appdir.PATH);
expectFirstMiddlewareParams(instructions)
.to.eql(absolutePathParams[0]);
});
it('converts paths in nested properties', function() {
givenMiddlewareEntrySync({ params: {
nestedObject: {
path: RELATIVE_PATH_PARAMS[0]
},
nestedArray: RELATIVE_PATH_PARAMS
}});
var instructions = boot.compile(appdir.PATH);
expectFirstMiddlewareParams(instructions)
.to.eql({
nestedObject: {
path: absolutePathParams[0]
},
nestedArray: absolutePathParams
});
});
it('does not convert values not starting with `./` or `../`', function() {
var PARAMS = ['$!.somerc', '$!/root', '$!hello!'];
givenMiddlewareEntrySync({ params: PARAMS});
var instructions = boot.compile(appdir.PATH);
expectFirstMiddlewareParams(instructions).to.eql(PARAMS);
});
});
});
for inspiration.

@bajtos bajtos self-assigned this Aug 13, 2015
@hacksparrow

Copy link
Copy Markdown
Member Author

Some points for tomorrow's discussion based on the feedback:

  1. ${var} has context in case of loopback#rest, so it works out fine. If we want to make it generic, we will need to have some sort of context mapping object, especially if the var may not come from app.get().
  2. lodash is used extensively in lib/compiler.js, do we want to redo everything using our own implementation? If no, then we might as well continue using its cloneDeep(), else we can do it a little later.
  3. Yes, unit test is required.

@bajtos

bajtos commented Aug 17, 2015

Copy link
Copy Markdown
Member

@hacksparrow I can see you added new commits, please let me know when the patch is finished and ready for the next round of review.

@hacksparrow

Copy link
Copy Markdown
Member Author

@bajtos not done with it yet. Need to add tests too. Will ping you once ready.

@hacksparrow

Copy link
Copy Markdown
Member Author

@bajtos you can review now.

@hacksparrow

Copy link
Copy Markdown
Member Author

@bajtos let's do the lodash thing after loopback-explorer is also loaded declaratively.

Comment thread lib/executor.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please simplify:

updatedConfigObject[configKey] = value.map(getConfigVariable);

@bajtos

bajtos commented Aug 22, 2015

Copy link
Copy Markdown
Member

Reviewed. The code looks mostly good, I left few minor comments to address. Please re-run npm test and fix coding style issues, the CI build is failing because of them.

@hacksparrow

Copy link
Copy Markdown
Member Author

Thanks for the feedback @bajtos. Will make the changes accordingly.

@hacksparrow

Copy link
Copy Markdown
Member Author

@bajtos, pushed the recommended changes.

@bajtos

bajtos commented Aug 25, 2015

Copy link
Copy Markdown
Member

The patch LGTM 👍

@hacksparrow Could you please squash the commits into a single one and provide a descriptive commit message following the 50/72 rule?

@hacksparrow
hacksparrow force-pushed the feature/declarative-loopback-rest branch 2 times, most recently from 0cfa620 to 09139ee Compare August 25, 2015 12:59
@bajtos bajtos changed the title declaratively load loopback.rest Resolve ${var} values in middleware.json Aug 25, 2015
App variables can now be specified in middleware.json using the ${var}
format. The value of var is looked up using app.get().

This allows loopback.rest and other loopback middleware which require
app variables, to be loaded declaratively using middleware.json.
@hacksparrow
hacksparrow force-pushed the feature/declarative-loopback-rest branch from 09139ee to 8b475a9 Compare August 25, 2015 13:29
bajtos added a commit that referenced this pull request Aug 25, 2015
…-rest

Resolve ${var} values in middleware.json
@bajtos
bajtos merged commit 0b0f0ed into strongloop:master Aug 25, 2015
@bajtos

bajtos commented Aug 25, 2015

Copy link
Copy Markdown
Member

Landed, thank you!

@hacksparrow

Copy link
Copy Markdown
Member Author

🎉

@raymondfeng

Copy link
Copy Markdown
Member

@hacksparrow Great stuff!

@superkhau

Copy link
Copy Markdown
Contributor

+1

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.

5 participants