HTTP Method Decorators
This file provides a set of method decorators for defining RESTful routes in HTTP controllers for the @sodacore/core
framework. Each decorator marks a controller method as a handler for a specific HTTP method (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS) and optionally associates it with a route path. The decorators attach metadata for later discovery and routing by the HTTP module.
Features
- RESTful Routing: Define controller methods as handlers for standard HTTP methods.
- Path Association: Optionally specify a route path for each method.
- Metadata Management: Stores method metadata for each route, enabling the framework to resolve and dispatch requests to the correct handler.
Usage
import {
Get,
Post,
Put,
Patch,
Delete,
Head,
Options
} from './decorator/methods';
class MyHttpController {
@Get('/users')
async getUsers(ctx) { /* ... */ }
@Post('/users')
async createUser(ctx) { /* ... */ }
@Put('/users/:id')
async updateUser(ctx) { /* ... */ }
@Patch('/users/:id')
async patchUser(ctx) { /* ... */ }
@Delete('/users/:id')
async deleteUser(ctx) { /* ... */ }
@Head('/users')
async headUsers(ctx) { /* ... */ }
@Options('/users')
async optionsUsers(ctx) { /* ... */ }
}
API
Get(path?: string): MethodDecorator
Defines a method as a route for the GET HTTP method.
- path: Optional route path.
Post(path?: string): MethodDecorator
Defines a method as a route for the POST HTTP method.
- path: Optional route path.
Put(path?: string): MethodDecorator
Defines a method as a route for the PUT HTTP method.
- path: Optional route path.
Patch(path?: string): MethodDecorator
Defines a method as a route for the PATCH HTTP method.
- path: Optional route path.
Delete(path?: string): MethodDecorator
Defines a method as a route for the DELETE HTTP method.
- path: Optional route path.
Head(path?: string): MethodDecorator
Defines a method as a route for the HEAD HTTP method.
- path: Optional route path.
Options(path?: string): MethodDecorator
Defines a method as a route for the OPTIONS HTTP method.
- path: Optional route path.
How It Works
- Each decorator sets metadata on the decorated method, including the HTTP method and optional path.
- The metadata is stored using
Utils.setMeta
and is later used by the HTTP integration to discover and route requests to the correct handler methods.
Notes
- These decorators are intended for use within HTTP controller classes in the Sodacore framework.
- Only methods decorated with these decorators will be registered as route handlers.
- The path parameter is optional; if omitted, the method will be registered for the base route.