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

Commit dcbbb99

Browse files
committed
Implement a basic decorator
Implement a `@txIdFromHeader()` decorator that can annotate a controller operation to receive the value of `X-Trasaction-Id` Header.
1 parent ab51d1a commit dcbbb99

File tree

7 files changed

+144
-1
lines changed

7 files changed

+144
-1
lines changed

index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
2+
// Node module: loopback-next-extension-starter
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
export * from './src';

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"lint": "tslint -c tslint.full.json --project tsconfig.json --type-check",
1414
"lint:fix": "npm run lint -- --fix",
1515
"prepublish": "npm run build",
16-
"pretest": "npm run build",
16+
"clean": "rm -rf dist dist6",
17+
"pretest": "npm run clean && npm run build",
1718
"test": "mocha",
1819
"posttest": "npm run lint",
1920
"vscode-test": "mocha && npm run lint"

src/decorators/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Decorators
2+
3+
## Overview
4+
5+
Decorators provide annotations for class methods and properties. Decorators use the form `@decorator` where `decorator` is the name of the function that will be called at runtime.
6+
7+
## Basic Usage
8+
9+
### txIdFromHeader
10+
11+
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.
12+
13+
**Example**
14+
```
15+
class MyController {
16+
@get('/')
17+
@txIdFromHeader()
18+
getHandler(txId: string) {
19+
return `Your transaction id is: ${txId}`;
20+
}
21+
}
22+
```
23+
24+
## Related Resources
25+
26+
You can check out the following resource to learn more about decorators and how they are used in LoopBack Next.
27+
28+
- [TypeScript Handbook: Decorators](https://www.typescriptlang.org/docs/handbook/decorators.html)
29+
- [Decorators in LoopBack](http://loopback.io/doc/en/lb4/Decorators.html)
30+
31+
## Contributions
32+
33+
- [Guidelines](http://loopback.io/doc/en/contrib/index.html)
34+
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)
35+
36+
## Tests
37+
38+
Run `npm test` from the root folder.
39+
40+
## Contributors
41+
42+
See [all contributors](https://github.com/strongloop/loopback-next-extension-starter/graphs/contributors).
43+
44+
## License
45+
46+
MIT

src/decorators/txIdFromHeader.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright IBM Corp. 2017. All Rights Reserved.
2+
// Node module: loopback-next-extension-starter
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
import {param} from '@loopback/core';
7+
8+
/**
9+
* Decorator to inject the current transaction-id from a request's
10+
* 'X-Transaction-Id' header into the decorated function.
11+
*/
12+
export function txIdFromHeader() {
13+
return param.header.string('X-Transaction-Id');
14+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
// Node module: loopback-next-extension-starter
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
5+
6+
export * from './decorators/txIdFromHeader';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright IBM Corp. 2017. All Rights Reserved.
2+
// Node module: @loopback/core
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
import {Application, get} from '@loopback/core';
7+
import {txIdFromHeader} from '../..';
8+
import {Client, createClientForApp} from '@loopback/testlab';
9+
10+
describe('@txIdFromHeader() tests', () => {
11+
let app: Application;
12+
13+
beforeEach(createApp);
14+
beforeEach(createController);
15+
16+
it('works with header set', async () => {
17+
const client: Client = createClientForApp(app);
18+
19+
await client
20+
.get('/')
21+
.set('X-Transaction-Id', 'testid123')
22+
.expect(200, 'Your id is testid123');
23+
});
24+
25+
it('works without header', async () => {
26+
const client: Client = createClientForApp(app);
27+
28+
await client.get('/').expect(200, 'Your id is undefined');
29+
});
30+
31+
function createApp() {
32+
app = new Application();
33+
}
34+
35+
function createController() {
36+
class MyController {
37+
@get('/')
38+
@txIdFromHeader()
39+
test(txId: string) {
40+
return `Your id is ${txId}`;
41+
}
42+
}
43+
44+
app.controller(MyController);
45+
}
46+
});

test/unit/txIdFromHeader.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright IBM Corp. 2017. All Rights Reserved.
2+
// Node module: @loopback/core
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
import {expect} from '@loopback/testlab';
7+
import {get, getControllerSpec} from '@loopback/core';
8+
import {txIdFromHeader} from '../..';
9+
10+
describe('@txHeaderFromId', () => {
11+
it('defines a parameter for X-Transaction-Id', () => {
12+
class MyController {
13+
@get('/')
14+
@txIdFromHeader()
15+
hello(txId: string) {}
16+
}
17+
18+
const actualSpec = getControllerSpec(MyController);
19+
20+
expect(actualSpec.paths['/']['get'].parameters).to.eql([
21+
{
22+
name: 'X-Transaction-Id',
23+
type: 'string',
24+
in: 'header',
25+
},
26+
]);
27+
});
28+
});

0 commit comments

Comments
 (0)