Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ktranish/hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ktranish committed Nov 16, 2024
2 parents 8322081 + 7fecdff commit 42dbd02
Showing 1 changed file with 91 additions and 48 deletions.
139 changes: 91 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Hook

**Hook** is a lightweight, flexible, and enterprise-ready TypeScript library for managing HTTP requests. Built on the native `fetch` API, it offers a clean API for all HTTP methods, robust global configurations, content-type handling, and lifecycle logging for requests and responses.
>A lightweight HTTP client built on the Fetch API, designed for flexibility, configurability, and detailed lifecycle logging.
## About

**Hook** is a flexible HTTP library built directly on the Fetch API. It supports global and local configurations, lifecycle logging, and all HTTP methods, making it an ideal choice for applications that demand granular control over network requests. With TypeScript compatibility, hook ensures type safety and an enhanced developer experience.

## Features

Expand All @@ -15,10 +19,13 @@
Install Hook via your preferred package manager:

```bash
# Using npm
# npm
npm install @ktranish/hook

# Using pnpm
# yarn
yarn add @ktranish/hook

# pnpm
pnpm add @ktranish/hook
```

Expand All @@ -29,89 +36,125 @@ pnpm add @ktranish/hook
```tsx
import hook from '@ktranish/hook';

const data = await hook.get('https://api.example.com/resource');
console.log(data);
const fetchData = async () => {
const data = await hook.get('https://api.example.com/resource');
console.log(data);
};
```

### 2. POST Request with Data

```tsx
const newResource = await hook.post(
'https://api.example.com/resource',
{ name: 'New Resource' }
);
console.log(newResource);
const createResource = async () => {
const data = await hook.post('https://api.example.com/resource', { name: 'New Resource' });
console.log(data);
};
```

### 3. DELETE Request
### 3. Global Configuration

Configure global headers, credentials, or other fetch options:

```tsx
await hook.del('https://api.example.com/resource/1');
console.log('Resource deleted');
import { configureGlobal } from '@ktranish/hook';

configureGlobal({
headers: { Authorization: 'Bearer your-token' },
credentials: 'include',
});

await hook.get('https://api.example.com/resource');
```

### 4. Handle Different Content Types
### 4. Local Overrides

#### JSON Response
Override global configurations for specific requests:

```tsx
const data = await hook.get('https://api.example.com/resource');
console.log(data); // JSON parsed data
await hook.get('https://api.example.com/resource', {
headers: { 'Custom-Header': 'CustomValue' }, // Overrides global headers
cache: 'no-cache', // Specific to this request
});
```

#### Text Response
### 5. Lifecycle Logging

#### Global Logging

Configure global loggers to track all HTTP requests:

```tsx
const text = await hook.get<string>('https://api.example.com/plain-text');
console.log(text);
import { configureLogger } from '@ktranish/hook';

configureLogger({
onRequest: (url, options) => console.log(`[Global Request] ${url}`, options),
onResponse: (url, response) => console.log(`[Global Response] ${url}`, response),
onError: (url, error) => console.error(`[Global Error] ${url}`, error),
});

await hook.get('https://api.example.com/resource'); // Automatically logged
```

#### Binary Data
#### Local Logging

Override global logging with a local logger:

```tsx
const fileBuffer = await hook.get<ArrayBuffer>(
'https://api.example.com/file',
{ headers: { Accept: 'application/octet-stream' } }
const localLogger = {
onRequest: (url, options) => console.log(`[Local Request] ${url}`, options),
onResponse: (url, response) => console.log(`[Local Response] ${url}`, response),
onError: (url, error) => console.error(`[Local Error] ${url}`, error),
};

await hook.post(
'https://api.example.com/resource',
{ name: 'New Resource' },
{ logger: localLogger }
);
console.log(fileBuffer);
```

### 5. Global Configuration

You can set global headers, modes, or credentials for all requests:
### 6. Advanced Example: Combining Configurations and Logging

```tsx
import { configureGlobal } from '@ktranish/hook';
import { configureGlobal, configureLogger } from '@ktranish/hook';

configureGlobal({
headers: { Authorization: 'Bearer token' },
headers: { Authorization: 'Bearer global-token' },
credentials: 'include',
});

const data = await hook.get('https://api.example.com/secure-resource');
console.log(data);
configureLogger({
onRequest: (url, options) => console.log(`[Global Request] ${url}`, options),
onResponse: (url, response) => console.log(`[Global Response] ${url}`, response),
onError: (url, error) => console.error(`[Global Error] ${url}`, error),
});

const localLogger = {
onRequest: (url, options) => console.log(`[Local Request] ${url}`, options),
onResponse: (url, response) => console.log(`[Local Response] ${url}`, response),
};

await hook.get('https://api.example.com/resource', {
headers: { 'Custom-Header': 'CustomValue' },
logger: localLogger,
});
```

### 6. Lifecycle Logging
### 7. Custom Fetch Options

Hook provides logging hooks for requests, responses, and errors:
Hook supports all valid Fetch API options:

```tsx
import { configureLogger } from '@ktranish/hook';

configureLogger({
onRequest: (url, options) => console.log(`[Request] ${url}`, options),
onResponse: (url, response) => console.log(`[Response] ${url}`, response),
onError: (url, error) => console.error(`[Error] ${url}`, error),
await hook.get('https://api.example.com/resource', {
cache: 'no-cache',
redirect: 'manual',
integrity: 'sha256-abcdef...',
});

const data = await hook.get('https://api.example.com/resource');
console.log(data);
```

## API Reference

### Method Shortcuts
### HTTP Method Shortcuts

- `hook.get<T>(url, options)`
- `hook.post<T>(url, data, options)`
Expand Down Expand Up @@ -155,10 +198,10 @@ describe('Hook Tests', () => {
Contributions are welcome! To contribute:

1. Fork the repository.
2. Create a new branch: `git checkout -b feature-name`.
3. Commit your changes: `git commit -m 'Add some feature'`.
4. Push to the branch: `git push origin feature-name`.
5. Open a pull request.
2. Create a feature branch (`git checkout -b feature-name`).
3. Commit your changes (`git commit -m 'Add new feature'`).
4. Push to the branch (`git push origin feature-name`).
5. Open a Pull Request.

## License

Expand Down

0 comments on commit 42dbd02

Please sign in to comment.