Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Handle Restify endpoints as Observables using RxJS
import { Observable, Subscription } from 'rxjs'
import {
Request,
Response,
Next
} from 'restify'
export interface RequestContext {
req: Request,
res: Response,
next: Next
}
export type RequestHandler = (
context: Observable<RequestContext>
) => Observable<any>
export function createHandler (fn: RequestHandler ) {
return (
req: Request,
res: Response,
next: Next
) =>
fn( Observable.of<RequestContext>({ req, res, next }) )
.catch( err => Observable.of(err) )
.take(1)
.subscribe( result => {
res.send(result)
next()
} )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.