0.4.6 • Published 10 months ago

uws-wrapper v0.4.6

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

ARCHIVED

Use uws-utils instead

uws-wrapper

Plugin for uWebSockets.js that wraps App instance to improve DX (Developer Experience).

Installation

yarn add uws-wrapper

Changes/Features:

  • Replace the parameters in the callback of the http request functions with a context object:

    -app[<HTTP_METHOD>](<ROUTE_PATTERN>, (res, req) => { ... })
    +app[<HTTP_METHOD>](<ROUTE_PATTERN>, ({ req, res }) => { ... })
  • Replace the result of res.getQuery() method with a query object (generated by fastest-qs):

    const query = req.getQuery()
    -console.log('Query:', query)   // q=1&q2
    +console.log('Query:', query)   // { q: 1, q: 2 }
  • Add req.body.json method

    const jsonBody = await req.body.json()
    console.log(jsonBody)   // { prop1: 1, prop2: 2 }

Register the Plugin / Usage

import { App } from 'uWebSockets.js'
import { transformCallback } from 'uws-wrapper'

const port = +(process.env.PORT || 3000)

// Register the Plugin
const app = transformCallback({
  // Default HTTP methods: 'get', 'post', 'options', 'del', 'patch', 'put', 'head', 'connect', 'trace', 'any'
  httpMethods: new Set(['get', 'post'])
})(App())

// Usage
app
  .get(pattern, async ({ req, res }) => {
    console.log('Query:', req.getQuery())   // { q: 1, q: 2 }
    console.log('JSON body:', await req.body.json())  // { prop1: 1, prop2: 2 }
    res.end('Hello World!')
  })
  .listen(port, (listenSocket) => {
    if (listenSocket) {
      console.log(`Server running at http://localhost:${port}`)
    } else {
      console.log(`Failed to listen to port ${port}`)
    }
  })

License

This project is licensed under the MIT License.

0.4.6

10 months ago

0.4.5

10 months ago

0.4.4

10 months ago

0.4.3

10 months ago

0.4.2

10 months ago

0.4.1

10 months ago

0.4.0

10 months ago

0.3.1

10 months ago

0.3.0

10 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.2.3

10 months ago

0.2.2

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.1

10 months ago