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

@choseohwan/express-utils

v1.0.3

Published

express utils for SeoHwan Cho's project

Downloads

19

Readme

@choseohwan/express-utils

express utilities for @choseohwan project

Install

npm

npm install @choseohwan/express-utils express

yarn

yarn add @choseohwan/utils express

Utilities

Application

Abstract class to easily register express app

Implementation functions

initBeforeMiddleware(): void

Middleware configuration setting before router execution in express app

Usage

import { Application } from "@choseohwan/express-utils";
// ...

class MyApp extends Application {
    protected initBeforeMiddleware(): void {
        // add before middleware
        this.app.use(myMiddleware);
        this.app.use(myMiddleware2);
        this.app.use(myMiddleware3);
        
        // ...
    }
    
    // ...
}

export default MyApp;

initRouter(): void

Router configuration for express app

Usage

import { Application } from "@choseohwan/express-utils";
// ...

class MyApp extends Application {
    protected initRouter(): void {
        // add express router 
        this.app.get('/', myRouter);
        this.app.post('/post', myRouter);
        
        // ...
    }
    
    // ...
}

export default MyApp;

initAfterMiddleware(): void

Middleware configuration setting after router execution in express app

Usage

import { Application } from "@choseohwan/express-utils";
// ...

class MyApp extends Application {
    protected initAfterMiddleware(): void {
        // add after middleware
        this.app.use(myAfterMiddleware);
        this.app.use(myAfterMiddleware2);
        
        // ...
    }
    
    // ...
}

export default MyApp;

getApp(): ReturnType<typeof express>

Get express application

Usage

# MyApp.ts

import { Application } from "@choseohwan/express-utils";
// ...

class MyApp extends Application {
    protected initBeforeMiddleware(): void {
        // ...
    }
    
    protected initRouter(): void {
        // ...
    }
    
    protected initAfterMiddleware(): void {
        // ...
    }
}

export default MyApp;
# app.ts

import MyApp from './MyApp';

const app = new MyApp().getApp();

// 서버 시작
app.listen(8001, () => {
    console.log('test');
});

RestApiRouter

Rest Api router for express

Implementation functions

index / show / store / update / delete (req: Request, res: Response, next: NextFunction);

Basic rest api method.

| router | http method | route path | desc | |:---------:|:-------------:|:--------------|:--------------------------------------| | index | GET | / | Show a list of the items in resource | | show | GET | /:id | Show an item of resource | | store | POST | / | Store a new item in resource | | update | PATCH | /:id | Edit an item of resource | | delete | DELETE | /:id | Delete an item of resource |

Usage

import { RestApiRouter } from '@choseohwan/express-utils';
import { NextFunction, Request, Response } from 'express';


class MyRouter extends RestApiRouter {
    protected index(req: Request, res: Response, next: NextFunction): void {
        res.json({
            method: 'index'
        });
        
        // ...
        
        next();
    }

    protected show(req: Request, res: Response, next: NextFunction): void {
        res.json({
            id: req.params.id
        });
        
        // ...

        next();
    }

    // ...
}

export default MyRouter;

RestApiRouter.registerRouter(method: HTTPMethod, path: string)

Decorator to register custom express router

Usage

import { RestApiRouter } from '@choseohwan/express-utils';
import { HTTPMethod } from '@choseohwan/utils/constant';
import { NextFunction, Request, Response } from 'express';

class MyRouter extends RestApiRouter {
    
    @RestApiRouter.registerRouter(HTTPMethod.PATCH, '/custom/:id')
    protected customPatch(
        req: Request,
        res: Response,
        next: NextFunction
    ): void {
        // ...
        
        res.json({
            router: 'customPost'
        });

        next();
    }
    
    // ...
}

export default MyRouter;

addMiddleware(): Handler[]

Register middleware list in express router

Usage

import { RestApiRouter } from '@choseohwan/express-utils';
import { Handler } from '@choseohwan/express-utils/type';

class MyRouter extends RestApiRouter {
    protected addMiddleware(): Handler[] {
        // ...
        
        return [myMiddleware, myMiddleware2];
    }
    
    // ...
}

export default MyRouter;

RestApiRouter.registerMiddleware

Decorator to register custom middleware

Usage

import { RestApiRouter } from '@choseohwan/express-utils';
import { NextFunction, Request, Response } from 'express';

class MyRouter extends RestApiRouter {
    @RestApiRouter.registerMiddleware
    protected customMiddleware(
        req: Request,
        res: Response,
        next: NextFunction
    ): void {
        // ...

        next();
    }
    
    // ...
}

export default MyRouter;

Extension functions

makeRouter(routerClass: RestApiRouterConstructor)

Make express router object

Usage

import express from "express";
import { makeRouter } from "@choseohwan/express-utils";

const app = express();

app.use('/resource', makeRouter(MyRouter));

// ...