@nestjs/common#SetMetadata TypeScript Examples

The following examples show how to use @nestjs/common#SetMetadata. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: processor.decorator.ts    From nestjs-bullmq with MIT License 7 votes vote down vote up
export function Processor(
  queueNameOrOptions?: string | ProcessorOptions,
): ClassDecorator {
  const options =
    queueNameOrOptions && typeof queueNameOrOptions === 'object'
      ? queueNameOrOptions
      : { name: queueNameOrOptions };

  // eslint-disable-next-line @typescript-eslint/ban-types
  return (target: Function) => {
    SetMetadata(SCOPE_OPTIONS_METADATA, options)(target);
    SetMetadata(BULL_MODULE_QUEUE, options)(target);
  };
}
Example #2
Source File: listen.decorator.ts    From nest-amqp with MIT License 6 votes vote down vote up
Listen: ListenOverload = <T>(
  source: string | Source,
  optionsOrConnection?: ListenOptions<T> | string,
  connectionName?: string,
): MethodDecorator => {
  return (target: Record<string, any>, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
    const connection = connectionName ?? (typeof optionsOrConnection === 'string' ? optionsOrConnection : AMQP_DEFAULT_CONNECTION_TOKEN);
    const options = typeof optionsOrConnection === 'object' ? optionsOrConnection : {};

    const metadata = new ListenerMetadata<T>({
      source,
      options,
      connection,
      targetName: target.constructor.name,
      target: target.constructor,
      callback: descriptor.value,
      callbackName: typeof propertyKey === 'string' ? propertyKey : propertyKey.toString(),
    });

    SetMetadata<string, ListenerMetadata<T>>(QUEUE_LISTEN_METADATA_KEY, metadata)(target, propertyKey, descriptor);
  };
}
Example #3
Source File: mqtt.decorator.ts    From nest-mqtt with MIT License 6 votes vote down vote up
export function Subscribe(topicOrOptions): CustomDecorator {
  if (typeof topicOrOptions === 'string' || Array.isArray(topicOrOptions)) {
    return SetMetadata(MQTT_SUBSCRIBE_OPTIONS, {
      topic: topicOrOptions,
    });
  } else {
    return SetMetadata(MQTT_SUBSCRIBE_OPTIONS, topicOrOptions);
  }
}
Example #4
Source File: queue-hooks.decorators.ts    From nestjs-bullmq with MIT License 6 votes vote down vote up
OnGlobalQueueEvent = (
  eventNameOrOptions: BullQueueGlobalEvents | BullQueueEventOptions,
): MethodDecorator =>
  SetMetadata(
    BULL_MODULE_ON_GLOBAL_QUEUE_EVENT,
    typeof eventNameOrOptions === 'string'
      ? { eventName: eventNameOrOptions }
      : eventNameOrOptions,
  )
Example #5
Source File: hook.decorator.ts    From nestjs-mercurius with MIT License 6 votes vote down vote up
export function GraphQLHook<T extends HookName>(hookName: T) {
  return (
    target: Function | Record<string, any>,
    key: any,
    descriptor?: TypedPropertyDescriptor<HookMap[T]>,
  ) => {
    SetMetadata(HOOK_METADATA, hookName)(target, key, descriptor);
  };
}
Example #6
Source File: public.decorator.ts    From nest-keycloak-connect with MIT License 5 votes vote down vote up
Public = (skipAuth = true) =>
  applyDecorators(
    SetMetadata(META_UNPROTECTED, true),
    SetMetadata(META_SKIP_AUTH, skipAuth),
  )
