From 870f58fcb8e0cf7eb295672c82f993c749292edb Mon Sep 17 00:00:00 2001 From: pxseu <57842793+pxseu@users.noreply.github.com> Date: Sun, 7 Nov 2021 00:36:51 +0100 Subject: [PATCH] feat(decorators): add simple decorator --- .eslintrc | 1 + src/lib/utils/decorators.ts | 25 +++++++++++++++++++++++++ tsconfig.json | 1 + 3 files changed, 27 insertions(+) create mode 100644 src/lib/utils/decorators.ts diff --git a/.eslintrc b/.eslintrc index 4e557a4..a71382e 100644 --- a/.eslintrc +++ b/.eslintrc @@ -23,6 +23,7 @@ "no-underscore-dangle": "off", "import/prefer-default-export": "off", "consistent-return": "off", + "arrow-body-style": "off", "no-restricted-syntax": "off" } } \ No newline at end of file diff --git a/src/lib/utils/decorators.ts b/src/lib/utils/decorators.ts new file mode 100644 index 0000000..f1190ea --- /dev/null +++ b/src/lib/utils/decorators.ts @@ -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; +}; diff --git a/tsconfig.json b/tsconfig.json index 63e4d5f..ab04534 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,6 +17,7 @@ "noImplicitReturns": true, "downlevelIteration": true, "noUnusedParameters": true, + "experimentalDecorators": true, "importsNotUsedAsValues": "error", "forceConsistentCasingInFileNames": true, // false stuff