Skip to content

Instantly share code, notes, and snippets.

@paprikka
Created December 7, 2017 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paprikka/71bc978dab7d32ab1d8db967d5cc86bc to your computer and use it in GitHub Desktop.
Save paprikka/71bc978dab7d32ab1d8db967d5cc86bc to your computer and use it in GitHub Desktop.
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