I wanted to try to rewrite some parts of my code using fp-ts and considered to refactor the following method:
export const createApiServer = () => {
logger.info("Starting World Server API");
const apiServer = express()
// 3rd party middleware
.use(helmet())
.use(bodyParser.json())
.use(cors())
// Root route
.get("/", (_, result) => result.sendStatus(httpStatusCodes.OK))
// 404 - Not Found
.use((_, result) => result.sendStatus(httpStatusCodes.NOT_FOUND))
// 500 - Internal Server Error
.use(errorLoggerMiddleware)
.use(errorResponseMiddleware);
logger.verbose(`Initialized World Server API in ${process.uptime().toFixed(3)} seconds`);
return apiServer;
};
After reading about the IO
type for sideffects I tried using it as follows:
const info = (message: string) => new IO(() => log.info(message));
const verbose = (message: string) => new IO(() => log.verbose(message));
const setupApiServer = () =>
new IO(() =>
express()
.use(helmet())
.use(bodyParser.json())
.use(cors())
.get("/", (_, result) => result.sendStatus(httpStatusCodes.OK))
.use((_, result) => result.sendStatus(httpStatusCodes.NOT_FOUND))
.use(errorLoggerMiddleware)
.use(errorResponseMiddleware)
);
export const createApiServer = () =>
info("Starting World Server API")
.chain(setupApiServer)
.chain((apiServer) =>
verbose(`Initialized World Server API in ${process.uptime().toFixed(3)} seconds`)
.map(() => apiServer)
)
.run();
It feels rather weird to arbitrarily use the map method to pipe through the apiServer object in the last chain call just to log inbetween receiving it and send it further. I'd like to know whether this is correct or whether there are any different patterns for this.