totoms 1.0.0

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.
Files changed (123) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +413 -0
  3. package/dist/api/TotoAPIController.d.ts +30 -0
  4. package/dist/api/TotoAPIController.d.ts.map +1 -0
  5. package/dist/api/TotoAPIController.js +226 -0
  6. package/dist/api/TotoAPIController.js.map +1 -0
  7. package/dist/auth/TotoToken.d.ts +3 -0
  8. package/dist/auth/TotoToken.d.ts.map +1 -0
  9. package/dist/auth/TotoToken.js +8 -0
  10. package/dist/auth/TotoToken.js.map +1 -0
  11. package/dist/core/TotoMicroservice.d.ts +27 -0
  12. package/dist/core/TotoMicroservice.d.ts.map +1 -0
  13. package/dist/core/TotoMicroservice.js +52 -0
  14. package/dist/core/TotoMicroservice.js.map +1 -0
  15. package/dist/dlg/SmokeDelegate.d.ts +12 -0
  16. package/dist/dlg/SmokeDelegate.d.ts.map +1 -0
  17. package/dist/dlg/SmokeDelegate.js +10 -0
  18. package/dist/dlg/SmokeDelegate.js.map +1 -0
  19. package/dist/evt/IMessageBus.d.ts +35 -0
  20. package/dist/evt/IMessageBus.d.ts.map +1 -0
  21. package/dist/evt/IMessageBus.js +21 -0
  22. package/dist/evt/IMessageBus.js.map +1 -0
  23. package/dist/evt/MessageBus.d.ts +37 -0
  24. package/dist/evt/MessageBus.d.ts.map +1 -0
  25. package/dist/evt/MessageBus.js +81 -0
  26. package/dist/evt/MessageBus.js.map +1 -0
  27. package/dist/evt/TotoMessage.d.ts +9 -0
  28. package/dist/evt/TotoMessage.d.ts.map +1 -0
  29. package/dist/evt/TotoMessage.js +2 -0
  30. package/dist/evt/TotoMessage.js.map +1 -0
  31. package/dist/evt/TotoMessageHandler.d.ts +16 -0
  32. package/dist/evt/TotoMessageHandler.d.ts.map +1 -0
  33. package/dist/evt/TotoMessageHandler.js +14 -0
  34. package/dist/evt/TotoMessageHandler.js.map +1 -0
  35. package/dist/evt/impl/aws/SNSImpl.d.ts +24 -0
  36. package/dist/evt/impl/aws/SNSImpl.d.ts.map +1 -0
  37. package/dist/evt/impl/aws/SNSImpl.js +201 -0
  38. package/dist/evt/impl/aws/SNSImpl.js.map +1 -0
  39. package/dist/evt/impl/aws/SQSImpl.d.ts +19 -0
  40. package/dist/evt/impl/aws/SQSImpl.d.ts.map +1 -0
  41. package/dist/evt/impl/aws/SQSImpl.js +96 -0
  42. package/dist/evt/impl/aws/SQSImpl.js.map +1 -0
  43. package/dist/evt/impl/gcp/GCPPubSubImpl.d.ts +22 -0
  44. package/dist/evt/impl/gcp/GCPPubSubImpl.d.ts.map +1 -0
  45. package/dist/evt/impl/gcp/GCPPubSubImpl.js +93 -0
  46. package/dist/evt/impl/gcp/GCPPubSubImpl.js.map +1 -0
  47. package/dist/index.d.ts +25 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +20 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/integration/RegistryCache.d.ts +24 -0
  52. package/dist/integration/RegistryCache.d.ts.map +1 -0
  53. package/dist/integration/RegistryCache.js +74 -0
  54. package/dist/integration/RegistryCache.js.map +1 -0
  55. package/dist/integration/TotoAPI.d.ts +22 -0
  56. package/dist/integration/TotoAPI.d.ts.map +1 -0
  57. package/dist/integration/TotoAPI.js +84 -0
  58. package/dist/integration/TotoAPI.js.map +1 -0
  59. package/dist/integration/TotoRegistryAPI.d.ts +26 -0
  60. package/dist/integration/TotoRegistryAPI.d.ts.map +1 -0
  61. package/dist/integration/TotoRegistryAPI.js +67 -0
  62. package/dist/integration/TotoRegistryAPI.js.map +1 -0
  63. package/dist/logger/TotoLogger.d.ts +13 -0
  64. package/dist/logger/TotoLogger.d.ts.map +1 -0
  65. package/dist/logger/TotoLogger.js +48 -0
  66. package/dist/logger/TotoLogger.js.map +1 -0
  67. package/dist/model/APIConfiguration.d.ts +17 -0
  68. package/dist/model/APIConfiguration.d.ts.map +1 -0
  69. package/dist/model/APIConfiguration.js +2 -0
  70. package/dist/model/APIConfiguration.js.map +1 -0
  71. package/dist/model/AuthProviders.d.ts +5 -0
  72. package/dist/model/AuthProviders.d.ts.map +1 -0
  73. package/dist/model/AuthProviders.js +5 -0
  74. package/dist/model/AuthProviders.js.map +1 -0
  75. package/dist/model/TotoControllerConfig.d.ts +29 -0
  76. package/dist/model/TotoControllerConfig.d.ts.map +1 -0
  77. package/dist/model/TotoControllerConfig.js +77 -0
  78. package/dist/model/TotoControllerConfig.js.map +1 -0
  79. package/dist/model/TotoDelegate.d.ts +19 -0
  80. package/dist/model/TotoDelegate.d.ts.map +1 -0
  81. package/dist/model/TotoDelegate.js +12 -0
  82. package/dist/model/TotoDelegate.js.map +1 -0
  83. package/dist/model/TotoEnvironment.d.ts +16 -0
  84. package/dist/model/TotoEnvironment.d.ts.map +1 -0
  85. package/dist/model/TotoEnvironment.js +2 -0
  86. package/dist/model/TotoEnvironment.js.map +1 -0
  87. package/dist/model/TotoPathOptions.d.ts +6 -0
  88. package/dist/model/TotoPathOptions.d.ts.map +1 -0
  89. package/dist/model/TotoPathOptions.js +2 -0
  90. package/dist/model/TotoPathOptions.js.map +1 -0
  91. package/dist/model/TotoRuntimeError.d.ts +7 -0
  92. package/dist/model/TotoRuntimeError.d.ts.map +1 -0
  93. package/dist/model/TotoRuntimeError.js +9 -0
  94. package/dist/model/TotoRuntimeError.js.map +1 -0
  95. package/dist/model/UserContext.d.ts +6 -0
  96. package/dist/model/UserContext.d.ts.map +1 -0
  97. package/dist/model/UserContext.js +2 -0
  98. package/dist/model/UserContext.js.map +1 -0
  99. package/dist/util/CorrelationId.d.ts +2 -0
  100. package/dist/util/CorrelationId.d.ts.map +1 -0
  101. package/dist/util/CorrelationId.js +7 -0
  102. package/dist/util/CorrelationId.js.map +1 -0
  103. package/dist/util/CrossCloudSecret.d.ts +10 -0
  104. package/dist/util/CrossCloudSecret.d.ts.map +1 -0
  105. package/dist/util/CrossCloudSecret.js +45 -0
  106. package/dist/util/CrossCloudSecret.js.map +1 -0
  107. package/dist/util/ErrorUtil.d.ts +3 -0
  108. package/dist/util/ErrorUtil.d.ts.map +1 -0
  109. package/dist/util/ErrorUtil.js +11 -0
  110. package/dist/util/ErrorUtil.js.map +1 -0
  111. package/dist/util/TokenUtil.d.ts +4 -0
  112. package/dist/util/TokenUtil.d.ts.map +1 -0
  113. package/dist/util/TokenUtil.js +21 -0
  114. package/dist/util/TokenUtil.js.map +1 -0
  115. package/dist/validation/GoogleAuthCheck.d.ts +7 -0
  116. package/dist/validation/GoogleAuthCheck.d.ts.map +1 -0
  117. package/dist/validation/GoogleAuthCheck.js +24 -0
  118. package/dist/validation/GoogleAuthCheck.js.map +1 -0
  119. package/dist/validation/Validator.d.ts +33 -0
  120. package/dist/validation/Validator.d.ts.map +1 -0
  121. package/dist/validation/Validator.js +111 -0
  122. package/dist/validation/Validator.js.map +1 -0
  123. package/package.json +90 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nicolas Matteazzi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,413 @@
