The developer writes a server-side idempotency interceptor in NestJS using TypeScript. The process involves creating a new file, implementing the intercept method to handle HTTP requests, injecting a store, and using RxJS operators to manage request life cycles, logging, and error handling to ensure consistent execution.
Length 20:20
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #nestjs #idempotency #interceptor #typescript #coding #web development #api #server-side #rxjs
0:00 All right, so now we're gonna start implementing the 0:02 server side for our 0:03 idempotency implementation. Now that we have the idempotency 0:07 store, 0:08 we can use it to actually make sure our HTTP routes are 0:12 idempotent. 0:14 And again, to do this, we're gonna use the NestJS interceptor, 0:17 which is 0:18 a concept in NestJS where we return an 0:22 observable that allows us to execute code before 0:26 and after every request inside of our HTTP server. 0:30 Now, this is gonna be super useful because we 0:32 can use this to apply our 0:33 idempotency and also make sure we cache and save 0:37 our idempotent requests so they can be replayed back to clients with-- if 0:41 they have the same key. So for this, let's create a new file inside 0:45 of HTTP that I will call 0:47 ServerIdempotency.Interceptor.ts. 0:53 And in here, I now want to create a new injectable 0:56 class. 0:57 This is going to be an injectable just like any other NestJS provider, where 1:01 we'll, we'll be able to inject our dependencies 1:04 directly 1:04 in here, and that's gonna be exactly what we're 1:07 gonna do by injecting the 1:08 idempotency store. So let's go ahead now and export class 1:12 ServerIdempotencyInterceptor. 1:16 And this will then implement a special Nest interceptor 1:20 interface from NestJS, where we have to im-implement 1:24 the intercept method here. Now, as a parameter, 1:28 this takes in the execution context, which is 1:32 a object that gives us access to details about the current request. 1:37 And then we have next, which is a call handler type from 1:40 NestJS, and this is what we use to actually 1:43 progress the 1:44 request through the request life cycle. 1:48 And importantly, calling this returns us back an observable 1:51 that we can then 1:52 use to pipe on additional functionality. 1:55 Just like any other observable, we can make use of observable chaining. 1:59 So for the return type, we then return back an observable as
The rest of the transcript is for members.
Sign in