Skip to Content
Drash v3 is in beta. APIs may change.

Resource

The base class every resource ultimately extends. It maps a set of paths to HTTP method handlers, and supplies a default for each method you do not write.

Good to know

The HTTP module extends this core Resource class and exports its own.

Properties

PropertyTypeDefaultNotes
pathsstring[][]The paths this resource answers to

A resource with an empty paths array is never matched. Paths may contain params (/users/:id), optional params (/users/:id?), and regular expressions (/([0-9]$)) — see Creating a Resource for the syntax.

Methods

The class defines one method per HTTP method:

CONNECT DELETE GET HEAD OPTIONS PATCH POST PUT TRACE

Each has the same signature:

public GET(request: unknown): unknown

The argument is typed unknown because a resource receives whatever was passed to app.handle() — a Web Request on Deno, Bun, and Cloudflare, or a context object on Node. Narrow it in your subclass. Use HTTPRequest when you want request.params typed.

The return value is unknown for the same reason. Whatever you return becomes the resolved value of app.handle().

Default Behavior

Every method throws HTTPError(Status.NotImplemented). That is the entire base implementation:

public GET(_request: unknown): unknown { throw new HTTPError(Status.NotImplemented); }

So a resource that defines only GET answers 501 Not Implemented to the other eight, following RFC 7231 Section 4.1 . To send 405 instead, override the method and throw it yourself:

public PATCH(_request: unknown) { throw new HTTPError(Status.MethodNotAllowed); }

Methods may be synchronous or asynchronous. The chain awaits whatever they return.

Notes

Subclassing for Your Own Defaults

Because the 501 behavior lives in this class, a base class of your own changes it for everything below it:

class BaseResource extends Resource { public override PATCH(_request: unknown) { throw new HTTPError(Status.MethodNotAllowed); } }

Every resource extending BaseResource answers 405 to PATCH without repeating the override.

Last updated on