UNPKG

4.92 kBTypeScriptView Raw
1import { CustomDecorator, Type } from '@nestjs/common';
2/**
3 * @publicApi
4 */
5export interface CreateDecoratorOptions<TParam = any, TTransformed = TParam> {
6 /**
7 * The key for the metadata.
8 * @default uid(21)
9 */
10 key?: string;
11 /**
12 * The transform function to apply to the metadata value.
13 * @default value => value
14 */
15 transform?: (value: TParam) => TTransformed;
16}
17type CreateDecoratorWithTransformOptions<TParam, TTransformed = TParam> = CreateDecoratorOptions<TParam, TTransformed> & Required<Pick<CreateDecoratorOptions<TParam, TTransformed>, 'transform'>>;
18/**
19 * @publicApi
20 */
21export type ReflectableDecorator<TParam, TTransformed = TParam> = ((opts?: TParam) => CustomDecorator) & {
22 KEY: string;
23};
24/**
25 * Helper class providing Nest reflection capabilities.
26 *
27 * @see [Reflection](https://docs.nestjs.com/guards#putting-it-all-together)
28 *
29 * @publicApi
30 */
31export declare class Reflector {
32 /**
33 * Creates a decorator that can be used to decorate classes and methods with metadata.
34 * Can be used as a strongly-typed alternative to `@SetMetadata`.
35 * @param options Decorator options.
36 * @returns A decorator function.
37 */
38 static createDecorator<TParam>(options?: CreateDecoratorOptions<TParam>): ReflectableDecorator<TParam>;
39 static createDecorator<TParam, TTransformed>(options: CreateDecoratorWithTransformOptions<TParam, TTransformed>): ReflectableDecorator<TParam, TTransformed>;
40 /**
41 * Retrieve metadata for a reflectable decorator for a specified target.
42 *
43 * @example
44 * `const roles = this.reflector.get(Roles, context.getHandler());`
45 *
46 * @param decorator reflectable decorator created through `Reflector.createDecorator`
47 * @param target context (decorated object) to retrieve metadata from
48 *
49 */
50 get<T extends ReflectableDecorator<any>>(decorator: T, target: Type<any> | Function): T extends ReflectableDecorator<any, infer R> ? R : unknown;
51 /**
52 * Retrieve metadata for a specified key for a specified target.
53 *
54 * @example
55 * `const roles = this.reflector.get<string[]>('roles', context.getHandler());`
56 *
57 * @param metadataKey lookup key for metadata to retrieve
58 * @param target context (decorated object) to retrieve metadata from
59 *
60 */
61 get<TResult = any, TKey = any>(metadataKey: TKey, target: Type<any> | Function): TResult;
62 /**
63 * Retrieve metadata for a specified decorator for a specified set of targets.
64 *
65 * @param decorator lookup decorator for metadata to retrieve
66 * @param targets context (decorated objects) to retrieve metadata from
67 *
68 */
69 getAll<T extends ReflectableDecorator<any>>(decorator: T, targets: (Type<any> | Function)[]): T extends ReflectableDecorator<infer R> ? R extends Array<any> ? R : R[] : unknown;
70 /**
71 * Retrieve metadata for a specified key for a specified set of targets.
72 *
73 * @param metadataKey lookup key for metadata to retrieve
74 * @param targets context (decorated objects) to retrieve metadata from
75 *
76 */
77 getAll<TResult extends any[] = any[], TKey = any>(metadataKey: TKey, targets: (Type<any> | Function)[]): TResult;
78 /**
79 * Retrieve metadata for a specified decorator for a specified set of targets and merge results.
80 *
81 * @param decorator lookup decorator for metadata to retrieve
82 * @param targets context (decorated objects) to retrieve metadata from
83 *
84 */
85 getAllAndMerge<T extends ReflectableDecorator<any>>(decorator: T, targets: (Type<any> | Function)[]): T extends ReflectableDecorator<infer R> ? R : unknown;
86 /**
87 * Retrieve metadata for a specified key for a specified set of targets and merge results.
88 *
89 * @param metadataKey lookup key for metadata to retrieve
90 * @param targets context (decorated objects) to retrieve metadata from
91 *
92 */
93 getAllAndMerge<TResult extends any[] | object = any[], TKey = any>(metadataKey: TKey, targets: (Type<any> | Function)[]): TResult;
94 /**
95 * Retrieve metadata for a specified decorator for a specified set of targets and return a first not undefined value.
96 *
97 * @param decorator lookup decorator for metadata to retrieve
98 * @param targets context (decorated objects) to retrieve metadata from
99 *
100 */
101 getAllAndOverride<T extends ReflectableDecorator<any>>(decorator: T, targets: (Type<any> | Function)[]): T extends ReflectableDecorator<infer R> ? R : unknown;
102 /**
103 * Retrieve metadata for a specified key for a specified set of targets and return a first not undefined value.
104 *
105 * @param metadataKey lookup key for metadata to retrieve
106 * @param targets context (decorated objects) to retrieve metadata from
107 *
108 */
109 getAllAndOverride<TResult = any, TKey = any>(metadataKey: TKey, targets: (Type<any> | Function)[]): TResult;
110}
111export {};
112
\No newline at end of file