import { createPlugin , JsonInvoice , PluginApi , STREAM , SPSP , receive , InvoiceReceiver } from '..' ; import { RequestHandler } from 'express' ; import { serializePayee } from '../lib/invoice' ; param { * } responseTemplate param { * } plugin export async function createMiddleware ( responseTemplate ? , plugin = createPlugin ( ) ) { const server = await STREAM . createServer ( { plugin } ) ; return ( req , rsp ) => { const reference = req . query . reference || undefined ; const payee = ( req . query . amount && ! isNaN ( + req . query . amount ) ) ? new InvoiceReceiver ( + req . query . amount , reference , server ) : server . generateAddressAndSecret ( reference ) ; const jsonPayee = serializePayee ( payee ) ; rsp . set ( 'Content-Type' , SPSP . CONTENT_TYPE ) ; rsp . send ( { ... responseTemplate , ... jsonPayee } ) ; } ; }