@superfunctions/http-express 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 +14 -32
- package/dist/adapter.d.ts +7 -6
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +7 -29
- package/dist/adapter.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm install @superfunctions/http @superfunctions/http-express express
|
|
|
13
13
|
```typescript
|
|
14
14
|
import express from 'express';
|
|
15
15
|
import { createRouter } from '@superfunctions/http';
|
|
16
|
-
import {
|
|
16
|
+
import { toExpress } from '@superfunctions/http-express';
|
|
17
17
|
|
|
18
18
|
// Define your router (framework-agnostic)
|
|
19
19
|
const apiRouter = createRouter({
|
|
@@ -31,32 +31,15 @@ const apiRouter = createRouter({
|
|
|
31
31
|
// Use with Express
|
|
32
32
|
const app = express();
|
|
33
33
|
app.use(express.json()); // Important: Add body parser
|
|
34
|
-
app.use('/api',
|
|
34
|
+
app.use('/api', toExpress(apiRouter));
|
|
35
35
|
app.listen(3000);
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
## API
|
|
39
39
|
|
|
40
|
-
### `
|
|
40
|
+
### `toExpress(router)`
|
|
41
41
|
|
|
42
|
-
Converts a `@superfunctions/http` router to an Express
|
|
43
|
-
|
|
44
|
-
**Parameters:**
|
|
45
|
-
- `router`: Router instance from `@superfunctions/http`
|
|
46
|
-
|
|
47
|
-
**Returns:** `express.Router`
|
|
48
|
-
|
|
49
|
-
**Example:**
|
|
50
|
-
```typescript
|
|
51
|
-
import { toExpressRouter } from '@superfunctions/http-express';
|
|
52
|
-
|
|
53
|
-
const expressRouter = toExpressRouter(myRouter);
|
|
54
|
-
app.use('/api', expressRouter);
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### `toExpressHandler(router)`
|
|
58
|
-
|
|
59
|
-
Converts a `@superfunctions/http` router to an Express RequestHandler (single middleware function).
|
|
42
|
+
Converts a `@superfunctions/http` router to an Express RequestHandler.
|
|
60
43
|
|
|
61
44
|
**Parameters:**
|
|
62
45
|
- `router`: Router instance from `@superfunctions/http`
|
|
@@ -65,10 +48,9 @@ Converts a `@superfunctions/http` router to an Express RequestHandler (single mi
|
|
|
65
48
|
|
|
66
49
|
**Example:**
|
|
67
50
|
```typescript
|
|
68
|
-
import {
|
|
51
|
+
import { toExpress } from '@superfunctions/http-express';
|
|
69
52
|
|
|
70
|
-
|
|
71
|
-
app.use('/api', handler);
|
|
53
|
+
app.use('/api', toExpress(myRouter));
|
|
72
54
|
```
|
|
73
55
|
|
|
74
56
|
## Usage Examples
|
|
@@ -77,7 +59,7 @@ app.use('/api', handler);
|
|
|
77
59
|
|
|
78
60
|
```typescript
|
|
79
61
|
import { createRouter } from '@superfunctions/http';
|
|
80
|
-
import {
|
|
62
|
+
import { toExpress } from '@superfunctions/http-express';
|
|
81
63
|
|
|
82
64
|
const router = createRouter({
|
|
83
65
|
middleware: [
|
|
@@ -99,7 +81,7 @@ const router = createRouter({
|
|
|
99
81
|
],
|
|
100
82
|
});
|
|
101
83
|
|
|
102
|
-
app.use('/api',
|
|
84
|
+
app.use('/api', toExpress(router));
|
|
103
85
|
```
|
|
104
86
|
|
|
105
87
|
### With Custom Context
|
|
@@ -123,7 +105,7 @@ const router = createRouter<AppContext>({
|
|
|
123
105
|
],
|
|
124
106
|
});
|
|
125
107
|
|
|
126
|
-
app.use('/api',
|
|
108
|
+
app.use('/api', toExpress(router));
|
|
127
109
|
```
|
|
128
110
|
|
|
129
111
|
### Multiple Routers
|
|
@@ -132,8 +114,8 @@ app.use('/api', toExpressRouter(router));
|
|
|
132
114
|
const usersRouter = createRouter({ routes: [/* user routes */] });
|
|
133
115
|
const postsRouter = createRouter({ routes: [/* post routes */] });
|
|
134
116
|
|
|
135
|
-
app.use('/api/users',
|
|
136
|
-
app.use('/api/posts',
|
|
117
|
+
app.use('/api/users', toExpress(usersRouter));
|
|
118
|
+
app.use('/api/posts', toExpress(postsRouter));
|
|
137
119
|
```
|
|
138
120
|
|
|
139
121
|
## Important Notes
|
|
@@ -170,7 +152,7 @@ const router = createRouter({
|
|
|
170
152
|
});
|
|
171
153
|
|
|
172
154
|
// Route accessible at: /api/hello
|
|
173
|
-
app.use('/api',
|
|
155
|
+
app.use('/api', toExpress(router));
|
|
174
156
|
```
|
|
175
157
|
|
|
176
158
|
## TypeScript
|
|
@@ -179,10 +161,10 @@ Full TypeScript support with proper types:
|
|
|
179
161
|
|
|
180
162
|
```typescript
|
|
181
163
|
import type { Router } from '@superfunctions/http';
|
|
182
|
-
import type {
|
|
164
|
+
import type { RequestHandler } from 'express';
|
|
183
165
|
|
|
184
166
|
const myRouter: Router = createRouter({ routes: [...] });
|
|
185
|
-
const
|
|
167
|
+
const handler: RequestHandler = toExpress(myRouter);
|
|
186
168
|
```
|
|
187
169
|
|
|
188
170
|
## Compatibility
|
package/dist/adapter.d.ts
CHANGED
|
@@ -3,13 +3,14 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import type { Router } from '@superfunctions/http';
|
|
5
5
|
import express from 'express';
|
|
6
|
-
/**
|
|
7
|
-
* Convert a @superfunctions/http Router to an Express Router
|
|
8
|
-
*/
|
|
9
|
-
export declare function toExpressRouter(router: Router): express.Router;
|
|
10
6
|
/**
|
|
11
7
|
* Convert a @superfunctions/http Router to an Express RequestHandler
|
|
12
|
-
*
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const router = createRouter({ routes: [...] });
|
|
12
|
+
* app.use('/api', toExpress(router));
|
|
13
|
+
* ```
|
|
13
14
|
*/
|
|
14
|
-
export declare function
|
|
15
|
+
export declare function toExpress(router: Router): express.RequestHandler;
|
|
15
16
|
//# sourceMappingURL=adapter.d.ts.map
|
package/dist/adapter.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAehE"}
|
package/dist/adapter.js
CHANGED
|
@@ -1,38 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Express adapter for @superfunctions/http
|
|
3
3
|
*/
|
|
4
|
-
import express from 'express';
|
|
5
|
-
/**
|
|
6
|
-
* Convert a @superfunctions/http Router to an Express Router
|
|
7
|
-
*/
|
|
8
|
-
export function toExpressRouter(router) {
|
|
9
|
-
const expressRouter = express.Router();
|
|
10
|
-
// Get all routes from the router
|
|
11
|
-
const routes = router.getRoutes();
|
|
12
|
-
// Register each route with Express
|
|
13
|
-
for (const route of routes) {
|
|
14
|
-
const method = route.method.toLowerCase();
|
|
15
|
-
expressRouter[method](route.path, async (req, res, next) => {
|
|
16
|
-
try {
|
|
17
|
-
// Convert Express Request to Web Standard Request
|
|
18
|
-
const webRequest = await convertToWebRequest(req);
|
|
19
|
-
// Handle with router
|
|
20
|
-
const webResponse = await router.handle(webRequest);
|
|
21
|
-
// Convert Web Response to Express Response
|
|
22
|
-
await convertToExpressResponse(webResponse, res);
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
next(error);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
return expressRouter;
|
|
30
|
-
}
|
|
31
4
|
/**
|
|
32
5
|
* Convert a @superfunctions/http Router to an Express RequestHandler
|
|
33
|
-
*
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const router = createRouter({ routes: [...] });
|
|
10
|
+
* app.use('/api', toExpress(router));
|
|
11
|
+
* ```
|
|
34
12
|
*/
|
|
35
|
-
export function
|
|
13
|
+
export function toExpress(router) {
|
|
36
14
|
return async (req, res, next) => {
|
|
37
15
|
try {
|
|
38
16
|
// Convert Express Request to Web Standard Request
|
package/dist/adapter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH;;;;;;;;GAQG;AACH,MAAM,UAAU,SAAS,CAAC,MAAc;IACtC,OAAO,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC9B,IAAI,CAAC;YACH,kDAAkD;YAClD,MAAM,UAAU,GAAG,MAAM,mBAAmB,CAAC,GAAG,CAAC,CAAC;YAElD,qBAAqB;YACrB,MAAM,WAAW,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YAEpD,2CAA2C;YAC3C,MAAM,wBAAwB,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;QACnD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,KAAK,CAAC,CAAC;QACd,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,mBAAmB,CAAC,GAAoB;IACrD,iBAAiB;IACjB,2EAA2E;IAC3E,MAAM,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC;IAC9B,MAAM,IAAI,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M,EAAE,GAAG,CAAC,MAAM;QAClB,OAAO;QACP,IAAI;KACL,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,wBAAwB,CACrC,WAAqB,EACrB,eAAiC;IAEjC,aAAa;IACb,eAAe,CAAC,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAE3C,+BAA+B;IAC/B,IAAI,WAAW,CAAC,UAAU,EAAE,CAAC;QAC3B,eAAe,CAAC,aAAa,GAAG,WAAW,CAAC,UAAU,CAAC;IACzD,CAAC;IAED,cAAc;IACd,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QACzC,eAAe,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;IAEH,YAAY;IACZ,IAAI,WAAW,CAAC,IAAI,EAAE,CAAC;QACrB,MAAM,IAAI,GAAG,MAAM,WAAW,CAAC,IAAI,EAAE,CAAC;QACtC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC;SAAM,CAAC;QACN,eAAe,CAAC,GAAG,EAAE,CAAC;IACxB,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC"}
|