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

@acathur/koa-decorator-ts

v2.7.4

Published

A fork of koa-decorator-ts, provides decorators for all of the standard HTTP methods.

Downloads

12

Readme

koa-decorator-ts

This module is a fork of koa-decorator-ts, provides decorators for all of the standard HTTP methods: @Get(), @Post(), @Put(), @Delete(), @Patch(), @Options(), and @Head(). In addition, @All() defines an endpoint that handles all of them.

Koa Decorator (with TypeScript)

Installation

npm i koa-decorator-ts --save
/* or */
yarn add koa-decorator-ts

Introduction

This package is a decorator for koa, including the koa-router, graohql. You can use decorators to define the path of routing or create a graphql resolver using existing koa controller

Usage

app.js

import Koa from 'koa';
// 1. Import Router
import Router from 'koa-decorator-ts/router';

const app = new Koa();
// 2. Create router
const router = new Router({
  dir: `${__dirname}/controllers` // The controllers directory
});

// [Optional] Graphql inital
const graphqlServer = new ApolloServer({ schema });
graphqlServer.applyMiddleware({ app });

// 3. Register the routers
app.use(router.routes());
app.use(router.allowedMethods());

...

app.listen(8080);

/controllers/user.ts

import Koa from 'koa';
import {
  Controller,
  Route,
  All,
  Post,
  Get,
  Patch,
  Put,
  Delete,
  Middleware,
  Required,
  Graphql,
  Priority,
  Meta,
} from 'koa-decorator-ts';
import { IsString } from 'class-validator';

async function middlewareLog(ctx: Koa.Context, next: Function) {
  await next();
}

//use a class with decorators (class-validator) for validation
class PatchUserRequest{
  @IsString()
  userNickName!:string
  @IsString()
  userAddress!:string
}

// Prefix of api path
@Controller('/user')
class UserController {
  @Priority(-1000)
  @All('*')
  async handleAll(ctx: Koa.Context, next: any) {
    ctx.body = 'haha';
  }

  // Post /user/login
  @Post('/login')
  @Required({
    // Require { userEmail, password } in the body
    body: {
      type: 'object',
      properties: {
        userEmail: {
          type: 'string',
        },
        password: {
          type: 'string',
        },
      },
      required: ['userEmail', 'password'],
    },
  })
  async login(ctx: Koa.Context) {
    ctx.body = true;
  }

  // Get /user/:userId
  @Get('/:userId')
  @Middleware(middlewareLog) // Add Middleware
  async getUserInfo(ctx: Koa.Context) {
    ctx.body = { userName: 'skm', userEmail: '[email protected]' };
  }

  // Get /user?top=10&star=1000000
  @Get('/')
  @Required({
    query: {
      type: 'object',
      properties: {
        top: { type: 'string' },
        star: { type: 'string' },
      },
      required: ['top', 'star'],
    },
  }) // Require for "top", "star" in the query
  @Middleware(middlewareLog)
  async getUsers(ctx: Koa.Context) {
    ctx.body = { userName: 'skm', userEmail: '[email protected]' };
  }

  // Patch /user/:userId
  @Patch('/:userId')
  @RequiredBody(PatchUserRequest) //do the same for query using @RequiredQuery
  async updateUserInfo(ctx: Koa.Context) {
    ctx.body = true;
  }

  // Put /user/:userId/follow
  @Put('/:userId/follow')
  async followUser(ctx: Koa.Context) {
    ctx.body = true;
  }

  // Delete /user/:userId/follow
  @Delete('/:userId/follow')
  async unfollowUser(ctx: Koa.Context) {
    ctx.body = true;
  }

  @Meta({ test: 'cc' })
  @Get('/meta')
  async metaTest(ctx: Koa.Context) {
    ctx.body = ctx.meta;
  }

  @Graphql
  @Middleware(middlewareLog)
  static async getUser(ctx: Koa.Context) {
    const { args } = ctx.graphql!;

    const users = [
      { username: 'skmdev', role: 'admin', userEmail: '[email protected]' },
      { username: 'foo', role: 'user', userEmail: 'bar' },
    ];

    ctx.body = users.find((user) => user.username === args.username);
  }

  @Graphql // transfrom to graphql resolver
  @Middleware(middlewareLog)
  static async getUsersGraph(ctx: Koa.Context) {
    /**
     * const { root, args, info } = ctx.graphql;
     *
     * root is representing rootObject
     *
     * args is representing the arguments of request
     *
     * info is representing the graphql info
     *
     */
    const { args } = ctx.graphql!;

    const users = [
      { username: 'skmdev', role: 'admin', userEmail: '[email protected]' },
      { username: 'foo', role: 'user', userEmail: 'bar' },
    ];

    // ctx.body is response data;
    ctx.body = users.filter((user) => user.role === args.role);
  }
}

export default UserController;