This project provides a TypeScript-based, reusable API client designed to interact with REST APIs, such as JSONPlaceholder. The client supports flexible filtering, sorting, and pagination, with optional authentication, making it adaptable for various resources.
- Features
- Getting Started
- Installation
- Usage
- Project Structure
- Testing
- Contributing
- Husky Integration
- Generic API Client: Provides a reusable
apiClient
function for making HTTP requests with optional authentication. - Filtering and Pagination: Allows for advanced filtering, sorting, and pagination options.
- Concurrent Requests: Supports making multiple requests simultaneously.
- Error Handling: Built-in error handling and logging for failed requests.
- TypeScript Support: Strongly typed for predictable, safe API interactions.
To use this API client, you need to have Node.js and npm installed. This project is configured to be used in a Node or browser-based JavaScript/TypeScript environment.
-
Clone the repository:
git clone https://github.com/your-username/api-client.git cd api-client
-
Install dependencies:
npm install
-
Build the TypeScript files:
npm run build
The apiClient
function allows you to make an API call with optional authentication.
import { apiClient } from "./apiClient";
async function fetchUser() {
const user = await apiClient<User>("/users/1");
console.log(user);
}
fetchUser();
The fetchFilteredData
function allows you to fetch resources with optional filters, sorting, and pagination.
import fetchFilteredData from "./fetchFilteredData";
async function fetchFilteredUsers() {
const users = await fetchFilteredData<User>(
"users",
{ name_like: "Lea" },
{ sortBy: "name", order: "asc", limit: 5 },
);
console.log(users);
}
fetchFilteredUsers();
The fetchMultipleResources
function allows you to perform multiple API requests concurrently and returns all results in a single call.
import fetchMultipleResources from "./fetchMultipleResources";
async function fetchUsersAndPosts() {
const requests = [
{ resource: "users", filters: { name_like: "Lea" }, options: { limit: 5 } },
{
resource: "posts",
filters: { userId: 1 },
options: { sortBy: "id", order: "desc" },
},
];
const [users, posts] = await fetchMultipleResources(requests);
console.log("Users:", users);
console.log("Posts:", posts);
}
fetchUsersAndPosts();
The project follows a modular structure to organize source code, configuration, and tests effectively.
/project-root
├── /src
│ ├── /services # Contains the API client and service functions
│ │ ├── apiClient.ts # Main API client function for making requests
│ │ └── userService.ts # User-related API requests
│ ├── /types # TypeScript type definitions and interfaces
│ └── /tests # Unit and integration tests
├── README.md # Project documentation
├── tsconfig.json # TypeScript configuration file
└── package.json # Project metadata and dependencies
- apiClient.ts: Core API client function for sending HTTP requests with optional authentication support.
- userService.ts: Provides reusable functions for managing user-related API requests, including fetching, creating, updating, and deleting users.
- tests: Contains Jest or Postman test files to verify API client functionality.
The API client can be tested using Jest. Tests cover the apiClient
, fetchFilteredData
, and fetchMultipleResources
functions to ensure they handle different API responses and errors correctly.
-
Install Jest:
npm install --save-dev jest
-
Run Tests:
npm test
- Import Postman Collection: Import the collection to Postman and set up any necessary environment variables.
- Run Tests with Newman:
newman run postman-collection.json -e postman-environment.json
Contributions are welcome! If you have suggestions, bug reports, or want to add features, feel free to open an issue or submit a pull request.
- Fork the repository.
- Create a new branch with a descriptive name.
- Make changes and commit with clear, concise commit messages.
- Push your branch and open a pull request.
This project includes Husky to automate testing on every commit.
- Husky is configured to run
npm test
automatically whenever you make agit commit
. - This ensures that all tests pass before the code is committed, helping to maintain code quality and prevent errors from being introduced.
If you need to reinstall Husky or modify its configuration, refer to the Husky documentation for more details on how to customize pre-commit hooks.
In addition to the automated testing, you can always run tests manually by executing:
npm test
With this setup, code quality is enforced through continuous testing with each commit.