@superfunctions/http 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,16 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
Framework-agnostic HTTP abstraction layer for building web APIs that work across Express, Hono, Fastify, Next.js, and more.
|
|
4
4
|
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- **Framework Agnostic**: Write your API logic once, deploy to any framework
|
|
8
|
-
- **Web Standards**: Built on standard `Request` and `Response` APIs
|
|
9
|
-
- **Type Safe**: Full TypeScript support with generic context types
|
|
10
|
-
- **Zero Dependencies**: Core package has no runtime dependencies
|
|
11
|
-
- **Middleware Support**: Compose middleware chains for authentication, CORS, etc.
|
|
12
|
-
- **Path Parameters**: Express-style path patterns with parameter extraction
|
|
13
|
-
- **Edge Compatible**: Works in edge runtimes (Cloudflare Workers, Deno, Bun)
|
|
14
|
-
|
|
15
5
|
## Installation
|
|
16
6
|
|
|
17
7
|
```bash
|
|
@@ -26,6 +16,15 @@ npm install @superfunctions/http-express
|
|
|
26
16
|
|
|
27
17
|
# Hono
|
|
28
18
|
npm install @superfunctions/http-hono
|
|
19
|
+
|
|
20
|
+
# Fastify
|
|
21
|
+
npm install @superfunctions/http-fastify
|
|
22
|
+
|
|
23
|
+
# Next.js
|
|
24
|
+
npm install @superfunctions/http-next
|
|
25
|
+
|
|
26
|
+
# SvelteKit
|
|
27
|
+
npm install @superfunctions/http-sveltekit
|
|
29
28
|
```
|
|
30
29
|
|
|
31
30
|
## Quick Start
|
|
@@ -73,12 +72,12 @@ const apiRouter = createRouter({
|
|
|
73
72
|
|
|
74
73
|
```typescript
|
|
75
74
|
import express from 'express';
|
|
76
|
-
import {
|
|
75
|
+
import { toExpress } from '@superfunctions/http-express';
|
|
77
76
|
import { apiRouter } from './api';
|
|
78
77
|
|
|
79
78
|
const app = express();
|
|
80
79
|
app.use(express.json());
|
|
81
|
-
app.use('/api',
|
|
80
|
+
app.use('/api', toExpress(apiRouter));
|
|
82
81
|
app.listen(3000);
|
|
83
82
|
```
|
|
84
83
|
|
|
@@ -86,11 +85,11 @@ app.listen(3000);
|
|
|
86
85
|
|
|
87
86
|
```typescript
|
|
88
87
|
import { Hono } from 'hono';
|
|
89
|
-
import {
|
|
88
|
+
import { toHono } from '@superfunctions/http-hono';
|
|
90
89
|
import { apiRouter } from './api';
|
|
91
90
|
|
|
92
91
|
const app = new Hono();
|
|
93
|
-
app.route('/api',
|
|
92
|
+
app.route('/api', toHono(apiRouter));
|
|
94
93
|
|
|
95
94
|
export default app;
|
|
96
95
|
```
|
|
@@ -106,14 +105,14 @@ const router = createRouter({ routes: [...] });
|
|
|
106
105
|
|
|
107
106
|
// Cloudflare Workers
|
|
108
107
|
export default {
|
|
109
|
-
fetch:
|
|
108
|
+
fetch: router.handle
|
|
110
109
|
};
|
|
111
110
|
|
|
112
111
|
// Deno
|
|
113
|
-
Deno.serve(
|
|
112
|
+
Deno.serve(router.handle);
|
|
114
113
|
|
|
115
114
|
// Bun
|
|
116
|
-
Bun.serve({ fetch:
|
|
115
|
+
Bun.serve({ fetch: router.handle });
|
|
117
116
|
```
|
|
118
117
|
|
|
119
118
|
## Features
|
|
@@ -187,6 +186,41 @@ const router = createRouter({
|
|
|
187
186
|
});
|
|
188
187
|
```
|
|
189
188
|
|
|
189
|
+
### Base Path
|
|
190
|
+
|
|
191
|
+
**⚠️ Important: Only use `basePath` for standalone deployments without framework mounting.**
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
// ✅ Good: Standalone deployment (Cloudflare Workers, Vercel Edge)
|
|
195
|
+
const router = createRouter({
|
|
196
|
+
basePath: '/api/v1',
|
|
197
|
+
routes: [
|
|
198
|
+
{ method: 'GET', path: '/users', handler: ... } // Accessible at /api/v1/users
|
|
199
|
+
]
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
export default { fetch: router.handle }; // or router.handler
|
|
203
|
+
|
|
204
|
+
// ❌ Bad: With framework mounting (creates double prefixing)
|
|
205
|
+
const router = createRouter({
|
|
206
|
+
basePath: '/v1', // Don't do this!
|
|
207
|
+
routes: [...]
|
|
208
|
+
});
|
|
209
|
+
app.use('/api', toExpress(router)); // Would need /api/v1/users
|
|
210
|
+
|
|
211
|
+
// ✅ Good: With framework mounting (no basePath)
|
|
212
|
+
const router = createRouter({
|
|
213
|
+
routes: [
|
|
214
|
+
{ method: 'GET', path: '/users', handler: ... }
|
|
215
|
+
]
|
|
216
|
+
});
|
|
217
|
+
app.use('/api/v1', toExpress(router)); // Accessible at /api/v1/users
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
**Rule of thumb:**
|
|
221
|
+
- **Standalone deployment** → Use `basePath` in router
|
|
222
|
+
- **Framework mounting** → Use framework's mounting (e.g., `app.use('/path', ...)`) and omit `basePath`
|
|
223
|
+
|
|
190
224
|
### CORS
|
|
191
225
|
|
|
192
226
|
```typescript
|
|
@@ -251,10 +285,11 @@ interface RouteContext {
|
|
|
251
285
|
|
|
252
286
|
| Framework | Package | Function |
|
|
253
287
|
|-----------|---------|----------|
|
|
254
|
-
| Express | `@superfunctions/http-express` | `
|
|
255
|
-
| Hono | `@superfunctions/http-hono` | `
|
|
256
|
-
|
|
257
|
-
|
|
288
|
+
| Express | `@superfunctions/http-express` | `toExpress()`, `toExpressHandler()` |
|
|
289
|
+
| Hono | `@superfunctions/http-hono` | `toHono()` |
|
|
290
|
+
| Fastify | `@superfunctions/http-fastify` | `toFastify()` |
|
|
291
|
+
| Next.js | `@superfunctions/http-next` | `toNextHandlers()` |
|
|
292
|
+
| SvelteKit | `@superfunctions/http-sveltekit` | `toSvelteKitHandler()`, `toSvelteKitHandlers()` |
|
|
258
293
|
|
|
259
294
|
## License
|
|
260
295
|
|