Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/decorators/description.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Description', () => {
it('should decorate a class', () => {
@Description('This is a class')
class Foo {
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo)
Expand All @@ -19,7 +19,7 @@ describe('Description', () => {

class Foo {
@Description('This is a property')
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo, 'foo')
Expand Down
3 changes: 2 additions & 1 deletion src/lib/decorators/description.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Class } from '@agape/types';
import { MetadataDescriptor } from '../descriptors/metadata.descriptor';

Expand Down Expand Up @@ -57,7 +58,7 @@ import { MetadataDescriptor } from '../descriptors/metadata.descriptor';
*/
export function Description(description: string) {

function Description(target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number) {
function Description(target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number) {

const descriptor = index !== undefined && typeof index === "number"
? MetadataDescriptor.for(target, name, index)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/decorators/example.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Example', () => {
it('should decorate a class', () => {
@Example({ foo: 'fooooo!!!'})
class Foo {
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo)
Expand All @@ -19,7 +19,7 @@ describe('Example', () => {

class Foo {
@Example('fooooo!!!')
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo, 'foo')
Expand Down
4 changes: 2 additions & 2 deletions src/lib/decorators/example.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Class } from '@agape/types';
import { MetadataDescriptor } from '../descriptors/metadata.descriptor';

Expand Down Expand Up @@ -61,7 +61,7 @@ import { MetadataDescriptor } from '../descriptors/metadata.descriptor';
*/
export function Example(value: any): (target: object, name?: string, propertyDescriptor?: TypedPropertyDescriptor<any> | number) => void {

function Example(target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number) {
function Example(target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number) {
const descriptor = index !== undefined && typeof index === "number"
? MetadataDescriptor.for(target, name, index)
: MetadataDescriptor.for(target, name);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/decorators/label.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Label', () => {
it('should decorate a class', () => {
@Label('Foo')
class Foo {
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo)
Expand All @@ -19,7 +19,7 @@ describe('Label', () => {

class Foo {
@Label('Foo')
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo, 'foo')
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('Label', () => {
it('should set the plural', () => {
@Label('Foo', 'Foos')
class Foo {
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo)
Expand Down
7 changes: 4 additions & 3 deletions src/lib/decorators/label.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Class } from '@agape/types';
import { MetadataDescriptor } from '../descriptors/metadata.descriptor';

Expand Down Expand Up @@ -55,10 +56,10 @@ import { MetadataDescriptor } from '../descriptors/metadata.descriptor';
* @decoratorKind Metadata
* @decoratorPropertyType any
*/
export function Label(label: string | undefined, plural?: string): (target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number) => void
export function Label(...args: string[]) {
export function Label(label: string | undefined, plural?: string): (target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number) => void
export function Label(...args: any[]) {

function Label(target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number) {
function Label(target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number) {

const descriptor = index !== undefined && typeof index === "number"
? MetadataDescriptor.for(target, name, index)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/decorators/noun.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Noun', () => {
it('should decorate a class', () => {
@Noun('foo')
class Foo {
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo)
Expand All @@ -19,7 +19,7 @@ describe('Noun', () => {

class Foo {
@Noun('foo')
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo, 'foo')
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('Noun', () => {
it('should set the plural', () => {
@Noun('foo', 'foos')
class Foo {
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo)
Expand Down
7 changes: 4 additions & 3 deletions src/lib/decorators/noun.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Class } from '@agape/types';
import { MetadataDescriptor } from '../descriptors/metadata.descriptor';

Expand Down Expand Up @@ -54,10 +55,10 @@ import { MetadataDescriptor } from '../descriptors/metadata.descriptor';
* @decoratorKind Metadata
* @decoratorPropertyType any
*/
export function Noun(noun: string | undefined, plural?: string): (target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number) => void
export function Noun(...args: string[]) {
export function Noun(noun: string | undefined, plural?: string): (target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number) => void
export function Noun(...args: any[]) {

function Noun(target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number) {
function Noun(target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number) {

const descriptor = index !== undefined && typeof index === "number"
? MetadataDescriptor.for(target, name, index)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/decorators/sensitive.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Label', () => {
it('should decorate a class', () => {
@Sensitive
class Foo {
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo)
Expand All @@ -17,7 +17,7 @@ describe('Label', () => {
it('should set sensitive to false', () => {
@Sensitive(false)
class Foo {
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo)
Expand All @@ -28,7 +28,7 @@ describe('Label', () => {

class Foo {
@Sensitive
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo, 'foo')
Expand Down
15 changes: 8 additions & 7 deletions src/lib/decorators/sensitive.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Class } from '@agape/types';
import { MetadataDescriptor } from '../descriptors/metadata.descriptor';

Expand Down Expand Up @@ -65,21 +66,21 @@ import { MetadataDescriptor } from '../descriptors/metadata.descriptor';
* @decoratorKind Metadata
* @decoratorPropertyType any
*/
export function Sensitive(sensitive?: boolean): (target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number) => void
export function Sensitive(target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number): void
export function Sensitive(...args: never[] ): ((target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number) => void) | void {
export function Sensitive(sensitive?: boolean): (target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number) => void
export function Sensitive(target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number): void
export function Sensitive(...args: any[]): ((target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number) => void) | void {

let sensitive = true;

let target: object;
let name: string;
let indexOrPropertyDescriptor: TypedPropertyDescriptor<unknown> | number;
let target: object | Class | undefined;
let name: string | undefined;
let indexOrPropertyDescriptor: TypedPropertyDescriptor<any> | number | undefined;

if (args.length === 0) sensitive = true;
else if (args.length === 1 && typeof args[0] === 'boolean') sensitive = args[0];
else [target, name, indexOrPropertyDescriptor] = args, sensitive = true;

function Sensitive(target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number) {
function Sensitive(target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number) {

const descriptor = index !== undefined && typeof index === "number"
? MetadataDescriptor.for(target, name, index)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/decorators/token.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Token', () => {
it('should decorate a class', () => {
@Token('foo')
class Foo {
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo)
Expand All @@ -19,7 +19,7 @@ describe('Token', () => {

class Foo {
@Token('foo')
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo, 'foo')
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('Token', () => {
it('should set the plural', () => {
@Token('foo', 'foos')
class Foo {
foo: string;
foo!: string;
}

const d = MetadataDescriptor.for(Foo)
Expand Down
7 changes: 4 additions & 3 deletions src/lib/decorators/token.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Class } from '@agape/types';
import { MetadataDescriptor } from '../descriptors/metadata.descriptor';

Expand Down Expand Up @@ -57,10 +58,10 @@ import { MetadataDescriptor } from '../descriptors/metadata.descriptor';
* @decoratorKind Metadata
* @decoratorPropertyType any
*/
export function Token(token: string | undefined, plural?: string): (target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number) => void
export function Token(...args: string[]) {
export function Token(token: string | undefined, plural?: string): (target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number) => void
export function Token(...args: any[]) {

function Token(target: object | Class, name?: string, index?: TypedPropertyDescriptor<unknown> | number) {
function Token(target: object | Class, name?: string, index?: TypedPropertyDescriptor<any> | number) {

const descriptor = index !== undefined && typeof index === "number"
? MetadataDescriptor.for(target, name, index)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/descriptors/metadata.descriptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MetadataDescriptor } from './metadata.descriptor';

describe('MetadataDescriptor', () => {

let d: MetadataDescriptor;
let d: MetadataDescriptor | undefined;

beforeEach(() => {
d = undefined;
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('MetadataDescriptor', () => {
})
it('should get the descriptor for a property', () => {
class Foo {
foo: string;
foo!: string;
}
d = MetadataDescriptor.for(Foo.prototype, 'foo')

Expand All @@ -61,7 +61,7 @@ describe('MetadataDescriptor', () => {
})
it('should get the descriptor for a parameter', () => {
class Foo {
foo: string;
foo!: string;
}
d = MetadataDescriptor.for(Foo.prototype, 'foo', 0)

Expand Down
11 changes: 9 additions & 2 deletions src/lib/descriptors/metadata.descriptor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Class } from '@agape/types';
import 'reflect-metadata';

Expand All @@ -22,11 +23,11 @@ export class MetadataDescriptor {

description?: string;

example?: unknown;
example?: any;

static for(target: Class | object, property?: string, index?: number): MetadataDescriptor {
const prototype = typeof target === 'function' ? target.prototype : target;
const token = index !== undefined ? property : `${property}:${index}`;
const token = index !== undefined ? `${property}:${index}` : property || '';

let descriptor = Reflect.getMetadata('agape:metadata', prototype, token);
if (descriptor) return descriptor;
Expand All @@ -36,4 +37,10 @@ export class MetadataDescriptor {

return descriptor;
}

static get(target: Class | object, property?: string, index?: number): MetadataDescriptor | undefined {
const prototype = typeof target === 'function' ? target.prototype : target;
const token = index !== undefined ? `${property}:${index}` : property || '';
return Reflect.getMetadata('agape:metadata', prototype, token);
}
}
2 changes: 1 addition & 1 deletion src/lib/functions/description.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('description', () => {
it('should work with properties', () => {
class Foo {
@Description('description')
foo: string;
foo!: string;
}

expect(description(Foo, 'foo')).toBe('description')
Expand Down
36 changes: 34 additions & 2 deletions src/lib/functions/description.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
import { Class } from '@agape/types';
import { MetadataDescriptor } from '../descriptors/metadata.descriptor';

export function description(target: Class, property?: string, index?: number) {
return MetadataDescriptor.for(target, property, index).description;
/**
* Retrieves the description metadata associated with a class, property, or method parameter.
*
* This function is used to fetch the `<property>description` value stored via metadata for the given target.
* It supports metadata attached at the class level, property level, or parameter level depending
* on which arguments are provided.
*
* ## Usage
*
* ### Class Level Description
*
* ```ts
* const desc = description(MyClass);
* ```
*
* ### Property Level Description
*
* ```ts
* const desc = description(MyClass, 'title');
* ```
*
* ### Parameter Level Description
*
* ```ts
* const desc = description(MyClass, 'setTitle', 0);
* ```
*
* @param target - The target class constructor to retrieve metadata from.
* @param property - The name of the property or method to retrieve metadata for.
* @param index - The index of the parameter if retrieving metadata for a method parameter.
* @returns The description metadata if found; otherwise, `undefined`.
*/
export function description(target: Class, property?: string, index?: number): string | undefined {
return MetadataDescriptor.get(target, property, index)?.description;
}
2 changes: 1 addition & 1 deletion src/lib/functions/example.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('sensitive', () => {
it('should work with properties', () => {
class Foo {
@Sensitive
foo: string;
foo!: string;
}

expect(sensitive(Foo, 'foo')).toBe(true);
Expand Down
Loading