import { BaseMessage } from "sharedTypes/messageModel" ; import { Transformer } from "transformers/types" ; import attachLinks from "transformers/attachLinks" ; import applyGiphyCommand from "transformers/applyGiphyCommand" ; import filterProfanity from "transformers/filterProfanity" ; const isMessage = ( message ) : message is BaseMessage => { return "type" in message && "senderId" in message ; } ; const messageTransformers = [ filterProfanity , applyGiphyCommand , attachLinks , ] ; const applyTransformers = async < T > ( transformers , message ) : Promise < T > => { return await transformers . reduce ( async ( message , transformer ) => { return transformer ( await message ) ; } , message ) ; } ; const beforePublish = async ( request ) => { const message = request . message ; if ( isMessage ( message ) ) { request . message = await applyTransformers ( messageTransformers , message ) ; } return request . ok ( ) ; } ; export default beforePublish ;