1.4.1 • Published 2 years ago

ombud v1.4.1

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

Ombud

Ombud (Swedish word for proxy, pronounced /ˈɒmbəd/) is a tiny utility library for making proxy servers with @fastify/http-proxy. The library acts as an abstraction layer to manage routes more efficiently using TypeScript's experimental decorators.

Overview

A simple proxy server with route based authorization will look like this.

import {Controller, Get, All} from 'ombud'

@Controller('https://example.com')
class Proxy {
  @Get('/public')
  async pub() {}

  @Get('/private')
  @All('/user/:id')
  async auth(request: FastifyRequest, reply: FastifyReply) {
    return reply.code(401).send('unauthorized')
  }
}

Usage

  1. Install required dependencies.
npm i ombud fastify reflect-metadata
  1. Make sure to import reflect-metadata shim once before using the decorators.
import 'reflect-metadata'
  1. Create a proxy server controller.
import {Controller, Get, All} from 'ombud'

@Controller('https://example.com', {proxyPaylods: false}) // all the options for fastify-http-proxy are available via second param
export class Proxy {
  // if you want to consume `fastify` instance from controller
  // set a constructor like this
  constructor(private readonly fastify: FastifyInstance) {}

  @Get('/public')
  async pub() {}

  @Get('/private')
  @All('/user/:id')
  async auth(request: FastifyRequest, reply: FastifyReply) {
    return reply.code(401).send('unauthorized')
  }

  // all other routes will be allowed by default
  // to disable that behavior, use a wildcard
  @All('*')
  async others() {}
}
  1. Register the controller to a fastify server.
import fastify from 'fastify'
import {setup} from 'ombud'

// ...

const server = fastify()
server.register(setup(Proxy))

server.listen(3000)

Acknowledgements

This library was inspired by NestJS & typestack/routing-controllers.

1.4.1

2 years ago

1.4.0

2 years ago

1.2.0

3 years ago

1.3.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

0.1.0

3 years ago