ts-rest / ts-rest

RPC-like client, contract, and server implementation for a pure REST API
https://ts-rest.com
MIT License
2.11k stars 91 forks source link

Using cache-manager's auto-caching responses in Nest results in empty cache content. #608

Open Codfisher opened 3 weeks ago

Codfisher commented 3 weeks ago

Describe the bug

When using CacheInterceptor, the response data of the first request is normal, but the response data of the second request becomes empty.

How to reproduce

The usage follows the documentation as described: https://docs.nestjs.com/techniques/caching#auto-caching-responses

Expected behavior

Cache content should not be empty.

Code reproduction

@UseInterceptors(CacheInterceptor)
@Controller()
export class SingleDataController {
  constructor(
    ...
  ) { }

  @TsRestHandler(singleDataContract.get, {
    validateResponses: true
  })
  async get() {
    return tsRestHandler(singleDataContract.get, async () => {
      const [error, data] = await to(this.singleDataService.get());
      if (error) {
        throw new HttpException(
          'error message',
          HttpStatus.INTERNAL_SERVER_ERROR,
        );
      }

      return {
        status: 200,
        body: data.toJSON(),
      };
    });
  }
}

ts-rest version

3.30.4