[](https://github.com/necipsunbul/algo-network) [](https://github.com/necipsunbul/alg
npm install algo-network-manager


A modern and flexible TypeScript library for HTTP client management. Simplifies request/response handling with interceptor support.
``bash`
npm install algo-network-manager
`typescriptnpm
import createAlgoInstance, { HttpClient } from "algo-network-manager";
const client = createAlgoInstance(
new HttpClient({
baseURL: "http://google.com.tr",
})
);
client.get("/").then((payload) => console.log(payload));
`
- 🚀 TypeScript support
- 🔧 Interceptor system
- 🌐 Flexible HTTP client configuration
- 📦 Reliable network operations based on Axios
- 🔌 Easy integration
The main function createAlgoInstance is used to create and return a NetworkManager instance.
Parameters:
- httpClient: Client that implements the IHttpClient interface (HttpClient)interceptors
- : (Optional) Array of IInterceptor
Return Value:
NetworkManager instance with the following methods:
- get(url, config?)post(url, data?, config?)
- put(url, data?, config?)
- delete(url, config?)
- patch(url, data?, config?)
-
Interceptors are used to intercept request and response operations.
`typescript
import { type IInterceptor } from "algo-network-manager";
export class AuthInterceptor implements IInterceptor {
constructor() {}
onRequest(config: any): any {
const token = localStorage.getItem("your_token_title");
if (token) {
config.headers = {
...config.headers,
Authorization: Bearer ${token},
};
}
return config;
}
onRequestError(error: any): any {
return Promise.reject(error);
}
}
`
`typescript
import createAlgoInstance, { HttpClient } from "algo-network-manager";
const authInterceptor = new AuthInterceptor(authService);
const client = createAlgoInstance(
new HttpClient({
baseURL: "https://api.example.com",
timeout: 10000,
}),
[authInterceptor]
);
// Usage
client.get("/users").then((response) => {
console.log(response);
});
client
.post("/users", {
name: "John Doe",
email: "john@example.com",
})
.then((response) => {
console.log("User created:", response);
});
`
`typescript`
const httpClient = new HttpClient({
baseURL: "https://api.example.com",
timeout: 5000,
headers: {
"Content-Type": "application/json",
},
validateStatus: (status: number) => status >= 200 && status < 300,
});
- baseURL: Base URL for all requeststimeout
- : Request timeout in milliseconds (default: 10000)headers
- : Default headers to be sent with every requestvalidateStatus
- : Function to determine if HTTP status code should be considered successful
- Type: (status: number) => booleanundefined
- Default: (uses Axios default: 200-299 range)
- Example:
`typescript
// Accept only 200 status as successful
validateStatus: (status) => status === 200;
// Accept 200-399 range as successful
validateStatus: (status) => status >= 200 && status < 400;
`
`typescript
const loggingInterceptor = new LoggingInterceptor();
const authInterceptor = new AuthInterceptor(authService);
const errorHandlerInterceptor = new ErrorHandlerInterceptor();
const client = createAlgoInstance(
new HttpClient({ baseURL: "https://api.example.com" }),
[loggingInterceptor, authInterceptor, errorHandlerInterceptor]
);
`
`typescript`
client
.get("/api/data")
.then((response) => {
console.log("Success:", response);
})
.catch((error) => {
console.error("Request failed:", error);
});
`typescript`
interface IInterceptor {
onRequest?(config: any): any;
onRequestError?(error: any): any;
onResponse?(response: any): any;
onResponseError?(error: any): any;
}
`typescript`
interface IHttpClient {
get
post
put
delete
patch
request
}
ISC
- GitHub: https://github.com/necipsunbul/algo-network
- Issues: Report bugs or request features
- Discussions: Join the community discussion
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
1. Fork the repository
2. Create your feature branch (git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature'
3. Commit your changes ()git push origin feature/amazing-feature
4. Push to the branch ()
5. Open a Pull Request
Please make sure to update tests as appropriate and follow the existing code style.
`bashClone the repository
git clone https://github.com/necipsunbul/algo-network.git