Skip to content

Function: PATCH() ​

Call Signature ​

PATCH<ClassPrototype, Method>(r): MethodDecorator<ClassPrototype, Method, ControllerProperty<ClassPrototype>>

Expose the controller method as an HTTP PATCH action on a route, to update a part of a resource: the shorthand of @HTTPAction(route, 'patch', …). The adapter assembles the input: by default, the query string merged with the parsed body (JSON; URL encoded with Express), then the route parameters over them. The middlewares run, the input is validated against the DTO (the defaults applied, the unknown keys removed; without a DTO, it is accepted as it is), the access control runs when acl is set, then the method receives the validated input as a plain object. The query values and the route parameters are strings and the DTO schemas are strict: convert them with .strict(false). The value returned is answered by the adapter's responder: as JSON by default, with the status 204 for undefined; Result.status() sets a status and headers. An error thrown is answered as {errno, errMsg} with the first 4xx statusCode along its causes (400 for an invalid input), else its own statusCode, else 500.

Type Parameters ​

ClassPrototype ​

ClassPrototype extends Controller

Method ​

Method

Parameters ​

r ​

string

The route, in the syntax of the HTTP adapter (/users/:id); the controller's @Prefix({route}) is prepended to it.

Returns ​

MethodDecorator<ClassPrototype, Method, ControllerProperty<ClassPrototype>>

The method decorator.

Example ​

typescript
import {DTO} from 'lakutata'
import {Controller} from 'lakutata/com/entrypoint'
import {PATCH} from 'lakutata/decorator/ctrl'
import {Expect} from 'lakutata/decorator/dto'

class RenameDTO extends DTO {
    @Expect(DTO.String().required())
    public id!: string

    @Expect(DTO.String().min(1).optional())
    public name?: string
}

class ProjectController extends Controller {
    //PATCH /projects/p1 with the body {"name": "Apollo"}: answered with the status 204 (no content)
    @PATCH('/projects/:id', RenameDTO)
    public async rename(input: RenameDTO): Promise<void> {
        console.log(`rename ${input.id} to ${input.name}`)
    }
}

Call Signature ​

PATCH<ClassPrototype, Method, DTOConstructor>(r, dtoConstructor): MethodDecorator<ClassPrototype, Method, ControllerProperty<ClassPrototype>>

Expose the controller method as an HTTP PATCH action on a route, its input validated against a DTO: the shorthand of @HTTPAction(route, 'patch', …). The input is the query string merged with the parsed body, then the route parameters over them (the default of the adapters); the method receives it validated, as a plain object. The value returned is answered as JSON (204 for undefined), an error as {errno, errMsg} with its status (400 for an invalid input, else 500 by default).

Type Parameters ​

ClassPrototype ​

ClassPrototype extends Controller

Method ​

Method

DTOConstructor ​

DTOConstructor extends typeof DTO = typeof DTO

Parameters ​

r ​

string

The route, in the syntax of the HTTP adapter (/users/:id); the controller's @Prefix({route}) is prepended to it.

dtoConstructor ​

DTOConstructor

The DTO class validating the input; the query values and the route parameters are strings, convert them with .strict(false).

Returns ​

MethodDecorator<ClassPrototype, Method, ControllerProperty<ClassPrototype>>

The method decorator.

Call Signature ​

PATCH<ClassPrototype, Method>(r, actionOptions): MethodDecorator<ClassPrototype, Method, ControllerProperty<ClassPrototype>>

Expose the controller method as an HTTP PATCH action on a route, with action options: the shorthand of @HTTPAction(route, 'patch', …). The input is the query string merged with the parsed body, then the route parameters over them (the default of the adapters), accepted as it is. The value returned is answered as JSON (204 for undefined), an error as {errno, errMsg} with its status (500 by default).

Type Parameters ​

ClassPrototype ​

ClassPrototype extends Controller

Method ​

Method

Parameters ​

r ​

string

The route, in the syntax of the HTTP adapter (/users/:id); the controller's @Prefix({route}) is prepended to it.

actionOptions ​

ActionOptions<HTTPContext<{ }>>

The action options: acl runs the access control rules (false by default); name (unique in the HTTP actions, ClassName:method by default), description and groups describe the action; rule is an access rule of the action; middlewares run before the ones @UseMiddlewares adds to the method.

Returns ​

MethodDecorator<ClassPrototype, Method, ControllerProperty<ClassPrototype>>

The method decorator.

Throws ​

When the class is declared, if the action options are invalid.

Throws ​

When the class is declared, if a middleware of the options is neither a function nor a class extending Middleware.

Call Signature ​

PATCH<ClassPrototype, Method, DTOConstructor>(r, dtoConstructor, actionOptions): MethodDecorator<ClassPrototype, Method, ControllerProperty<ClassPrototype>>

Expose the controller method as an HTTP PATCH action on a route, its input validated against a DTO, with action options: the shorthand of @HTTPAction(route, 'patch', …). The input is the query string merged with the parsed body, then the route parameters over them (the default of the adapters); the method receives it validated, as a plain object. The value returned is answered as JSON (204 for undefined), an error as {errno, errMsg} with its status (400 for an invalid input, else 500 by default).

Type Parameters ​

ClassPrototype ​

ClassPrototype extends Controller

Method ​

Method

DTOConstructor ​

DTOConstructor extends typeof DTO = typeof DTO

Parameters ​

r ​

string

The route, in the syntax of the HTTP adapter (/users/:id); the controller's @Prefix({route}) is prepended to it.

dtoConstructor ​

DTOConstructor

The DTO class validating the input; the query values and the route parameters are strings, convert them with .strict(false).

actionOptions ​

ActionOptions<HTTPContext<{ }>>

The action options: acl runs the access control rules (false by default); name (unique in the HTTP actions, ClassName:method by default), description and groups describe the action; rule is an access rule of the action; middlewares run before the ones @UseMiddlewares adds to the method.

Returns ​

MethodDecorator<ClassPrototype, Method, ControllerProperty<ClassPrototype>>

The method decorator.

Throws ​

When the class is declared, if the action options are invalid.

Throws ​

When the class is declared, if a middleware of the options is neither a function nor a class extending Middleware.

Released under the MIT License.