Skip to content

Commit

Permalink
feat: input args metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEVolk committed May 17, 2024
1 parent ef72a18 commit 66cdc2c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/util/reflector.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
};
}
2 changes: 1 addition & 1 deletion packages/input/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/input/src/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const INPUT_ARGUMENTS_METADATA = 'input:arguments';
export const INPUT_ATTACHMENTS_METADATA = 'input:attachments';
24 changes: 20 additions & 4 deletions packages/input/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = any> {
handler: (context: IRequestContext) => Promise<unknown[]>;
Expand Down Expand Up @@ -50,11 +56,17 @@ export function CustomRequest(
}

export function ArgumentRequest(params: Partial<IArgumentRequest> = {}) {
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 }) });` */
Expand Down Expand Up @@ -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?) {
Expand Down
14 changes: 14 additions & 0 deletions packages/input/src/decorators/add-fn-metadata.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default function AddFnMetadata<K = string, V = any>(
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;
};
}
1 change: 1 addition & 0 deletions packages/input/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit 66cdc2c

Please sign in to comment.