Sick of writing the same fetch or axios boilerplate over and over? π
Meet em-use-controller
β a tiny but powerful utility for React, and supported for other SPAs that turns your REST API routes into declarative, type-safe controller functions.
const getUser = useController('getUser');
const result = await getUser({
method: 'GET',
pathParams: { id: 123 },
auth: { type: 'bearer', token },
});
π― No more:
Hardcoded URLs
Manual query string building
Duplicated headers/auth logic
Messy error handling
π How it Works
Define your routes once in a config file:
export default {
getUser: '/api/users/:id',
updateUser: '/api/users/:id',
};
Set global defaults for headers, baseURL, and error handling:
setControllerDefaults(config, {
baseURL: 'https://api.myapp.dev',
headers: { 'Content-Type': 'application/json' },
errorHandler: (e) => console.error('API Error', e),
});
Call any endpoint with one-liners using useController.
π Features
β Works with all HTTP methods
β Handles path + query params
β Supports Bearer & custom auth
β Built-in global error handling
β Swappable axios instance
β Upload files with FormData
π§ Ideal For:
Frontend devs (React, other SPAs, tested on react based) using RESTful APIs (especially .NET/Java/Spring/Node)
Anyone who hates repeated boilerplate
Teams that want cleaner, safer API usage
π¦ Try it Out
npm install em-use-controller
And view on npm
Finally grab the demo on https://github.com/Ethern-Myth/use-controller-demo
All the best! π
Top comments (0)