Example #7
Source File: use-ability.ts    From nest-casl with MIT License 5 votes vote down vote up
export function UseAbility<Subject = AnyObject, Request = AuthorizableRequest>(
  action: string,
  subject: AnyClass<Subject>,
  subjectHook?: AnyClass<SubjectBeforeFilterHook<Subject, Request>> | SubjectBeforeFilterTuple<Subject, Request>,
): CustomDecorator {
  return SetMetadata(CASL_META_ABILITY, { action, subject, subjectHook });
}
Example #8
Source File: has-roles.decorator.ts    From nestjs-rest-sample with GNU General Public License v3.0 5 votes vote down vote up
HasRoles = (...args: RoleType[]) => SetMetadata(HAS_ROLES_KEY, args)
Example #9
Source File: roles.decorator.ts    From pandaid with MIT License 5 votes vote down vote up
Roles = (...roles: string[]) => SetMetadata('roles', roles)
Example #10
Source File: resolve-loader.decorator.ts    From nestjs-mercurius with MIT License 5 votes vote down vote up
export function ResolveLoader(
  propertyNameOrFunc?: string | ReturnTypeFunc,
  typeFuncOrOptions?: ReturnTypeFunc | ResolveLoaderOptions,
  resolveFieldOptions?: ResolveLoaderOptions,
): MethodDecorator {
  return (
    target: Function | Record<string, any>,
    key: any,
    descriptor?: any,
  ) => {
    // eslint-disable-next-line prefer-const
    let [propertyName, typeFunc, options] = isFunction(propertyNameOrFunc)
      ? typeFuncOrOptions && typeFuncOrOptions.name
        ? [typeFuncOrOptions.name, propertyNameOrFunc, typeFuncOrOptions]
        : [undefined, propertyNameOrFunc, typeFuncOrOptions]
      : [propertyNameOrFunc, typeFuncOrOptions, resolveFieldOptions];
    SetMetadata(LOADER_NAME_METADATA, propertyName)(target, key, descriptor);
    SetMetadata(LOADER_PROPERTY_METADATA, true)(target, key, descriptor);

    SetMetadata(
      FIELD_RESOLVER_MIDDLEWARE_METADATA,
      (options as ResolveLoaderOptions)?.middleware,
    )(target, key, descriptor);

    options = isObject(options)
      ? {
          name: propertyName as string,
          ...options,
        }
      : propertyName
      ? { name: propertyName as string }
      : {};

    LazyMetadataStorage.store(
      target.constructor as Type<unknown>,
      function resolveLoader() {
        let typeOptions: TypeOptions, typeFn: (type?: any) => GqlTypeReference;
        try {
          const implicitTypeMetadata = reflectTypeFromMetadata({
            metadataKey: 'design:returntype',
            prototype: target,
            propertyKey: key,
            explicitTypeFn: typeFunc as ReturnTypeFunc,
            typeOptions: options as any,
          });
          typeOptions = implicitTypeMetadata.options;
          typeFn = implicitTypeMetadata.typeFn;
        } catch {}

        TypeMetadataStorage.addResolverPropertyMetadata({
          kind: 'external',
          methodName: key,
          schemaName: options.name || key,
          target: target.constructor,
          typeFn,
          typeOptions,
          description: (options as ResolveLoaderOptions).description,
          deprecationReason: (options as ResolveLoaderOptions)
            .deprecationReason,
          complexity: (options as ResolveLoaderOptions).complexity,
        });
      },
    );
  };
}
Example #11
Source File: role.decorator.ts    From nestjs-starter-rest-api with MIT License 5 votes vote down vote up
Roles = (...roles: ROLE[]): CustomDecorator<string> =>
  SetMetadata(ROLES_KEY, roles)
