Skip to content
This repository has been archived by the owner on Feb 2, 2018. It is now read-only.

feat: Implement a basic decorator #6

Merged
merged 1 commit into from
Oct 11, 2017
Merged

feat: Implement a basic decorator #6

merged 1 commit into from
Oct 11, 2017

Conversation

virkt25
Copy link
Collaborator

@virkt25 virkt25 commented Sep 28, 2017

Implement a @txIdFromHeader() decorator that can annotate a
controller operation to receive the value of X-Trasaction-Id Header.

connect to loopbackio/loopback-next#525

@virkt25 virkt25 changed the title Implement a basic decorator feat: Implement a basic decorator Sep 28, 2017
class MyController {
@get('/')
@txIdFromHeader()
getHandler(txId: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be a parameter decorator: getHandler(@transactionId() txId: string)

Copy link
Contributor

@raymondfeng raymondfeng left a comment

Choose a reason for hiding this comment

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

Please fix the decorator so that it applies to method parameters.

Copy link

@kjdelisle kjdelisle left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@bajtos bajtos left a comment

Choose a reason for hiding this comment

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

This is changing project infrastructure in the same way as #7, in a way I am objecting against. Let's resolve my comments and land #7 before continuing the work on this pull request please.

Copy link
Contributor

@bajtos bajtos left a comment

Choose a reason for hiding this comment

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

Pull requests should be focused on a single change, they should not mix new features with tsc setup. I opened #10 to address compilation issues, hopefully that's the last project-setup-related change needed!

The rest of this pull request looks mostly good, please see my few comments below.

Also please remove test/smoke.test.ts, it was a temporary placeholder until we have the first real test.


### txIdFromHeader

This simple decorator allows you to annotate a Controller operation. The decorator will annotate the operation with the value of the header `X-Transaction-Id` from the request.
Copy link
Contributor

Choose a reason for hiding this comment

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

"a Controller operation parameter" or "a Controller method argument"

also

"annotate the operation parameter" or "annotate the method argument"

(use the same variant in both places please)

import {txIdFromHeader} from '../..';
import {Client, createClientForApp} from '@loopback/testlab';

describe('@txIdFromHeader() tests', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this file to test/acceptance/decorators/txIdFromHeader.acceptance.ts.

  • We should use the same directory structure as in src for decorators/controllers/providers etc.
  • The test file name should use a different type suffix for acceptance and unit tests. In loopback-next core, we are using .acceptance.ts, .integration.ts and .test.ts, I think we should do the same here. Maybe use .unit.ts instead of .test.ts?

For the top-level describe block, our convention in loopback-next is to add "(acceptance)" or "(unit)" to the name, e.g. describe('@tsIdFromHeader() (acceptance)').

import {get, getControllerSpec} from '@loopback/core';
import {txIdFromHeader} from '../..';

describe('@txHeaderFromId', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this file to test/unit/decorators/txIdFromHeader.test.ts. Considering that this is a unit-test that should be executing only a single class (single source file), it may be better to include ".decorator" type in the file name too, e.g. test/unit/decorators/txIdFromHeader.decorator.test.ts.

@virkt25
Copy link
Collaborator Author

virkt25 commented Oct 4, 2017

@bajtos feedback applied. Tests will fail till #11 lands.

Copy link
Contributor

@bajtos bajtos left a comment

Choose a reason for hiding this comment

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

There are still two files that have the same filename txIdFromHeader.test.ts and differs by the path (acceptance vs. unit). I makes it more difficult for me to quickly understand what file I am reading or editing, because the path is not always shown or is de-emphasized in the UI.

Other than that, the patch LGTM. No further review is needed as far as I am concerned.

@bajtos
Copy link
Contributor

bajtos commented Oct 4, 2017

Tests will fail till #11 lands.

I think you may have took the idea that project infrastructure changes should go to standalone pull requests too far. New dependencies are usually added as part of the pull request that is adding code using them.

Not a big deal, I'm fine to land #11 first and then this #6 next.

@virkt25 virkt25 force-pushed the decorator branch 3 times, most recently from b688c4a to b625ad6 Compare October 11, 2017 17:38
Implement a `@txIdFromHeader()` decorator that can annotate a
controller operation to receive the value of `X-Trasaction-Id` Header.
@virkt25 virkt25 merged commit 29bff47 into master Oct 11, 2017
@virkt25 virkt25 deleted the decorator branch October 11, 2017 19:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants