@heliosjs/aws 1.0.7 → 1.0.9
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 +15 -529
- package/dist/lambda.js +2 -2
- package/dist/lambda.js.map +1 -1
- package/dist/types/aws/lambda.d.ts +5 -3
- package/dist/types/aws/plugin.d.ts +1 -2
- package/dist/types/core/common.d.ts +7 -7
- package/dist/types/core/error.d.ts +27 -10
- package/dist/types/core/request.d.ts +11 -11
- package/dist/types/core/response.d.ts +18 -3
- package/dist/types/core/sse.d.ts +3 -3
- package/dist/types/ws/index.d.ts +7 -7
- package/dist/utils/aws/lambda.d.ts +15 -3
- package/dist/utils/aws/lambda.event.normalizers.d.ts +1 -1
- package/dist/utils/aws/lambda.event.normalizers.js +5 -6
- package/dist/utils/aws/lambda.event.normalizers.js.map +1 -1
- package/dist/utils/aws/lambda.js +97 -8
- package/dist/utils/aws/lambda.js.map +1 -1
- package/dist/utils/aws/parsers.d.ts +0 -2
- package/dist/utils/aws/parsers.js +1 -51
- package/dist/utils/aws/parsers.js.map +1 -1
- package/dist/utils/aws/plugin.d.ts +3 -3
- package/dist/utils/aws/plugin.js.map +1 -1
- package/dist/utils/aws/request.factory.js +1 -3
- package/dist/utils/aws/request.factory.js.map +1 -1
- package/dist/utils/core/controller.d.ts +4 -4
- package/dist/utils/core/controller.js +12 -13
- package/dist/utils/core/controller.js.map +1 -1
- package/dist/utils/core/cors.d.ts +3 -5
- package/dist/utils/core/cors.js +5 -69
- package/dist/utils/core/cors.js.map +1 -1
- package/dist/utils/core/error/apperror.d.ts +2 -2
- package/dist/utils/core/error/apperror.js +1 -1
- package/dist/utils/core/error/apperror.js.map +1 -1
- package/dist/utils/core/error/base.d.ts +7 -3
- package/dist/utils/core/error/base.js +8 -4
- package/dist/utils/core/error/base.js.map +1 -1
- package/dist/utils/core/error/helpers.d.ts +1 -1
- package/dist/utils/core/error/helpers.js +1 -1
- package/dist/utils/core/error/helpers.js.map +1 -1
- package/dist/utils/core/multipart.d.ts +3 -3
- package/dist/utils/core/multipart.js +2 -1
- package/dist/utils/core/multipart.js.map +1 -1
- package/dist/utils/core/request.d.ts +5 -5
- package/dist/utils/core/request.js +2 -2
- package/dist/utils/core/request.js.map +1 -1
- package/dist/utils/core/response.d.ts +32 -10
- package/dist/utils/core/response.js +7 -7
- package/dist/utils/core/response.js.map +1 -1
- package/dist/utils/core/routeWalker.d.ts +1 -1
- package/dist/utils/core/routeWalker.js +8 -8
- package/dist/utils/core/routeWalker.js.map +1 -1
- package/dist/utils/core/sanitize.d.ts +1 -1
- package/dist/utils/core/sanitize.js +1 -1
- package/dist/utils/core/sanitize.js.map +1 -1
- package/dist/utils/shared/parsers.js +2 -2
- package/dist/utils/shared/parsers.js.map +1 -1
- package/dist/utils/shared/validate.d.ts +1 -1
- package/dist/utils/shared/validate.js +1 -0
- package/dist/utils/shared/validate.js.map +1 -1
- package/dist/utils/socket/server.d.ts +3 -3
- package/dist/utils/socket/server.js +2 -2
- package/dist/utils/socket/server.js.map +1 -1
- package/dist/utils/socket/service.d.ts +3 -3
- package/dist/utils/socket/service.js.map +1 -1
- package/dist/utils/socket/socket.js.map +1 -1
- package/package.json +1 -1
- package/dist/utils/aws/helpers.d.ts +0 -4
- package/dist/utils/aws/helpers.js +0 -84
- package/dist/utils/aws/helpers.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,540 +1,26 @@
|
|
|
1
|
-
# Project Overview
|
|
2
|
-
|
|
3
|
-
This project is a decorator-based API framework supporting both HTTP and WebSocket servers. It is designed to simplify the creation of scalable and maintainable APIs with features such as controllers, middlewares, interceptors, error handling, and WebSocket support. Additionally, it supports AWS Lambda integration for serverless deployments.
|
|
4
|
-
|
|
5
|
-
# Installation
|
|
6
|
-
|
|
7
|
-
To install dependencies and build the project, run:
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
yarn install
|
|
11
|
-
# or npm install
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
# Usage
|
|
15
|
-
|
|
16
|
-
You can use controllers and server functionality by importing controllers and creating server instances as shown in the examples above. Use your preferred testing framework to write unit and integration tests.
|
|
17
|
-
|
|
18
|
-
# Project Structure
|
|
19
|
-
|
|
20
|
-
- `quantum-flow/http` - Main application source code for HTTP servers.
|
|
21
|
-
- `quantum-flow/aws` - Main application source code for AWS Lambda.
|
|
22
|
-
- `quantum-flow/core` - Core framework components like Controller and Endpoint.
|
|
23
|
-
- `quantum-flow/middlewares` - Core middlewares to use within the application.
|
|
24
|
-
- `quantum-flow/ws` - Websocket decorators.
|
|
25
|
-
- `quantum-flow/sse` - Server side events decorators.
|
|
26
|
-
- `quantum-flow/plugins/aws` - Exports plugins types for lambda.
|
|
27
|
-
- `quantum-flow/plugins/http` - Exports plugins types for http server.
|
|
28
|
-
|
|
29
1
|
---
|
|
30
2
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
Use the `@Controller` decorator to define controllers with options such as prefix, sub-controllers, middlewares, and interceptors.
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
import {
|
|
37
|
-
Body,
|
|
38
|
-
Controller,
|
|
39
|
-
Headers,
|
|
40
|
-
IWebSocketService,
|
|
41
|
-
Params,
|
|
42
|
-
PUT,
|
|
43
|
-
Query,
|
|
44
|
-
Request,
|
|
45
|
-
Response,
|
|
46
|
-
Status,
|
|
47
|
-
ANY,
|
|
48
|
-
Request,
|
|
49
|
-
Response
|
|
50
|
-
} from 'quantum-flow/core';
|
|
51
|
-
import {IsString} from 'class-validator'
|
|
52
|
-
import { Catch, Cors, Sanitize, Use, SANITIZER } from 'quantum-flow/middlewares';
|
|
53
|
-
import { InjectWS } from 'quantum-flow/ws';
|
|
54
|
-
import { HttpRequest } from 'quantum-flow/http';
|
|
55
|
-
// SANITIZER - prefilled Joi schema for common data
|
|
56
|
-
class UserDto {
|
|
57
|
-
constructor() {}
|
|
58
|
-
@IsString()
|
|
59
|
-
name: string;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
@Controller({
|
|
63
|
-
prefix: 'user',
|
|
64
|
-
controllers: [UserMetadata, ...],
|
|
65
|
-
interceptor: (data, req, res) => data,
|
|
66
|
-
})
|
|
67
|
-
@Cors({ origin: '*' })
|
|
68
|
-
@Catch((err) => ({ status: 500, err }))
|
|
69
|
-
@Use(()=>{})
|
|
70
|
-
@Sanitize({
|
|
71
|
-
schema: Joi.object({name: Joi.string().trim().min(2).max(50).required()}),
|
|
72
|
-
action: 'both',
|
|
73
|
-
options: { abortEarly: false },
|
|
74
|
-
stripUnknown: true,
|
|
75
|
-
type: 'body',
|
|
76
|
-
})
|
|
77
|
-
export class User {
|
|
78
|
-
@Status(201)
|
|
79
|
-
@PUT(':id',[(req, res)=>{} , ...middlewares])
|
|
80
|
-
@Cors({ origin: '*' })
|
|
81
|
-
async createUser(
|
|
82
|
-
@Body(UserDto) body: UserDto,
|
|
83
|
-
@Query() query: Record<string, string | string[]>,
|
|
84
|
-
@Headers() headers: Record<string, string | string[]>,
|
|
85
|
-
@Params(ParamDTO, 'param') params: string,
|
|
86
|
-
@Req() req: Response,
|
|
87
|
-
@Response() resp: Request,
|
|
88
|
-
@InjectWS() ws: IWebSocketService,
|
|
89
|
-
) {}
|
|
90
|
-
|
|
91
|
-
@ANY([...middlewares])
|
|
92
|
-
async any(@Response() resp: any) {...}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
@Controller(api, [...middlewares])
|
|
96
|
-
@Controller({
|
|
97
|
-
prefix: 'api',
|
|
98
|
-
controllers: [UserController, SocketController],
|
|
99
|
-
middlewares: [...middlewares],
|
|
100
|
-
interceptor: (data, req, res) => data,
|
|
101
|
-
})
|
|
102
|
-
@Catch((error) => ({ status: 400, error }))
|
|
103
|
-
class RootController {}
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
## Creating a http Server
|
|
107
|
-
|
|
108
|
-
Use the `@Server` decorator with configuration options like port, host, controllers, and WebSocket enablement.
|
|
109
|
-
|
|
110
|
-
```typescript
|
|
111
|
-
import { Server, Port, Host, Use, Catch, HttpServer } from 'quantum-flow/http';
|
|
112
|
-
|
|
113
|
-
@Server({ controllers: [RootController], cors: { origin: '*' } })
|
|
114
|
-
@Port(3000)
|
|
115
|
-
@Host('localhost')
|
|
116
|
-
@Use((data) => data)
|
|
117
|
-
@Catch((error) => ({ status: 400, error }))
|
|
118
|
-
class App {}
|
|
119
|
-
|
|
120
|
-
const server = new HttpServer(App);
|
|
121
|
-
|
|
122
|
-
server.listen().catch(console.error);
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
## Middlewares, Interceptors, and Error Handlers
|
|
126
|
-
|
|
127
|
-
- Use `@Use` to apply middlewares.
|
|
128
|
-
- Use `@Catch` to handle errors.
|
|
129
|
-
- Use `@Port` and `@Host` to configure server port and host.
|
|
130
|
-
- Use `@Cors` to configure cors.
|
|
131
|
-
- Use `@Sanitize` to apply sanitization to reqest.
|
|
132
|
-
|
|
133
|
-
## Request decorators
|
|
134
|
-
|
|
135
|
-
- Use `@Body` to handle request bodies.
|
|
136
|
-
- Use `@Headers` to access request headers.
|
|
137
|
-
- Use `@Query` to handle query parameters.
|
|
138
|
-
- Use `@Params` to access route parameters.
|
|
139
|
-
- Use `@Files` to access files sent within multipart/form-data requests.
|
|
140
|
-
Text sent as multipart/form-data is exposed with `@Body` decorators
|
|
141
|
-
- Use `@Req` to access the original request object.
|
|
142
|
-
- Use `@Response` to access the original object.
|
|
143
|
-
- Use `@InjectWS` to access the websocket service.
|
|
144
|
-
- Use `@InjectSSE` to access the server-side-event service.
|
|
145
|
-
|
|
146
|
-
# AWS Lambda Support
|
|
147
|
-
|
|
148
|
-
Use `LambdaAdapter` to convert API Gateway events to requests and responses. Create Lambda handlers from controllers.
|
|
149
|
-
|
|
150
|
-
```typescript
|
|
151
|
-
Example Lambda handler creation
|
|
152
|
-
import { LambdaAdapter, LambdaRequest, LambdaResponse } from 'quantum-flow/aws';
|
|
153
|
-
import { Request, Query, Headers, Params, Response, Response, Request } from 'quantum-flow/core'
|
|
154
|
-
|
|
155
|
-
@Controller({ prefix: 'user' })
|
|
156
|
-
class UserController {
|
|
157
|
-
@Body(UserDto) body: UserDto,
|
|
158
|
-
@Query() query: Record<string, string | string[]>,
|
|
159
|
-
@Headers() headers: Record<string, string | string[]>,
|
|
160
|
-
@Params(ParamDTO, 'param') params: string,
|
|
161
|
-
@Req() req: Request,
|
|
162
|
-
@Response() res: Response
|
|
163
|
-
) { }
|
|
164
|
-
}
|
|
165
|
-
const lambdaAdapter = new LambdaAdapter(UserController);
|
|
166
|
-
export const handler = lambdaAdapter.handler;
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
You can access context and event throught @Req() decorator:
|
|
170
|
-
@Req() request: LambdaRequest
|
|
171
|
-
request.context
|
|
172
|
-
request.event
|
|
173
|
-
|
|
174
|
-
# WebSocket Support
|
|
175
|
-
|
|
176
|
-
Enable WebSocket in the server configuration and register WebSocket controllers.
|
|
177
|
-
|
|
178
|
-
## Enabling WebSocket Support in Server
|
|
179
|
-
|
|
180
|
-
To enable WS support, configure your HTTP server with the `path: string` option and register controllers that use SSE.
|
|
181
|
-
|
|
182
|
-
Example server setup:
|
|
183
|
-
|
|
184
|
-
```typescript
|
|
185
|
-
@Server( { websocket: { path:'/ws' } })
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
## Injecting WebSocket events in Controller
|
|
189
|
-
|
|
190
|
-
```typescript
|
|
191
|
-
import { OnConnection, Subscribe, OnMessage } from 'quantum-flow/ws';
|
|
192
|
-
@Controller('socket')
|
|
193
|
-
export class Socket {
|
|
194
|
-
@OnConnection()
|
|
195
|
-
onConnection(event: WebSocketEvent) {
|
|
196
|
-
// Send greeting ONLY to this client
|
|
197
|
-
event.client.socket.send(JSON.stringify({ type: 'welcome', data: { message: 'Welcome!' } }));
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* 2. @Subscribe - AUTOMATIC broadcast to all subscribers
|
|
202
|
-
* No need to use WebSocketService!
|
|
203
|
-
*/
|
|
204
|
-
@Subscribe('chat')
|
|
205
|
-
onChatMessage(event: WebSocketEvent) {
|
|
206
|
-
// This method is called for EACH subscriber
|
|
207
|
-
// The message is ALREADY automatically broadcast to all!
|
|
208
|
-
|
|
209
|
-
const msg = event.message?.data;
|
|
210
|
-
// You can add logic, but no need to broadcast
|
|
211
|
-
if (msg?.text.includes('bad')) {
|
|
212
|
-
// If return empty, the message will not be sent
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// That's it, the message will be sent to subscribers automatically!
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* 4. @OnMessage for commands (without WebSocketService)
|
|
221
|
-
*/
|
|
222
|
-
@OnMessage('ping')
|
|
223
|
-
onPing(event: WebSocketEvent) {
|
|
224
|
-
// Send response only to this client
|
|
225
|
-
event.client.socket.send(
|
|
226
|
-
JSON.stringify({
|
|
227
|
-
type: 'pong',
|
|
228
|
-
data: { time: Date.now() },
|
|
229
|
-
}),
|
|
230
|
-
);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* 5. @OnMessage for subscription
|
|
235
|
-
*/
|
|
236
|
-
@OnMessage('subscribe')
|
|
237
|
-
onSubscribe(event: WebSocketEvent) {
|
|
238
|
-
const topic = event.message?.data.topic;
|
|
239
|
-
|
|
240
|
-
// Server will save the subscription automatically, no need to do anything!
|
|
241
|
-
// Just confirm
|
|
242
|
-
event.client.socket.send(
|
|
243
|
-
JSON.stringify({
|
|
244
|
-
type: 'subscribed',
|
|
245
|
-
topic,
|
|
246
|
-
data: { success: true },
|
|
247
|
-
}),
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
# Server-Sent Events (SSE) Support
|
|
254
|
-
|
|
255
|
-
The framework supports Server-Sent Events (SSE) to enable real-time, one-way communication from the server to clients over HTTP.
|
|
256
|
-
|
|
257
|
-
## Defining SSE Controllers
|
|
258
|
-
|
|
259
|
-
Use the `@Controller` decorator to define controllers with a `prefix` and optionally include sub-controllers. This allows modular organization of your API endpoints.
|
|
260
|
-
|
|
261
|
-
Example:
|
|
262
|
-
|
|
263
|
-
```typescript
|
|
264
|
-
@Controller({
|
|
265
|
-
prefix: 'user',
|
|
266
|
-
controllers: [UserMetadata],
|
|
267
|
-
middlewares: [function UserGlobalUse() {}],
|
|
268
|
-
interceptor: (data, req, res) => {
|
|
269
|
-
return { data, intercepted: true };
|
|
270
|
-
},
|
|
271
|
-
})
|
|
272
|
-
export class User {}
|
|
273
|
-
```
|
|
274
|
-
|
|
275
|
-
## Injecting SSE Service
|
|
276
|
-
|
|
277
|
-
Use the `@InjectSSE` decorator in your controller methods to create and manage SSE connections. This service allows sending events to connected clients.
|
|
278
|
-
|
|
279
|
-
Example method using SSE:
|
|
280
|
-
|
|
281
|
-
```typescript
|
|
282
|
-
import { InjectSSE } from 'quantum-flow/sse';
|
|
283
|
-
|
|
284
|
-
@Controller('user')
|
|
285
|
-
export class UserMetadata {
|
|
286
|
-
@GET('/subscribesse')
|
|
287
|
-
async subscribesse(@InjectSSE() sse) {
|
|
288
|
-
const client = sse.createConnection(res);
|
|
289
|
-
sse.sendToClient(client.id, {
|
|
290
|
-
event: 'welcome message',
|
|
291
|
-
data: { message: 'Connected to notifications' },
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
```
|
|
296
|
-
|
|
297
|
-
## SSE Event Decorators
|
|
298
|
-
|
|
299
|
-
The framework provides decorators to handle SSE connection lifecycle events:
|
|
3
|
+
````markdown
|
|
4
|
+
# HeliosJS
|
|
300
5
|
|
|
301
|
-
|
|
302
|
-
- `@OnSSEError()`: Decorate a method to handle SSE errors.
|
|
303
|
-
- `@OnSSEClose()`: Decorate a method to handle SSE connection closures.
|
|
6
|
+
🎯 A modern decorator-based Node.js framework for building scalable applications.
|
|
304
7
|
|
|
305
|
-
|
|
8
|
+
## Documentation
|
|
306
9
|
|
|
307
|
-
|
|
308
|
-
import { OnSSEConnection, OnSSEError, OnSSEClose } from 'quantum-flow/sse';
|
|
10
|
+
👉 **[Full Documentation](https://naumovoleg.github.io/helios/)**
|
|
309
11
|
|
|
310
|
-
|
|
311
|
-
export class User {
|
|
312
|
-
@OnSSEConnection()
|
|
313
|
-
async onsseconnection(@Req() req, @Response() res) {
|
|
314
|
-
console.log('SSE connection established');
|
|
315
|
-
return req.body;
|
|
316
|
-
}
|
|
12
|
+
## Packages
|
|
317
13
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
@OnSSEClose()
|
|
325
|
-
async onsseclose(@Req() req, @Response() res) {
|
|
326
|
-
console.log('SSE connection closed');
|
|
327
|
-
return req.body;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
## Sending SSE Events
|
|
333
|
-
|
|
334
|
-
Use the injected SSE service to send events to clients. You can send events to all clients or specific clients by ID.
|
|
335
|
-
|
|
336
|
-
Example:
|
|
337
|
-
|
|
338
|
-
```typescript
|
|
339
|
-
sse.sendToClient(clientId, { event: 'eventName', data: { key: 'value' } });
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
## Enabling SSE Support in Server
|
|
343
|
-
|
|
344
|
-
To enable SSE support, configure your HTTP server with the `sse: true` option and register controllers that use SSE.
|
|
345
|
-
|
|
346
|
-
Example server setup:
|
|
347
|
-
|
|
348
|
-
```typescript
|
|
349
|
-
import { Server, HttpServer } from 'quantum-flow/http';
|
|
350
|
-
import { User, UserMetadata } from './controllers';
|
|
351
|
-
|
|
352
|
-
@Server({
|
|
353
|
-
controllers: [User, UserMetadata],
|
|
354
|
-
sse: { enabled: true },
|
|
355
|
-
})
|
|
356
|
-
class App {}
|
|
357
|
-
|
|
358
|
-
const server = new HttpServer(App);
|
|
359
|
-
server.listen().catch(console.error);
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
## Enabling static files
|
|
363
|
-
|
|
364
|
-
To enable serving static files, use statics option.
|
|
365
|
-
|
|
366
|
-
Example server setup:
|
|
367
|
-
|
|
368
|
-
```typescript
|
|
369
|
-
import { Server, HttpServer } from 'quantum-flow/http';
|
|
370
|
-
import { User, UserMetadata } from './controllers';
|
|
371
|
-
|
|
372
|
-
@Server({
|
|
373
|
-
statics: [
|
|
374
|
-
{
|
|
375
|
-
path: path.join(__dirname, './public'),
|
|
376
|
-
options: {
|
|
377
|
-
index: 'index.html',
|
|
378
|
-
extensions: ['html', 'htm', 'css', 'js'],
|
|
379
|
-
maxAge: 86400,
|
|
380
|
-
immutable: true,
|
|
381
|
-
dotfiles: 'deny',
|
|
382
|
-
fallthrough: false,
|
|
383
|
-
setHeaders: (res, filePath, stats) => {
|
|
384
|
-
if (filePath.endsWith('.css')) {
|
|
385
|
-
res.setHeader('Content-Type', 'text/css');
|
|
386
|
-
}
|
|
387
|
-
},
|
|
388
|
-
},
|
|
389
|
-
},
|
|
390
|
-
],
|
|
391
|
-
})
|
|
392
|
-
class App {}
|
|
393
|
-
|
|
394
|
-
const server = new HttpServer(App);
|
|
395
|
-
server.listen().catch(console.error);
|
|
396
|
-
```
|
|
14
|
+
| Package | Version | Description |
|
|
15
|
+
| ------------------------------------------------------------------------------------------------ | ----------------------- | ---------------------- |
|
|
16
|
+
| [@heliosjs/core](https://github.com/NaumovOleg/heliosjs/tree/master/packages/core) | | Core decorators and DI |
|
|
17
|
+
| [@heliosjs/http](https://github.com/NaumovOleg/heliosjs/tree/master/packages/http) | HTTP server and routing |
|
|
18
|
+
| [@heliosjs/middlewares](https://github.com/NaumovOleg/heliosjs/tree/master/packages/middlewares) | Built-in middleware |
|
|
19
|
+
| [@heliosjs/aws](https://github.com/NaumovOleg/heliosjs/tree/master/packages/aws) | Aws support |
|
|
397
20
|
|
|
398
|
-
##
|
|
399
|
-
|
|
400
|
-
Use plugins to extend app with custom logic.
|
|
401
|
-
|
|
402
|
-
Example server setup:
|
|
403
|
-
|
|
404
|
-
```typescript
|
|
405
|
-
import { Server, HttpServer } from 'quantum-flow/http';
|
|
406
|
-
import { User, UserMetadata } from './controllers';
|
|
407
|
-
import client from 'prom-client';
|
|
408
|
-
|
|
409
|
-
const register = new client.Registry();
|
|
410
|
-
client.collectDefaultMetrics({ register });
|
|
411
|
-
|
|
412
|
-
const httpRequestsTotal = new client.Counter({
|
|
413
|
-
name: 'http_requests_total',
|
|
414
|
-
help: 'Total number of HTTP requests',
|
|
415
|
-
labelNames: ['method', 'path', 'status'],
|
|
416
|
-
});
|
|
417
|
-
|
|
418
|
-
@Controller({ prefix: 'metrics' })
|
|
419
|
-
export class MetricsController {...}
|
|
420
|
-
@Server({...})
|
|
421
|
-
class App {}
|
|
422
|
-
|
|
423
|
-
const server = new HttpServer(App);
|
|
424
|
-
server.usePligun(metricsPlugin)
|
|
425
|
-
server.listen().catch(console.error);
|
|
426
|
-
|
|
427
|
-
// Lambda setup
|
|
428
|
-
let connection;
|
|
429
|
-
const dbConnectionPlugin: Plugin = {
|
|
430
|
-
name: 'metric',
|
|
431
|
-
beforeRequest: async(server) => {
|
|
432
|
-
if(!connection){
|
|
433
|
-
connection = await connetct(...)
|
|
434
|
-
}
|
|
435
|
-
},
|
|
436
|
-
hooks: {...},
|
|
437
|
-
};
|
|
438
|
-
|
|
439
|
-
const lambdaAdapter = new LambdaAdapter(Root);
|
|
440
|
-
lambdaAdapter.usePlugin(dbConnectionPlugin);
|
|
441
|
-
|
|
442
|
-
export const handler = lambdaAdapter.handler;
|
|
443
|
-
```
|
|
444
|
-
|
|
445
|
-
## GraphQL
|
|
446
|
-
|
|
447
|
-
Quantum Flow provides seamless GraphQL integration using the popular **TypeGraphQL** library. You can build fully typed GraphQL APIs with decorators, while the framework handles the server setup, subscriptions, and PubSub.
|
|
448
|
-
|
|
449
|
-
### Installation
|
|
21
|
+
## Quick Start
|
|
450
22
|
|
|
451
23
|
```bash
|
|
452
|
-
npm install
|
|
453
|
-
# or
|
|
454
|
-
yarn add type-graphql graphql graphql-yoga @graphql-yoga/subscriptions
|
|
455
|
-
```
|
|
456
|
-
|
|
457
|
-
```typescript
|
|
458
|
-
@Server({
|
|
459
|
-
controllers: [/* your REST controllers */],
|
|
460
|
-
graphql: { // GraphQL endpoint (default: '/graphql')
|
|
461
|
-
path: '/graphql', // Enable GraphQL Playground
|
|
462
|
-
resolvers: [UserResolver, MessageResolver], // Your resolver classes
|
|
463
|
-
pubSub: pubSub, // Your PubSub instance (required for subscriptions)
|
|
464
|
-
},
|
|
465
|
-
})
|
|
466
|
-
```
|
|
467
|
-
|
|
468
|
-
## Usage example
|
|
469
|
-
|
|
470
|
-
```typescript
|
|
471
|
-
import { IsEmail, MinLength } from 'class-validator';
|
|
472
|
-
import {
|
|
473
|
-
Arg,
|
|
474
|
-
Ctx,
|
|
475
|
-
Field,
|
|
476
|
-
InputType,
|
|
477
|
-
Mutation,
|
|
478
|
-
ObjectType,
|
|
479
|
-
Query,
|
|
480
|
-
Resolver,
|
|
481
|
-
Root,
|
|
482
|
-
Subscription,
|
|
483
|
-
} from 'type-graphql';
|
|
484
|
-
|
|
485
|
-
import { createPubSub } from 'graphql-yoga';
|
|
486
|
-
|
|
487
|
-
export type PubSubChannels = {
|
|
488
|
-
NOTIFICATIONS: [{ id: string; userId: string; message: string }];
|
|
489
|
-
USER_UPDATED: [{ user: User }];
|
|
490
|
-
};
|
|
491
|
-
|
|
492
|
-
export const pubSub = createPubSub<PubSubChannels>();
|
|
493
|
-
|
|
494
|
-
@ObjectType()
|
|
495
|
-
export class User {
|
|
496
|
-
@Field()
|
|
497
|
-
id: string;
|
|
498
|
-
@Field()
|
|
499
|
-
name: string;
|
|
500
|
-
@Field()
|
|
501
|
-
email: string;
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
@InputType()
|
|
505
|
-
export class CreateUserInput {
|
|
506
|
-
@Field()
|
|
507
|
-
@MinLength(2)
|
|
508
|
-
name: string;
|
|
509
|
-
@Field()
|
|
510
|
-
@IsEmail()
|
|
511
|
-
email: string;
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
@Resolver()
|
|
515
|
-
export class UserResolver {
|
|
516
|
-
@Query(() => [User])
|
|
517
|
-
async users(): Promise<User[]> {
|
|
518
|
-
// pubSub.publish('USER_UPDATED', {...});
|
|
519
|
-
return [];
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
@Mutation(() => User)
|
|
523
|
-
async createUser(@Ctx() ctx: any, @Arg('input') input: CreateUserInput): Promise<User> {
|
|
524
|
-
const user = {
|
|
525
|
-
id: Date.now().toString(),
|
|
526
|
-
...input,
|
|
527
|
-
};
|
|
528
|
-
|
|
529
|
-
return user;
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
@Subscription(() => User, {
|
|
533
|
-
topics: 'USER_UPDATED',
|
|
534
|
-
filter: ({ payload, args }) => true,
|
|
535
|
-
})
|
|
536
|
-
newUser(@Root() user: User): User {
|
|
537
|
-
return user;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
24
|
+
npm install @heliosjs/core @heliosjs/http reflect-metadata
|
|
540
25
|
```
|
|
26
|
+
````
|
package/dist/lambda.js
CHANGED
|
@@ -91,13 +91,13 @@ class Helios extends aws_1.Plugin {
|
|
|
91
91
|
}
|
|
92
92
|
const body = JSON.stringify({
|
|
93
93
|
success: statusCode < 400,
|
|
94
|
-
data: response?.data
|
|
94
|
+
data: response?.data,
|
|
95
95
|
timestamp: new Date().toISOString(),
|
|
96
96
|
});
|
|
97
97
|
const commonResponse = {
|
|
98
98
|
statusCode,
|
|
99
99
|
headers,
|
|
100
|
-
body: response?.data
|
|
100
|
+
body: response?.data,
|
|
101
101
|
timestamp: new Date().toISOString(),
|
|
102
102
|
};
|
|
103
103
|
this.callPluginHook('afterResponse', request, response);
|
package/dist/lambda.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lambda.js","sourceRoot":"","sources":["../src/lambda.ts"],"names":[],"mappings":";;;AAAA,2CAA4C;
|
|
1
|
+
{"version":3,"file":"lambda.js","sourceRoot":"","sources":["../src/lambda.ts"],"names":[],"mappings":";;;AAAA,2CAA4C;AAW5C,qCAAoF;AACpF,uCAA0E;AAE1E,MAAa,MAAO,SAAQ,YAAM;IAChC,OAAO,CAAU;IACjB,WAAW,GAAsB,EAAE,CAAC;IACpC,OAAO,GAAmB,EAAE,CAAC;IAC7B,YAAY,UAA2B;QACrC,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;IACtC,CAAC;IAEO,aAAa;QACnB,OAAO,KAAK,EAAE,KAAkB,EAAE,OAAgB,EAAE,EAAE;YACpD,MAAM,IAAI,CAAC,cAAc,CAAC,eAAe,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;YAE3D,MAAM,SAAS,GAAG,IAAA,kBAAY,EAAC,KAAK,CAAC,CAAC;YACtC,MAAM,OAAO,GAAG,oBAAc,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YACtD,MAAM,QAAQ,GAAG,qBAAe,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAEjD,OAAO,IAAI,CAAC,cAAc,CAAC;gBACzB,OAAO;gBACP,KAAK;gBACL,SAAS;gBACT,OAAO;gBACP,QAAQ;aACT,CAAC,CAAC;QACL,CAAC,CAAC;IACJ,CAAC;IAEO,KAAK,CAAC,cAAc,CAAC,IAM5B;QACC,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC;QAC9C,IAAI,SAAS,CAAC;QAEd,KAAK,MAAM,eAAe,IAAI,IAAI,CAAC,WAAW,IAAI,EAAE,EAAE,CAAC;YACrD,MAAM,QAAQ,GAAG,IAAI,eAAe,EAAE,CAAC;YAEv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W,CAAC,KAAkB,EAAE,OAAgB;QACtD,MAAM,MAAM,GAAG;YACb,YAAY,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;YACnD,SAAS,EAAE,IAAI;SAChB,CAAC;QAEF,MAAM,UAAU,GAAG,IAAI,uBAAgB,CAAC,KAAK,EAAE;YAC7C,IAAI,EAAE,OAAO;YACb,MAAM;SACP,CAAC,CAAC;QACH,MAAM,SAAS,GAAG,IAAA,kBAAY,EAAC,OAAO,CAAC,cAAc,EAAiB,CAAC,CAAC;QACxE,MAAM,UAAU,GAAG,UAAU,CAAC,MAAM,IAAI,GAAG,CAAC;QAC5C,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;QAExC,MAAM,OAAO,GAA2B;YACtC,cAAc,EAAE,kBAAkB;YAClC,cAAc,EAAE,OAAO,CAAC,SAAS;YACjC,6BAA6B,EAAE,GAAG;YAClC,kCAAkC,EAAE,MAAM;SAC3C,CAAC;QAEF,QAAQ,SAAS,EAAE,CAAC;YAClB,KAAK,MAAM;gBACT,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,KAAK,EAAE,CAAC;YAC/D;gBACE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;QACzC,CAAC;IACH,CAAC;CACF;AAnLD,wBAmLC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { ALBEvent, APIGatewayProxyEvent, APIGatewayProxyEventV2, CloudFrontRequestEvent, Handler, LambdaFunctionURLEvent } from 'aws-lambda';
|
|
1
|
+
import { ALBEvent, ALBEventRequestContext, APIGatewayEventRequestContext, APIGatewayEventRequestContextV2, APIGatewayProxyEvent, APIGatewayProxyEventV2, CloudFrontEvent, CloudFrontRequestEvent, Handler, LambdaFunctionURLEvent } from 'aws-lambda';
|
|
2
|
+
import { Request } from '../core';
|
|
2
3
|
import { ControllerClass } from '../core/controller';
|
|
3
4
|
import { Plugin } from './plugin';
|
|
4
5
|
export interface LambdaFunctionUrlEvent {
|
|
@@ -28,6 +29,7 @@ export interface LambdaFunctionUrlEvent {
|
|
|
28
29
|
timeEpoch: number;
|
|
29
30
|
};
|
|
30
31
|
}
|
|
32
|
+
export type RequestContext = ALBEventRequestContext | APIGatewayEventRequestContext | CloudFrontEvent['config'] | APIGatewayEventRequestContextV2;
|
|
31
33
|
export type LambdaEvent = ALBEvent | LambdaFunctionURLEvent | CloudFrontRequestEvent | APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent;
|
|
32
34
|
export interface NormalizedEvent {
|
|
33
35
|
httpMethod: string;
|
|
@@ -39,14 +41,14 @@ export interface NormalizedEvent {
|
|
|
39
41
|
body: string | null;
|
|
40
42
|
isBase64Encoded: boolean;
|
|
41
43
|
cookies?: string[];
|
|
42
|
-
requestContext:
|
|
44
|
+
requestContext: RequestContext;
|
|
43
45
|
}
|
|
44
46
|
export interface LambdaApp {
|
|
45
47
|
beforeStart?: () => void;
|
|
46
48
|
}
|
|
47
49
|
export interface Lambda {
|
|
48
50
|
beforeStart?: () => Promise<void>;
|
|
49
|
-
handleRequest(request:
|
|
51
|
+
handleRequest(request: Request): Promise<unknown>;
|
|
50
52
|
}
|
|
51
53
|
export interface ILambdaAdapter {
|
|
52
54
|
handler: Handler;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { Context } from 'aws-lambda';
|
|
2
|
-
import { IncomingMessage } from 'http';
|
|
3
2
|
import { Request, Response } from '../core';
|
|
4
3
|
import { ILambdaAdapter, LambdaEvent } from './lambda';
|
|
5
4
|
export interface Hooks {
|
|
6
|
-
beforeRequest?: (req:
|
|
5
|
+
beforeRequest?: (req: LambdaEvent, context: Context) => void | Promise<void>;
|
|
7
6
|
beforeRoute?: (req: Request, response: Response) => void | Promise<void>;
|
|
8
7
|
afterResponse?: (req: Request, res: Response) => void | Promise<void>;
|
|
9
8
|
}
|
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
import { ServerResponse } from 'http';
|
|
2
|
-
import {
|
|
2
|
+
import { HeliosError } from './error';
|
|
3
3
|
import { Request } from './request';
|
|
4
4
|
import { Response } from './response';
|
|
5
5
|
export type Router = (req: Request, res?: ServerResponse) => Promise<{
|
|
6
6
|
status: number;
|
|
7
|
-
data:
|
|
7
|
+
data: unknown;
|
|
8
8
|
message?: string;
|
|
9
9
|
}>;
|
|
10
10
|
export interface IController {
|
|
11
11
|
handleRequest: Router;
|
|
12
12
|
}
|
|
13
|
-
export type MiddlewareCB = (request: Request, response: Response, next: (
|
|
14
|
-
export type InterceptorCB = (data:
|
|
15
|
-
export type ErrorCB = (error:
|
|
13
|
+
export type MiddlewareCB = (request: Request, response: Response, next: (err?: HeliosError) => Promise<any> | any) => void | Promise<Request> | Request | Promise<void> | void;
|
|
14
|
+
export type InterceptorCB = (data: unknown, req?: Request, res?: Response) => Promise<unknown> | unknown;
|
|
15
|
+
export type ErrorCB = (error: HeliosError, req?: Request, res?: Response) => unknown;
|
|
16
16
|
export type ParamDecoratorType = 'body' | 'params' | 'query' | 'request' | 'headers' | 'cookies' | 'response' | 'multipart' | 'event' | 'context' | 'sse' | 'ws';
|
|
17
17
|
export interface ParamMetadata {
|
|
18
18
|
index: number;
|
|
19
19
|
type: ParamDecoratorType;
|
|
20
|
-
dto?:
|
|
20
|
+
dto?: unknown;
|
|
21
21
|
name?: string;
|
|
22
22
|
}
|
|
23
23
|
export type ResponseWithStatus = {
|
|
24
24
|
status: number;
|
|
25
|
-
[key: string]:
|
|
25
|
+
[key: string]: unknown;
|
|
26
26
|
};
|
|
27
27
|
export declare enum HTTP_METHODS {
|
|
28
28
|
ANY = "ANY",
|