getjerry / nest-casl

Casl integration for NestJS
MIT License
225 stars 29 forks source link

params are not typed on run method when implementing SubjectBeforeFilterHook #856

Closed Omar-V2 closed 8 months ago

Omar-V2 commented 9 months ago

Firstly, thanks for the nice library!

When we define a class which implements the SubjectBeforeFilterHook as outlined in the readme, the params object that is pulled from Request seems to have any typed which is not ideal. Is this the intended behaviour or am I doing something wrong here?

I also inspected the example code in this repo under: https://github.com/getjerry/nest-casl/blob/master/src/__specs__/app/post/post.hook.ts#L11 and noticed the same issue.

Is there a way to have this typed?

import { Injectable } from '@nestjs/common'
import type { Answer } from '@prisma/client/my-service'
import type { SubjectBeforeFilterHook, Request } from 'nest-casl'

import type { AnswerService } from './answer.service'

Injectable()
export class AnswerHook implements SubjectBeforeFilterHook<Answer, Request> {
  constructor(readonly answerService: AnswerService) {}

  async run({ params }: Request): Promise<Answer> {
    // params has any type.
    return this.answerService.get(params.someField)
  }
}

Thanks!

liquidautumn commented 8 months ago

@Omar-V2 you can define your own request interface with properly typed params, ie

interface CustomRequest extends Request {
  params: {
    id: string;
  };
}

and use it instead of Request as generic param and run argument type