Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/frencojobs/ombud
A tiny decorator based abstraction layer for making HTTP proxy servers with fastify-http-proxy
https://github.com/frencojobs/ombud
decorators fastify proxy
Last synced: about 2 months ago
JSON representation
A tiny decorator based abstraction layer for making HTTP proxy servers with fastify-http-proxy
- Host: GitHub
- URL: https://github.com/frencojobs/ombud
- Owner: frencojobs
- License: mit
- Created: 2021-10-24T16:43:12.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-04T13:49:11.000Z (over 2 years ago)
- Last Synced: 2024-09-19T12:09:15.381Z (4 months ago)
- Topics: decorators, fastify, proxy
- Language: TypeScript
- Homepage: https://npm.im/ombud
- Size: 188 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Ombud
Ombud (Swedish word for proxy, pronounced `/ˈɒmbəd/`) is a tiny utility library for making proxy servers with [@fastify/http-proxy](https://github.com/fastify/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.
```ts
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
```2. Make sure to import `reflect-metadata` shim once before using the decorators.
```ts
import 'reflect-metadata'
```3. Create a proxy server controller.
```ts
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() {}
}
```4. Register the controller to a fastify server.
```ts
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](https://nestjs.com) & [typestack/routing-controllers](https://github.com/typestack/routing-controllers).