import { sequenceS } from "fp-ts/lib/Apply" ; import { toError } from "fp-ts/lib/Either" ; import { taskEither , tryCatch } from "fp-ts/lib/TaskEither" ; import { TaskEither } from "fp-ts/lib/TaskEither" ; import { BonusActivationWithQrCode } from "generated/bonus/BonusActivationWithQrCode" ; import { BonusActivation } from "generated/io-bonus-api/BonusActivation" ; import { image } from "qr-image" ; const MIME_TYPES = { png : "image/png" , svg : "image/svg+xml" } ; const fixQrcodeFill = ( svgStr ) : string => svgStr . replace ( "<path" , '<path fill="black"' ) ; function streamToString ( stream ) { const chunks = [ ] ; return new Promise ( ( resolve , reject ) => { stream . on ( "data" , chunk => chunks . push ( chunk ) ) ; stream . on ( "error" , reject ) ; stream . on ( "end" , ( ) => resolve ( Buffer . concat ( chunks ) . toString ( "utf8" ) ) ) ; } ) ; } function streamToBuffer ( stream ) { const chunks = [ ] ; return new Promise ( ( resolve , reject ) => { stream . on ( "data" , chunk => chunks . push ( chunk ) ) ; stream . on ( "error" , reject ) ; stream . on ( "end" , ( ) => resolve ( Buffer . concat ( chunks ) ) ) ; } ) ; } const streamToBufferTask = ( stream ) : TaskEither < Error , Buffer > => tryCatch ( ( ) => streamToBuffer ( stream ) , toError ) ; const streamToStringTask = ( stream ) : TaskEither < Error , string > => tryCatch ( ( ) => streamToString ( stream ) , toError ) ; export function withQrcode ( bonus ) { return sequenceS ( taskEither ) ( { pngBuffer : streamToBufferTask ( image ( bonus . id , { type : "png" } ) ) , svgString : streamToStringTask ( image ( bonus . id , { type : "svg" } ) ) } ) . map ( ( { pngBuffer , svgString } ) => ( { ... bonus , qr_code : [ { content : pngBuffer . toString ( "base64" ) , mime_type : MIME_TYPES . png } , { content : Buffer . from ( fixQrcodeFill ( svgString ) ) . toString ( "base64" ) , mime_type : MIME_TYPES . svg } ] } ) ) ; }