A lightweight, type-safe HTTP client for JavaScript and TypeScript applications. Built on top of the Fetch API with extended functionality for modern web and Node.js applications.
- π Simple and intuitive API
- π¦ Tiny footprint with zero dependencies
- π Request and response interceptors
- βοΈ Configurable defaults
- π Type-safe with TypeScript generics
- π Works in browsers and Node.js
- β±οΈ Timeout support
- π οΈ Chainable instance configuration
# Using npm
npm install @breimerct/brex
# Using yarn
yarn add @breimerct/brex
# Using pnpm
pnpm add @breimerct/breximport { GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS } from '@breimerct/brex';
// Simple GET request
GET('https://api.example.com/users').then((response) => {
if (!response.error) {
console.log(response.content);
} else {
console.error(response.error);
}
});
// POST request with body
POST('https://api.example.com/users', {
name: 'John Doe',
email: 'john@example.com',
}).then((response) => console.log(response.content));
// PUT request
PUT('https://api.example.com/users/1', {
name: 'Updated Name',
}).then((response) => console.log(response.status));
// DELETE request
DELETE('https://api.example.com/users/1').then((response) =>
console.log('User deleted:', !response.error),
);
// PATCH request
PATCH('https://api.example.com/users/1', {
email: 'john.new@example.com',
}).then((response) => {
console.log('User updated:', !response.error);
});
// HEAD request
HEAD('https://api.example.com/users/1').then((response) => {
console.log('Response headers:', response.headers);
});
// OPTIONS request
OPTIONS('https://api.example.com/users').then((response) => {
console.log('Allowed methods:', response.headers['allow']);
});interface User {
id: number;
name: string;
email: string;
}
// Type-safe responses
GET<User[]>('https://api.example.com/users').then((response) => {
const users = response.content;
users.forEach((user) => console.log(user.name));
});
// Type-safe single response
GET<User>('https://api.example.com/users/1').then((response) => {
const user = response.content;
console.log(user.email);
});import { createBrexClient, Brex } from '@breimerct/brex';
// Create a custom client with configuration
const api = createBrexClient({
baseURL: 'https://api.example.com',
headers: {
Authorization: 'Bearer your-token',
'Content-Type': 'application/json',
},
timeout: 5000, // 5 seconds
});
// Use the custom client
api.get('/users').then((response) => console.log(response.content));
// Chain configuration methods
api
.setHeader('X-API-Key', 'your-api-key')
.setTimeout(10000)
.get('/users/premium')
.then((response) => console.log(response.content));
// Or use the Brex class directly
const client = new Brex({
baseURL: 'https://api.example.com',
});
client.get('/users').then((response) => console.log(response.content));import { createBrexClient } from '@breimerct/brex';
const api = createBrexClient({
baseURL: 'https://api.example.com',
});
// Add request interceptor
api.addRequestInterceptor((config) => {
console.log('Request:', config);
// Add authentication token
config.headers = {
...config.headers,
Authorization: 'Bearer ' + localStorage.getItem('token'),
};
return config;
});
// Add response interceptor
api.addResponseInterceptor((response) => {
console.log('Response:', response);
// Transform response data
if (response.content && !response.error) {
response.content = response.content.data;
}
return response;
});
// Make requests with interceptors applied
api.get('/users').then((response) => console.log(response.content));import { GET } from '@breimerct/brex';
GET('https://api.example.com/users')
.then((response) => {
if (response.error) {
console.error(`Error ${response.status}: ${response.error.message}`);
console.error('Error code:', response.error.code);
return;
}
console.log('Success:', response.content);
})
.catch((error) => {
// This catches network errors or other exceptions
console.error('Unexpected error:', error);
});GET<T>(url, config?): Make a GET requestPOST<T>(url, body?, config?): Make a POST requestPUT<T>(url, body?, config?): Make a PUT requestDELETE<T>(url, config?): Make a DELETE requestcreateBrexClient(config?): Create a new instance with custom configuration
The HttpClient class (exported as Brex) provides more control:
const client = new Brex({
baseURL: 'https://api.example.com',
headers: { 'Content-Type': 'application/json' },
timeout: 30000,
});get<T>(url, config?): Make a GET requestpost<T>(url, body?, config?): Make a POST requestput<T>(url, body?, config?): Make a PUT requestdelete<T>(url, config?): Make a DELETE requestpatch<T>(url, body?, config?): Make a PATCH requesthead<T>(url, config?): Make a HEAD requestoptions<T>(url, config?): Make an OPTIONS requestrequest<T>(config): Make a custom request
setBaseURL(url): Set the base URLsetHeaders(headers): Set multiple headerssetHeader(key, value): Set a single headersetParams(params): Set query parameterssetParam(key, value): Set a single query parametersetTimeout(timeout): Set the request timeoutaddRequestInterceptor(interceptor): Add a request interceptoraddResponseInterceptor(interceptor): Add a response interceptor
interface HttpResponse<T> {
content: T;
error: HttpError | null;
status: number;
}interface HttpError {
message: string;
status: number;
code: string;
}interface RequestConfig {
url: string;
method: HttpMethod;
headers?: HttpHeaders;
params?: QueryParams;
body?: any;
timeout?: number;
}Contributions are welcome! Here's how you can contribute to Brex:
- Fork the repository
- Clone your fork:
git clone https://github.com/breimerct/brex.git - Install dependencies:
npm install - Build the project:
npm run build - Run tests:
npm run test
- Create a new branch:
git checkout -b feature/your-feature-name - Make your changes
- Build and test:
npm run build - Commit your changes:
git commit -m "Add your feature description" - Push to your fork:
git push origin feature/your-feature-name - Create a pull request to the main repository
- Follow the existing code style
- Use TypeScript features appropriately
- Write clear, descriptive comments
- Use meaningful variable and function names
- Update the README.md if needed
- Ensure all code is properly tested
- Make sure the code builds without errors or warnings
- Update the version in package.json following semantic versioning
- Your pull request will be reviewed by maintainers
This project is licensed under the MIT License - see the LICENSE file for details.
Breimer Correa breimerct@gmail.com
Made with β€οΈ by Breimer Correa