Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/express/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This example demonstrates how to integrate SmoothAPI into an Express.js applicat
- Automatic retries for transient failures
- Circuit breaker protection
- Graceful fallback responses
- Request deduplication for concurrent identical requests
- Express.js integration using SmoothAPI

---
Expand Down Expand Up @@ -96,6 +97,17 @@ GET http://localhost:3001/always-fail

After repeated failures, the circuit breaker opens and returns the configured fallback response immediately without making another network request.

### GET /dedup-demo

Demonstrates SmoothAPI's request deduplication by making three identical
requests concurrently using `Promise.all()`.

All three requests target the same sandbox URL:

```text
GET http://localhost:3001/health
```

---

## Example Requests
Expand All @@ -106,6 +118,7 @@ You can test the endpoints with curl:
curl http://localhost:3002/health
curl http://localhost:3002/retry-demo
curl http://localhost:3002/circuit-demo
curl http://localhost:3002/dedup-demo
```

Or you could open the endpoints on any browser
Expand Down
32 changes: 31 additions & 1 deletion examples/express/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,21 @@ const customSmoothFetch = createSmoothFetch({
timeoutMs: 5000
});

const dedupSmoothFetch = createSmoothFetch({
deduplication: {}
});
Comment thread
AryanSharma48 marked this conversation as resolved.

app.use(express.json());

app.get('/', (_req, res) => {
// The root endpoint of the example express server.
res.json({
message: 'Welcome to the SmoothAPI Express Example!',
description: 'Demonstrates retries, circuit breakers, and fallback handling using SmoothAPI.',
description: 'Demonstrates retries, circuit breakers, fallback handling, and request deduplication using SmoothAPI.',
endpoints: {
retry_demo: '/retry-demo',
circuit_demo: '/circuit-demo',
dedup_demo: '/dedup-demo',
},
});
});
Expand Down Expand Up @@ -114,6 +119,31 @@ app.get('/circuit-demo', async (_req, res) => {
}
});

app.get('/dedup-demo', async (_req, res) => {
try {
const [response1, response2, response3] = await Promise.all([
dedupSmoothFetch(`${SANDBOX_URL}/health`),
dedupSmoothFetch(`${SANDBOX_URL}/health`),
dedupSmoothFetch(`${SANDBOX_URL}/health`),
]);

const [data1, data2, data3] = await Promise.all([
response1.json(),
response2.json(),
response3.json(),
]);

return res.json({
message: 'Three identical requests were made concurrently.',
results: [data1, data2, data3],
});
} catch (error) {
return res.status(502).json({
error: error instanceof Error ? error.message : String(error),
});
}
});

app.get('/health', (_req, res) => {
res.json({ status: 'ok' });
});
Expand Down
Loading