Skip to content

Commit

Permalink
feat(decorators): add simple decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed Nov 6, 2021
1 parent 6efc786 commit 870f58f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"no-underscore-dangle": "off",
"import/prefer-default-export": "off",
"consistent-return": "off",
"arrow-body-style": "off",
"no-restricted-syntax": "off"
}
}
25 changes: 25 additions & 0 deletions src/lib/utils/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Error as ImpError } from "../errors";
import { Base } from "../client/Base";
/**
* Check if the client has a token provided
* @internal
*/
export const requireToken: MethodDecorator = (_target, _key, descriptor) => {
const originalMethod = descriptor.value as unknown as Function;

if (!originalMethod) {
throw new Error("Method not provided!?");
}

return {
value: function value(...args: any[]) {
if (!(this instanceof Base)) {
throw new Error("type check");
}

if (!this.client._token) throw new ImpError("NO_TOKEN");

return originalMethod.apply(this, args);
},
} as any;
};
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"noImplicitReturns": true,
"downlevelIteration": true,
"noUnusedParameters": true,
"experimentalDecorators": true,
"importsNotUsedAsValues": "error",
"forceConsistentCasingInFileNames": true,
// false stuff
Expand Down

0 comments on commit 870f58f

Please sign in to comment.