@@ -3,7 +3,9 @@ import { Logger } from '@rocket.chat/logger';
33import { Users } from '@rocket.chat/models' ;
44import { Random } from '@rocket.chat/random' ;
55import type { JoinPathPattern , Method } from '@rocket.chat/rest-typings' ;
6+ import { ajv } from '@rocket.chat/rest-typings/src/v1/Ajv' ;
67import { tracerSpan } from '@rocket.chat/tracing' ;
8+ import type { ValidateFunction } from 'ajv' ;
79import { Accounts } from 'meteor/accounts-base' ;
810import { DDP } from 'meteor/ddp' ;
911import { DDPCommon } from 'meteor/ddp-common' ;
@@ -493,51 +495,119 @@ export class APIClass<
493495 return routeActions . map ( ( action ) => this . getFullRouteName ( route , action , apiVersion ) ) ;
494496 }
495497
496- private method < TSubPathPattern extends string , TOptions extends TypedOptions , TPathPattern extends `${ TBasePath } /${ TSubPathPattern } ` > (
498+ private registerTypedRoutesLegacy < TSubPathPattern extends string , TOptions extends Options > (
497499 method : Method ,
498500 subpath : TSubPathPattern ,
499501 options : TOptions ,
500- action : TypedAction < TOptions > ,
501- ) : APIClass <
502- TBasePath ,
503- | TOperations
504- | ( {
505- method : Method ;
506- path : TPathPattern ;
507- } & Omit < TOptions , 'response' > )
508- > {
509- const path = `${ this . _config . apiPath } /${ subpath } ` . replace ( '//' , '/' ) as TPathPattern ;
510- this . addRoute ( [ subpath ] , { ...options } , { [ method . toLowerCase ( ) ] : { action } } as any ) ;
502+ ) : void {
503+ const { authRequired, validateParams } = options ;
504+
505+ const opt = {
506+ authRequired,
507+ ...( validateParams &&
508+ method . toLowerCase ( ) === 'get' &&
509+ ( 'GET' in validateParams
510+ ? { query : validateParams . GET }
511+ : {
512+ query : validateParams as ValidateFunction < any > ,
513+ } ) ) ,
514+
515+ ...( validateParams &&
516+ method . toLowerCase ( ) === 'post' &&
517+ ( 'POST' in validateParams ? { query : validateParams . POST } : { body : validateParams as ValidateFunction < any > } ) ) ,
518+
519+ ...( validateParams &&
520+ method . toLowerCase ( ) === 'put' &&
521+ ( 'PUT' in validateParams ? { query : validateParams . PUT } : { body : validateParams as ValidateFunction < any > } ) ) ,
522+ ...( validateParams &&
523+ method . toLowerCase ( ) === 'delete' &&
524+ ( 'DELETE' in validateParams ? { query : validateParams . DELETE } : { body : validateParams as ValidateFunction < any > } ) ) ,
525+
526+ tags : [ 'Missing Documentation' ] ,
527+ response : {
528+ 200 : ajv . compile ( {
529+ type : 'object' ,
530+ properties : {
531+ success : { type : 'boolean' } ,
532+ error : { type : 'string' } ,
533+ } ,
534+ required : [ 'success' ] ,
535+ } ) ,
536+ } ,
537+ } ;
538+
539+ this . registerTypedRoutes ( method , subpath , opt ) ;
540+ }
541+
542+ private registerTypedRoutes <
543+ TSubPathPattern extends string ,
544+ TOptions extends TypedOptions ,
545+ TPathPattern extends `${TBasePath } /${TSubPathPattern } `,
546+ > ( method : Method , subpath : TSubPathPattern , options : TOptions ) : void {
547+ const path = `/${ this . _config . apiPath } /${ subpath } ` . replaceAll ( '//' , '/' ) as TPathPattern ;
511548 this . typedRoutes = this . typedRoutes || { } ;
512549 this . typedRoutes [ path ] = this . typedRoutes [ subpath ] || { } ;
513- const { query, authRequired, response, body, ...rest } = options ;
550+ const { query, authRequired, response, body, tags , ...rest } = options ;
514551 this . typedRoutes [ path ] [ method . toLowerCase ( ) ] = {
515- ...rest ,
516552 ...( response && {
517- responses : {
518- ...Object . fromEntries (
519- Object . entries ( response ) . map ( ( [ status , schema ] ) => [
520- status ,
521- {
522- content : {
523- 'application/json' : 'schema' in schema ? { schema : schema . schema } : schema ,
524- } ,
553+ responses : Object . fromEntries (
554+ Object . entries ( response ) . map ( ( [ status , schema ] ) => [
555+ status ,
556+ {
557+ description : '' ,
558+ content : {
559+ 'application/json' : 'schema' in schema ? { schema : schema . schema } : schema ,
525560 } ,
526- ] ) ,
527- ) ,
561+ } ,
562+ ] ) ,
563+ ) ,
564+ } ) ,
565+ ...( query && {
566+ parameters : [
567+ {
568+ schema : query . schema ,
569+ in : 'query' ,
570+ name : 'query' ,
571+ required : true ,
572+ } ,
573+ ] ,
574+ } ) ,
575+ ...( body && {
576+ requestBody : {
577+ required : true ,
578+ content : {
579+ 'application/json' : { schema : body . schema } ,
580+ } ,
528581 } ,
529582 } ) ,
530- ...( query && { query : query . schema } ) ,
531- ...( body && { body : body . schema } ) ,
532583 ...( authRequired && {
584+ ...rest ,
533585 security : [
534586 {
535587 userId : [ ] ,
536588 authToken : [ ] ,
537589 } ,
538590 ] ,
539591 } ) ,
592+ tags,
540593 } ;
594+ }
595+
596+ private method < TSubPathPattern extends string , TOptions extends TypedOptions , TPathPattern extends `${TBasePath } /${TSubPathPattern } `> (
597+ method : Method ,
598+ subpath : TSubPathPattern ,
599+ options : TOptions ,
600+ action : TypedAction < TOptions > ,
601+ ) : APIClass <
602+ TBasePath ,
603+ | TOperations
604+ | ( {
605+ method : Method ;
606+ path : TPathPattern ;
607+ } & Omit < TOptions , 'response' > )
608+ > {
609+ this . addRoute ( [ subpath ] , { ...options } , { [ method . toLowerCase ( ) ] : { action } } as any ) ;
610+ this . registerTypedRoutes ( method , subpath , options ) ;
541611 return this ;
542612 }
543613
@@ -869,6 +939,11 @@ export class APIClass<
869939
870940 // Allow the endpoints to make usage of the logger which respects the user's settings
871941 ( operations [ method as keyof Operations < TPathPattern , TOptions > ] as Record < string , any > ) . logger = logger ;
942+
943+ this . registerTypedRoutesLegacy ( method as Method , route , {
944+ ...options ,
945+ ...operations [ method as keyof Operations < TPathPattern , TOptions > ] ,
946+ } ) ;
872947 } ) ;
873948 super . addRoute ( route , options , operations ) ;
874949 } ) ;
0 commit comments