1
+ # Toto Microservice SDK - NodeJS
2
+
3
+ The Toto Microservice SDK is a framework for building cloud-agnostic microservices. <br>
4
+ This is the NodeJS SDK documentation.
5
+
6
+ ## Table of Contents
7
+
8
+ 1. [Installation](#1-installation)
9
+ 2. [Overview](#2-overview)
10
+ 3. [Usage](#3-usage)
11
+ - [3.1. The Toto Microservice Configuration](#31-the-toto-microservice-configuration)
12
+ - [3.2. Create and Register APIs](#32-create-and-register-apis)
13
+ - [3.3. Use a Message Bus](#33-use-a-message-bus)
14
+ - [3.4. Load Secrets](#34-load-secrets)
15
+ - [3.5. Custom Configurations](#35-custom-configurations)
16
+
17
+ Other:
18
+ * [Build and Deploy on NPM](./docs/buildpublish.md)
19
+
20
+ ## 1. Installation
21
+
22
+ ```bash
23
+ npm install totoms
24
+ ```
25
+
26
+ ### Cloud-Specific Dependencies
27
+
28
+ Install the peer dependencies for your target cloud platform:
29
+
30
+ **AWS:**
31
+ ```bash
32
+ npm install @aws-sdk/client-secrets-manager @aws-sdk/client-sns @aws-sdk/client-sqs
33
+ ```
34
+
35
+ **GCP:**
36
+ ```bash
37
+ npm install @google-cloud/pubsub @google-cloud/secret-manager
38
+ ```
39
+
40
+ ## 2. Overview
41
+
42
+ Everything starts with `TotoMicroservice` and the `TotoMicroserviceConfiguration`.<br>
43
+ `TotoMicroservice` is the main orchestrator that coordinates your entire microservice. It initializes and manages:
44
+
45
+ - **API Controller & API Endpoints**: Express-based REST API setup with automatic endpoint registration
46
+ - **Message Bus & Message Handlers**: Event-driven communication via Pub/Sub and Queues. Registration and routing of event handlers to appropriate topics.
47
+ - **Secrets Management**: Automatic loading of secrets from your cloud provider
48
+ - **Service Lifecycle**: Initialization, startup, and shutdown management
49
+
50
+ The configuration is **declarative**. The goal is to make it very simple to configure a full microservice, with a syntax that will look like this:
51
+
52
+ ```typescript
53
+ import { TotoMicroservice, TotoMicroserviceConfiguration } from 'totoms';
54
+
55
+ const config: TotoMicroserviceConfiguration = {
56
+ serviceName: "my-service",
57
+ basePath: '/myservice',
58
+ environment: {
59
+ hyperscaler: "aws", // or "gcp", "azure"
60
+ aws: {
61
+ region: "us-east-1"
62
+ }
63
+ },
64
+ config: new MyControllerConfig()
65
+ };
66
+
67
+ await TotoMicroservice.init(config);
68
+ ```
69
+
70
+ The `TotoMicroserviceConfiguration` object specifies:
71
+
72
+ - **Service Metadata**: Service name and base path for API endpoints
73
+ - **Environment**: Cloud provider (AWS, GCP, Azure) information
74
+ - **API Configuration**: REST endpoints with their handlers
75
+ - **Message Bus Configuration**: Topics to subscribe to and message handlers
76
+ - **Custom Configuration**: Your application-specific settings
77
+
78
+ ## 3. Usage
79
+
80
+ ### 3.1. The Toto Microservice Configuration
81
+
82
+ The microservice is configured through the `TotoMicroserviceConfiguration` object and the `TotoControllerConfig` base class.
83
+
84
+ ```typescript
85
+ import { TotoControllerConfig } from 'totoms';
86
+
87
+ export class MyControllerConfig extends TotoControllerConfig {
88
+
89
+ mongoUser: string | undefined;
90
+ mongoPwd: string | undefined;
91
+
92
+ getMongoSecretNames() {
93
+ return {
94
+ userSecretName: 'my-mongo-user',
95
+ pwdSecretName: 'my-mongo-pswd'
96
+ };
97
+ }
98
+
99
+ getDBName() {
100
+ return 'mydb'
101
+ }
102
+
103
+ getCollections() {
104
+ return { users: 'users' }
105
+ }
106
+ }
107
+ ```
108
+
109
+ ### 3.2. Create and Register APIs
110
+
111
+ Your microservice exposes REST API endpoints using Express. <br>
112
+ Endpoints are defined when creating the API controller and are automatically set up.
113
+
114
+ #### Create a Toto Delegate
115
+
116
+ Every endpoint needs to be managed by a **Toto Delegate**. <br>
117
+ Toto Delegates implement the `TotoDelegate` interface.
118
+
119
+ This is how you define a Toto Delegate. <br>
120
+ *The following example shows a delegate that processes user creation*.
121
+
122
+ ```typescript
123
+ import { TotoDelegate, UserContext } from 'totoms';
124
+ import { Request } from 'express';
125
+
126
+ class CreateUserDelegate implements TotoDelegate {
127
+ async do(req: Request, userContext: UserContext, config: any) {
128
+ // Extract data from the request
129
+ const { name, email } = req.body;
130
+
131
+ // Your business logic here
132
+ const userId = await createUser(name, email);
133
+
134
+ // Return the response
135
+ return {
136
+ userId,
137
+ status: "success"
138
+ };
139
+ }
140
+ }
141
+ ```
142
+
143
+ #### Register Your Delegate
144
+
145
+ You can now register your endpoints with the API controller:
146
+
147
+ ```typescript
148
+ import { TotoAPIController } from 'totoms';
149
+
150
+ const api = new TotoAPIController(config, { basePath: '/myservice' });
151
+
152
+ // Register the endpoint
153
+ api.path('POST', '/users', new CreateUserDelegate());
154
+
155
+ // Start listening
156
+ api.listen();
157
+ ```
158
+
159
+ The microservice will start an Express application with all registered endpoints available at the specified base path.
160
+
161
+ ---
162
+
163
+ ### 3.3. Use a Message Bus
164
+
165
+ The Message Bus enables event-driven communication between microservices.<br>
166
+ It supports both PUSH (webhook-based from cloud Pub/Sub) and PULL (polling) delivery models, depending on your cloud provider and configuration.
167
+
168
+ #### 3.3.1. React to Messages
169
+
170
+ Message handlers are the primary way to react to events.
171
+
172
+ ##### Create a Message Handler
173
+
174
+ Create a handler by **extending** `TotoMessageHandler` and implementing the required methods:
175
+
176
+ ```typescript
177
+ import { TotoMessageHandler, TotoMessage, ProcessingResponse } from 'totoms';
178
+
179
+ class TopicRefreshedEventHandler extends TotoMessageHandler {
180
+
181
+ getHandledMessageType(): string {
182
+ // Return the message type this handler processes
183
+ return "topicRefreshed";
184
+ }
185
+
186
+ async processMessage(message: TotoMessage): Promise<ProcessingResponse> {
187
+ // Access message metadata
188
+ const correlationId = message.correlationId;
189
+ const messageId = message.id;
190
+
191
+ // Extract event data
192
+ const topicName = message.payload.name;
193
+ const blogUrl = message.payload.blogURL;
194
+ const user = message.payload.user;
195
+
196
+ // Your handler has access to context
197
+ this.logger.compute(correlationId, `Processing topic refresh for: ${topicName}`);
198
+
199
+ // Perform your business logic
200
+ await this.refreshTopic(topicName, blogUrl, user);
201
+
202
+ // Return success or failure
203
+ return { success: true };
204
+ }
205
+
206
+ private async refreshTopic(name: string, url: string, user: string) {
207
+ // Implementation here
208
+ }
209
+ }
210
+ ```
211
+
212
+ ##### Register a Message Handler
213
+
214
+ Register your message handlers with the message bus configuration.
215
+
216
+ **IMPORTANT NOTE:** <br>
217
+ * When using PubSub infrastructure, you need to register topics. <br>
218
+ Topics are registered by giving them:
219
+ * A `logical name` which is the name that will be used in the application to reference the topic.
220
+ * A topic identifier (e.g., ARN on AWS or fully-qualified Topic Name on GCP)
221
+
222
+ ```typescript
223
+ import { TotoMessageBus, MessageHandlerRegistrationOptions } from 'totoms';
224
+
225
+ const messageBus = new TotoMessageBus(config, environment);
226
+
227
+ // Register topics
228
+ messageBus.registerTopic({
229
+ logicalName: "topic-events",
230
+ topicName: process.env.TOPIC_EVENTS_TOPIC_NAME! // From environment or secrets
231
+ });
232
+
233
+ // Register message handlers
234
+ const handlerOptions: MessageHandlerRegistrationOptions = {
235
+ topic: { logicalName: "topic-events" }
236
+ };
237
+
238
+ messageBus.registerMessageHandler(
239
+ new TopicRefreshedEventHandler(),
240
+ handlerOptions
241
+ );
242
+ ```
243
+
244
+ When the microservice starts, it automatically subscribes to the configured topics and routes incoming messages to the appropriate handlers based on their message type.
245
+
246
+ #### 3.3.2. Publish Messages
247
+
248
+ You can always publish messages to topics.
249
+
250
+ **NOTE:**
251
+ * In the Message Destination, the topic is the **logical name of the topic** (see above).
252
+
253
+ ```typescript
254
+ import { TotoMessage, MessageDestination } from 'totoms';
255
+
256
+ async function publishTopicUpdate(messageBus: any, topicId: string, topicName: string) {
257
+ // Create the message
258
+ const message = new TotoMessage({
259
+ type: "topicUpdated",
260
+ correlationId: "correlation-id-123",
261
+ id: topicId,
262
+ payload: {
263
+ name: topicName,
264
+ timestamp: new Date().toISOString()
265
+ }
266
+ });
267
+
268
+ const destination: MessageDestination = {
269
+ topicName: "topic-events"
270
+ };
271
+
272
+ await messageBus.publishMessage(destination, message);
273
+ }
274
+ ```
275
+
276
+ ##### Getting Access to the Message Bus
277
+
278
+ There are different ways to get access to the Message Bus instance:
279
+
280
+ * Through the `TotoMicroservice` singleton: <br>
281
+ `TotoMicroservice.getInstance().messageBus`
282
+
283
+ * Through an existing instance of `TotoMicroservice`
284
+
285
+ * In a `TotoMessageHandler` you will have `messageBus` as an instance variable: <br>
286
+ `this.messageBus`
287
+
288
+ * In a `TotoDelegate`, you can access it through the config or by maintaining a reference in your application
289
+
290
+ ---
291
+
292
+ ### 3.4. Load Secrets
293
+
294
+ The SDK handles secret loading from your cloud provider automatically. Access secrets through the configuration or use the `SecretsManager` directly:
295
+
296
+ ```typescript
297
+ import { SecretsManager } from 'totoms';
298
+
299
+ const secrets = new SecretsManager({ hyperscaler: "aws" });
300
+
301
+ // Load a secret by name
302
+ const apiKey = await secrets.getSecret("api-key");
303
+ const databaseUrl = await secrets.getSecret("database-url");
304
+ ```
305
+
306
+ Secrets are typically stored as environment variable names or secret manager references, depending on your deployment environment.
307
+
308
+ ---
309
+
310
+ ### 3.5. Custom Configurations
311
+
312
+ You can define your own custom configurations by extending the `TotoControllerConfig` base class.
313
+
314
+ An example:
315
+
316
+ ```typescript
317
+ import { TotoControllerConfig } from 'totoms';
318
+
319
+ export class MyServiceConfig extends TotoControllerConfig {
320
+
321
+ apiKey: string | undefined;
322
+
323
+ async load(): Promise<void> {
324
+ // Load secrets using the secrets manager
325
+ this.apiKey = await this.secretsManager.getSecret("my-api-key");
326
+ }
327
+
328
+ getMongoSecretNames() {
329
+ // Return null if your service doesn't use MongoDB
330
+ return null;
331
+ }
332
+ }
333
+ ```
334
+
335
+ What you can do with a Custom Configuration:
336
+
337
+ 1. **Load Secrets** <br>
338
+ You can do that by overriding the `load()` async method and using `this.secretsManager.getSecret("your-secret-name")` to load secrets.
339
+
340
+ 2. **Configure MongoDB** <br>
341
+ Override `getMongoSecretNames()`, `getDBName()`, and `getCollections()` to configure MongoDB integration.
342
+
343
+ 3. **Custom Authentication** <br>
344
+ Override `getCustomAuthVerifier()` to provide custom authentication logic.
345
+
346
+ ## Core Components
347
+
348
+ ### TotoAPIController
349
+ The main controller for building REST APIs with Express. Provides:
350
+ - Automatic route registration
351
+ - Built-in validation
352
+ - CORS support
353
+ - Health check endpoints
354
+ - File upload support
355
+ - API documentation generation
356
+
357
+ ### TotoMicroservice
358
+ High-level wrapper that initializes the entire microservice stack including API controller, message bus, and environment configuration.
359
+
360
+ ### TotoMessageBus
361
+ Unified interface for pub/sub messaging across cloud platforms:
362
+ - **AWS**: SNS/SQS
363
+ - **GCP**: Cloud Pub/Sub
364
+ - **Azure**: Service Bus (in development)
365
+
366
+ ### TotoControllerConfig
367
+ Base configuration class for microservices with support for:
368
+ - MongoDB connection management
369
+ - Authentication settings
370
+ - Secrets management
371
+ - Custom validators
372
+
373
+ ### Logger
374
+ Structured logging with correlation ID support for request tracing.
375
+
376
+ ### Validator
377
+ Request validation framework with support for:
378
+ - JWT token validation
379
+ - Google OAuth
380
+ - Custom validation logic
381
+
382
+ ## Cloud Platform Support
383
+
384
+ ### AWS
385
+ - **Messaging**: SNS (topics), SQS (queues)
386
+ - **Secrets**: AWS Secrets Manager
387
+ - **Region Configuration**: Configurable per service
388
+
389
+ ### GCP
390
+ - **Messaging**: Cloud Pub/Sub
391
+ - **Secrets**: Secret Manager
392
+ - **Project Configuration**: Uses default project credentials
393
+
394
+ ### Azure
395
+ - **Messaging**: Service Bus (in development)
396
+ - **Secrets**: Key Vault (in development)
397
+
398
+ ## License
399
+
400
+ MIT
401
+
402
+ ## Author
403
+
404
+ nicolasances
405
+
406
+ ## Contributing
407
+
408
+ Contributions are welcome! Please feel free to submit a Pull Request to the [toto-microservice-sdk repository](https://github.com/nicolasances/toto-microservice-sdk).
409
+
410
+ ## Related Projects
411
+
412
+ - [Toto Ecosystem](https://github.com/nicolasances/toto)
413
+ - [Python Toto Microservice SDK](../python)
@@ -0,0 +1,30 @@
1
+ import { Express, Request } from 'express';
2
+ import { TotoControllerConfig } from '../model/TotoControllerConfig';
3
+ import { TotoDelegate } from '../model/TotoDelegate';
4
+ import { TotoPathOptions } from '../model/TotoPathOptions';
5
+ import { TotoEnvironment } from '..';
6
+ export declare class TotoControllerOptions {
7
+ debugMode?: boolean;
8
+ basePath?: string;
9
+ port?: number;
10
+ }
11
+ export interface TotoControllerProps {
12
+ apiName: string;
13
+ environment: TotoEnvironment;
14
+ config: TotoControllerConfig;
15
+ }
16
+ export declare class TotoAPIController {
17
+ app: Express;
18
+ apiName: string;
19
+ props: TotoControllerProps;
20
+ options: TotoControllerOptions;
21
+ constructor(props: TotoControllerProps, options?: TotoControllerOptions);
22
+ init(): Promise<void>;
23
+ staticContent(path: string, folder: string, options?: TotoPathOptions): void;
24
+ streamGET(path: string, delegate: TotoDelegate, options?: TotoPathOptions): void;
25
+ fileUploadPath(path: string, delegate: TotoDelegate, options?: TotoPathOptions): void;
26
+ registerPubSubMessageEndpoint(path: string, handler: (req: Request) => Promise<any>, options?: TotoPathOptions): void;
27
+ path(method: string, path: string, delegate: TotoDelegate, options?: TotoPathOptions): void;
28
+ listen(): void;
29
+ }
30
+ //# sourceMappingURL=TotoAPIController.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TotoAPIController.d.ts","sourceRoot":"","sources":["../../src/api/TotoAPIController.ts"],"names":[],"mappings":"AAGA,OAAgB,EAAE,OAAO,EAAE,OAAO,EAAY,MAAM,SAAS,CAAA;AAI7D,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAA;AAEpE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAGrD,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAI3D,OAAO,EAAE,eAAe,EAAE,MAAM,IAAI,CAAC;AAErC,qBAAa,qBAAqB;IAC9B,SAAS,CAAC,EAAE,OAAO,CAAQ;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAY;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,mBAAmB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,eAAe,CAAC;IAC7B,MAAM,EAAE,oBAAoB,CAAC;CAChC;AASD,qBAAa,iBAAiB;IAE1B,GAAG,EAAE,OAAO,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,mBAAmB,CAAC;IAC3B,OAAO,EAAE,qBAAqB,CAAC;gBAQnB,KAAK,EAAE,mBAAmB,EAAE,OAAO,GAAE,qBAA0B;IA2CrE,IAAI;IAgBV,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe;IAiBrE,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,eAAe;IAgDzE,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,eAAe;IAqF9E,6BAA6B,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,eAAe;IAuD9G,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,eAAe;IAyDpF,MAAM;CAgBT"}
@@ -0,0 +1,226 @@
1
+ import bodyParser from 'body-parser';
2
+ import busboy from 'connect-busboy';
3
+ import fs from 'fs-extra';
4
+ import express from 'express';
5
+ import { v4 as uuidv4 } from 'uuid';
6
+ import { Logger } from '../logger/TotoLogger';
7
+ import { ValidationError, Validator } from '../validation/Validator';
8
+ import { SmokeDelegate } from '../dlg/SmokeDelegate';
9
+ import { TotoRuntimeError } from '../model/TotoRuntimeError';
10
+ import path from 'path';
11
+ import { TotoRegistryAPI } from '../integration/TotoRegistryAPI';
12
+ import { RegistryCache } from '../integration/RegistryCache';
13
+ export class TotoControllerOptions {
14
+ constructor() {
15
+ this.debugMode = false;
16
+ this.basePath = undefined;
17
+ }
18
+ }
19
+ export class TotoAPIController {
20
+ constructor(props, options = {}) {
21
+ this.app = express();
22
+ this.props = props;
23
+ this.apiName = props.apiName;
24
+ this.options = {
25
+ debugMode: options.debugMode ?? false,
26
+ basePath: options.basePath,
27
+ port: options.port ?? 8080
28
+ };
29
+ const logger = Logger.getInstance();
30
+ if (options.debugMode)
31
+ logger.compute("INIT", `[TotoAPIController Debug] - Config Properties: ${JSON.stringify(this.props.config.getProps())}`);
32
+ this.app.use(function (req, res, next) {
33
+ res.header("Access-Control-Allow-Origin", "*");
34
+ res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, x-correlation-id, x-msg-id, auth-provider, x-app-version, x-client, x-client-id");
35
+ res.header("Access-Control-Allow-Methods", "OPTIONS, GET, PUT, POST, DELETE");
36
+ next();
37
+ });
38
+ this.app.use(bodyParser.json());
39
+ this.app.use(busboy());
40
+ this.app.use(express.static(path.join(__dirname, 'public')));
41
+ const smokeEndpoint = new SmokeDelegate(null, this.props.config);
42
+ smokeEndpoint.apiName = this.apiName;
43
+ this.path('GET', '/', smokeEndpoint, { noAuth: true, contentType: 'application/json' });
44
+ this.path('GET', '/', smokeEndpoint, { noAuth: true, contentType: 'application/json', ignoreBasePath: true });
45
+ this.path('GET', '/health', smokeEndpoint, { noAuth: true, contentType: 'application/json', ignoreBasePath: true });
46
+ this.staticContent = this.staticContent.bind(this);
47
+ this.fileUploadPath = this.fileUploadPath.bind(this);
48
+ this.path = this.path.bind(this);
49
+ }
50
+ async init() {
51
+ const registrationResponse = await new TotoRegistryAPI(this.props.config).registerAPI({ apiName: this.apiName, basePath: this.options.basePath?.replace("/", ""), hyperscaler: this.props.environment.hyperscaler });
52
+ Logger.getInstance().compute("INIT", `API ${this.apiName} successfully registered with Toto API Registry: ${JSON.stringify(registrationResponse)}`, 'info');
53
+ RegistryCache.getInstance(this.props.config).refresh();
54
+ }
55
+ staticContent(path, folder, options) {
56
+ const correctedPath = (this.options.basePath && (!options || !options.ignoreBasePath)) ? this.options.basePath.replace(/\/$/, '').trim() + path : path;
57
+ this.app.use(correctedPath, express.static(folder));
58
+ }
59
+ streamGET(path, delegate, options) {
60
+ const correctedPath = (this.options.basePath && (!options || !options.ignoreBasePath)) ? this.options.basePath.replace(/\/$/, '').trim() + path : path;
61
+ const validator = new Validator(this.props.config, this.options.debugMode || false);
62
+ const logger = Logger.getInstance();
63
+ this.app.route(correctedPath).get((req, res, next) => {
64
+ validator.validate(req, options).then((userContext) => {
65
+ logger.apiIn(req.headers['x-correlation-id'], 'GET', correctedPath);
66
+ delegate.processRequest(req, userContext).then((stream) => {
67
+ if (options && options.contentType)
68
+ res.header('Content-Type', options.contentType);
69
+ res.writeHead(200);
70
+ stream.on('data', (data) => {
71
+ res.write(data);
72
+ });
73
+ stream.on('end', () => {
74
+ res.end();
75
+ });
76
+ }, (err) => {
77
+ logger.compute(req.headers['x-correlation-id'], err, 'error');
78
+ if (err != null && err.code == '400')
79
+ res.status(400).type('application/json').send(err);
80
+ else
81
+ res.status(500).type('application/json').send(err);
82
+ });
83
+ });
84
+ });
85
+ }
86
+ fileUploadPath(path, delegate, options) {
87
+ const correctedPath = (this.options.basePath && (!options || !options.ignoreBasePath)) ? this.options.basePath.replace(/\/$/, '').trim() + path : path;
88
+ const validator = new Validator(this.props.config, this.options.debugMode || false);
89
+ const logger = Logger.getInstance();
90
+ this.app.route(correctedPath).post(async (req, res, next) => {
91
+ logger.apiIn(req.headers['x-correlation-id'], 'POST', correctedPath);
92
+ const userContext = await validator.validate(req);
93
+ let fstream;
94
+ let filename;
95
+ let filepath;
96
+ let additionalData = {};
97
+ req.pipe(req.busboy);
98
+ req.busboy.on('field', (fieldname, value, metadata) => {
99
+ additionalData[fieldname] = value;
100
+ });
101
+ req.busboy.on('file', (fieldname, file, metadata) => {
102
+ logger.compute(req.headers['x-correlation-id'], 'Uploading file ' + metadata.filename, 'info');
103
+ let dir = __dirname + '/app-docs';
104
+ filename = metadata.filename;
105
+ filepath = dir + '/' + metadata.filename;
106
+ fs.ensureDirSync(dir);
107
+ fstream = fs.createWriteStream(dir + '/' + metadata.filename);
108
+ file.pipe(fstream);
109
+ });
110
+ req.busboy.on("finish", () => {
111
+ delegate.processRequest({
112
+ query: req.query,
113
+ params: req.params,
114
+ headers: req.headers,
115
+ body: { filepath: filepath, filename: filename, ...additionalData }
116
+ }, userContext).then((data) => {
117
+ res.status(200).type('application/json').send(data);
118
+ }, (err) => {
119
+ logger.compute(req.headers['x-correlation-id'], err, 'error');
120
+ if (err != null && err.code == '400')
121
+ res.status(400).type('application/json').send(err);
122
+ else
123
+ res.status(500).type('application/json').send(err);
124
+ });
125
+ });
126
+ });
127
+ logger.compute("INIT", '[' + this.apiName + '] - Successfully added method ' + 'POST' + ' ' + correctedPath);
128
+ }
129
+ registerPubSubMessageEndpoint(path, handler, options) {
130
+ const correctedPath = (this.options.basePath && (!options || !options.ignoreBasePath)) ? this.options.basePath.replace(/\/$/, '').trim() + path : path;
131
+ const logger = Logger.getInstance();
132
+ const handleRequest = async (req, res) => {
133
+ const cid = req.get('x-correlation-id') || uuidv4();
134
+ try {
135
+ logger.compute(cid, `Received event on path ${path}`);
136
+ const data = await handler(req);
137
+ logger.compute(cid, `Event on path ${path} processed with result: ${JSON.stringify(data)}`);
138
+ res.status(200).type('application/json').send(data);
139
+ }
140
+ catch (error) {
141
+ logger.compute(cid, `${error}`, "error");
142
+ if (error instanceof ValidationError || error instanceof TotoRuntimeError) {
143
+ res.status(error.code).type("application/json").send(error);
144
+ }
145
+ else {
146
+ console.log(error);
147
+ res.status(500).type('application/json').send(error);
148
+ }
149
+ }
150
+ };
151
+ this.app.post(correctedPath, parseTextAsJson, handleRequest);
152
+ logger.compute("INIT", "Successfully added event handler POST " + correctedPath);
153
+ }
154
+ path(method, path, delegate, options) {
155
+ const correctedPath = (this.options.basePath && (!options || !options.ignoreBasePath)) ? this.options.basePath.replace(/\/$/, '').trim() + path : path;
156
+ const validator = new Validator(this.props.config, this.options.debugMode || false);
157
+ const logger = Logger.getInstance();
158
+ const handleRequest = async (req, res) => {
159
+ const cid = String(req.headers['x-correlation-id']);
160
+ try {
161
+ logger.apiIn(cid, method, correctedPath);
162
+ const userContext = await validator.validate(req, options);
163
+ const data = await delegate.processRequest(req, userContext);
164
+ let contentType = 'application/json';
165
+ let dataToReturn = data;
166
+ res.status(200).type(contentType).send(dataToReturn);
167
+ }
168
+ catch (error) {
169
+ logger.compute(cid, `${error}`, "error");
170
+ if (error instanceof ValidationError || error instanceof TotoRuntimeError) {
171
+ res.status(error.code).type("application/json").send(error);
172
+ }
173
+ else {
174
+ console.log(error);
175
+ res.status(500).type('application/json').send(error);
176
+ }
177
+ }
178
+ };
179
+ if (method == "GET")
180
+ this.app.get(correctedPath, handleRequest);
181
+ else if (method == "POST")
182
+ this.app.post(correctedPath, handleRequest);
183
+ else if (method == "PUT")
184
+ this.app.put(correctedPath, handleRequest);
185
+ else if (method == "DELETE")
186
+ this.app.delete(correctedPath, handleRequest);
187
+ else
188
+ this.app.get(correctedPath, handleRequest);
189
+ logger.compute("INIT", '[' + this.apiName + '] - Successfully added method ' + method + ' ' + correctedPath);
190
+ }
191
+ listen() {
192
+ const validator = new Validator(this.props.config, this.options.debugMode || false);
193
+ const logger = Logger.getInstance();
194
+ if (!validator) {
195
+ logger.compute("INIT", "Waiting for the configuration to load...");
196
+ setTimeout(() => { this.listen(); }, 300);
197
+ return;
198
+ }
199
+ this.app.listen(this.options.port, () => {
200
+ logger.compute("INIT", `[${this.apiName}] - Microservice listening on port ${this.options.port}`, 'info');
201
+ });
202
+ }
203
+ }
204
+ const parseTextAsJson = (req, res, next) => {
205
+ const contentType = req.get('content-type') || '';
206
+ if (contentType.includes('text/plain')) {
207
+ bodyParser.text({ type: 'text/plain' })(req, res, (err) => {
208
+ if (err)
209
+ return next(err);
210
+ try {
211
+ if (typeof req.body === 'string') {
212
+ req.body = JSON.parse(req.body);
213
+ }
214
+ next();
215
+ }
216
+ catch (parseError) {
217
+ console.log(`Failed to parse SNS text/plain body as JSON: ${parseError}`, 'error');
218
+ next(parseError);
219
+ }
220
+ });
221
+ }
222
+ else {
223
+ next();
224
+ }
225
+ };
226
+ //# sourceMappingURL=TotoAPIController.js.map