This repository houses a TypeScript-based Request Manager built around the Observer design pattern. It provides a simple yet powerful way to handle API requests and state changes in modern JavaScript applications.
- Observer Pattern: This design pattern promotes good coding practices and provides a robust system for tracking request states.
- State Management: Gracefully handle state changes in your application. Our manager allows you to track the status of your requests from loading to completion, making debugging easier.
- API Utilities: Utility functions to interact with specific API endpoints, simplifying the process of sending requests and handling responses.
To install the necessary dependencies for the project, run the following command:
npm installThe Observer Request Manager can be used to handle HTTP requests efficiently. Below is a simple example of how to utilize the request manager:
In the src/api/api.ts file, you can implement your API calls:
import {
RequestManager,
RequestMethod,
} from "../requestManager/requestManager";
// Create an instance of the RequestManager
const requestManager = new RequestManager();
// Make a GET request to a specific endpoint
async function fetchData() {
try {
const data = await requestManager.makeRequest<ResponseData>(
RequestMethod.GET,
"https://jsonplaceholder.typicode.com/posts"
);
console.log("Fetched Data:", data);
} catch (error) {
console.error("Error fetching data:", error);
}
}
fetchData();Here's an overview of the project structure:
observer-request-manager/
βββ package.json
βββ rollup.config.cjs
βββ src/
β βββ api/
β β βββ api.ts # API request utilities
β β βββ index.ts # Example usage
β βββ requestManager/
β βββ observer.ts # Observer implementation
β βββ requestManager.ts # Request management logic
β βββ types.ts # Type definitions
βββ dist/ # Compiled output (after build)
βββ README.md # Project documentation
This project includes several scripts that can be run using npm:
build: Compiles the TypeScript files into JavaScript using Rollup. Run with:npm run build
serve: Launches a lightweight web server to serve theindex.htmlfile. Run with:npm run serve
This project is licensed under the MIT License. See the LICENSE file for details.