1.0.2 • Published 2 years ago

@hitachi567/core v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Core

hitachi567 <abidai790@gmail.com>

Wrapper for express based app

Requirements

  • node.js
  • npm or yarn

Getting started

npm install @hitachi567/core

# or

yarn add @hitachi567/core

Usage

Initialize express app

import { initApp } from '@hitachi567/core'

const app = initApp({
  cookieSecret: 'secret',
})

app.listen(5000, () => console.log('listening on 5000...'))

Define middleware

import Lib from '@hitachi567/core'
import { Router } from 'express'

interface Body {
  username: string
  password: string
}

interface Locals extends Lib.Locals {
  hashedPassword: string
}

let tryPartOfMiddleware: Lib.Middleware<Body, Locals> = async (
  request,
  response,
  next,
) => {
  let hashedPassword = await Lib.Hashing.hashing(request.body.password)

  response.locals.hashedPassword = hashedPassword

  next()
}

let middleware1 = Lib.asyncMiddleware<Body, Locals>(tryPartOfMiddleware)

let middleware2: Lib.Middleware<Body, Locals> = async (
  request,
  response,
  next,
) => {
  try {
    await tryPartOfMiddleware(request, response, next)
  } catch (error) {
    next(error)
  }
}

function middleware3(): Lib.Middleware<Body, Locals> {
  return Lib.asyncMiddleware<Body, Locals>(tryPartOfMiddleware)
}

// the expressions below are equivalent to each other
Router().use(middleware1)
Router().use(middleware2)
Router().use(middleware3())

Links

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago