0

How is the elegant way to handle non standard HTTP methods like COPY, LINK, LOCK, UNLOCK in NestJs Controller?

For standard HTTP Methods Request such GET, POST, PUT... We have a decorator like @Get, @Post, etc. But i want to handle some requests with LOCK, UNLOCK and some others... in an elegant way....

I Try to use @All decorator, but i think its not ideal...

I Lookig for something like:

@COPY('auth/copy-user-rights')
async copy_user_rights(): Promise<... {
......
}

2 Answers2

1

I know its not elegant, buts....

My Workaround

    @All('assign-except-claim')
    async assign_except_claim(@Req() req: Request) {
      
      switch (req.method) {
        case 'LINK':
          ...
          break;

        case 'UNLINK':
          ...
          break;

        default:
          throw new NotFoundException();
          break;
      }    
    }
0

You can make use of the @RequestMapping() decorator and manually apply the path and method, and tell Typescript to ignore the error. It seems to work on a minimum reproduction

  @RequestMapping({ path: 'copy', method: 'COPY' as any })
  copyPath() {
    console.log('called copy path');
    return this.appService.getHello();
  }

Startup logs

[Nest] 3501141  - 02/20/2023, 11:58:28 AM     LOG [NestFactory] Starting Nest application...
[Nest] 3501141  - 02/20/2023, 11:58:28 AM     LOG [InstanceLoader] AppModule dependencies initialized +10ms
[Nest] 3501141  - 02/20/2023, 11:58:28 AM     LOG [RoutesResolver] AppController {/}: +5ms
[Nest] 3501141  - 02/20/2023, 11:58:28 AM     LOG [RouterExplorer] Mapped {/, GET} route +1ms
[Nest] 3501141  - 02/20/2023, 11:58:28 AM     LOG [RouterExplorer] Mapped {/copy, undefined} route +1ms
[Nest] 3501141  - 02/20/2023, 11:58:28 AM     LOG [NestApplication] Nest application successfully started +1ms
called copy path

curl

curl -X COPY http://localhost:3000/copy -v
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> COPY /copy HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.86.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: text/html; charset=utf-8
< Content-Length: 12
< ETag: W/"c-Lve95gjOVATpfV8EL5X4nxwjKHE"
< Date: Mon, 20 Feb 2023 19:58:34 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< 
* Connection #0 to host localhost left intact
Hello World!
Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • 1
    its work, but... any others verbs reach its too... to avoid this i think i need put some verification inside body function like `@RequestMapping({ path: 'test', method: 'COPY' as any }) async assign_except_claim( @Req() req: Request, ) { if (req.method != 'COPY') throw new NotFoundException(); console.log("test"); }` – Ermindo Lopes Feb 21 '23 at 11:33
  • In short, its act as @All() decorator if use verbs others than RequestMethod enum – Ermindo Lopes Feb 21 '23 at 11:39
  • Looking through the code, if `method` doesn't match a method in the `RequestMethods` enum, then `app.use` gets called, so it probably will trigger for each method type. You could make a guard or interceptor to handle this check though – Jay McDoniel Feb 21 '23 at 16:28
  • I cannot understand why COPY, LINK/UNLINK is not officially supported on Nest.js. Even Node express has supported that.. – cyan-kinesin May 10 '23 at 14:28
  • @cyan-kinesin Nest also supports the use of fastify, [which looks like it only supports the `COPY` method](https://www.fastify.io/docs/latest/Reference/Routes/#routes-options), so **if** these were to start being supported we'd then have to make sure to avoid registering incorrect routes per adapter – Jay McDoniel May 10 '23 at 15:49
  • Well, I think framework should respect specification like RFC first, implementation range must have no dependency about platform provider. You know, Web Standardization makes the web more easily, enable to develop fastly. I wish Nest.js would be the RESTful-easy Framework like Jersey Framework is the first implementation framework of RESTful API on the world. @JayMcDoniel – cyan-kinesin May 10 '23 at 16:43
  • And I hope you can make custom http method support too. RESTful is the story about 'Software Architecture' and that a communication way. when your service is required a function for clone resources, that is regarding to 'create'(POST) but not necessary request body. The wrong RESTful API Like 'resources/copy', cannot represent that clearly. Roy fielding had intended action should be represented as http method. by Seperation of Concern with http method, we can control proxy, cache more easily with plenty strategies for happy UX including Happy Development @JayMcDoniel – cyan-kinesin May 10 '23 at 16:55