Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat(common): ES5 support, Catch accepts arguments passed into decora…
Browse files Browse the repository at this point in the history
…ted function (#38)
  • Loading branch information
serhiisol authored Feb 28, 2017
1 parent 2711529 commit 4b36bd0
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 36 deletions.
1 change: 1 addition & 0 deletions common/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
src/**/*.ts
!src/**/*.d.ts
index.ts
tsconfig.json
10 changes: 10 additions & 0 deletions common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Common#1.1.0
* Moved project back to es5
* **@Catch()** now accepts all passed arguments of the original function and error, e.g.:
```typescript
@Catch((volume: number, e: Error) => {
// ...
})
sound(volume: number) {}
```

# Common#1.0.0
* New common decorators
* **@Log(loggerFn?: Function)**
Expand Down
6 changes: 3 additions & 3 deletions common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ class Animal {
}
}
```
* **@Catch(func: (e) => void)** - Catches unhandled error in the function, passes error in input function
* **@Catch(func: (...args: any, e: Error) => void)** - Catches unhandled error in the function, passes all arguments came to the function with error in input function
```typescript
class Animal {
@Catch((e) => {
@Catch((volume: number, e: Error) => {
console.log(e); //ReferenceError: name is not defined...
});
})
sound(volume: number) {
console.log(name, ': Auuuu', volume);
}
Expand Down
4 changes: 2 additions & 2 deletions common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@decorators/common",
"version": "1.0.0",
"version": "1.1.0",
"description": "node decorators",
"main": "src/index.js",
"dependencies": {
Expand All @@ -17,7 +17,7 @@
"url": "https://github.com/serhiiso/node-decorators/issues"
},
"engines": {
"node": "^6.3.0"
"node": ">=6.3.0"
},
"repository": {
"type": "git",
Expand Down
6 changes: 3 additions & 3 deletions common/src/catch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ export function Catch(catchFn: Function) {
return function Catch(target, key, descriptor) {
const originalMethod = descriptor.value;

descriptor.value = function (...args) {
descriptor.value = function(...args) {
try {
let value = originalMethod.apply(this, args);
if (value !== undefined && value.catch) {
value.catch(catchFn);
}
return value;
} catch (error) {
catchFn(error);
catchFn(...args, error);
}
};

}
};
}
9 changes: 9 additions & 0 deletions common/src/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface CommonMeta {
parameters: {
[key: string]: number[];
};
}

export interface CommonClass extends Object {
__meta__: CommonMeta;
}
2 changes: 2 additions & 0 deletions common/src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CommonClass, CommonMeta } from './interface';

/**
* Get or initiate metadata on target
* @param target
Expand Down
22 changes: 22 additions & 0 deletions common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compileOnSave": false,
"compilerOptions": {
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
"noImplicitAny": false,
"rootDir": ".",
"sourceMap": true,
"target": "es5",
"pretty": true,
"lib": [
"es2015"
]
},
"files": [
"src/index.ts"
]
}
24 changes: 7 additions & 17 deletions playground/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
import { Log, Bind } from '@decorators/common';
import { Catch } from '@decorators/common';

@Log()
class Animal {

constructor(
public name: string
) {}

@Bind()
@Catch((volume: number, err: Error) => {
console.log(volume);
console.log(err);
})
sound(volume: number) {
console.log(this.name, ': Auuuu', volume);
throw new Error(volume.toString());
}

}

let rabbit: Animal = new Animal('Rabbit');

function exec(fn, arg) {
//some heavy logic
return fn(arg);
}



rabbit.sound = function () {
console.log(this.name);
};

exec(rabbit.sound, 500);
rabbit.sound(10);
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"co/index.ts",
"express/index.ts",
"mongoose/index.ts",
"socket/index.ts",
"common/src/index.ts"
"socket/index.ts"
]
}
9 changes: 0 additions & 9 deletions types/common.d.ts

This file was deleted.

0 comments on commit 4b36bd0

Please sign in to comment.