From 66cdc2cad8e9e464500d6c3ce9c08586fbcf157a Mon Sep 17 00:00:00 2001 From: TheEVolk Date: Sat, 18 May 2024 00:38:03 +0300 Subject: [PATCH] feat: input args metadata --- packages/core/package.json | 2 +- packages/core/src/index.ts | 2 ++ packages/core/src/util/reflector.ts | 20 ++++++++++++++++ packages/input/package.json | 2 +- packages/input/src/consts.ts | 2 ++ packages/input/src/decorators.ts | 24 +++++++++++++++---- .../decorators/add-fn-metadata.decorator.ts | 14 +++++++++++ packages/input/src/index.ts | 1 + 8 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 packages/core/src/util/reflector.ts create mode 100644 packages/input/src/consts.ts create mode 100644 packages/input/src/decorators/add-fn-metadata.decorator.ts diff --git a/packages/core/package.json b/packages/core/package.json index e1d1101..669c45d 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -2,7 +2,7 @@ "name": "@henta/core", "description": "henta core", "type": "module", - "version": "4.2.1", + "version": "4.3.0", "main": "lib/index.js", "types": "./lib/index.d.ts", "exports": { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 5efb636..24cc281 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -15,6 +15,8 @@ import UpdatesBehaviour from './platform/updates.behaviour.js'; import type ISendMessageOptions from './sendMessageOptions.js'; import KB from './util/kb.js'; +export * from './util/reflector.js'; + export { Attachment, ISendMessageOptions, diff --git a/packages/core/src/util/reflector.ts b/packages/core/src/util/reflector.ts new file mode 100644 index 0000000..27434f8 --- /dev/null +++ b/packages/core/src/util/reflector.ts @@ -0,0 +1,20 @@ +/* eslint-disable @typescript-eslint/ban-types */ +export function applyDecorators( + ...decorators: unknown[] +): Function { + return ( + target: any, + propertyKey?: string | symbol, + descriptor?: PropertyDescriptor, + ): void => { + decorators.forEach((decorator) => { + if (propertyKey !== undefined && descriptor !== undefined) { + // It's a method or accessor decorator + decorator(target, propertyKey, descriptor); + } else { + // It's a class decorator + (decorator as ClassDecorator)(target); + } + }); + }; +} diff --git a/packages/input/package.json b/packages/input/package.json index 6e7e15e..cb65893 100644 --- a/packages/input/package.json +++ b/packages/input/package.json @@ -10,7 +10,7 @@ "types": "./lib/index.d.ts" } }, - "version": "0.1.6", + "version": "0.2.0", "author": "TheEVolk (https://t.me/theevolk)", "repository": "github:u14-team/henta", "bugs": "https://github.com/u14-team/henta/issues", diff --git a/packages/input/src/consts.ts b/packages/input/src/consts.ts new file mode 100644 index 0000000..87c29a8 --- /dev/null +++ b/packages/input/src/consts.ts @@ -0,0 +1,2 @@ +export const INPUT_ARGUMENTS_METADATA = 'input:arguments'; +export const INPUT_ATTACHMENTS_METADATA = 'input:attachments'; diff --git a/packages/input/src/decorators.ts b/packages/input/src/decorators.ts index 0aaa622..55292bb 100644 --- a/packages/input/src/decorators.ts +++ b/packages/input/src/decorators.ts @@ -7,8 +7,14 @@ import type { IArgumentRequest } from './arguments/interfaces.js'; import { StringParser } from './arguments/parsers.js'; import requireArguments from './arguments/processor.js'; import requireAttachments from './attachments/processor.js'; +import { applyDecorators } from '@henta/core'; +import AddFnMetadata from './decorators/add-fn-metadata.decorator.js'; +import { + INPUT_ARGUMENTS_METADATA, + INPUT_ATTACHMENTS_METADATA, +} from './consts.js'; -const inputRequestsMetadataKey = Symbol('input_requests'); +export const inputRequestsMetadataKey = Symbol('input_requests'); export interface IRequestContextItem { handler: (context: IRequestContext) => Promise; @@ -50,11 +56,17 @@ export function CustomRequest( } export function ArgumentRequest(params: Partial = {}) { - return CustomRequest(requireArguments, { + const options: IArgumentRequest = { parser: new StringParser(), isRequired: params.default === undefined, ...params, - } as IArgumentRequest); + }; + + return applyDecorators( + (target, key) => + AddFnMetadata(INPUT_ARGUMENTS_METADATA, options)(target[key]), + CustomRequest(requireArguments, options), + ); } /** @alias `ArgumentRequest({ parser: new StringParser({ toEnd: true }) });` */ @@ -83,7 +95,11 @@ export function AttachmentRequest( request.to = to; } - return CustomRequest(requireAttachments, request); + return applyDecorators( + (target, key) => + AddFnMetadata(INPUT_ATTACHMENTS_METADATA, request)(target[key]), + CustomRequest(requireAttachments, request), + ); } export async function requireInputArgs(fn: any, ctx, attachmentsHistory?) { diff --git a/packages/input/src/decorators/add-fn-metadata.decorator.ts b/packages/input/src/decorators/add-fn-metadata.decorator.ts new file mode 100644 index 0000000..52f775d --- /dev/null +++ b/packages/input/src/decorators/add-fn-metadata.decorator.ts @@ -0,0 +1,14 @@ +export default function AddFnMetadata( + metadataKey: K, + ...values: V[] +) { + return (target: object, propertyKey?: string, descriptor?) => { + const metadataTarget = descriptor ? descriptor.value : target; + const array = Reflect.getMetadata(metadataKey, metadataTarget) || []; + array.push(...values); + + Reflect.defineMetadata(metadataKey, array, metadataTarget); + + return metadataTarget; + }; +} diff --git a/packages/input/src/index.ts b/packages/input/src/index.ts index 1eebe83..71dbf07 100644 --- a/packages/input/src/index.ts +++ b/packages/input/src/index.ts @@ -13,5 +13,6 @@ export type { IFoundAttachment }; export * from './decorators.js'; export * from './arguments/parsers.js'; export * from './arguments/interfaces.js'; +export * from './consts.js'; export { ArgumentError };