Code Monkey home page Code Monkey logo

mith's Introduction

Mith

mith ci deno doc

A middleware framework for Deno's http/s server

Highly inspired by the Express middleware system

Differently from Express, the main Mith application is only responsible to handle the middleware system, it has no routes support, and it tries to leave the request and response objects form Deno untouched as much as possible

Available middlewares

  • mith_router - router middleware
  • cookieSession.ts - session middleware using cookies
  • mith_static - static file server

Note: These middlewares will eventually move to their own repositories to split responsibilities and improve maintenance

Usage

Basic integration

import { Mith, NextFunction, Request, Response } from 'https://deno.land/x/[email protected]/mod.ts'

const app = new Mith()

app.before(
  async (req: Request, res: Response, next: NextFunction) => {
    res.body.before = true
    next()
  }
)

app.after(
  (req: Request, res: Response, next: NextFunction) => {
    const encoder = new TextEncoder();
    Deno.writeFile('./after.dat', encoder.encode(`${Deno.pid} - ${JSON.stringify(res.body)}\n`), {append: true})
    next()
  }
)

app.use(
  async (req: Request, res: Response, next: NextFunction) => {
    const body = await req.body()
    switch (body.type) {
      case 'error':
        return next(new Error('error'))
      case 'redirect':
        res.redirect('/')
        break
      case 'urlencoded':
      case 'json':
        res.body.test = body.type
        break
      default:
        res.body.test = body
    }

    next()
  }
)

app.error(
  (req: Request, res: Response, next: NextFunction) => {
    res.status = res.error.status || 500
    res.body = res.error.message
    next()
  }
)

export default app

Right now I'm still working on the documentation, so you can check the example folder for full usage examples

Multiple stacks

image

app.before()

Intended to be used by middleware the enhances the request or response objects. Multipart body parser for example

app.main()

Main business logic of the application goes here, routing definitions for example Using app.use() will default to the main stack if no stack is passed

app.error()

This middleware stack will be triggered if the callback function next is called with an error: next(something)

app.after()

This middleware stack runs after sending the response to the user, it's intended to be used with extra integrations that are not directly related to the user response. Analytics or logging for example.

Middleware parameters

Request

The request contains information about the request received

properties:

  • body() Parses the body of the request and returns it in json format
  • query() Parses the query string of the request and returns an URLSearchParams object
  • serverRequest The original Deno server request

Response

The response contains information about the response that will be sent back to the requestor.

properties:

  • error Contains the error sent when calling next(error)
  • body The body of the response
  • headers A Headers instance which contains the headers for the response
  • finished A boolean indicating that the response is completed
  • sent A boolean indicating that the response has been already sent to the requestor
  • send Sends the response to the requestor
  • redirect Redirects the user to another location

Next

A function that triggers the next middleware in line.

Triggers the next middleware

next()

Jumps to the error middleware stack

next([someinput])

On the error middleware stack calling next([someinput]) has no effect because connection is already on the error stack

mith's People

Contributors

jwebcoder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mith's Issues

Fix mithCors example errors

Getting the following error on running mithCors examples you added to cors, Is there any way to fix it?

error: TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'.
  Type 'URL' is not assignable to type 'string'.
  return new URL(url).pathname
                 ~~~
    at https://deno.land/[email protected]/path/win32.ts:911:18

TS2345 [ERROR]: Argument of type 'string | URL' is not assignable to parameter of type 'string'.
  Type 'URL' is not assignable to type 'string'.
  return new URL(url).pathname;
                 ~~~
    at https://deno.land/[email protected]/path/posix.ts:433:18

Found 2 errors.

I'm also tried :

import { Mith } from "https://deno.land/x/[email protected]/mod.ts";
import { Router } from "https://deno.land/x/[email protected]/mod.ts";

and it wasn't helpful!

Using:
deno 1.2.3
v8 8.6.334
typescript 3.9.2

Upgrade Mith to support Deno 1.4.0 and std 0.69.0

Issue description: ako-deno/negotiator#1

I beleaveThis also requires a number of Mith's dependency updates

error: TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { serve, Server, HTTPOptions, ServerRequest, Response } from 'https://deno.land/[email protected]/http/server.ts'
                        ~~~~~~~~~~~
    at https://deno.land/x/[email protected]/deps.ts:1:25

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { serve, Server, HTTPOptions, ServerRequest, Response } from 'https://deno.land/[email protected]/http/server.ts'
                                                    ~~~~~~~~
    at https://deno.land/x/[email protected]/deps.ts:1:53

TS1371 [ERROR]: This import is never used as a value and must use 'import type' because the 'importsNotUsedAsValues' is set to 'error'.
import { Response as DenoResponse, ServerRequest } from './deps.ts'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/response.ts:1:1

TS1371 [ERROR]: This import is never used as a value and must use 'import type' because the 'importsNotUsedAsValues' is set to 'error'.
import { ServerRequest} from './deps.ts';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/bodyParser.ts:1:1

TS1371 [ERROR]: This import is never used as a value and must use 'import type' because the 'importsNotUsedAsValues' is set to 'error'.
import { ServerRequest } from "./deps.ts"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/request.ts:1:1

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { Mith, Middleware, NextFunction } from './application.ts'
               ~~~~~~~~~~
    at https://deno.land/x/[email protected]/mod.ts:1:16

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { Mith, Middleware, NextFunction } from './application.ts'
                           ~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/mod.ts:1:28

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { Response, IResponse } from './response.ts'
                   ~~~~~~~~~
    at https://deno.land/x/[email protected]/mod.ts:2:20

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { Request, IRequest } from './request.ts'
                  ~~~~~~~~
    at https://deno.land/x/[email protected]/mod.ts:3:19

Found 9 errors.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.