Skip to content

Commit af2673b

Browse files
authored
feat: add docs (#3)
1 parent ac0b9ca commit af2673b

16 files changed

Lines changed: 2007 additions & 92 deletions

docs/v1/auth.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Auth
2+
3+
`@dfsync/client` supports auth configuration so you can attach tokens or other credentials to requests.
4+
5+
This allows you to centralize auth logic instead of repeating it in every request.
6+
7+
## Example
8+
9+
```ts
10+
import { createClient } from '@dfsync/client';
11+
12+
const client = createClient({
13+
baseURL: 'https://api.example.com',
14+
15+
auth: async ({ request }) => {
16+
request.headers.set('Authorization', 'Bearer TOKEN');
17+
},
18+
});
19+
```
20+
21+
Every request sent by the client will include the Authorization header.
22+
23+
## Common use cases
24+
25+
- internal service authentication
26+
- bearer tokens
27+
- API tokens
28+
- integration services
29+
30+
## Why configure auth on the client?
31+
32+
Centralizing authentication inside the client configuration provides:
33+
34+
- consistent authentication across requests
35+
- cleaner application code
36+
- easier token rotation or refresh logic
37+
38+
## Notes
39+
40+
Keep auth logic centralized in the client configuration instead of repeating it in every request.

docs/v1/create-client.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Create Client
2+
3+
Use `createClient` to configure a reusable HTTP client.
4+
5+
## Basic example
6+
7+
```ts
8+
import { createClient } from '@dfsync/client';
9+
10+
const client = createClient({
11+
baseURL: 'https://api.example.com',
12+
});
13+
```
14+
15+
## Configuration
16+
17+
```ts
18+
const client = createClient({
19+
baseURL: 'https://api.example.com',
20+
timeout: 5000,
21+
headers: {
22+
Accept: 'application/json',
23+
},
24+
});
25+
```
26+
27+
## Making requests
28+
29+
```ts
30+
const users = await client.get('/users');
31+
32+
const createdUser = await client.post('/users', {
33+
name: 'Roman',
34+
});
35+
```
36+
37+
Using a shared client keeps configuration centralized and consistent across requests.

docs/v1/errors.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Errors
2+
3+
Requests may fail due to different reasons such as HTTP errors, network issues, or timeouts.
4+
5+
Use standard `try/catch` handling to handle failures.
6+
7+
## Example
8+
9+
```ts
10+
try {
11+
const users = await client.get('/users');
12+
} catch (error) {
13+
console.error(error);
14+
}
15+
```
16+
17+
## Typical error types
18+
19+
- HTTP errors (non-2xx responses)
20+
- network errors
21+
- timeout errors
22+
23+
## Note
24+
25+
Handle errors close to the application logic that depends on the request result.

docs/v1/examples.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Examples
2+
3+
## Basic client
4+
5+
```ts
6+
const client = createClient({
7+
baseURL: 'https://api.example.com',
8+
});
9+
```
10+
11+
## GET request
12+
13+
```ts
14+
const users = await client.get('/users');
15+
```
16+
17+
## POST JSON
18+
19+
```ts
20+
const createdUser = await client.post('/users', {
21+
name: 'Tom',
22+
});
23+
```
24+
25+
## Client with auth
26+
27+
```ts
28+
const client = createClient({
29+
baseURL: 'https://api.example.com',
30+
31+
auth: async ({ request }) => {
32+
request.headers.set('Authorization', 'Bearer TOKEN');
33+
},
34+
});
35+
```

docs/v1/getting-started.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Getting Started
2+
3+
`@dfsync/client` is a lightweight TypeScript HTTP client designed for reliable service-to-service communication.
4+
5+
It provides sensible defaults for:
6+
7+
- microservices
8+
- internal APIs
9+
- worker and integration services
10+
- external API communication
11+
12+
The client focuses on predictable behavior, extensibility, and a clean developer experience.
13+
14+
## Quick example
15+
16+
```ts
17+
import { createClient } from '@dfsync/client';
18+
19+
const client = createClient({
20+
baseURL: 'https://api.example.com',
21+
});
22+
23+
const users = await client.get('/users');
24+
```
25+
26+
## Features
27+
28+
- simple HTTP client API
29+
- built-in auth support
30+
- lifecycle hooks
31+
- consistent error handling
32+
- good defaults for service communication

docs/v1/hooks.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Hooks
2+
3+
Hooks allow you to extend the behavior of the HTTP client during the request lifecycle.
4+
5+
They can be used to implement cross-cutting concerns such as logging, tracing, or instrumentation.
6+
7+
## Example
8+
9+
```ts
10+
import { createClient } from '@dfsync/client';
11+
12+
const client = createClient({
13+
baseURL: 'https://api.example.com',
14+
hooks: {
15+
beforeRequest: [
16+
async (ctx) => {
17+
console.log('Outgoing request:', ctx.request.url);
18+
},
19+
],
20+
afterResponse: [
21+
async (ctx) => {
22+
console.log('Response status:', ctx.response.status);
23+
},
24+
],
25+
},
26+
});
27+
```
28+
29+
## Common use cases
30+
31+
Hooks can be used for:
32+
33+
- request logging
34+
- tracing and correlation IDs
35+
- metrics and observability
36+
- debugging outgoing requests
37+
- modifying requests before they are sent
38+
39+
## Note
40+
41+
Use hooks for infrastructure concerns instead of duplicating logic across your application.

docs/v1/installation.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Installation
2+
3+
Install the package with your preferred package manager.
4+
5+
## npm
6+
7+
```bash
8+
npm install @dfsync/client
9+
```
10+
11+
## Requirements
12+
13+
- Node.js 18+
14+
- TypeScript (recommended)
15+
16+
## After installation
17+
18+
Create your first client instance:
19+
20+
```typescript
21+
import { createClient } from '@dfsync/client';
22+
23+
const client = createClient({
24+
baseURL: 'https://api.example.com',
25+
});
26+
```

docs/v1/retry.md

Whitespace-only changes.

0 commit comments

Comments
 (0)