npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dikur/http

v0.2.3

Published

The http package provides decorators that map a class to a router, it consists of the following decorators: - [Class Decorators](#class-decorators): - [Http](#http) - [Middleware](#middleware) - [Method Decorators](#method-decorators): -

Downloads

820

Readme

@dikur/http

The http package provides decorators that map a class to a router, it consists of the following decorators:

Example Usage:

@Middleware(authGuard)
@Http("/users")
class UserController {

    @Get("/")
    getUsers(@Context() ctx: Ctx, @Query(PageQuerySchema) {page, size}: PageQuerySchema) {
        let result = await users.paginate({page, size});
        return ctx.json({data: result});
    }

    @Patch("/:id")
    updateUsers(
        @Context() ctx: Ctx,
        @Param(IdParamSchema): {id}: IdParamSchema,
        @Body(UserUpdateSchema) data: UserUpdateSchema
    ) {
        await users.update(id, data);
        return ctx.json({message: "user updated successfully"})
    }
}

Class Decorators

Http

function Http(basePath?: string): ClassDecorator Maps the class to a router, it takes a single parameter, basePath. which is prefixed to all other routes defined in the class.

Middleware

function Middleware(handler: MiddlewareHandler | (...args: any[]) => Promise<any>): ClassOrMethodDecorator Registers a middleware function to the router or route, it takes any kind of function as the middleware signature depends on the specific router implementation. While less type safe, this allows existing router middleware libraries to be used as is.

Method Decorators

Get | Post | Patch | Put | Delete

function [Get|Post|Patch|Put|Delete](path?: string): MethodDecorator Registers the method as a route handler on the router set by the Http decorator. It takes a a single optional parameter, path, which if not provided, will have the method name be used instead.

class {
    @Get()
    async getUsers() {} // path is "/getUsers"
}

The path is passed to the router as is, so in case you which to use path parameters, use the same syntax your router uses e.g: express: get('/:id') // req.params: { id: string };

Middleware

Identical to the class decorator, but applied to only a specific route.

class {
    // this route does not have the middleware applied
    @Get()
    async getProducts() {}

    // but this one does
    @Middleware(authGuard)
    @Post()
    async addProduct() {}
}

Parameter Decorators

Parameter decorators are used to inject values into the route handlers assigned using method decorators like Get and Post. Aside from telling Dikur which parameters are in use and what order they are in, they can also be used for validation and documentation.

Context

function Context(): ParameterDecorator Injects the handler context in the decorated parameter's stead, the actual value will depend on the router implementation. Whatever the router passes to their route handlers will be included in the context object.

  • For Hono it will inject the Context object.
  • For Express it will inject an object containing the request, response and next parameters.

Body

function Body(schema?: Schema, mediatype?: "json" | "form"): ParameterDecorator Injects the request's body in the decorated parameter's stead, it accepts two optional parameters:

  • schema: a JSON schema, used with Ajv for validating the response
  • mediaType: specifies whether it's a JSON or FormData, defaults to JSON
    • no-op, adapters currently parse based on headers, but it's used by the openapi doc generator.

Param | Query

function [Param | Query](schema?: Schema): ParameterDecorator Injects the entire request's path parameter or query object in the decorated parameter's stead. It accepts an optional schema parameter for validation.

Static Property Decorators

NestedRouter

function NestedRouter(): StaticPropertyDecorator Registers the static property as a nested router, expects the property to be another class that was decorated using the Http decorator.