@asterflow/adapter 1.0.1
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 +228 -0
- package/dist/cjs/index.cjs +149 -0
- package/dist/cjs/package.json +3 -0
- package/dist/mjs/index.js +131 -0
- package/dist/mjs/package.json +3 -0
- package/dist/types/adapters/bun.d.ts +4 -0
- package/dist/types/adapters/express.d.ts +4 -0
- package/dist/types/adapters/fastify.d.ts +4 -0
- package/dist/types/adapters/node.d.ts +4 -0
- package/dist/types/controllers/Adapter.d.ts +9 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/types/adapter.d.ts +22 -0
- package/dist/types/utils/errorHandler.d.ts +5 -0
- package/package.json +42 -0
- package/tsconfig.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# @asterflow/adapter
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+

|
|
9
|
+

|
|
10
|
+

|
|
11
|
+
|
|
12
|
+

|
|
13
|
+

|
|
14
|
+
|
|
15
|
+
</div>
|
|
16
|
+
|
|
17
|
+
> HTTP adapters for the AsterFlow framework, providing a unified interface for different runtimes.
|
|
18
|
+
|
|
19
|
+
## 📦 Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @asterflow/adapter
|
|
23
|
+
# or
|
|
24
|
+
bun install @asterflow/adapter
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 💡 About
|
|
28
|
+
|
|
29
|
+
@asterflow/adapter is an HTTP adapter system that allows AsterFlow applications to run on different execution environments. The package provides an abstraction layer that unifies the interface of different HTTP servers, allowing you to write your code once and run it on any supported runtime.
|
|
30
|
+
|
|
31
|
+
## ✨ Features
|
|
32
|
+
|
|
33
|
+
- **Multiple Runtimes:** Native support for:
|
|
34
|
+
- Node.js HTTP Server
|
|
35
|
+
- Bun
|
|
36
|
+
- Express
|
|
37
|
+
- Fastify
|
|
38
|
+
- **Unified Interface:** Consistent API regardless of runtime
|
|
39
|
+
- **Error Handling:** Robust error handling system with standardized responses
|
|
40
|
+
- **Strong Typing:** Full TypeScript support with type inference
|
|
41
|
+
- **Router Integration:** Works seamlessly with AsterFlow's routing system
|
|
42
|
+
- **Zero Configuration:** Works immediately after installation
|
|
43
|
+
- **Middleware Support:** Compatible with runtime-specific middleware
|
|
44
|
+
|
|
45
|
+
## 🚀 Usage
|
|
46
|
+
|
|
47
|
+
### Basic Example
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { AsterFlow } from '@asterflow/core'
|
|
51
|
+
import { adapters } from '@asterflow/adapter'
|
|
52
|
+
import { Router } from '@asterflow/router'
|
|
53
|
+
|
|
54
|
+
// Create an AsterFlow instance with the desired adapter
|
|
55
|
+
const app = new AsterFlow({
|
|
56
|
+
driver: adapters.node // or adapters.bun, adapters.express, adapters.fastify
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
// Define your routes
|
|
60
|
+
const router = new Router({
|
|
61
|
+
path: '/hello',
|
|
62
|
+
methods: {
|
|
63
|
+
get({ response }) {
|
|
64
|
+
return response.send('Hello World!')
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
// Register routes
|
|
70
|
+
app.controller(router)
|
|
71
|
+
|
|
72
|
+
// Start the server
|
|
73
|
+
app.listen({ port: 3000 })
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Available Adapters
|
|
77
|
+
|
|
78
|
+
#### Node.js HTTP Server
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { AsterFlow } from '@asterflow/core'
|
|
82
|
+
import { adapters } from '@asterflow/adapter'
|
|
83
|
+
|
|
84
|
+
const app = new AsterFlow({
|
|
85
|
+
driver: adapters.node
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
app.listen({ port: 3000 })
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
#### Bun
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { AsterFlow } from '@asterflow/core'
|
|
95
|
+
import { adapters } from '@asterflow/adapter'
|
|
96
|
+
|
|
97
|
+
const app = new AsterFlow({
|
|
98
|
+
driver: adapters.bun
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
app.listen({ port: 3000 })
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
#### Express
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import { AsterFlow } from '@asterflow/core'
|
|
108
|
+
import { adapters } from '@asterflow/adapter'
|
|
109
|
+
import express from 'express'
|
|
110
|
+
|
|
111
|
+
const expressApp = express()
|
|
112
|
+
const app = new AsterFlow({
|
|
113
|
+
driver: adapters.express
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
// Use Express middleware
|
|
117
|
+
expressApp.use(express.json())
|
|
118
|
+
|
|
119
|
+
app.listen(expressApp, 3000)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
#### Fastify
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
import { AsterFlow } from '@asterflow/core'
|
|
126
|
+
import { adapters } from '@asterflow/adapter'
|
|
127
|
+
import fastify from 'fastify'
|
|
128
|
+
|
|
129
|
+
const server = fastify()
|
|
130
|
+
const app = new AsterFlow({
|
|
131
|
+
driver: adapters.fastify
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
app.listen(server, { port: 3000 }, (err) => {
|
|
135
|
+
if (err) {
|
|
136
|
+
console.error(err)
|
|
137
|
+
process.exit(1)
|
|
138
|
+
}
|
|
139
|
+
console.log('Server listening!')
|
|
140
|
+
})
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## 🔧 Architecture
|
|
144
|
+
|
|
145
|
+
### Adapter System
|
|
146
|
+
|
|
147
|
+
The package uses an adapter system that implements the `Adapter` interface:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
class Adapter<Type extends Runtime> {
|
|
151
|
+
readonly runtime: Type
|
|
152
|
+
readonly listen: OptionsDriver<Type>['listen']
|
|
153
|
+
onRequest?: (request: Request, response: Response) => Promise<Response> | Response
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Each adapter implements:
|
|
158
|
+
- Runtime-specific server initialization
|
|
159
|
+
- Request conversion to AsterFlow format
|
|
160
|
+
- Standardized error handling
|
|
161
|
+
- Integration with the routing system
|
|
162
|
+
|
|
163
|
+
### Error Handling
|
|
164
|
+
|
|
165
|
+
The system includes robust error handling that:
|
|
166
|
+
- Standardizes error responses
|
|
167
|
+
- Provides stack traces in development environment
|
|
168
|
+
- Logs errors for diagnostics
|
|
169
|
+
- Maintains security by not exposing sensitive details in production
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
interface ErrorPayload {
|
|
173
|
+
statusCode: number
|
|
174
|
+
error: string
|
|
175
|
+
message: string
|
|
176
|
+
details?: unknown
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## 📚 API Reference
|
|
181
|
+
|
|
182
|
+
### Main Types
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
enum Runtime {
|
|
186
|
+
Bun = 'bun',
|
|
187
|
+
Express = 'express',
|
|
188
|
+
Node = 'node',
|
|
189
|
+
Fastify = 'fastify'
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
interface OptionsDriver<Type extends Runtime> {
|
|
193
|
+
runtime: Type
|
|
194
|
+
listen: (...params: ListenParams<Type>) => void
|
|
195
|
+
onRequest?: (request: Request, response?: Response) => Response | Promise<Response>
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Configuration Options
|
|
200
|
+
|
|
201
|
+
Each adapter accepts runtime-specific options while maintaining proper typing:
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
// Node/Bun
|
|
205
|
+
interface ListenOptions {
|
|
206
|
+
port: number
|
|
207
|
+
host?: string
|
|
208
|
+
backlog?: number
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Fastify
|
|
212
|
+
interface FastifyOptions extends FastifyListenOptions {
|
|
213
|
+
// Fastify-specific options
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Express
|
|
217
|
+
type ExpressOptions = Express.Application
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## 🔗 Related Packages
|
|
221
|
+
|
|
222
|
+
- [@asterflow/core](https://github.com/Ashu11-A/AsterFlow/tree/main/core) - Core framework
|
|
223
|
+
- [@asterflow/router](https://github.com/Ashu11-A/AsterFlow/tree/main/packages/router) - Routing system
|
|
224
|
+
- [@asterflow/request](https://github.com/Ashu11-A/AsterFlow/tree/main/packages/request) - Unified HTTP request system
|
|
225
|
+
|
|
226
|
+
## 📄 License
|
|
227
|
+
|
|
228
|
+
MIT - See [LICENSE](https://github.com/Ashu11-A/AsterFlow/blob/main/LICENSE) for more details.
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var a = Object.defineProperty;
|
|
3
|
+
var w = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var T = Object.getOwnPropertyNames;
|
|
5
|
+
var F = Object.prototype.hasOwnProperty;
|
|
6
|
+
var O = (e, t) => {
|
|
7
|
+
for (var r in t)
|
|
8
|
+
a(e, r, { get: t[r], enumerable: !0 });
|
|
9
|
+
}, C = (e, t, r, n) => {
|
|
10
|
+
if (t && typeof t == "object" || typeof t == "function")
|
|
11
|
+
for (let s of T(t))
|
|
12
|
+
!F.call(e, s) && s !== r && a(e, s, { get: () => t[s], enumerable: !(n = w(t, s)) || n.enumerable });
|
|
13
|
+
return e;
|
|
14
|
+
};
|
|
15
|
+
var L = (e) => C(a({}, "__esModule", { value: !0 }), e);
|
|
16
|
+
|
|
17
|
+
// packages/adapter/src/index.ts
|
|
18
|
+
var S = {};
|
|
19
|
+
O(S, {
|
|
20
|
+
Adapter: () => o,
|
|
21
|
+
Runtime: () => i,
|
|
22
|
+
adapters: () => A
|
|
23
|
+
});
|
|
24
|
+
module.exports = L(S);
|
|
25
|
+
|
|
26
|
+
// packages/adapter/src/types/adapter.ts
|
|
27
|
+
var B = require("@asterflow/router"), N = require("express"), i = /* @__PURE__ */ ((s) => (s.Bun = "bun", s.Express = "express", s.Node = "node", s.Fastify = "fastify", s))(i || {});
|
|
28
|
+
|
|
29
|
+
// packages/adapter/src/controllers/Adapter.ts
|
|
30
|
+
var U = require("@asterflow/router"), o = class {
|
|
31
|
+
runtime;
|
|
32
|
+
listen;
|
|
33
|
+
onRequest = void 0;
|
|
34
|
+
constructor({ runtime: t, listen: r }) {
|
|
35
|
+
this.runtime = t, this.listen = r;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// packages/adapter/src/adapters/bun.ts
|
|
40
|
+
var m = require("@asterflow/request");
|
|
41
|
+
var f = require("@asterflow/router"), d = new o({
|
|
42
|
+
runtime: "bun",
|
|
43
|
+
listen(e, t) {
|
|
44
|
+
try {
|
|
45
|
+
Bun.serve({
|
|
46
|
+
...e,
|
|
47
|
+
fetch: async (r) => this.onRequest ? (await this.onRequest(new m.BunRequest(r))).toResponse() : new f.Response().notFound({
|
|
48
|
+
statusCode: 500,
|
|
49
|
+
error: "Internal Server Error",
|
|
50
|
+
message: "The onRequest() function must be defined before the listen() function."
|
|
51
|
+
}).toResponse()
|
|
52
|
+
}), t?.(null);
|
|
53
|
+
} catch (r) {
|
|
54
|
+
t?.(r);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// packages/adapter/src/adapters/fastify.ts
|
|
60
|
+
var l = require("@asterflow/router");
|
|
61
|
+
var R = require("@asterflow/request"), y = new o({
|
|
62
|
+
runtime: "fastify",
|
|
63
|
+
listen(e, t, r) {
|
|
64
|
+
e.all("*", async (n) => this.onRequest ? (await this.onRequest(new R.FastifyRequest(n))).toResponse() : new l.Response().notFound({
|
|
65
|
+
statusCode: 500,
|
|
66
|
+
error: "Internal Server Error",
|
|
67
|
+
message: "The onRequest() function must be defined before the listen() function."
|
|
68
|
+
})), e.listen(t, r);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// packages/adapter/src/adapters/node.ts
|
|
73
|
+
var x = require("http"), E = require("@asterflow/router");
|
|
74
|
+
var q = require("@asterflow/request");
|
|
75
|
+
|
|
76
|
+
// packages/adapter/src/utils/errorHandler.ts
|
|
77
|
+
var u = require("@asterflow/router");
|
|
78
|
+
function c(e) {
|
|
79
|
+
let t;
|
|
80
|
+
return e instanceof u.Response ? e : (e instanceof Error ? t = {
|
|
81
|
+
statusCode: 500,
|
|
82
|
+
error: e.name,
|
|
83
|
+
message: e.message,
|
|
84
|
+
// Optionally attach stack in non-prod
|
|
85
|
+
details: process.env.NODE_ENV !== "production" ? e.stack : void 0
|
|
86
|
+
} : t = {
|
|
87
|
+
statusCode: 500,
|
|
88
|
+
error: "InternalServerError",
|
|
89
|
+
message: typeof e == "string" ? e : "An unexpected error occurred.",
|
|
90
|
+
details: e
|
|
91
|
+
}, console.error("Unhandled exception in adapter:", e), new u.Response().status(t.statusCode).json({
|
|
92
|
+
error: t.error,
|
|
93
|
+
message: t.message,
|
|
94
|
+
...t.details ? { details: t.details } : {}
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// packages/adapter/src/adapters/node.ts
|
|
99
|
+
var h = new o({
|
|
100
|
+
runtime: "node",
|
|
101
|
+
listen(e, t) {
|
|
102
|
+
try {
|
|
103
|
+
(0, x.createServer)(async (r, n) => {
|
|
104
|
+
if (!this.onRequest) return new E.Response().notFound({
|
|
105
|
+
statusCode: 500,
|
|
106
|
+
error: "Internal Server Error",
|
|
107
|
+
message: "The onRequest() function must be defined before the listen() function."
|
|
108
|
+
});
|
|
109
|
+
try {
|
|
110
|
+
return (await this.onRequest(new q.NodeRequest(r))).toServerResponse(n);
|
|
111
|
+
} catch (s) {
|
|
112
|
+
return c(s).toServerResponse(n);
|
|
113
|
+
}
|
|
114
|
+
}).listen(e), t?.(null);
|
|
115
|
+
} catch (r) {
|
|
116
|
+
t?.(r);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// packages/adapter/src/adapters/express.ts
|
|
122
|
+
var $ = require("express"), g = require("@asterflow/router");
|
|
123
|
+
var v = require("@asterflow/request"), b = new o({
|
|
124
|
+
runtime: "express",
|
|
125
|
+
listen(e, ...t) {
|
|
126
|
+
return e.all("/{*path}", async (r, n) => {
|
|
127
|
+
if (!this.onRequest) {
|
|
128
|
+
let p = new g.Response().notFound({
|
|
129
|
+
statusCode: 500,
|
|
130
|
+
error: "Internal Server Error",
|
|
131
|
+
message: "The onRequest() function must be defined before the listen() function."
|
|
132
|
+
}).toResponse();
|
|
133
|
+
n.status(p.status).set(Object.fromEntries(p.headers)).send(await p.text());
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
let s = (await this.onRequest(new v.ExpressRequest(r))).toResponse();
|
|
137
|
+
n.status(s.status).set(Object.fromEntries(s.headers)).send(await s.text());
|
|
138
|
+
}), e.listen(...t), e;
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// packages/adapter/src/index.ts
|
|
143
|
+
var A = { bun: d, fastify: y, node: h, express: b };
|
|
144
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
145
|
+
0 && (module.exports = {
|
|
146
|
+
Adapter,
|
|
147
|
+
Runtime,
|
|
148
|
+
adapters
|
|
149
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// packages/adapter/src/types/adapter.ts
|
|
2
|
+
import "@asterflow/router";
|
|
3
|
+
import "express";
|
|
4
|
+
var i = /* @__PURE__ */ ((s) => (s.Bun = "bun", s.Express = "express", s.Node = "node", s.Fastify = "fastify", s))(i || {});
|
|
5
|
+
|
|
6
|
+
// packages/adapter/src/controllers/Adapter.ts
|
|
7
|
+
import "@asterflow/router";
|
|
8
|
+
var o = class {
|
|
9
|
+
runtime;
|
|
10
|
+
listen;
|
|
11
|
+
onRequest = void 0;
|
|
12
|
+
constructor({ runtime: t, listen: r }) {
|
|
13
|
+
this.runtime = t, this.listen = r;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// packages/adapter/src/adapters/bun.ts
|
|
18
|
+
import { BunRequest as R } from "@asterflow/request";
|
|
19
|
+
import { Response as y } from "@asterflow/router";
|
|
20
|
+
var a = new o({
|
|
21
|
+
runtime: "bun",
|
|
22
|
+
listen(e, t) {
|
|
23
|
+
try {
|
|
24
|
+
Bun.serve({
|
|
25
|
+
...e,
|
|
26
|
+
fetch: async (r) => this.onRequest ? (await this.onRequest(new R(r))).toResponse() : new y().notFound({
|
|
27
|
+
statusCode: 500,
|
|
28
|
+
error: "Internal Server Error",
|
|
29
|
+
message: "The onRequest() function must be defined before the listen() function."
|
|
30
|
+
}).toResponse()
|
|
31
|
+
}), t?.(null);
|
|
32
|
+
} catch (r) {
|
|
33
|
+
t?.(r);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// packages/adapter/src/adapters/fastify.ts
|
|
39
|
+
import { Response as c } from "@asterflow/router";
|
|
40
|
+
import { FastifyRequest as x } from "@asterflow/request";
|
|
41
|
+
var u = new o({
|
|
42
|
+
runtime: "fastify",
|
|
43
|
+
listen(e, t, r) {
|
|
44
|
+
e.all("*", async (n) => this.onRequest ? (await this.onRequest(new x(n))).toResponse() : new c().notFound({
|
|
45
|
+
statusCode: 500,
|
|
46
|
+
error: "Internal Server Error",
|
|
47
|
+
message: "The onRequest() function must be defined before the listen() function."
|
|
48
|
+
})), e.listen(t, r);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// packages/adapter/src/adapters/node.ts
|
|
53
|
+
import { createServer as E } from "http";
|
|
54
|
+
import { Response as q } from "@asterflow/router";
|
|
55
|
+
import { NodeRequest as h } from "@asterflow/request";
|
|
56
|
+
|
|
57
|
+
// packages/adapter/src/utils/errorHandler.ts
|
|
58
|
+
import { Response as m } from "@asterflow/router";
|
|
59
|
+
function f(e) {
|
|
60
|
+
let t;
|
|
61
|
+
return e instanceof m ? e : (e instanceof Error ? t = {
|
|
62
|
+
statusCode: 500,
|
|
63
|
+
error: e.name,
|
|
64
|
+
message: e.message,
|
|
65
|
+
// Optionally attach stack in non-prod
|
|
66
|
+
details: process.env.NODE_ENV !== "production" ? e.stack : void 0
|
|
67
|
+
} : t = {
|
|
68
|
+
statusCode: 500,
|
|
69
|
+
error: "InternalServerError",
|
|
70
|
+
message: typeof e == "string" ? e : "An unexpected error occurred.",
|
|
71
|
+
details: e
|
|
72
|
+
}, console.error("Unhandled exception in adapter:", e), new m().status(t.statusCode).json({
|
|
73
|
+
error: t.error,
|
|
74
|
+
message: t.message,
|
|
75
|
+
...t.details ? { details: t.details } : {}
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// packages/adapter/src/adapters/node.ts
|
|
80
|
+
var d = new o({
|
|
81
|
+
runtime: "node",
|
|
82
|
+
listen(e, t) {
|
|
83
|
+
try {
|
|
84
|
+
E(async (r, n) => {
|
|
85
|
+
if (!this.onRequest) return new q().notFound({
|
|
86
|
+
statusCode: 500,
|
|
87
|
+
error: "Internal Server Error",
|
|
88
|
+
message: "The onRequest() function must be defined before the listen() function."
|
|
89
|
+
});
|
|
90
|
+
try {
|
|
91
|
+
return (await this.onRequest(new h(r))).toServerResponse(n);
|
|
92
|
+
} catch (s) {
|
|
93
|
+
return f(s).toServerResponse(n);
|
|
94
|
+
}
|
|
95
|
+
}).listen(e), t?.(null);
|
|
96
|
+
} catch (r) {
|
|
97
|
+
t?.(r);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// packages/adapter/src/adapters/express.ts
|
|
103
|
+
import "express";
|
|
104
|
+
import { Response as g } from "@asterflow/router";
|
|
105
|
+
import { ExpressRequest as v } from "@asterflow/request";
|
|
106
|
+
var l = new o({
|
|
107
|
+
runtime: "express",
|
|
108
|
+
listen(e, ...t) {
|
|
109
|
+
return e.all("/{*path}", async (r, n) => {
|
|
110
|
+
if (!this.onRequest) {
|
|
111
|
+
let p = new g().notFound({
|
|
112
|
+
statusCode: 500,
|
|
113
|
+
error: "Internal Server Error",
|
|
114
|
+
message: "The onRequest() function must be defined before the listen() function."
|
|
115
|
+
}).toResponse();
|
|
116
|
+
n.status(p.status).set(Object.fromEntries(p.headers)).send(await p.text());
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
let s = (await this.onRequest(new v(r))).toResponse();
|
|
120
|
+
n.status(s.status).set(Object.fromEntries(s.headers)).send(await s.text());
|
|
121
|
+
}), e.listen(...t), e;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// packages/adapter/src/index.ts
|
|
126
|
+
var pe = { bun: a, fastify: u, node: d, express: l };
|
|
127
|
+
export {
|
|
128
|
+
o as Adapter,
|
|
129
|
+
i as Runtime,
|
|
130
|
+
pe as adapters
|
|
131
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Request } from '@asterflow/request';
|
|
2
|
+
import { Runtime, type OptionsDriver } from '../types/adapter';
|
|
3
|
+
import { Response as ResponseCustom } from '@asterflow/router';
|
|
4
|
+
export declare class Adapter<Type extends Runtime> {
|
|
5
|
+
readonly runtime: Type;
|
|
6
|
+
readonly listen: OptionsDriver<Type>['listen'];
|
|
7
|
+
onRequest: (<RequestType>(request: Request<RequestType>, response: ResponseCustom) => Promise<Response> | Response) | undefined;
|
|
8
|
+
constructor({ runtime, listen }: OptionsDriver<Type>);
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './controllers/Adapter';
|
|
2
|
+
export * from './types/adapter';
|
|
3
|
+
declare const adapters: {
|
|
4
|
+
bun: import(".").Adapter<import(".").Runtime.Bun>;
|
|
5
|
+
fastify: import(".").Adapter<import(".").Runtime.Fastify>;
|
|
6
|
+
node: import(".").Adapter<import(".").Runtime.Node>;
|
|
7
|
+
express: import(".").Adapter<import(".").Runtime.Express>;
|
|
8
|
+
};
|
|
9
|
+
export { adapters };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Request } from '@asterflow/request';
|
|
2
|
+
import { Response as ResponseCustom } from '@asterflow/router';
|
|
3
|
+
import type { ServeOptions as BunServeOptions } from 'bun';
|
|
4
|
+
import { type Express } from 'express';
|
|
5
|
+
import type { FastifyInstance, FastifyListenOptions } from 'fastify';
|
|
6
|
+
import type { ListenOptions } from 'net';
|
|
7
|
+
export declare enum Runtime {
|
|
8
|
+
Bun = "bun",
|
|
9
|
+
Express = "express",
|
|
10
|
+
Node = "node",
|
|
11
|
+
Fastify = "fastify"
|
|
12
|
+
}
|
|
13
|
+
export type ExpressListenArgs = [port: number, hostname: string, backlog: number, callback?: (err?: Error) => void] | [port: number, hostname: string, callback?: (err?: Error) => void] | [port: number, callback?: (err?: Error) => void] | [path: string, callback?: (err?: Error) => void];
|
|
14
|
+
export type FastifyListenArgs = [fastify: FastifyInstance, params: FastifyListenOptions, callback: (err: Error | null, address: string) => void];
|
|
15
|
+
export type BunListenArgs = [options: Omit<BunServeOptions, 'fetch'>, callback?: (err: Error | null) => void];
|
|
16
|
+
export type NodeListenArgs = [options: ListenOptions, callback?: (err: Error | null) => void];
|
|
17
|
+
export type ListenParams<Type extends Runtime> = Type extends Runtime.Bun ? BunListenArgs : Type extends Runtime.Fastify ? FastifyListenArgs : Type extends Runtime.Express ? [app: Express, ExpressListenArgs] : NodeListenArgs;
|
|
18
|
+
export type OptionsDriver<Type extends Runtime> = {
|
|
19
|
+
runtime: Type;
|
|
20
|
+
listen: (...params: ListenParams<Type>) => void;
|
|
21
|
+
onRequest?: <RequestType>(request: Request<RequestType>, response?: ResponseCustom) => ResponseCustom | Promise<ResponseCustom>;
|
|
22
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@asterflow/adapter",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"main": "dist/cjs/index.cjs",
|
|
5
|
+
"module": "dist/mjs/index.js",
|
|
6
|
+
"types": "dist/types/index.d.ts",
|
|
7
|
+
"typings": "dist/types/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Ashu11-A",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/Ashu11-A/AsterFlow.git"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/Ashu11-A/AsterFlow/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/Ashu11-A/AsterFlow",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/types/index.d.ts",
|
|
22
|
+
"import": "./dist/mjs/index.js",
|
|
23
|
+
"require": "./dist/cjs/index.cjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=20"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/bun": "^1.2.15",
|
|
31
|
+
"fastify": "^5.3.3",
|
|
32
|
+
"@asterflow/router": "workspace:^1.0.1",
|
|
33
|
+
"@asterflow/request": "workspace:^1.0.1"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"typescript": "^5.8.3"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@types/express": "^5.0.3",
|
|
40
|
+
"express": "^5.1.0"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"lib": [
|
|
4
|
+
"ESNext"
|
|
5
|
+
],
|
|
6
|
+
"target": "ESNext",
|
|
7
|
+
"module": "ESNext",
|
|
8
|
+
"moduleDetection": "force",
|
|
9
|
+
"jsx": "react-jsx",
|
|
10
|
+
"allowJs": true,
|
|
11
|
+
"moduleResolution": "bundler",
|
|
12
|
+
"allowImportingTsExtensions": true,
|
|
13
|
+
"verbatimModuleSyntax": true,
|
|
14
|
+
"noEmit": true,
|
|
15
|
+
"strict": true,
|
|
16
|
+
"skipLibCheck": true,
|
|
17
|
+
"noFallthroughCasesInSwitch": true,
|
|
18
|
+
"noUncheckedIndexedAccess": true,
|
|
19
|
+
"noUnusedLocals": false,
|
|
20
|
+
"noUnusedParameters": false,
|
|
21
|
+
"noPropertyAccessFromIndexSignature": false
|
|
22
|
+
},
|
|
23
|
+
"include": [
|
|
24
|
+
"dist"
|
|
25
|
+
]
|
|
26
|
+
}
|