Example #12
Source File: roles.decorator.ts    From api with GNU Affero General Public License v3.0 5 votes vote down vote up
Roles = (...roles: rolesType) => SetMetadata('roles', roles)
Example #13
Source File: roles.decorator.ts    From bank-server with MIT License 5 votes vote down vote up
Roles = (...roles: RoleType[]) => SetMetadata('roles', roles)
Example #14
Source File: roles.decorator.ts    From MyAPI with MIT License 5 votes vote down vote up
Roles = (...roles: DefaultRole[]): CustomDecorator<string> => {
  return SetMetadata('roles', roles)
}
Example #15
Source File: HttpAuth.ts    From typescript-clean-architecture with MIT License 5 votes vote down vote up
HttpAuth = (...roles: UserRole[]): (...args: any) => void => {
  return applyDecorators(
    SetMetadata('roles', roles),
    UseGuards(HttpJwtAuthGuard, HttpRoleAuthGuard)
  );
}
Example #16
Source File: audit-log.decorator.ts    From radiopanel with GNU General Public License v3.0 5 votes vote down vote up
AuditLog = (eventName: string) => SetMetadata('auditLog', eventName)
Example #17
Source File: roles.decorator.ts    From office-hours with GNU General Public License v3.0 5 votes vote down vote up
Roles = (...roles: string[]): CustomDecorator<string> =>
  SetMetadata('roles', roles)
Example #18
Source File: roles.decorators.ts    From nestjs-angular-starter with MIT License 5 votes vote down vote up
Roles = (...roles: string[]) => SetMetadata('roles', roles)
Example #19
Source File: sqs.decorators.ts    From nestjs-sqs with MIT License 5 votes vote down vote up
SqsMessageHandler = (name: string, batch?: boolean) => SetMetadata(SQS_CONSUMER_METHOD, { name, batch })
Example #20
Source File: scope.decorator.ts    From nestjs-keycloak-admin with MIT License 5 votes vote down vote up
DefineScope = (scope: string): CustomDecorator<string> =>
  SetMetadata<string, string>(META_SCOPE, scope)
Example #21
Source File: roles.decorator.ts    From nest-js-boilerplate with MIT License 5 votes vote down vote up
Roles = (...roles: RolesEnum[]) => SetMetadata('roles', roles)
Example #22
Source File: public.decorator.ts    From amplication with Apache License 2.0 5 votes vote down vote up
PublicAuthMiddleware = SetMetadata(IS_PUBLIC_KEY, true)
Example #23
Source File: fetch.resources.decorator.ts    From nestjs-keycloak-admin with MIT License 5 votes vote down vote up
FetchResources = (): CustomDecorator<string> =>
  SetMetadata<string, boolean>(META_FETCH_RESOURCES, true)
Example #24
Source File: public.decorator.ts    From nest-keycloak-connect with MIT License 5 votes vote down vote up
Unprotected = (skipAuth = true) =>
  applyDecorators(
    SetMetadata(META_UNPROTECTED, true),
    SetMetadata(META_SKIP_AUTH, skipAuth),
  )
Example #25
Source File: auth.decorator.ts    From aqualink-app with MIT License 5 votes vote down vote up
Auth = (...levels: AdminLevel[]) => {
  return applyDecorators(
    SetMetadata('levels', levels),
    UseGuards(FirebaseAuthGuard, LevelsGuard),
  );
}
Example #26
Source File: set-recaptcha-options.ts    From google-recaptcha with MIT License 5 votes vote down vote up
export function SetRecaptchaOptions(options?: VerifyResponseDecoratorOptions): MethodDecorator & ClassDecorator {
    return SetMetadata(RECAPTCHA_VALIDATION_OPTIONS, options);
}
Example #27
Source File: roles.decorator.ts    From codeclannigeria-backend with MIT License 5 votes vote down vote up
Roles = (...roles: UserRole[]): CustomDecorator<typeof UserRole> =>
  SetMetadata(UserRole, roles)
Example #28
Source File: form-data.ts    From nestjs-form-data with MIT License 5 votes vote down vote up
export function FormDataRequest(config?: FormDataInterceptorConfig) {
  
  return applyDecorators(
    SetMetadata(FORM_DATA_REQUEST_METADATA_KEY, config),
    UseInterceptors(FormDataInterceptor),
  );

}
Example #29
Source File: except-tracing.decorator.ts    From nestjs-jaeger-tracing with MIT License 5 votes vote down vote up
export function ExceptTracing(): MethodDecorator {
  return applyDecorators(SetMetadata(EXCEPT_TRACING, true));
}