em-use-controller
is a lightweight, flexible HTTP controller hook for React apps that simplifies API calling by abstracting route definitions, authorization, headers, query/path params, and error handling — all in one unified setup.
- ✅ Declarative API calling using controller keys instead of hardcoded URLs.
- 🔐 Built-in auth support (Bearer, custom headers, etc).
- 🔄 Full HTTP method support (
GET
,POST
,PATCH
,DELETE
, etc.). - 🌐 Dynamic path and query param handling.
- 🧪 Centralized error handling and reusable configuration.
- 🪶 Lightweight – No dependencies beyond
axios
. - ⚙️ Works with any backend (especially powerful with RESTful .NET controllers).
npm install em-use-controller
Create a controller.config.ts file:
import type { ControllerConfig } from 'em-use-controller';
const config: ControllerConfig = {
getSample: "/api/Sample/:id",
updateSample: "/api/Sample/:id",
deleteSample: "/api/Sample/:id",
createSample: "/api/Sample",
uploadSample: "/api/Sample/upload",
securePath: "/api/Sample/secure",
};
export default config;
In your app setup (e.g., App.tsx or main.tsx) or javascript:
import { setControllerDefaults } from 'em-use-controller';
import config from './controller.config.ts';
setControllerDefaults(config, {
baseURL: 'https://localhost:7269',
headers: {
'Content-Type': 'application/json',
},
errorHandler: (error) => {
console.error('Controller Error:', error);
throw error;
},
});
import { useController } from 'em-use-controller';
const getUser = useController('getUser');
const loadUser = async () => {
const result = await getUser({
method: 'GET',
pathParams: { id: 123 },
auth: {
type: 'bearer',
token: localStorage.getItem('token')!,
},
});
console.log(result.data);
};
Built-in support for multiple auth modes:
auth: { type: 'none' } // default
auth: { type: 'bearer', token: 'YOUR_JWT_TOKEN' }
auth: {
type: 'custom',
apply: (headers) => {
headers['x-api-key'] = 'custom-value';
},
}
✅ You want consistent, declarative API calls across your React app.
✅ You work with RESTful APIs (especially ASP.NET Core MVC).
✅ You need dynamic path/query support without boilerplate.
✅ You want to define routes once and reuse them easily.
✅ You need centralized error and auth handling.
🧼 No boilerplate: Write less, do more.
🧱 Scalable: Keeps your API calls clean even as your app grows.
🔌 Pluggable: Use any auth system, any backend, any content-type.
⚙️ Customizable: Swap out axios, add global error logic, support file uploads, etc.
🔐 Secure by design: Avoid accidental hardcoded tokens/URLs.
✅ Works great with token refresh systems.
✅ Plug into React Query or SWR.
✅ Add file upload support (just pass FormData).
✅ Use with custom Axios instances (e.g., interceptors, retries).
Use the demo provided to start testing, easily swap out the backend provided for any comfortable technology. For this, there was already an existing testing backend to use.
https://github.com/Ethern-Myth/use-controller-demo
MIT
Created and Maintained by: Ethern-Myth
Give a like to this project and let's share it and spread it more. Thanks.