@wabot-dev/framework 0.1.0-beta.6 → 0.1.0-beta.61
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/dist/src/addon/async/pg/PgJobRepository.js +26 -0
- package/dist/src/addon/auth/api-key/@apiKeyConnectionGuard.js +16 -0
- package/dist/src/addon/auth/api-key/@apiKeyGuard.js +17 -0
- package/dist/src/addon/auth/api-key/ApiKey.js +53 -0
- package/dist/src/addon/auth/api-key/ApiKeyConnectionGuardMiddleware.js +57 -0
- package/dist/src/addon/auth/api-key/ApiKeyGuardMiddleware.js +45 -0
- package/dist/src/addon/auth/api-key/ApiKeyRepository.js +35 -0
- package/dist/src/addon/auth/api-key/PgApiKeyRepository.js +28 -0
- package/dist/src/addon/auth/api-key/RemoteApiKeyRepository.js +42 -0
- package/dist/src/addon/auth/jwt/@jwtConnectionGuard.js +16 -0
- package/dist/src/addon/auth/jwt/@jwtGuard.js +17 -0
- package/dist/src/addon/auth/jwt/Jwt.js +53 -0
- package/dist/src/addon/auth/jwt/JwtAccessAndRefreshTokenDto.js +20 -0
- package/dist/src/addon/auth/jwt/JwtConfig.js +28 -0
- package/dist/src/addon/auth/jwt/JwtConnectionGuardMiddleware.js +57 -0
- package/dist/src/addon/auth/jwt/JwtGuardMiddleware.js +45 -0
- package/dist/src/addon/auth/jwt/JwtRefreshToken.js +56 -0
- package/dist/src/addon/auth/jwt/JwtRefreshTokenRepository.js +25 -0
- package/dist/src/addon/auth/jwt/JwtSigner.js +36 -0
- package/dist/src/addon/auth/jwt/JwtTokenDto.js +22 -0
- package/dist/src/addon/auth/jwt/PgJwtRefreshTokenRepository.js +21 -0
- package/dist/src/addon/chat-bot/anthropic/AnthropicChatAdapter.js +135 -0
- package/dist/src/addon/chat-bot/deepseek/DeepSeekChatAdapter.js +137 -0
- package/dist/src/addon/chat-bot/google/GoogleChatAdapter.js +128 -0
- package/dist/src/addon/chat-bot/openia/OpenaiChatAdapter.js +117 -0
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatMemory.js +6 -4
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatRepository.js +6 -5
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatRepository.js +2 -2
- package/dist/src/addon/chat-bot/wabot/WabotChatAdapter.js +41 -0
- package/dist/src/addon/chat-controller/cmd/@cmd.js +23 -0
- package/dist/src/{channels → addon/chat-controller}/cmd/CmdChannel.js +4 -26
- package/dist/src/{channels → addon/chat-controller}/socket/@socket.js +9 -4
- package/dist/src/{channels → addon/chat-controller}/socket/SocketChannel.js +5 -23
- package/dist/src/{channels → addon/chat-controller}/socket/SocketChannelConfig.js +1 -1
- package/dist/src/{channels → addon/chat-controller}/telegram/@telegram.js +9 -4
- package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannel.js +4 -22
- package/dist/src/addon/chat-controller/whatsapp/@whatsApp.js +25 -0
- package/dist/src/{channels → addon/chat-controller}/whatsapp/EnvWhatsAppRepository.js +3 -3
- package/dist/src/{channels → addon/chat-controller}/whatsapp/PgWhatsAppRepository.js +2 -2
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsApp.js +2 -4
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannel.js +6 -18
- package/dist/src/addon/chat-controller/whatsapp/WhatsAppReceiver.js +10 -0
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppSender.js +20 -41
- package/dist/src/addon/chat-controller/whatsapp/cloud-api/WhatsAppReceiverByCloudApi.js +97 -0
- package/dist/src/{channels/whatsapp → addon/chat-controller/whatsapp/cloud-api}/WhatsAppSenderByCloudApi.js +26 -20
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppProxyContracts.js +5 -0
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppReceiverByWabotProxy.js +65 -0
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppSenderByWabotProxy.js +60 -0
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppWabotProxyConnection.js +45 -0
- package/dist/src/addon/mindset/html/HtmlModule.js +70 -0
- package/dist/src/core/auth/Auth.js +24 -0
- package/dist/src/core/{Persistent.js → entity/Entity.js} +24 -12
- package/dist/src/core/env/Env.js +39 -0
- package/dist/src/core/error/CustomError.js +15 -0
- package/dist/src/core/injection/index.js +4 -0
- package/dist/src/core/mapper/Mapper.js +42 -0
- package/dist/src/core/password/Password.js +30 -0
- package/dist/src/core/random/Random.js +65 -0
- package/dist/src/core/storable/Storable.js +8 -0
- package/dist/src/core/validation/core/validateArray.js +51 -0
- package/dist/src/core/validation/core/validateIsOptional.js +5 -0
- package/dist/src/core/validation/core/validateModel.js +36 -0
- package/dist/src/core/validation/metadata/@isArray.js +18 -0
- package/dist/src/core/validation/metadata/@isModel.js +18 -0
- package/dist/src/core/validation/metadata/@isOptional.js +17 -0
- package/dist/src/core/validation/metadata/ValidationMetadataStore.js +98 -0
- package/dist/src/core/validation/modelInfo.js +9 -0
- package/dist/src/core/validation/validate.js +11 -0
- package/dist/src/core/validation/validators/is-boolean/@isBoolean.js +17 -0
- package/dist/src/core/validation/validators/is-boolean/validateIsBoolean.js +12 -0
- package/dist/src/core/validation/validators/is-date/@isDate.js +17 -0
- package/dist/src/core/validation/validators/is-date/validateIsDate.js +16 -0
- package/dist/src/core/validation/validators/is-in/@isIn.js +18 -0
- package/dist/src/core/validation/validators/is-in/validateIsIn.js +12 -0
- package/dist/src/core/validation/validators/is-not-empty/@isNotEmpty.js +17 -0
- package/dist/src/core/validation/validators/is-not-empty/validateIsNotEmpty.js +12 -0
- package/dist/src/core/validation/validators/is-number/@isNumber.js +17 -0
- package/dist/src/core/validation/validators/is-number/validateIsNumber.js +12 -0
- package/dist/src/core/validation/validators/is-present/@isPresent.js +17 -0
- package/dist/src/core/validation/validators/is-present/validateIsPresent.js +12 -0
- package/dist/src/core/validation/validators/is-string/@isString.js +17 -0
- package/dist/src/core/validation/validators/is-string/validateIsString.js +12 -0
- package/dist/src/core/validation/validators/max/@max.js +18 -0
- package/dist/src/core/validation/validators/max/validateMax.js +17 -0
- package/dist/src/core/validation/validators/min/@min.js +18 -0
- package/dist/src/core/validation/validators/min/validateMin.js +17 -0
- package/dist/src/feature/async/@command.js +11 -0
- package/dist/src/feature/async/@commandHandler.js +12 -0
- package/dist/src/feature/async/Async.js +38 -0
- package/dist/src/feature/async/Command.js +9 -0
- package/dist/src/feature/async/CommandMetadataStore.js +38 -0
- package/dist/src/feature/async/Job.js +27 -0
- package/dist/src/feature/async/JobRepository.js +31 -0
- package/dist/src/feature/async/JobRunner.js +48 -0
- package/dist/src/feature/async/JobsEventsHub.js +36 -0
- package/dist/src/feature/async/runCommandHandlers.js +29 -0
- package/dist/src/{core/chat → feature/chat-bot}/Chat.js +2 -2
- package/dist/src/feature/chat-bot/ChatAdapter.js +7 -0
- package/dist/src/feature/chat-bot/ChatBot.js +73 -0
- package/dist/src/feature/chat-bot/ChatItem.js +24 -0
- package/dist/src/feature/chat-bot/ChatMemory.js +10 -0
- package/dist/src/{core/chat/repository/IChatRepository.js → feature/chat-bot/ChatRepository.js} +2 -8
- package/dist/src/feature/chat-bot/IChatItem.js +3 -0
- package/dist/src/{chatbot → feature/chat-bot}/metadata/@chatBot.js +1 -1
- package/dist/src/{chatbot → feature/chat-bot}/metadata/ChatBotMetadataStore.js +1 -1
- package/dist/src/{controller/channel → feature/chat-controller}/ChatResolver.js +6 -4
- package/dist/src/{controller → feature/chat-controller}/metadata/ControllerMetadataStore.js +1 -1
- package/dist/src/{controller → feature/chat-controller}/metadata/controller/@chatController.js +1 -1
- package/dist/src/feature/chat-controller/runChatControllers.js +69 -0
- package/dist/src/{channels → feature}/express/ExpressProvider.js +2 -4
- package/dist/src/{channels → feature}/http/HttpServerProvider.js +2 -2
- package/dist/src/{mindset → feature/mindset}/IMindset.js +3 -0
- package/dist/src/{mindset → feature/mindset}/MindsetOperator.js +69 -42
- package/dist/src/{mindset → feature/mindset}/metadata/MindsetMetadataStore.js +1 -1
- package/dist/src/{mindset → feature/mindset}/metadata/functions/@mindsetFunction.js +1 -1
- package/dist/src/{mindset → feature/mindset}/metadata/mindsets/@mindset.js +1 -1
- package/dist/src/{mindset → feature/mindset}/metadata/modules/@mindsetModule.js +1 -2
- package/dist/src/{mindset → feature/mindset}/metadata/params/@param.js +1 -1
- package/dist/src/feature/money/Money.js +61 -0
- package/dist/src/feature/money/MoneyDto.js +22 -0
- package/dist/src/{repository → feature}/pg/PgCrudRepository.js +30 -9
- package/dist/src/{repository → feature}/pg/PgRepositoryBase.js +2 -2
- package/dist/src/feature/rest-controller/injection-tokens.js +4 -0
- package/dist/src/feature/rest-controller/metadata/@middleware.js +16 -0
- package/dist/src/feature/rest-controller/metadata/@onDelete.js +7 -0
- package/dist/src/feature/rest-controller/metadata/@onGet.js +7 -0
- package/dist/src/feature/rest-controller/metadata/@onPost.js +7 -0
- package/dist/src/feature/rest-controller/metadata/@onPut.js +7 -0
- package/dist/src/feature/rest-controller/metadata/@restController.js +15 -0
- package/dist/src/feature/rest-controller/metadata/RestControllerMetadataStore.js +50 -0
- package/dist/src/feature/rest-controller/metadata/methodDecorator.js +19 -0
- package/dist/src/feature/rest-controller/runRestControllers.js +103 -0
- package/dist/src/{channels → feature}/socket/SocketServerProvider.js +2 -2
- package/dist/src/feature/socket-controller/metadata/@connectionMiddleware.js +16 -0
- package/dist/src/feature/socket-controller/metadata/@socketConnection.js +18 -0
- package/dist/src/feature/socket-controller/metadata/@socketController.js +15 -0
- package/dist/src/feature/socket-controller/metadata/@socketEvent.js +18 -0
- package/dist/src/feature/socket-controller/metadata/SocketControllerMetadataStore.js +65 -0
- package/dist/src/feature/socket-controller/runSocketControllers.js +99 -0
- package/dist/src/index.d.ts +1210 -576
- package/dist/src/index.js +147 -76
- package/package.json +10 -4
- package/dist/src/ai/deepseek/DeepSeekChatBotAdapter.js +0 -106
- package/dist/src/ai/openia/OpenaiChatBotAdapter.js +0 -88
- package/dist/src/channels/cmd/@cmd.js +0 -18
- package/dist/src/channels/wabot/WabotDevConnection.js +0 -57
- package/dist/src/channels/wabot/WabotDevSocketContracts.js +0 -10
- package/dist/src/channels/whatsapp/@whatsapp.js +0 -20
- package/dist/src/channels/whatsapp/WhatsAppReceiver.js +0 -59
- package/dist/src/channels/whatsapp/WhatsAppReceiverByDevConnection.js +0 -32
- package/dist/src/channels/whatsapp/WhatsAppReceiverByWebHook.js +0 -63
- package/dist/src/channels/whatsapp/WhatsAppSenderByDevConnection.js +0 -61
- package/dist/src/chatbot/ChatBot.js +0 -51
- package/dist/src/chatbot/ChatBotAdapter.js +0 -72
- package/dist/src/controller/channel/UserResolver.js +0 -21
- package/dist/src/core/IMessageContext.js +0 -12
- package/dist/src/core/chat/ChatItem.js +0 -15
- package/dist/src/core/chat/repository/IChatMemory.js +0 -10
- package/dist/src/core/user/IUserRepository.js +0 -19
- package/dist/src/core/user/User.js +0 -26
- package/dist/src/env/WabotEnv.js +0 -27
- package/dist/src/injection/index.js +0 -4
- package/dist/src/mindset/metadata/params/@isOptional.js +0 -21
- package/dist/src/pre-made/module/authentication/AuthenticationModule.js +0 -97
- package/dist/src/pre-made/module/authentication/requests/SendOneTimePasswordRequest.js +0 -25
- package/dist/src/pre-made/module/authentication/requests/ValidateOneTimePasswordRequest.js +0 -25
- package/dist/src/pre-made/module/register-user/RegisterUserModule.js +0 -56
- package/dist/src/pre-made/module/register-user/requests/RegisterUserWithEmailRequest.js +0 -25
- package/dist/src/pre-made/repository/user/pg/PgUserRepository.js +0 -33
- package/dist/src/pre-made/repository/user/ram/RamUserRepository.js +0 -27
- package/dist/src/pre-made/service/EmailService.js +0 -13
- package/dist/src/pre-made/service/OtpService.js +0 -14
- package/dist/src/server/prepareChatContainer.js +0 -43
- package/dist/src/server/runChannel.js +0 -27
- package/dist/src/server/runServer.js +0 -40
- /package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatMemory.js +0 -0
- /package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannelConfig.js +0 -0
- /package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannelConfig.js +0 -0
- /package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppRepository.js +0 -0
- /package/dist/src/{injection → core/injection}/Container.js +0 -0
- /package/dist/src/{logger → core/logger}/Logger.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/functions/decoratorNames.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/mindsets/decoratorNames.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/modules/decoratorNames.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/params/decoratorNames.js +0 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import { injectable } from '
|
|
2
|
+
import { injectable } from '../../core/injection/index.js';
|
|
3
3
|
import { Mindset } from './IMindset.js';
|
|
4
4
|
import { MindsetMetadataStore } from './metadata/MindsetMetadataStore.js';
|
|
5
|
-
import { Container } from '
|
|
5
|
+
import { Container } from '../../core/injection/Container.js';
|
|
6
6
|
|
|
7
7
|
let MindsetOperator = class MindsetOperator {
|
|
8
8
|
mindset;
|
|
@@ -22,64 +22,69 @@ let MindsetOperator = class MindsetOperator {
|
|
|
22
22
|
limits() {
|
|
23
23
|
return this.mindset.limits();
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
.map((module) => module.functions)
|
|
28
|
-
.flat()
|
|
29
|
-
.find((fn) => fn.name === name);
|
|
30
|
-
if (!fnMetadata) {
|
|
31
|
-
throw new Error(`Function ${name} not found`);
|
|
32
|
-
}
|
|
33
|
-
const paramsObj = JSON.parse(params);
|
|
34
|
-
const module = this.container.resolve(fnMetadata.moduleConstructor);
|
|
35
|
-
try {
|
|
36
|
-
const response = await module[name](paramsObj);
|
|
37
|
-
if (!response) {
|
|
38
|
-
return 'success';
|
|
39
|
-
}
|
|
40
|
-
return response.toString();
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
return `Error: ${error}`;
|
|
44
|
-
}
|
|
25
|
+
llms() {
|
|
26
|
+
return this.mindset.llms();
|
|
45
27
|
}
|
|
46
|
-
async
|
|
28
|
+
async systemPrompt() {
|
|
29
|
+
let [identity, skills, limits] = await Promise.all([
|
|
30
|
+
this.identity(),
|
|
31
|
+
this.skills(),
|
|
32
|
+
this.limits(),
|
|
33
|
+
]);
|
|
34
|
+
const language = identity.language.replaceAll('#', ' ');
|
|
35
|
+
const name = identity.name.replaceAll('#', ' ');
|
|
36
|
+
const age = identity.age ? identity.age.toString().replaceAll('#', ' ') : null;
|
|
37
|
+
const personality = identity.personality ? identity.personality.replaceAll('#', ' ') : null;
|
|
38
|
+
skills = skills.replaceAll('#', ' ');
|
|
39
|
+
limits = limits.replaceAll('#', ' ');
|
|
40
|
+
const systemPrompt = `
|
|
41
|
+
# System Instructions
|
|
42
|
+
you should act as a assistant.
|
|
43
|
+
your main language is ${language}.
|
|
44
|
+
your name is ${name}.
|
|
45
|
+
${age ? 'you are ' + age + ' years old.' : ''}
|
|
46
|
+
|
|
47
|
+
${personality ? '## Personality (in your main language) \n' + personality : ''}
|
|
48
|
+
|
|
49
|
+
## Skills (in your main language)
|
|
50
|
+
${skills}
|
|
51
|
+
|
|
52
|
+
## System limitations (in your main language)
|
|
53
|
+
${limits}
|
|
54
|
+
|
|
55
|
+
## Chat memory
|
|
56
|
+
Next you will receive a chat history,
|
|
57
|
+
you should use this information to answer the user.
|
|
58
|
+
`;
|
|
59
|
+
return systemPrompt;
|
|
60
|
+
}
|
|
61
|
+
tools() {
|
|
47
62
|
return this.metadata.modules
|
|
48
|
-
.map((module) => module.functions.map((fn) => this.
|
|
63
|
+
.map((module) => module.functions.map((fn) => this.tool(fn, module)))
|
|
49
64
|
.flat();
|
|
50
65
|
}
|
|
51
|
-
|
|
66
|
+
tool(fn, module) {
|
|
52
67
|
const description = fn.config.description.replaceAll('#', ' ');
|
|
53
68
|
return {
|
|
54
|
-
|
|
69
|
+
language: module.config.language ?? 'english',
|
|
55
70
|
name: fn.name,
|
|
56
71
|
description,
|
|
57
|
-
parameters:
|
|
58
|
-
type: 'object',
|
|
59
|
-
properties: fn.params.reduce((prev, param) => ({
|
|
60
|
-
...prev,
|
|
61
|
-
[param.name]: this.toolParam(param),
|
|
62
|
-
}), {}),
|
|
63
|
-
required: fn.params.filter((param) => !param.config.optional).map((param) => param.name)
|
|
64
|
-
},
|
|
72
|
+
parameters: fn.params.map((param) => this.toolParameter(param)),
|
|
65
73
|
};
|
|
66
74
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
description: `
|
|
75
|
+
toolParameter(param) {
|
|
76
|
+
let description = `
|
|
70
77
|
### description (in your main language)
|
|
71
78
|
${param.config.description.replaceAll('#', ' ')}
|
|
72
|
-
|
|
73
|
-
};
|
|
79
|
+
`;
|
|
74
80
|
const type = (() => {
|
|
75
81
|
if (param.type === Number)
|
|
76
82
|
return 'number';
|
|
77
83
|
if (param.type === String)
|
|
78
84
|
return 'string';
|
|
79
85
|
if (param.type === Date) {
|
|
80
|
-
|
|
86
|
+
description = `${description}
|
|
81
87
|
### format: ISO 8681 - YYYY-MM-DDTHH:mm:ssZ
|
|
82
|
-
${addons.description}
|
|
83
88
|
`;
|
|
84
89
|
return 'string';
|
|
85
90
|
}
|
|
@@ -87,9 +92,31 @@ let MindsetOperator = class MindsetOperator {
|
|
|
87
92
|
})();
|
|
88
93
|
return {
|
|
89
94
|
type,
|
|
90
|
-
|
|
95
|
+
name: param.name,
|
|
96
|
+
description,
|
|
91
97
|
};
|
|
92
98
|
}
|
|
99
|
+
async callFunction(name, params) {
|
|
100
|
+
const fnMetadata = this.metadata.modules
|
|
101
|
+
.map((module) => module.functions)
|
|
102
|
+
.flat()
|
|
103
|
+
.find((fn) => fn.name === name);
|
|
104
|
+
if (!fnMetadata) {
|
|
105
|
+
throw new Error(`Function ${name} not found`);
|
|
106
|
+
}
|
|
107
|
+
const paramsObj = JSON.parse(params);
|
|
108
|
+
const module = this.container.resolve(fnMetadata.moduleConstructor);
|
|
109
|
+
try {
|
|
110
|
+
const response = await module[name](paramsObj);
|
|
111
|
+
if (!response) {
|
|
112
|
+
return 'success';
|
|
113
|
+
}
|
|
114
|
+
return response.toString();
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
return `Error: ${error}`;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
93
120
|
};
|
|
94
121
|
MindsetOperator = __decorate([
|
|
95
122
|
injectable(),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { __decorate } from 'tslib';
|
|
2
|
-
import { singleton } from '
|
|
2
|
+
import { singleton } from '../../../core/injection/index.js';
|
|
3
3
|
import { MINDSET_FUNCTION_DECORATION_FUNCTION } from './functions/decoratorNames.js';
|
|
4
4
|
import { MINDSET_DECORATION_MINDSET } from './mindsets/decoratorNames.js';
|
|
5
5
|
import { MINDSET_MODULE_DECORATION_MODULE } from './modules/decoratorNames.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
-
import { container } from '
|
|
2
|
+
import { container } from '../../../../core/injection/index.js';
|
|
3
3
|
import { MindsetMetadataStore } from '../MindsetMetadataStore.js';
|
|
4
4
|
import { MINDSET_FUNCTION_DECORATION_FUNCTION } from './decoratorNames.js';
|
|
5
5
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
-
import { container, injectable } from '
|
|
2
|
+
import { container, injectable } from '../../../../core/injection/index.js';
|
|
3
3
|
import { MindsetMetadataStore } from '../MindsetMetadataStore.js';
|
|
4
4
|
import { MINDSET_DECORATION_MINDSET } from './decoratorNames.js';
|
|
5
5
|
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import '
|
|
2
|
-
import { container, injectable } from '../../../injection/index.js';
|
|
1
|
+
import { container, injectable } from '../../../../core/injection/index.js';
|
|
3
2
|
import { MindsetMetadataStore } from '../MindsetMetadataStore.js';
|
|
4
3
|
import { MINDSET_MODULE_DECORATION_MODULE } from './decoratorNames.js';
|
|
5
4
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
2
|
import { MindsetMetadataStore } from '../MindsetMetadataStore.js';
|
|
3
|
-
import { container } from '
|
|
3
|
+
import { container } from '../../../../core/injection/index.js';
|
|
4
4
|
import { PARAM_DECORATION_PARAM } from './decoratorNames.js';
|
|
5
5
|
|
|
6
6
|
function param(config) {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Storable } from '../../core/storable/Storable.js';
|
|
2
|
+
import { Big } from 'big.js';
|
|
3
|
+
|
|
4
|
+
Big.strict = true;
|
|
5
|
+
class Money extends Storable {
|
|
6
|
+
getData() {
|
|
7
|
+
return { ...this.data };
|
|
8
|
+
}
|
|
9
|
+
get amount() {
|
|
10
|
+
return Big(this.data.amount);
|
|
11
|
+
}
|
|
12
|
+
get currency() {
|
|
13
|
+
return this.data.currency;
|
|
14
|
+
}
|
|
15
|
+
plus(money) {
|
|
16
|
+
this.validateEqualCurrencies(this.data.currency, money.data.currency);
|
|
17
|
+
return new Money({
|
|
18
|
+
amount: this.amount.plus(money.amount).toFixed(6),
|
|
19
|
+
currency: this.currency,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
minus(money) {
|
|
23
|
+
this.validateEqualCurrencies(this.data.currency, money.data.currency);
|
|
24
|
+
return new Money({
|
|
25
|
+
amount: this.amount.minus(money.amount).toFixed(6),
|
|
26
|
+
currency: this.currency,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
times(value) {
|
|
30
|
+
return new Money({
|
|
31
|
+
amount: this.amount.times(new Big(String(value))).toFixed(6),
|
|
32
|
+
currency: this.currency,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
div(value) {
|
|
36
|
+
return new Money({
|
|
37
|
+
amount: this.amount.div(new Big(String(value))).toFixed(6),
|
|
38
|
+
currency: this.currency,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
negative() {
|
|
42
|
+
return new Money({
|
|
43
|
+
amount: new Big('0').minus(this.amount).toFixed(6),
|
|
44
|
+
currency: this.currency,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
isGretterThan(money) {
|
|
48
|
+
this.validateEqualCurrencies(this.data.currency, money.data.currency);
|
|
49
|
+
return this.amount.gt(money.amount);
|
|
50
|
+
}
|
|
51
|
+
validateEqualCurrencies(left, right) {
|
|
52
|
+
if (left !== right) {
|
|
53
|
+
throw new Error(`Is not posible operate with diferent currencies: '${left}' and '${right}'`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
static zero(currency) {
|
|
57
|
+
return new Money({ currency, amount: '0' });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { Money };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import '../../core/injection/index.js';
|
|
3
|
+
import '../../core/validation/metadata/ValidationMetadataStore.js';
|
|
4
|
+
import { isNotEmpty } from '../../core/validation/validators/is-not-empty/@isNotEmpty.js';
|
|
5
|
+
import { isString } from '../../core/validation/validators/is-string/@isString.js';
|
|
6
|
+
|
|
7
|
+
class MoneyDto {
|
|
8
|
+
amount = '';
|
|
9
|
+
currency = '';
|
|
10
|
+
}
|
|
11
|
+
__decorate([
|
|
12
|
+
isString(),
|
|
13
|
+
isNotEmpty(),
|
|
14
|
+
__metadata("design:type", String)
|
|
15
|
+
], MoneyDto.prototype, "amount", void 0);
|
|
16
|
+
__decorate([
|
|
17
|
+
isString(),
|
|
18
|
+
isNotEmpty(),
|
|
19
|
+
__metadata("design:type", String)
|
|
20
|
+
], MoneyDto.prototype, "currency", void 0);
|
|
21
|
+
|
|
22
|
+
export { MoneyDto };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as shortUUID from 'short-uuid';
|
|
2
2
|
import { PgRepositoryBase } from './PgRepositoryBase.js';
|
|
3
|
+
import { CustomError } from '../../core/error/CustomError.js';
|
|
3
4
|
|
|
4
5
|
class PgCrudRepository extends PgRepositoryBase {
|
|
5
6
|
config;
|
|
@@ -17,6 +18,28 @@ class PgCrudRepository extends PgRepositoryBase {
|
|
|
17
18
|
const items = await this.query(sql, [id]);
|
|
18
19
|
return items.at(0) ?? null;
|
|
19
20
|
}
|
|
21
|
+
async findByIds(ids) {
|
|
22
|
+
if (ids.length === 0) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
const sql = `
|
|
26
|
+
SELECT ${this.columns}
|
|
27
|
+
FROM ${this.table}
|
|
28
|
+
WHERE id IN (${ids.map((_, i) => '$' + (i + 1)).join(',')})
|
|
29
|
+
`;
|
|
30
|
+
const items = await this.query(sql, ids);
|
|
31
|
+
return items;
|
|
32
|
+
}
|
|
33
|
+
async findOrThrow(id) {
|
|
34
|
+
const item = await this.find(id);
|
|
35
|
+
if (!item) {
|
|
36
|
+
throw new CustomError({
|
|
37
|
+
message: `Not found ${this.config.constructor.name} with id = '${id}'`,
|
|
38
|
+
httpCode: 404,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return item;
|
|
42
|
+
}
|
|
20
43
|
async findAll() {
|
|
21
44
|
const sql = `
|
|
22
45
|
SELECT ${this.columns}
|
|
@@ -46,16 +69,14 @@ class PgCrudRepository extends PgRepositoryBase {
|
|
|
46
69
|
SET ${this.updates}
|
|
47
70
|
WHERE id = $${this.columnsList.length + 1}
|
|
48
71
|
`;
|
|
49
|
-
await this.exec(sql, [...this.values(item), item.
|
|
72
|
+
await this.exec(sql, [...this.values(item), item.id]);
|
|
50
73
|
}
|
|
51
|
-
async
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
item.
|
|
57
|
-
_item.discard();
|
|
58
|
-
await this.update(_item);
|
|
74
|
+
async delete(item) {
|
|
75
|
+
const sql = `
|
|
76
|
+
DELETE FROM ${this.table}
|
|
77
|
+
WHERE id = $1
|
|
78
|
+
`;
|
|
79
|
+
await this.exec(sql, [item.id]);
|
|
59
80
|
}
|
|
60
81
|
}
|
|
61
82
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { RestControllerMetadataStore } from './RestControllerMetadataStore.js';
|
|
3
|
+
|
|
4
|
+
function middleware(middlewareConstructor) {
|
|
5
|
+
return function (target, propertyKey) {
|
|
6
|
+
const functionName = propertyKey.toString();
|
|
7
|
+
const store = container.resolve(RestControllerMetadataStore);
|
|
8
|
+
store.saveMiddlewareMetadata({
|
|
9
|
+
controllerConstructor: target.constructor,
|
|
10
|
+
functionName,
|
|
11
|
+
middlewareConstructor: middlewareConstructor,
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { middleware };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { container, injectable } from '../../../core/injection/index.js';
|
|
2
|
+
import { RestControllerMetadataStore } from './RestControllerMetadataStore.js';
|
|
3
|
+
|
|
4
|
+
function restController(config) {
|
|
5
|
+
return function (target) {
|
|
6
|
+
const metaDataStore = container.resolve(RestControllerMetadataStore);
|
|
7
|
+
metaDataStore.saveControllerMetadata({
|
|
8
|
+
controllerConstructor: target,
|
|
9
|
+
path: typeof config === 'string' ? config : config.path,
|
|
10
|
+
});
|
|
11
|
+
injectable()(target);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { restController };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { __decorate } from 'tslib';
|
|
2
|
+
import { singleton } from '../../../core/injection/index.js';
|
|
3
|
+
|
|
4
|
+
let RestControllerMetadataStore = class RestControllerMetadataStore {
|
|
5
|
+
endPoints = new Map();
|
|
6
|
+
middlewares = new Map();
|
|
7
|
+
restControllers = new Map();
|
|
8
|
+
saveControllerMetadata(controllerMetadata) {
|
|
9
|
+
this.restControllers.set(controllerMetadata.controllerConstructor, controllerMetadata);
|
|
10
|
+
}
|
|
11
|
+
saveEndPointMetadata(endPointMetadata) {
|
|
12
|
+
let controllerEndPoints = this.endPoints.get(endPointMetadata.controllerConstructor);
|
|
13
|
+
if (!controllerEndPoints) {
|
|
14
|
+
this.endPoints.set(endPointMetadata.controllerConstructor, (controllerEndPoints = new Map()));
|
|
15
|
+
}
|
|
16
|
+
controllerEndPoints.set(endPointMetadata.functionName, endPointMetadata);
|
|
17
|
+
}
|
|
18
|
+
saveMiddlewareMetadata(middlewareMetadata) {
|
|
19
|
+
let controllerMiddlewares = this.middlewares.get(middlewareMetadata.controllerConstructor);
|
|
20
|
+
if (!controllerMiddlewares) {
|
|
21
|
+
this.middlewares.set(middlewareMetadata.controllerConstructor, (controllerMiddlewares = new Map()));
|
|
22
|
+
}
|
|
23
|
+
let methodMiddlewares = controllerMiddlewares.get(middlewareMetadata.functionName);
|
|
24
|
+
if (!methodMiddlewares) {
|
|
25
|
+
controllerMiddlewares.set(middlewareMetadata.functionName, (methodMiddlewares = []));
|
|
26
|
+
}
|
|
27
|
+
methodMiddlewares.unshift(middlewareMetadata);
|
|
28
|
+
}
|
|
29
|
+
getControllerEndPointsInfo(controllerConstructor) {
|
|
30
|
+
const controller = this.restControllers.get(controllerConstructor);
|
|
31
|
+
if (!controller) {
|
|
32
|
+
throw new Error(`${controllerConstructor.name} should be decorated with @restController`);
|
|
33
|
+
}
|
|
34
|
+
const endPoints = this.endPoints.get(controllerConstructor);
|
|
35
|
+
if (!endPoints?.size) {
|
|
36
|
+
// TODO: Warning
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
return [...endPoints.values()].map((endPoint) => ({
|
|
40
|
+
...endPoint,
|
|
41
|
+
middlewares: this.middlewares.get(endPoint.controllerConstructor)?.get(endPoint.functionName) ?? [],
|
|
42
|
+
controller: this.restControllers.get(endPoint.controllerConstructor),
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
RestControllerMetadataStore = __decorate([
|
|
47
|
+
singleton()
|
|
48
|
+
], RestControllerMetadataStore);
|
|
49
|
+
|
|
50
|
+
export { RestControllerMetadataStore };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { RestControllerMetadataStore } from './RestControllerMetadataStore.js';
|
|
3
|
+
|
|
4
|
+
function methodDecorator(method, config) {
|
|
5
|
+
return function (target, propertyKey) {
|
|
6
|
+
const functionName = propertyKey.toString();
|
|
7
|
+
const paramsTypes = Reflect.getMetadata('design:paramtypes', target, functionName);
|
|
8
|
+
const store = container.resolve(RestControllerMetadataStore);
|
|
9
|
+
store.saveEndPointMetadata({
|
|
10
|
+
controllerConstructor: target.constructor,
|
|
11
|
+
method,
|
|
12
|
+
config: typeof config === 'string' ? { path: config } : config,
|
|
13
|
+
functionName,
|
|
14
|
+
paramsTypes,
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { methodDecorator };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { CustomError } from '../../core/error/CustomError.js';
|
|
2
|
+
import { container } from '../../core/injection/index.js';
|
|
3
|
+
import { Logger } from '../../core/logger/Logger.js';
|
|
4
|
+
import '../../core/validation/metadata/ValidationMetadataStore.js';
|
|
5
|
+
import { validate } from '../../core/validation/validate.js';
|
|
6
|
+
import { ExpressProvider } from '../express/ExpressProvider.js';
|
|
7
|
+
import { json, urlencoded } from 'express';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import { EXPRESS_REQ, EXPRESS_RES } from './injection-tokens.js';
|
|
10
|
+
import { RestControllerMetadataStore } from './metadata/RestControllerMetadataStore.js';
|
|
11
|
+
|
|
12
|
+
function buildRequest(req) {
|
|
13
|
+
return Object.assign({}, req.body, req.query, req.params);
|
|
14
|
+
}
|
|
15
|
+
function runRestControllers(controllers) {
|
|
16
|
+
const logger = new Logger('wabot:rest');
|
|
17
|
+
const metadataStore = container.resolve(RestControllerMetadataStore);
|
|
18
|
+
const expressProvider = container.resolve(ExpressProvider);
|
|
19
|
+
const expressApp = expressProvider.getExpress();
|
|
20
|
+
controllers.forEach((controller) => {
|
|
21
|
+
const endPoints = metadataStore.getControllerEndPointsInfo(controller);
|
|
22
|
+
endPoints.forEach((endPoint) => {
|
|
23
|
+
const method = endPoint.method;
|
|
24
|
+
const route = path
|
|
25
|
+
.join(endPoint.controller.path, endPoint.config?.path ?? '')
|
|
26
|
+
.replaceAll('\\', '/');
|
|
27
|
+
logger.info(`config ${endPoint.method.toUpperCase()} ${route}`);
|
|
28
|
+
const rawMiddlewares = [];
|
|
29
|
+
if (!endPoint.config?.disableJsonParser) {
|
|
30
|
+
rawMiddlewares.push(json());
|
|
31
|
+
}
|
|
32
|
+
if (!endPoint.config?.disableUrlEncodedParser) {
|
|
33
|
+
rawMiddlewares.push(urlencoded({ extended: true }));
|
|
34
|
+
}
|
|
35
|
+
expressApp[method](route, ...rawMiddlewares, async (req, res) => {
|
|
36
|
+
const requestContainer = container.createChildContainer();
|
|
37
|
+
requestContainer.register(EXPRESS_REQ, { useValue: req });
|
|
38
|
+
requestContainer.register(EXPRESS_RES, { useValue: req });
|
|
39
|
+
try {
|
|
40
|
+
const middlewares = endPoint.middlewares.map((x) => requestContainer.resolve(x.middlewareConstructor));
|
|
41
|
+
for (const middleware of middlewares) {
|
|
42
|
+
await middleware.handle(req, res, requestContainer);
|
|
43
|
+
}
|
|
44
|
+
const controllerInstance = requestContainer.resolve(endPoint.controllerConstructor);
|
|
45
|
+
const endPointArgs = [];
|
|
46
|
+
let defaultArgFound = false;
|
|
47
|
+
for (let paramIndex = 0; paramIndex < endPoint.paramsTypes.length; paramIndex++) {
|
|
48
|
+
const paramType = endPoint.paramsTypes[paramIndex];
|
|
49
|
+
if (defaultArgFound) {
|
|
50
|
+
throw new Error(`Cant determine de parameter ${paramIndex} value`);
|
|
51
|
+
}
|
|
52
|
+
defaultArgFound = true;
|
|
53
|
+
if (typeof paramType === 'function') {
|
|
54
|
+
const { value, error } = validate(buildRequest(req), paramType);
|
|
55
|
+
if (error) {
|
|
56
|
+
throw new CustomError({ httpCode: 400, message: error.description, info: error });
|
|
57
|
+
}
|
|
58
|
+
endPointArgs.push(value);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const response = await controllerInstance[endPoint.functionName].apply(controllerInstance, endPointArgs);
|
|
62
|
+
res.status(200).json(response);
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
logger.error(err);
|
|
66
|
+
if (err instanceof Error) {
|
|
67
|
+
const keys = Object.keys(err).filter((key) => !['message', 'stack'].includes(key));
|
|
68
|
+
const { httpCode, ...info } = keys.reduce((acc, key) => {
|
|
69
|
+
acc[key] = err[key];
|
|
70
|
+
return acc;
|
|
71
|
+
}, {});
|
|
72
|
+
res
|
|
73
|
+
.status(httpCode ?? 500)
|
|
74
|
+
.json(removeCircular({ error: { message: err.message, stack: err.stack, ...info } }));
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
res.status(500).json({ error: { message: 'Unknown error' } });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
requestContainer.dispose();
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
expressProvider.listen();
|
|
87
|
+
}
|
|
88
|
+
function removeCircular(obj, seen = new WeakSet()) {
|
|
89
|
+
if (obj && typeof obj === 'object') {
|
|
90
|
+
if (seen.has(obj)) {
|
|
91
|
+
return undefined; // remove circular ref
|
|
92
|
+
}
|
|
93
|
+
seen.add(obj);
|
|
94
|
+
const clone = Array.isArray(obj) ? [] : {};
|
|
95
|
+
for (const key in obj) {
|
|
96
|
+
clone[key] = removeCircular(obj[key], seen);
|
|
97
|
+
}
|
|
98
|
+
return clone;
|
|
99
|
+
}
|
|
100
|
+
return obj;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export { runRestControllers };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import {
|
|
2
|
+
import { singleton } from '../../core/injection/index.js';
|
|
3
|
+
import { Logger } from '../../core/logger/Logger.js';
|
|
3
4
|
import { HttpServerProvider } from '../http/HttpServerProvider.js';
|
|
4
5
|
import { Server } from 'socket.io';
|
|
5
|
-
import { singleton } from 'tsyringe';
|
|
6
6
|
|
|
7
7
|
let SocketServerProvider = class SocketServerProvider {
|
|
8
8
|
httpServerProvider;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SocketControllerMetadataStore } from './SocketControllerMetadataStore.js';
|
|
2
|
+
import { container } from '../../../core/injection/index.js';
|
|
3
|
+
|
|
4
|
+
function connectionMiddleware(middlewareConstructor) {
|
|
5
|
+
return function (target, propertyKey) {
|
|
6
|
+
const functionName = propertyKey.toString();
|
|
7
|
+
const store = container.resolve(SocketControllerMetadataStore);
|
|
8
|
+
store.saveConnectionMiddlewareMetadata({
|
|
9
|
+
controllerConstructor: target.constructor,
|
|
10
|
+
functionName,
|
|
11
|
+
middlewareConstructor: middlewareConstructor,
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { connectionMiddleware };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { SocketControllerMetadataStore } from './SocketControllerMetadataStore.js';
|
|
3
|
+
|
|
4
|
+
function socketConnection(config) {
|
|
5
|
+
return function (target, propertyKey) {
|
|
6
|
+
const functionName = propertyKey.toString();
|
|
7
|
+
const paramsTypes = Reflect.getMetadata('design:paramtypes', target, functionName);
|
|
8
|
+
const store = container.resolve(SocketControllerMetadataStore);
|
|
9
|
+
store.saveSocketConnectionMetadata({
|
|
10
|
+
controllerConstructor: target.constructor,
|
|
11
|
+
config: typeof config === 'string' ? { namespace: config } : config,
|
|
12
|
+
functionName,
|
|
13
|
+
paramsTypes,
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { socketConnection };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { SocketControllerMetadataStore } from './SocketControllerMetadataStore.js';
|
|
2
|
+
import { container, injectable } from '../../../core/injection/index.js';
|
|
3
|
+
|
|
4
|
+
function socketController(config) {
|
|
5
|
+
return function (target) {
|
|
6
|
+
const metaDataStore = container.resolve(SocketControllerMetadataStore);
|
|
7
|
+
metaDataStore.saveControllerMetadata({
|
|
8
|
+
controllerConstructor: target,
|
|
9
|
+
config: typeof config === 'string' ? { namespace: config } : config,
|
|
10
|
+
});
|
|
11
|
+
injectable()(target);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { socketController };
|