@wabot-dev/framework 0.1.0-beta.4 → 0.1.0-beta.41
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/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 +127 -0
- package/dist/src/addon/chat-bot/openia/OpenaiChatAdapter.js +116 -0
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatMemory.js +5 -4
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatRepository.js +5 -5
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatRepository.js +2 -2
- package/dist/src/addon/chat-controller/cmd/@cmd.js +22 -0
- package/dist/src/{channels → addon/chat-controller}/cmd/CmdChannel.js +4 -26
- package/dist/src/{channels → addon/chat-controller}/socket/@socket.js +8 -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 +8 -4
- package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannel.js +4 -22
- package/dist/src/{channels → addon/chat-controller}/whatsapp/@whatsapp.js +8 -4
- package/dist/src/{channels → addon/chat-controller}/whatsapp/EnvWhatsAppRepository.js +3 -3
- package/dist/src/{channels → addon/chat-controller}/whatsapp/PgWhatsAppRepository.js +1 -1
- package/dist/src/{channels/wabot → addon/chat-controller/whatsapp}/WabotDevConnection.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/{channels → addon/chat-controller}/whatsapp/WhatsAppReceiver.js +4 -7
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppReceiverByDevConnection.js +4 -10
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppReceiverByWebHook.js +6 -12
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppSender.js +39 -10
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppSenderByCloudApi.js +10 -13
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppSenderByDevConnection.js +13 -15
- package/dist/src/addon/mindset/html/HtmlModule.js +70 -0
- package/dist/src/addon/money/MoneyDto.js +22 -0
- package/dist/src/{repository → addon/repository}/pg/PgCrudRepository.js +23 -2
- package/dist/src/{repository → addon/repository}/pg/PgRepositoryBase.js +2 -2
- package/dist/src/addon/rest-controller/jwt/@jwtGuard.js +17 -0
- package/dist/src/addon/rest-controller/jwt/Jwt.js +31 -0
- package/dist/src/addon/rest-controller/jwt/JwtAccessAndRefreshTokenDto.js +20 -0
- package/dist/src/addon/rest-controller/jwt/JwtConfig.js +28 -0
- package/dist/src/addon/rest-controller/jwt/JwtGuardMiddleware.js +45 -0
- package/dist/src/addon/rest-controller/jwt/JwtRefreshToken.js +9 -0
- package/dist/src/addon/rest-controller/jwt/JwtRefreshTokenRepository.js +21 -0
- package/dist/src/addon/rest-controller/jwt/JwtSigner.js +48 -0
- package/dist/src/addon/rest-controller/jwt/JwtTokenDto.js +22 -0
- package/dist/src/addon/validation/is-boolean/@isBoolean.js +17 -0
- package/dist/src/addon/validation/is-boolean/validateIsBoolean.js +12 -0
- package/dist/src/addon/validation/is-date/@isDate.js +17 -0
- package/dist/src/addon/validation/is-date/validateIsDate.js +16 -0
- package/dist/src/addon/validation/is-not-empty/@isNotEmpty.js +17 -0
- package/dist/src/addon/validation/is-not-empty/validateIsNotEmpty.js +12 -0
- package/dist/src/addon/validation/is-number/@isNumber.js +17 -0
- package/dist/src/addon/validation/is-number/validateIsNumber.js +12 -0
- package/dist/src/addon/validation/is-present/@isPresent.js +17 -0
- package/dist/src/addon/validation/is-present/validateIsPresent.js +12 -0
- package/dist/src/addon/validation/is-string/@isString.js +17 -0
- package/dist/src/addon/validation/is-string/validateIsString.js +12 -0
- package/dist/src/addon/validation/max/@max.js +18 -0
- package/dist/src/addon/validation/max/validateMax.js +17 -0
- package/dist/src/addon/validation/min/@min.js +18 -0
- package/dist/src/addon/validation/min/validateMin.js +17 -0
- package/dist/src/core/auth/Auth.js +24 -0
- package/dist/src/core/{Persistent.js → entity/Entity.js} +24 -9
- 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/random/Random.js +37 -0
- package/dist/src/core/storable/Storable.js +8 -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/validateArray.js +51 -0
- package/dist/src/core/validation/validators/validateIsOptional.js +5 -0
- package/dist/src/core/validation/validators/validateModel.js +36 -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 +24 -0
- package/dist/src/feature/async/JobRepository.js +31 -0
- package/dist/src/feature/async/JobRunner.js +45 -0
- package/dist/src/feature/async/JobsEventsHub.js +24 -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 +50 -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 +5 -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}/MindsetOperator.js +66 -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/rest-controller/injection-tokens.js +4 -0
- package/dist/src/feature/rest-controller/metadata/@get.js +19 -0
- package/dist/src/feature/rest-controller/metadata/@middleware.js +16 -0
- package/dist/src/feature/rest-controller/metadata/@post.js +19 -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/runRestControllers.js +88 -0
- package/dist/src/{channels → feature}/socket/SocketServerProvider.js +2 -2
- package/dist/src/index.d.ts +848 -483
- package/dist/src/index.js +122 -76
- package/package.json +9 -3
- package/dist/src/ai/deepseek/DeepSeekChatBotAdapter.js +0 -106
- package/dist/src/ai/openia/OpenaiChatBotAdapter.js +0 -84
- package/dist/src/channels/cmd/@cmd.js +0 -18
- 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/wabot → addon/chat-controller/whatsapp}/WabotDevSocketContracts.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}/IMindset.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,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,26 @@ 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
|
+
const sql = `
|
|
23
|
+
SELECT ${this.columns}
|
|
24
|
+
FROM ${this.table}
|
|
25
|
+
WHERE id IN (${ids.map((_, i) => '$' + (i + 1)).join(',')})
|
|
26
|
+
LIMIT 1
|
|
27
|
+
`;
|
|
28
|
+
const items = await this.query(sql, ids);
|
|
29
|
+
return items;
|
|
30
|
+
}
|
|
31
|
+
async findOrThrow(id) {
|
|
32
|
+
const item = await this.find(id);
|
|
33
|
+
if (!item) {
|
|
34
|
+
throw new CustomError({
|
|
35
|
+
message: `Not found ${this.config.constructor.name} with id = '${id}'}`,
|
|
36
|
+
httpCode: 404,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return item;
|
|
40
|
+
}
|
|
20
41
|
async findAll() {
|
|
21
42
|
const sql = `
|
|
22
43
|
SELECT ${this.columns}
|
|
@@ -46,10 +67,10 @@ class PgCrudRepository extends PgRepositoryBase {
|
|
|
46
67
|
SET ${this.updates}
|
|
47
68
|
WHERE id = $${this.columnsList.length + 1}
|
|
48
69
|
`;
|
|
49
|
-
await this.exec(sql, [...this.values(item), item.
|
|
70
|
+
await this.exec(sql, [...this.values(item), item.id]);
|
|
50
71
|
}
|
|
51
72
|
async discard(item) {
|
|
52
|
-
const _item = await this.find(item.
|
|
73
|
+
const _item = await this.find(item.id);
|
|
53
74
|
if (!_item) {
|
|
54
75
|
throw new Error('Not found');
|
|
55
76
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import '../../../core/injection/index.js';
|
|
2
|
+
import '../../../feature/rest-controller/metadata/RestControllerMetadataStore.js';
|
|
3
|
+
import { middleware } from '../../../feature/rest-controller/metadata/@middleware.js';
|
|
4
|
+
import 'debug';
|
|
5
|
+
import '../../../core/validation/metadata/ValidationMetadataStore.js';
|
|
6
|
+
import '../../../feature/express/ExpressProvider.js';
|
|
7
|
+
import 'express';
|
|
8
|
+
import 'path';
|
|
9
|
+
import { JwtGuardMiddleware } from './JwtGuardMiddleware.js';
|
|
10
|
+
|
|
11
|
+
function jwtGuard() {
|
|
12
|
+
return function (target, propertyKey) {
|
|
13
|
+
middleware(JwtGuardMiddleware)(target, propertyKey);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { jwtGuard };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { JwtSigner } from './JwtSigner.js';
|
|
3
|
+
import { JwtRefreshTokenRepository } from './JwtRefreshTokenRepository.js';
|
|
4
|
+
import { JwtRefreshToken } from './JwtRefreshToken.js';
|
|
5
|
+
import { injectable } from '../../../core/injection/index.js';
|
|
6
|
+
import { Auth } from '../../../core/auth/Auth.js';
|
|
7
|
+
|
|
8
|
+
let Jwt = class Jwt {
|
|
9
|
+
auth;
|
|
10
|
+
jwtSigner;
|
|
11
|
+
jwtRefreshTokenRepository;
|
|
12
|
+
constructor(auth, jwtSigner, jwtRefreshTokenRepository) {
|
|
13
|
+
this.auth = auth;
|
|
14
|
+
this.jwtSigner = jwtSigner;
|
|
15
|
+
this.jwtRefreshTokenRepository = jwtRefreshTokenRepository;
|
|
16
|
+
}
|
|
17
|
+
async createToken() {
|
|
18
|
+
const authInfo = this.auth.require();
|
|
19
|
+
const refreshToken = new JwtRefreshToken({ authInfo });
|
|
20
|
+
await this.jwtRefreshTokenRepository.create(refreshToken);
|
|
21
|
+
return await this.jwtSigner.signAccessAndRefreshToken(refreshToken);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
Jwt = __decorate([
|
|
25
|
+
injectable(),
|
|
26
|
+
__metadata("design:paramtypes", [Auth,
|
|
27
|
+
JwtSigner,
|
|
28
|
+
JwtRefreshTokenRepository])
|
|
29
|
+
], Jwt);
|
|
30
|
+
|
|
31
|
+
export { Jwt };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { isModel } from '../../../core/validation/metadata/@isModel.js';
|
|
3
|
+
import '../../../core/injection/index.js';
|
|
4
|
+
import '../../../core/validation/metadata/ValidationMetadataStore.js';
|
|
5
|
+
import { JwtTokenDto } from './JwtTokenDto.js';
|
|
6
|
+
|
|
7
|
+
class JwtAccessAndRefreshTokenDto {
|
|
8
|
+
access;
|
|
9
|
+
refresh;
|
|
10
|
+
}
|
|
11
|
+
__decorate([
|
|
12
|
+
isModel(JwtTokenDto),
|
|
13
|
+
__metadata("design:type", JwtTokenDto)
|
|
14
|
+
], JwtAccessAndRefreshTokenDto.prototype, "access", void 0);
|
|
15
|
+
__decorate([
|
|
16
|
+
isModel(JwtTokenDto),
|
|
17
|
+
__metadata("design:type", JwtTokenDto)
|
|
18
|
+
], JwtAccessAndRefreshTokenDto.prototype, "refresh", void 0);
|
|
19
|
+
|
|
20
|
+
export { JwtAccessAndRefreshTokenDto };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { Env } from '../../../core/env/Env.js';
|
|
3
|
+
import { singleton } from '../../../core/injection/index.js';
|
|
4
|
+
|
|
5
|
+
let JwtConfig = class JwtConfig {
|
|
6
|
+
secretOrPublicKey;
|
|
7
|
+
secretOrPrivateKey;
|
|
8
|
+
algorithm;
|
|
9
|
+
accessExpirationSeconds;
|
|
10
|
+
refreshExpirationSeconds;
|
|
11
|
+
constructor(env) {
|
|
12
|
+
this.algorithm = env.requireString('JWT_ALGORITHM', { default: 'HS256' });
|
|
13
|
+
this.secretOrPublicKey = env.requireString('JWT_SECRET');
|
|
14
|
+
this.secretOrPrivateKey = env.requireString('JWT_SECRET');
|
|
15
|
+
this.accessExpirationSeconds = env.requireNumber('JWT_ACCESS_EXPIRATION_SECONDS', {
|
|
16
|
+
default: 10 * 60,
|
|
17
|
+
});
|
|
18
|
+
this.refreshExpirationSeconds = env.requireNumber('JWT_REFRESH_EXPIRATION_SECONDS', {
|
|
19
|
+
default: 365 * 24 * 3600,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
JwtConfig = __decorate([
|
|
24
|
+
singleton(),
|
|
25
|
+
__metadata("design:paramtypes", [Env])
|
|
26
|
+
], JwtConfig);
|
|
27
|
+
|
|
28
|
+
export { JwtConfig };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { CustomError } from '../../../core/error/CustomError.js';
|
|
3
|
+
import { injectable } from '../../../core/injection/index.js';
|
|
4
|
+
import jwt from 'jsonwebtoken';
|
|
5
|
+
import { JwtConfig } from './JwtConfig.js';
|
|
6
|
+
import { Auth } from '../../../core/auth/Auth.js';
|
|
7
|
+
|
|
8
|
+
let JwtGuardMiddleware = class JwtGuardMiddleware {
|
|
9
|
+
config;
|
|
10
|
+
auth;
|
|
11
|
+
constructor(config, auth) {
|
|
12
|
+
this.config = config;
|
|
13
|
+
this.auth = auth;
|
|
14
|
+
}
|
|
15
|
+
async handle(req, res, container) {
|
|
16
|
+
const authorization = req.header('Authorization');
|
|
17
|
+
if (!authorization) {
|
|
18
|
+
throw new CustomError({ httpCode: 401, message: 'Authorization header not available' });
|
|
19
|
+
}
|
|
20
|
+
const [bearer, token] = authorization.split(' ');
|
|
21
|
+
if (bearer.toLowerCase() !== 'bearer' || !token) {
|
|
22
|
+
throw new CustomError({ httpCode: 401, message: 'Authorization should be a bearer token' });
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const jwtPayload = jwt.verify(token, this.config.secretOrPublicKey, {
|
|
26
|
+
algorithms: [this.config.algorithm],
|
|
27
|
+
});
|
|
28
|
+
this.auth.assign(jwtPayload);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
throw new CustomError({
|
|
32
|
+
httpCode: 401,
|
|
33
|
+
message: err instanceof Error ? `Invalid token: ${err.message}` : 'Invalid token',
|
|
34
|
+
cause: err instanceof Error ? err : undefined,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
JwtGuardMiddleware = __decorate([
|
|
40
|
+
injectable(),
|
|
41
|
+
__metadata("design:paramtypes", [JwtConfig,
|
|
42
|
+
Auth])
|
|
43
|
+
], JwtGuardMiddleware);
|
|
44
|
+
|
|
45
|
+
export { JwtGuardMiddleware };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { singleton } from '../../../core/injection/index.js';
|
|
3
|
+
import { JwtRefreshToken } from './JwtRefreshToken.js';
|
|
4
|
+
import { Pool } from 'pg';
|
|
5
|
+
import { PgCrudRepository } from '../../repository/pg/PgCrudRepository.js';
|
|
6
|
+
|
|
7
|
+
let JwtRefreshTokenRepository = class JwtRefreshTokenRepository extends PgCrudRepository {
|
|
8
|
+
constructor(pool) {
|
|
9
|
+
super(pool, {
|
|
10
|
+
schema: 'wabot',
|
|
11
|
+
table: 'jwt_refresh_token',
|
|
12
|
+
constructor: JwtRefreshToken,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
JwtRefreshTokenRepository = __decorate([
|
|
17
|
+
singleton(),
|
|
18
|
+
__metadata("design:paramtypes", [Pool])
|
|
19
|
+
], JwtRefreshTokenRepository);
|
|
20
|
+
|
|
21
|
+
export { JwtRefreshTokenRepository };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { JwtConfig } from './JwtConfig.js';
|
|
3
|
+
import jwt from 'jsonwebtoken';
|
|
4
|
+
import { JwtTokenDto } from './JwtTokenDto.js';
|
|
5
|
+
import { JwtRefreshToken } from './JwtRefreshToken.js';
|
|
6
|
+
import { JwtAccessAndRefreshTokenDto } from './JwtAccessAndRefreshTokenDto.js';
|
|
7
|
+
import { injectable } from '../../../core/injection/index.js';
|
|
8
|
+
import { Mapper } from '../../../core/mapper/Mapper.js';
|
|
9
|
+
|
|
10
|
+
let JwtSigner = class JwtSigner {
|
|
11
|
+
config;
|
|
12
|
+
mapper;
|
|
13
|
+
constructor(config, mapper) {
|
|
14
|
+
this.config = config;
|
|
15
|
+
this.mapper = mapper;
|
|
16
|
+
}
|
|
17
|
+
async signAccessToken(info) {
|
|
18
|
+
const _authInfo = info instanceof JwtRefreshToken
|
|
19
|
+
? {
|
|
20
|
+
...info.authInfo,
|
|
21
|
+
refreshTokenId: info.id,
|
|
22
|
+
}
|
|
23
|
+
: info;
|
|
24
|
+
const token = jwt.sign(_authInfo, this.config.secretOrPrivateKey, {
|
|
25
|
+
expiresIn: this.config.accessExpirationSeconds,
|
|
26
|
+
});
|
|
27
|
+
const expiration = new Date().getTime() + this.config.accessExpirationSeconds * 1000;
|
|
28
|
+
return this.mapper.map({ token, expiration }, JwtTokenDto);
|
|
29
|
+
}
|
|
30
|
+
async signRefreshToken(refreshToken) {
|
|
31
|
+
const token = jwt.sign({ refreshTokenId: refreshToken.id }, this.config.secretOrPrivateKey, {
|
|
32
|
+
expiresIn: this.config.refreshExpirationSeconds,
|
|
33
|
+
});
|
|
34
|
+
const expiration = new Date().getTime() + this.config.refreshExpirationSeconds * 1000;
|
|
35
|
+
return this.mapper.map({ token, expiration }, JwtTokenDto);
|
|
36
|
+
}
|
|
37
|
+
async signAccessAndRefreshToken(refreshToken) {
|
|
38
|
+
const access = await this.signAccessToken(refreshToken);
|
|
39
|
+
const refresh = await this.signRefreshToken(refreshToken);
|
|
40
|
+
return this.mapper.map({ access, refresh }, JwtAccessAndRefreshTokenDto);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
JwtSigner = __decorate([
|
|
44
|
+
injectable(),
|
|
45
|
+
__metadata("design:paramtypes", [JwtConfig, Mapper])
|
|
46
|
+
], JwtSigner);
|
|
47
|
+
|
|
48
|
+
export { JwtSigner };
|
|
@@ -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 { isDate } from '../../validation/is-date/@isDate.js';
|
|
5
|
+
import { isNotEmpty } from '../../validation/is-not-empty/@isNotEmpty.js';
|
|
6
|
+
import { isString } from '../../validation/is-string/@isString.js';
|
|
7
|
+
|
|
8
|
+
class JwtTokenDto {
|
|
9
|
+
token;
|
|
10
|
+
expiration;
|
|
11
|
+
}
|
|
12
|
+
__decorate([
|
|
13
|
+
isString(),
|
|
14
|
+
isNotEmpty(),
|
|
15
|
+
__metadata("design:type", String)
|
|
16
|
+
], JwtTokenDto.prototype, "token", void 0);
|
|
17
|
+
__decorate([
|
|
18
|
+
isDate(),
|
|
19
|
+
__metadata("design:type", Date)
|
|
20
|
+
], JwtTokenDto.prototype, "expiration", void 0);
|
|
21
|
+
|
|
22
|
+
export { JwtTokenDto };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ValidationMetadataStore } from '../../../core/validation/metadata/ValidationMetadataStore.js';
|
|
3
|
+
import { validateIsBoolean } from './validateIsBoolean.js';
|
|
4
|
+
|
|
5
|
+
function isBoolean() {
|
|
6
|
+
return function (target, propertyKey) {
|
|
7
|
+
const propertyName = propertyKey.toString();
|
|
8
|
+
const store = container.resolve(ValidationMetadataStore);
|
|
9
|
+
store.saveValidatorMetadata({
|
|
10
|
+
modelConstructor: target.constructor,
|
|
11
|
+
propertyName,
|
|
12
|
+
validator: validateIsBoolean,
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { isBoolean };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { validateIsDate } from './validateIsDate.js';
|
|
3
|
+
import { ValidationMetadataStore } from '../../../core/validation/metadata/ValidationMetadataStore.js';
|
|
4
|
+
|
|
5
|
+
function isDate() {
|
|
6
|
+
return function (target, propertyKey) {
|
|
7
|
+
const propertyName = propertyKey.toString();
|
|
8
|
+
const store = container.resolve(ValidationMetadataStore);
|
|
9
|
+
store.saveValidatorMetadata({
|
|
10
|
+
modelConstructor: target.constructor,
|
|
11
|
+
propertyName,
|
|
12
|
+
validator: validateIsDate,
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { isDate };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
function validateIsDate(value) {
|
|
2
|
+
const _value = new Date(value);
|
|
3
|
+
const time = _value.getTime();
|
|
4
|
+
if ((typeof value !== 'number' && typeof value !== 'string' && !(value instanceof Date)) ||
|
|
5
|
+
isNaN(time) ||
|
|
6
|
+
time < 0) {
|
|
7
|
+
return {
|
|
8
|
+
error: { description: `Date value is not valid` },
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
value: _value,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { validateIsDate };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ValidationMetadataStore } from '../../../core/validation/metadata/ValidationMetadataStore.js';
|
|
3
|
+
import { validateIsNotEmpty } from './validateIsNotEmpty.js';
|
|
4
|
+
|
|
5
|
+
function isNotEmpty() {
|
|
6
|
+
return function (target, propertyKey) {
|
|
7
|
+
const propertyName = propertyKey.toString();
|
|
8
|
+
const store = container.resolve(ValidationMetadataStore);
|
|
9
|
+
store.saveValidatorMetadata({
|
|
10
|
+
modelConstructor: target.constructor,
|
|
11
|
+
propertyName,
|
|
12
|
+
validator: validateIsNotEmpty,
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { isNotEmpty };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { validateIsNumber } from './validateIsNumber.js';
|
|
3
|
+
import { ValidationMetadataStore } from '../../../core/validation/metadata/ValidationMetadataStore.js';
|
|
4
|
+
|
|
5
|
+
function isNumber() {
|
|
6
|
+
return function (target, propertyKey) {
|
|
7
|
+
const propertyName = propertyKey.toString();
|
|
8
|
+
const store = container.resolve(ValidationMetadataStore);
|
|
9
|
+
store.saveValidatorMetadata({
|
|
10
|
+
modelConstructor: target.constructor,
|
|
11
|
+
propertyName,
|
|
12
|
+
validator: validateIsNumber,
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { isNumber };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ValidationMetadataStore } from '../../../core/validation/metadata/ValidationMetadataStore.js';
|
|
3
|
+
import { validateIsPresent } from './validateIsPresent.js';
|
|
4
|
+
|
|
5
|
+
function isPresent() {
|
|
6
|
+
return function (target, propertyKey) {
|
|
7
|
+
const propertyName = propertyKey.toString();
|
|
8
|
+
const store = container.resolve(ValidationMetadataStore);
|
|
9
|
+
store.saveValidatorMetadata({
|
|
10
|
+
modelConstructor: target.constructor,
|
|
11
|
+
propertyName,
|
|
12
|
+
validator: validateIsPresent,
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { isPresent };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ValidationMetadataStore } from '../../../core/validation/metadata/ValidationMetadataStore.js';
|
|
3
|
+
import { validateIsString } from './validateIsString.js';
|
|
4
|
+
|
|
5
|
+
function isString() {
|
|
6
|
+
return function (target, propertyKey) {
|
|
7
|
+
const propertyName = propertyKey.toString();
|
|
8
|
+
const store = container.resolve(ValidationMetadataStore);
|
|
9
|
+
store.saveValidatorMetadata({
|
|
10
|
+
modelConstructor: target.constructor,
|
|
11
|
+
propertyName,
|
|
12
|
+
validator: validateIsString,
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { isString };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ValidationMetadataStore } from '../../../core/validation/metadata/ValidationMetadataStore.js';
|
|
3
|
+
import { validateMax } from './validateMax.js';
|
|
4
|
+
|
|
5
|
+
function max(limit) {
|
|
6
|
+
return function (target, propertyKey) {
|
|
7
|
+
const propertyName = propertyKey.toString();
|
|
8
|
+
const store = container.resolve(ValidationMetadataStore);
|
|
9
|
+
store.saveValidatorMetadata({
|
|
10
|
+
modelConstructor: target.constructor,
|
|
11
|
+
propertyName,
|
|
12
|
+
validator: validateMax,
|
|
13
|
+
validatorOptions: { limit },
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { max };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function validateMax(value, options) {
|
|
2
|
+
if (value == null) {
|
|
3
|
+
return {
|
|
4
|
+
error: { description: `null or undefined value can't be validated with max` },
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
if (Number(value) > Number(options.limit)) {
|
|
8
|
+
return {
|
|
9
|
+
error: { description: `exceeds the established max limit` },
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
value,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { validateMax };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ValidationMetadataStore } from '../../../core/validation/metadata/ValidationMetadataStore.js';
|
|
3
|
+
import { validateMin } from './validateMin.js';
|
|
4
|
+
|
|
5
|
+
function min(limit) {
|
|
6
|
+
return function (target, propertyKey) {
|
|
7
|
+
const propertyName = propertyKey.toString();
|
|
8
|
+
const store = container.resolve(ValidationMetadataStore);
|
|
9
|
+
store.saveValidatorMetadata({
|
|
10
|
+
modelConstructor: target.constructor,
|
|
11
|
+
propertyName,
|
|
12
|
+
validator: validateMin,
|
|
13
|
+
validatorOptions: { limit },
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { min };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function validateMin(value, options) {
|
|
2
|
+
if (value == null) {
|
|
3
|
+
return {
|
|
4
|
+
error: { description: `null or undefined value can't be validated with min` },
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
if (Number(value) < Number(options.limit)) {
|
|
8
|
+
return {
|
|
9
|
+
error: { description: `exceeds the established min limit` },
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
value,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { validateMin };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { __decorate } from 'tslib';
|
|
2
|
+
import { CustomError } from '../error/CustomError.js';
|
|
3
|
+
import { scoped, Lifecycle } from '../injection/index.js';
|
|
4
|
+
|
|
5
|
+
let Auth = class Auth {
|
|
6
|
+
authInfo = null;
|
|
7
|
+
require() {
|
|
8
|
+
if (!this.authInfo) {
|
|
9
|
+
throw new CustomError({ message: 'Unauthorized', httpCode: 401 });
|
|
10
|
+
}
|
|
11
|
+
return this.authInfo;
|
|
12
|
+
}
|
|
13
|
+
assign(authInfo) {
|
|
14
|
+
if (this.authInfo) {
|
|
15
|
+
throw new CustomError({ message: 'Authorization info already assigned', httpCode: 401 });
|
|
16
|
+
}
|
|
17
|
+
this.authInfo = authInfo;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
Auth = __decorate([
|
|
21
|
+
scoped(Lifecycle.ContainerScoped)
|
|
22
|
+
], Auth);
|
|
23
|
+
|
|
24
|
+
export { Auth };
|
|
@@ -1,22 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
getId() {
|
|
1
|
+
import { Storable } from '../storable/Storable.js';
|
|
2
|
+
|
|
3
|
+
class Entity extends Storable {
|
|
4
|
+
get id() {
|
|
7
5
|
if (!this.data.id) {
|
|
8
6
|
throw new Error('id is required');
|
|
9
7
|
}
|
|
10
8
|
return this.data.id;
|
|
11
9
|
}
|
|
12
|
-
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated use id
|
|
12
|
+
*/
|
|
13
|
+
getId() {
|
|
14
|
+
return this.id;
|
|
15
|
+
}
|
|
16
|
+
get createdAt() {
|
|
13
17
|
if (!this.data.createdAt) {
|
|
14
18
|
throw new Error('createdAt is required');
|
|
15
19
|
}
|
|
16
20
|
return new Date(this.data.createdAt);
|
|
17
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated use createdAt
|
|
24
|
+
*/
|
|
25
|
+
getCreatedAt() {
|
|
26
|
+
return this.createdAt;
|
|
27
|
+
}
|
|
18
28
|
update(newData) {
|
|
19
|
-
this.data = newData;
|
|
29
|
+
this.data = { ...this.data, ...newData, updatedAt: new Date().getTime() };
|
|
20
30
|
}
|
|
21
31
|
wasCreated() {
|
|
22
32
|
return !!this.data.createdAt || !!this.data.id;
|
|
@@ -33,5 +43,10 @@ class Persistent {
|
|
|
33
43
|
this.data.discardedAt = new Date().getTime();
|
|
34
44
|
}
|
|
35
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* @deprecated Should use Entity
|
|
48
|
+
*/
|
|
49
|
+
class Persistent extends Entity {
|
|
50
|
+
}
|
|
36
51
|
|
|
37
|
-
export { Persistent };
|
|
52
|
+
export { Entity, Persistent };
|