2

I've learned that idempotent requests must return the same response given that the same request sent. But not really sure how to implement this in Nestjs Architecture ? If resource has been already created and duplicate request was recognized, then this resource should be returned. I doubt the logic must be in the service level...

I've tried to use interceptors, but as far as I know they cannot but handle request. I mean, I cannot do something like "Hey, I know this request, so return this stored data and do not handle it twice".

But ok, there're middlewares for that. I can send response before main handling request. But it this case, I cannot cache/store the response of the request... Just because with middlewares I cannot have access to response object.

Any ideas?

Denys Rybkin
  • 637
  • 1
  • 6
  • 18

1 Answers1

0

by definition idempotent HTTP method (as a result from a request is

An HTTP method is idempotent if an identical request can be made once or several times in a row with the same effect while leaving the server in the same state.

so caching is not exactly what makes the method handler idempotent but rather the actions made by it.

if you want to cache the results of a request you can use nestjs built-it CacheInterceptor. use it either globally, per controller or even per method handler (same as every interceptor).
first import the caching module to your needed module (or root module and make it global) CacheModule.register({ isGlobal: true, }) and then decorate your controller/method handler with it @UseInterceptor(CacheInterceptor).

using the interceptor globally can be done in several ways but the mot straight forward is the simple app.useGlobalInterceptors(new CacheInterceptor())

Note: it only caches GET requests out-of-the-box so if you need other you can extend it to add your logic

Hagai Kalinhoff
  • 606
  • 3
  • 10
  • You're right that caching is not good approach. But what another options? I have many typeorm entities and each of them has status field that will change (and fields like updatedAt also). I don't know how to prevent creating extra entity without caching and returning cached result. Of course, it's possible put all this logic within my `createEntity` method where the business logic is -- but it's awful, don't you think? How would you implement that? – Denys Rybkin Jan 28 '22 at 07:53
  • thats a question of what you are trying to achieve? if you wish to boost your request performance since there are many identical requests (lets say tens of requests to fetch some resource) than caching is a good approach, of-course you can change the cache TTL according with your needs. if you want multiple POST (create) requests would always return the same entity then yeah, have that verified in your business logic. for example i have a resource without any uniqueness constrains (same values for columns) so every request i create a new one and dont need to return an already existing one – Hagai Kalinhoff Jan 28 '22 at 19:36