import { RouteParamtypes } from '@nestjs/common/enums/route-paramtypes.enum' ; import { CUSTOM_ROUTE_AGRS_METADATA , INTERCEPTORS_METADATA , METHOD_METADATA , PARAMTYPES_METADATA , PATH_METADATA , ROUTE_ARGS_METADATA , } from '@nestjs/common/constants' ; import { ArgumentsHost } from '@nestjs/common' ; import { isFunction } from '@nestjsx/util' ; import { BaseRoute , MergedCrudOptions , AuthOptions } from '../interfaces' ; import { BaseRouteName } from '../types' ; import { CRUD_OPTIONS_METADATA , ACTION_NAME_METADATA , PARSED_CRUD_REQUEST_KEY , PARSED_BODY_METADATA , OVERRIDE_METHOD_METADATA , CRUD_AUTH_OPTIONS_METADATA , } from '../constants' ; import { CrudActions } from '../enums' ; export class R { static set ( metadataKey , metadataValue , target , propertyKey = undefined ) { if ( propertyKey ) { Reflect . defineMetadata ( metadataKey , metadataValue , target , propertyKey ) ; } else { Reflect . defineMetadata ( metadataKey , metadataValue , target ) ; } } static get < T extends any > ( metadataKey , target , propertyKey = undefined ) { return propertyKey ? Reflect . getMetadata ( metadataKey , target , propertyKey ) : Reflect . getMetadata ( metadataKey , target ) ; } static createCustomRouteArg ( paramtype , index , pipes = [ ] , data = undefined ) { return { [ `${ paramtype }${ CUSTOM_ROUTE_AGRS_METADATA }:${ index }` ] : { index , factory : ( _ , ctx ) => R . getContextRequest ( ctx ) [ paramtype ] , data , pipes , } , } ; } static createRouteArg ( paramtype , index , pipes = [ ] , data = undefined ) { return { [ `${ paramtype }:${ index }` ] : { index , pipes , data , } , } ; } static setDecorators ( decorators , target , name ) { const decoratedDescriptor = Reflect . decorate ( decorators , target , name , Reflect . getOwnPropertyDescriptor ( target , name ) ) ; Reflect . defineProperty ( target , name , decoratedDescriptor ) ; } static setParsedRequestArg ( index ) { return R . createCustomRouteArg ( PARSED_CRUD_REQUEST_KEY , index ) ; } static setBodyArg ( index , pipes = [ ] ) { return R . createRouteArg ( RouteParamtypes . BODY , index , pipes ) ; } static setCrudOptions ( options , target ) { R . set ( CRUD_OPTIONS_METADATA , options , target ) ; } static setRoute ( route , func ) { R . set ( PATH_METADATA , route . path , func ) ; R . set ( METHOD_METADATA , route . method , func ) ; } static setInterceptors ( interceptors , func ) { R . set ( INTERCEPTORS_METADATA , interceptors , func ) ; } static setRouteArgs ( metadata , target , name ) { R . set ( ROUTE_ARGS_METADATA , metadata , target , name ) ; } static setRouteArgsTypes ( metadata , target , name ) { R . set ( PARAMTYPES_METADATA , metadata , target , name ) ; } static setAction ( action , func ) { R . set ( ACTION_NAME_METADATA , action , func ) ; } static setCrudAuthOptions ( metadata , target ) { R . set ( CRUD_AUTH_OPTIONS_METADATA , metadata , target ) ; } static getCrudAuthOptions ( target ) { return R . get ( CRUD_AUTH_OPTIONS_METADATA , target ) ; } static getCrudOptions ( target ) { return R . get ( CRUD_OPTIONS_METADATA , target ) ; } static getAction ( func ) { return R . get ( ACTION_NAME_METADATA , func ) ; } static getOverrideRoute ( func ) { return R . get ( OVERRIDE_METHOD_METADATA , func ) ; } static getInterceptors ( func ) { return R . get ( INTERCEPTORS_METADATA , func ) || [ ] ; } static getRouteArgs ( target , name ) { return R . get ( ROUTE_ARGS_METADATA , target , name ) ; } static getRouteArgsTypes ( target , name ) { return R . get ( PARAMTYPES_METADATA , target , name ) || [ ] ; } static getParsedBody ( func ) { return R . get ( PARSED_BODY_METADATA , func ) ; } static getContextRequest ( ctx ) { return isFunction ( ctx . switchToHttp ) ? ctx . switchToHttp ( ) . getRequest ( ) : ctx ; } }