Refactor apirequest.js