Skip to content

HTTP Method Decorators

This file provides a set of method decorators for defining HTTP route handlers in controller classes for the @sodacore/core HTTP module. Each decorator corresponds to a standard HTTP method (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS) and attaches metadata to the method for routing and request handling.


Features

  • RESTful Routing: Decorators for all major HTTP methods: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS.
  • Optional Path Support: Each decorator accepts an optional path to override or specify the route for the method.
  • Metadata Management: Stores route metadata for each method, enabling the framework to discover and route HTTP requests appropriately.

Usage

typescript
import { Get, Post, Put, Patch, Delete, Head, Options } from './decorator/methods';

class UserController {
	@Get('/users')
	async listUsers() { /* ... */ }

	@Post('/users')
	async createUser() { /* ... */ }

	@Put('/users/:id')
	async updateUser() { /* ... */ }

	@Patch('/users/:id')
	async partialUpdateUser() { /* ... */ }

	@Delete('/users/:id')
	async deleteUser() { /* ... */ }

	@Head('/users')
	async headUsers() { /* ... */ }

	@Options('/users')
	async optionsUsers() { /* ... */ }
}

API

Get(path?: string): MethodDecorator

Defines a method as a route handler for the HTTP GET method.

Post(path?: string): MethodDecorator

Defines a method as a route handler for the HTTP POST method.

Put(path?: string): MethodDecorator

Defines a method as a route handler for the HTTP PUT method.

Patch(path?: string): MethodDecorator

Defines a method as a route handler for the HTTP PATCH method.

Delete(path?: string): MethodDecorator

Defines a method as a route handler for the HTTP DELETE method.

Head(path?: string): MethodDecorator

Defines a method as a route handler for the HTTP HEAD method.

Options(path?: string): MethodDecorator

Defines a method as a route handler for the HTTP OPTIONS method.


How It Works

  • Each decorator adds an entry to the method's metadata, specifying the HTTP method and optional path.
  • The metadata is used by the framework's router to match incoming requests to the correct controller method.

Notes

  • These decorators are intended for use within HTTP controller classes decorated with @Controller.
  • If no path is provided, the method will use the base path defined by the controller or be matched by convention.
  • The metadata set by these decorators is used internally by the Sodacore HTTP routing system.

Released under the Apache-2.0 License.