@vezlo/assistant-server 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.
- package/Dockerfile +64 -0
- package/LICENSE +661 -0
- package/README.md +432 -0
- package/bin/vezlo-server.js +36 -0
- package/database-schema.sql +154 -0
- package/dist/knexfile.d.ts +6 -0
- package/dist/knexfile.d.ts.map +1 -0
- package/dist/knexfile.js +85 -0
- package/dist/knexfile.js.map +1 -0
- package/dist/src/config/database.d.ts +4 -0
- package/dist/src/config/database.d.ts.map +1 -0
- package/dist/src/config/database.js +30 -0
- package/dist/src/config/database.js.map +1 -0
- package/dist/src/config/global.d.ts +65 -0
- package/dist/src/config/global.d.ts.map +1 -0
- package/dist/src/config/global.js +113 -0
- package/dist/src/config/global.js.map +1 -0
- package/dist/src/config/knex.d.ts +9 -0
- package/dist/src/config/knex.d.ts.map +1 -0
- package/dist/src/config/knex.js +67 -0
- package/dist/src/config/knex.js.map +1 -0
- package/dist/src/config/logger.d.ts +4 -0
- package/dist/src/config/logger.d.ts.map +1 -0
- package/dist/src/config/logger.js +30 -0
- package/dist/src/config/logger.js.map +1 -0
- package/dist/src/config/swagger.d.ts +18 -0
- package/dist/src/config/swagger.d.ts.map +1 -0
- package/dist/src/config/swagger.js +230 -0
- package/dist/src/config/swagger.js.map +1 -0
- package/dist/src/controllers/ChatController.d.ts +16 -0
- package/dist/src/controllers/ChatController.d.ts.map +1 -0
- package/dist/src/controllers/ChatController.js +281 -0
- package/dist/src/controllers/ChatController.js.map +1 -0
- package/dist/src/controllers/KnowledgeController.d.ts +13 -0
- package/dist/src/controllers/KnowledgeController.d.ts.map +1 -0
- package/dist/src/controllers/KnowledgeController.js +210 -0
- package/dist/src/controllers/KnowledgeController.js.map +1 -0
- package/dist/src/middleware/errorHandler.d.ts +63 -0
- package/dist/src/middleware/errorHandler.d.ts.map +1 -0
- package/dist/src/middleware/errorHandler.js +226 -0
- package/dist/src/middleware/errorHandler.js.map +1 -0
- package/dist/src/migrations/001_initial_schema.d.ts +4 -0
- package/dist/src/migrations/001_initial_schema.d.ts.map +1 -0
- package/dist/src/migrations/001_initial_schema.js +144 -0
- package/dist/src/migrations/001_initial_schema.js.map +1 -0
- package/dist/src/schemas/ConversationSchemas.d.ts +132 -0
- package/dist/src/schemas/ConversationSchemas.d.ts.map +1 -0
- package/dist/src/schemas/ConversationSchemas.js +71 -0
- package/dist/src/schemas/ConversationSchemas.js.map +1 -0
- package/dist/src/schemas/FeedbackSchemas.d.ts +222 -0
- package/dist/src/schemas/FeedbackSchemas.d.ts.map +1 -0
- package/dist/src/schemas/FeedbackSchemas.js +116 -0
- package/dist/src/schemas/FeedbackSchemas.js.map +1 -0
- package/dist/src/schemas/KnowledgeSchemas.d.ts +266 -0
- package/dist/src/schemas/KnowledgeSchemas.d.ts.map +1 -0
- package/dist/src/schemas/KnowledgeSchemas.js +115 -0
- package/dist/src/schemas/KnowledgeSchemas.js.map +1 -0
- package/dist/src/schemas/MessageSchemas.d.ts +145 -0
- package/dist/src/schemas/MessageSchemas.d.ts.map +1 -0
- package/dist/src/schemas/MessageSchemas.js +79 -0
- package/dist/src/schemas/MessageSchemas.js.map +1 -0
- package/dist/src/schemas/index.d.ts +752 -0
- package/dist/src/schemas/index.d.ts.map +1 -0
- package/dist/src/schemas/index.js +31 -0
- package/dist/src/schemas/index.js.map +1 -0
- package/dist/src/server.d.ts +2 -0
- package/dist/src/server.d.ts.map +1 -0
- package/dist/src/server.js +609 -0
- package/dist/src/server.js.map +1 -0
- package/dist/src/services/AIService.d.ts +17 -0
- package/dist/src/services/AIService.d.ts.map +1 -0
- package/dist/src/services/AIService.js +174 -0
- package/dist/src/services/AIService.js.map +1 -0
- package/dist/src/services/ChatManager.d.ts +18 -0
- package/dist/src/services/ChatManager.d.ts.map +1 -0
- package/dist/src/services/ChatManager.js +174 -0
- package/dist/src/services/ChatManager.js.map +1 -0
- package/dist/src/services/KnowledgeBaseService.d.ts +72 -0
- package/dist/src/services/KnowledgeBaseService.d.ts.map +1 -0
- package/dist/src/services/KnowledgeBaseService.js +442 -0
- package/dist/src/services/KnowledgeBaseService.js.map +1 -0
- package/dist/src/storage/ConversationRepository.d.ts +15 -0
- package/dist/src/storage/ConversationRepository.d.ts.map +1 -0
- package/dist/src/storage/ConversationRepository.js +123 -0
- package/dist/src/storage/ConversationRepository.js.map +1 -0
- package/dist/src/storage/FeedbackRepository.d.ts +15 -0
- package/dist/src/storage/FeedbackRepository.d.ts.map +1 -0
- package/dist/src/storage/FeedbackRepository.js +141 -0
- package/dist/src/storage/FeedbackRepository.js.map +1 -0
- package/dist/src/storage/MessageRepository.d.ts +13 -0
- package/dist/src/storage/MessageRepository.d.ts.map +1 -0
- package/dist/src/storage/MessageRepository.js +165 -0
- package/dist/src/storage/MessageRepository.js.map +1 -0
- package/dist/src/storage/SupabaseStorage.d.ts +20 -0
- package/dist/src/storage/SupabaseStorage.d.ts.map +1 -0
- package/dist/src/storage/SupabaseStorage.js +347 -0
- package/dist/src/storage/SupabaseStorage.js.map +1 -0
- package/dist/src/storage/UnifiedStorage.d.ts +45 -0
- package/dist/src/storage/UnifiedStorage.d.ts.map +1 -0
- package/dist/src/storage/UnifiedStorage.js +94 -0
- package/dist/src/storage/UnifiedStorage.js.map +1 -0
- package/dist/src/storage/index.d.ts +11 -0
- package/dist/src/storage/index.d.ts.map +1 -0
- package/dist/src/storage/index.js +21 -0
- package/dist/src/storage/index.js.map +1 -0
- package/dist/src/types/index.d.ts +163 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +3 -0
- package/dist/src/types/index.js.map +1 -0
- package/docker-compose.yml +32 -0
- package/env.example +46 -0
- package/knexfile.ts +86 -0
- package/package.json +92 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.KnowledgeController = void 0;
|
|
7
|
+
const logger_1 = __importDefault(require("../config/logger"));
|
|
8
|
+
class KnowledgeController {
|
|
9
|
+
constructor(knowledgeBase) {
|
|
10
|
+
this.knowledgeBase = knowledgeBase;
|
|
11
|
+
}
|
|
12
|
+
async createItem(req, res) {
|
|
13
|
+
try {
|
|
14
|
+
const { parent_uuid, company_uuid, title, description, type, content, file_url, file_size, file_type, metadata, created_by_uuid } = req.body;
|
|
15
|
+
if (!title || !type || !created_by_uuid) {
|
|
16
|
+
res.status(400).json({ error: 'title, type, and created_by_uuid are required' });
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
// Validate type
|
|
20
|
+
const validTypes = ['folder', 'document', 'file', 'url', 'url_directory'];
|
|
21
|
+
if (!validTypes.includes(type)) {
|
|
22
|
+
res.status(400).json({ error: `type must be one of: ${validTypes.join(', ')}` });
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
// Validate required fields based on type
|
|
26
|
+
if ((type === 'document') && !content) {
|
|
27
|
+
res.status(400).json({ error: 'content is required for document type' });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if ((type === 'file' || type === 'url') && !file_url) {
|
|
31
|
+
res.status(400).json({ error: 'file_url is required for file and url types' });
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const itemId = await this.knowledgeBase.createItem({
|
|
35
|
+
parent_id: parent_uuid,
|
|
36
|
+
company_id: company_uuid || 1,
|
|
37
|
+
title,
|
|
38
|
+
description,
|
|
39
|
+
type,
|
|
40
|
+
content,
|
|
41
|
+
file_url,
|
|
42
|
+
file_size,
|
|
43
|
+
file_type,
|
|
44
|
+
metadata,
|
|
45
|
+
created_by: parseInt(created_by_uuid) || 1
|
|
46
|
+
});
|
|
47
|
+
res.json({
|
|
48
|
+
success: true,
|
|
49
|
+
uuid: itemId
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
logger_1.default.error('Create knowledge item error:', error);
|
|
54
|
+
res.status(500).json({
|
|
55
|
+
error: 'Failed to create knowledge item',
|
|
56
|
+
message: error instanceof Error ? error.message : 'Unknown error'
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async getItem(req, res) {
|
|
61
|
+
try {
|
|
62
|
+
const { uuid } = req.params;
|
|
63
|
+
const item = await this.knowledgeBase.getItem(uuid);
|
|
64
|
+
if (!item) {
|
|
65
|
+
res.status(404).json({ error: 'Knowledge item not found' });
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
res.json({
|
|
69
|
+
uuid: item.id,
|
|
70
|
+
parent_uuid: item.parent_id,
|
|
71
|
+
company_uuid: item.company_id,
|
|
72
|
+
title: item.title,
|
|
73
|
+
description: item.description,
|
|
74
|
+
type: item.type,
|
|
75
|
+
content: item.content,
|
|
76
|
+
file_url: item.file_url,
|
|
77
|
+
file_size: item.file_size,
|
|
78
|
+
file_type: item.file_type,
|
|
79
|
+
metadata: item.metadata,
|
|
80
|
+
created_by: item.created_by
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
logger_1.default.error('Get knowledge item error:', error);
|
|
85
|
+
res.status(500).json({
|
|
86
|
+
error: 'Failed to get knowledge item',
|
|
87
|
+
message: error instanceof Error ? error.message : 'Unknown error'
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async listItems(req, res) {
|
|
92
|
+
try {
|
|
93
|
+
const { parent_uuid, company_uuid, type, limit = '50', offset = '0' } = req.query;
|
|
94
|
+
const options = {
|
|
95
|
+
parent_id: parent_uuid,
|
|
96
|
+
company_id: company_uuid ? parseInt(company_uuid) : undefined,
|
|
97
|
+
type: type,
|
|
98
|
+
limit: parseInt(limit),
|
|
99
|
+
offset: parseInt(offset)
|
|
100
|
+
};
|
|
101
|
+
const result = await this.knowledgeBase.listItems(options);
|
|
102
|
+
res.json({
|
|
103
|
+
items: result.items.map(item => ({
|
|
104
|
+
uuid: item.id,
|
|
105
|
+
parent_uuid: item.parent_id,
|
|
106
|
+
company_uuid: item.company_id,
|
|
107
|
+
title: item.title,
|
|
108
|
+
description: item.description,
|
|
109
|
+
type: item.type,
|
|
110
|
+
file_url: item.file_url,
|
|
111
|
+
file_size: item.file_size,
|
|
112
|
+
file_type: item.file_type,
|
|
113
|
+
metadata: item.metadata,
|
|
114
|
+
created_by: item.created_by
|
|
115
|
+
})),
|
|
116
|
+
total: result.total,
|
|
117
|
+
limit: options.limit,
|
|
118
|
+
offset: options.offset
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
logger_1.default.error('List knowledge items error:', error);
|
|
123
|
+
res.status(500).json({
|
|
124
|
+
error: 'Failed to list knowledge items',
|
|
125
|
+
message: error instanceof Error ? error.message : 'Unknown error'
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async updateItem(req, res) {
|
|
130
|
+
try {
|
|
131
|
+
const { uuid } = req.params;
|
|
132
|
+
const { title, description, content, file_url, file_size, file_type, metadata } = req.body;
|
|
133
|
+
const success = await this.knowledgeBase.updateItem(uuid, {
|
|
134
|
+
title,
|
|
135
|
+
description,
|
|
136
|
+
content,
|
|
137
|
+
file_url,
|
|
138
|
+
file_size,
|
|
139
|
+
file_type,
|
|
140
|
+
metadata
|
|
141
|
+
});
|
|
142
|
+
if (!success) {
|
|
143
|
+
res.status(404).json({ error: 'Knowledge item not found or could not be updated' });
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
res.json({ success: true });
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
logger_1.default.error('Update knowledge item error:', error);
|
|
150
|
+
res.status(500).json({
|
|
151
|
+
error: 'Failed to update knowledge item',
|
|
152
|
+
message: error instanceof Error ? error.message : 'Unknown error'
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async deleteItem(req, res) {
|
|
157
|
+
try {
|
|
158
|
+
const { uuid } = req.params;
|
|
159
|
+
const success = await this.knowledgeBase.deleteItem(uuid);
|
|
160
|
+
if (!success) {
|
|
161
|
+
res.status(404).json({ error: 'Knowledge item not found or could not be deleted' });
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
res.json({ success: true });
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
logger_1.default.error('Delete knowledge item error:', error);
|
|
168
|
+
res.status(500).json({
|
|
169
|
+
error: 'Failed to delete knowledge item',
|
|
170
|
+
message: error instanceof Error ? error.message : 'Unknown error'
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
async search(req, res) {
|
|
175
|
+
try {
|
|
176
|
+
const { query, limit = '5', threshold = '0.7', type = 'hybrid', company_uuid } = req.body;
|
|
177
|
+
if (!query) {
|
|
178
|
+
res.status(400).json({ error: 'query is required' });
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const results = await this.knowledgeBase.search(query, {
|
|
182
|
+
limit: parseInt(limit),
|
|
183
|
+
threshold: parseFloat(threshold),
|
|
184
|
+
type: type,
|
|
185
|
+
company_id: company_uuid ? parseInt(company_uuid) : undefined
|
|
186
|
+
});
|
|
187
|
+
res.json({
|
|
188
|
+
query,
|
|
189
|
+
results: results.map(result => ({
|
|
190
|
+
uuid: result.id,
|
|
191
|
+
title: result.title,
|
|
192
|
+
description: result.description,
|
|
193
|
+
content: result.content,
|
|
194
|
+
type: result.type,
|
|
195
|
+
score: result.score,
|
|
196
|
+
metadata: result.metadata
|
|
197
|
+
}))
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
logger_1.default.error('Search knowledge items error:', error);
|
|
202
|
+
res.status(500).json({
|
|
203
|
+
error: 'Failed to search knowledge items',
|
|
204
|
+
message: error instanceof Error ? error.message : 'Unknown error'
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
exports.KnowledgeController = KnowledgeController;
|
|
210
|
+
//# sourceMappingURL=KnowledgeController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KnowledgeController.js","sourceRoot":"","sources":["../../../src/controllers/KnowledgeController.ts"],"names":[],"mappings":";;;;;;AAEA,8DAAsC;AAEtC,MAAa,mBAAmB;IAG9B,YAAY,aAAmC;QAC7C,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACrC,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,GAAY,EAAE,GAAa;QAC1C,IAAI,CAAC;YACH,MAAM,EACJ,WAAW,EACX,YAAY,EACZ,KAAK,EACL,WAAW,EACX,IAAI,EACJ,OAAO,EACP,QAAQ,EACR,SAAS,EACT,SAAS,EACT,QAAQ,EACR,eAAe,EAChB,GAAG,GAAG,CAAC,IAAI,CAAC;YAEb,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;gBACxC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,+CAA+C,EAAE,CAAC,CAAC;gBACjF,OAAO;YACT,CAAC;YAED,gBAAgB;YAChB,MAAM,UAAU,GAAG,CAAC,QAAQ,EAAE,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,eAAe,CAAC,CAAC;YAC1E,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC/B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,wBAAwB,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;gBACjF,OAAO;YACT,CAAC;YAED,yCAAyC;YACzC,IAAI,CAAC,IAAI,KAAK,UAAU,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;gBAC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AAmB,CAAC;gBAC1C,IAAI,EAAE,IAAyC;gBAC/C,UAAU,EAAE,YAAY,CAAC,CAAC,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,SAAS;aAC9D,CAAC,CAAC;YAEH,GAAG,CAAC,IAAI,CAAC;gBACP,KAAK;gBACL,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;oBAC9B,IAAI,EAAE,MAAM,CAAC,EAAE;oBACf,KAAK,EAAE,MAAM,CAAC,KAAK;oBACnB,WAAW,EAAE,MAAM,CAAC,WAAW;oBAC/B,OAAO,EAAE,MAAM,CAAC,OAAO;oBACvB,IAAI,EAAE,MAAM,CAAC,IAAI;oBACjB,KAAK,EAAE,MAAM,CAAC,KAAK;oBACnB,QAAQ,EAAE,MAAM,CAAC,QAAQ;iBAC1B,CAAC,CAAC;aACJ,CAAC,CAAC;QAEL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,gBAAM,CAAC,KAAK,CAAC,+BAA+B,EAAE,KAAK,CAAC,CAAC;YACrD,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;gBACnB,KAAK,EAAE,kCAAkC;gBACzC,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe;aAClE,CAAC,CAAC;QACL,CAAC;IACH,CAAC;CACF;AA7PD,kDA6PC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
export interface ApiError extends Error {
|
|
3
|
+
statusCode?: number;
|
|
4
|
+
code?: string;
|
|
5
|
+
details?: any;
|
|
6
|
+
}
|
|
7
|
+
export declare class ValidationError extends Error {
|
|
8
|
+
statusCode: number;
|
|
9
|
+
code: string;
|
|
10
|
+
details: any;
|
|
11
|
+
constructor(message: string, details?: any);
|
|
12
|
+
}
|
|
13
|
+
export declare class NotFoundError extends Error {
|
|
14
|
+
statusCode: number;
|
|
15
|
+
code: string;
|
|
16
|
+
constructor(resource?: string);
|
|
17
|
+
}
|
|
18
|
+
export declare class UnauthorizedError extends Error {
|
|
19
|
+
statusCode: number;
|
|
20
|
+
code: string;
|
|
21
|
+
constructor(message?: string);
|
|
22
|
+
}
|
|
23
|
+
export declare class ForbiddenError extends Error {
|
|
24
|
+
statusCode: number;
|
|
25
|
+
code: string;
|
|
26
|
+
constructor(message?: string);
|
|
27
|
+
}
|
|
28
|
+
export declare class ConflictError extends Error {
|
|
29
|
+
statusCode: number;
|
|
30
|
+
code: string;
|
|
31
|
+
constructor(message?: string);
|
|
32
|
+
}
|
|
33
|
+
export declare class RateLimitError extends Error {
|
|
34
|
+
statusCode: number;
|
|
35
|
+
code: string;
|
|
36
|
+
constructor(message?: string);
|
|
37
|
+
}
|
|
38
|
+
export declare class InternalServerError extends Error {
|
|
39
|
+
statusCode: number;
|
|
40
|
+
code: string;
|
|
41
|
+
constructor(message?: string);
|
|
42
|
+
}
|
|
43
|
+
export declare const errorHandler: (err: ApiError, req: Request, res: Response, next: NextFunction) => void;
|
|
44
|
+
export declare const notFoundHandler: (req: Request, res: Response) => void;
|
|
45
|
+
export declare const asyncHandler: (fn: Function) => (req: Request, res: Response, next: NextFunction) => void;
|
|
46
|
+
export declare const validateRequired: (body: any, requiredFields: string[]) => void;
|
|
47
|
+
export declare const validateTypes: (body: any, fieldTypes: Record<string, string>) => void;
|
|
48
|
+
declare const _default: {
|
|
49
|
+
errorHandler: (err: ApiError, req: Request, res: Response, next: NextFunction) => void;
|
|
50
|
+
notFoundHandler: (req: Request, res: Response) => void;
|
|
51
|
+
asyncHandler: (fn: Function) => (req: Request, res: Response, next: NextFunction) => void;
|
|
52
|
+
validateRequired: (body: any, requiredFields: string[]) => void;
|
|
53
|
+
validateTypes: (body: any, fieldTypes: Record<string, string>) => void;
|
|
54
|
+
ValidationError: typeof ValidationError;
|
|
55
|
+
NotFoundError: typeof NotFoundError;
|
|
56
|
+
UnauthorizedError: typeof UnauthorizedError;
|
|
57
|
+
ForbiddenError: typeof ForbiddenError;
|
|
58
|
+
ConflictError: typeof ConflictError;
|
|
59
|
+
RateLimitError: typeof RateLimitError;
|
|
60
|
+
InternalServerError: typeof InternalServerError;
|
|
61
|
+
};
|
|
62
|
+
export default _default;
|
|
63
|
+
//# sourceMappingURL=errorHandler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorHandler.d.ts","sourceRoot":"","sources":["../../../src/middleware/errorHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAG1D,MAAM,WAAW,QAAS,SAAQ,KAAK;IACrC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,GAAG,CAAC;CACf;AAED,qBAAa,eAAgB,SAAQ,KAAK;IACjC,UAAU,SAAO;IACjB,IAAI,SAAsB;IAC1B,OAAO,EAAE,GAAG,CAAC;gBAER,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG;CAK3C;AAED,qBAAa,aAAc,SAAQ,KAAK;IAC/B,UAAU,SAAO;IACjB,IAAI,SAAe;gBAEd,QAAQ,GAAE,MAAmB;CAI1C;AAED,qBAAa,iBAAkB,SAAQ,KAAK;IACnC,UAAU,SAAO;IACjB,IAAI,SAAkB;gBAEjB,OAAO,GAAE,MAA8B;CAIpD;AAED,qBAAa,cAAe,SAAQ,KAAK;IAChC,UAAU,SAAO;IACjB,IAAI,SAAe;gBAEd,OAAO,GAAE,MAA2B;CAIjD;AAED,qBAAa,aAAc,SAAQ,KAAK;IAC/B,UAAU,SAAO;IACjB,IAAI,SAAc;gBAEb,OAAO,GAAE,MAA4B;CAIlD;AAED,qBAAa,cAAe,SAAQ,KAAK;IAChC,UAAU,SAAO;IACjB,IAAI,SAAyB;gBAExB,OAAO,GAAE,MAA8B;CAIpD;AAED,qBAAa,mBAAoB,SAAQ,KAAK;IACrC,UAAU,SAAO;IACjB,IAAI,SAA2B;gBAE1B,OAAO,GAAE,MAAgC;CAItD;AAgBD,eAAO,MAAM,YAAY,GACvB,KAAK,QAAQ,EACb,KAAK,OAAO,EACZ,KAAK,QAAQ,EACb,MAAM,YAAY,KACjB,IAwFF,CAAC;AAGF,eAAO,MAAM,eAAe,GAAI,KAAK,OAAO,EAAE,KAAK,QAAQ,KAAG,IAa7D,CAAC;AAGF,eAAO,MAAM,YAAY,GAAI,IAAI,QAAQ,MAAM,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,SAE7F,CAAC;AAGF,eAAO,MAAM,gBAAgB,GAAI,MAAM,GAAG,EAAE,gBAAgB,MAAM,EAAE,KAAG,IAYtE,CAAC;AAGF,eAAO,MAAM,aAAa,GAAI,MAAM,GAAG,EAAE,YAAY,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAG,IAmB7E,CAAC;;wBAtJK,QAAQ,OACR,OAAO,OACP,QAAQ,QACP,YAAY,KACjB,IAAI;2BA2F8B,OAAO,OAAO,QAAQ,KAAG,IAAI;uBAgBjC,QAAQ,MAAM,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY;6BAKvD,GAAG,kBAAkB,MAAM,EAAE,KAAG,IAAI;0BAevC,GAAG,cAAc,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAG,IAAI;;;;;;;;;AAqBlF,wBAaE"}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateTypes = exports.validateRequired = exports.asyncHandler = exports.notFoundHandler = exports.errorHandler = exports.InternalServerError = exports.RateLimitError = exports.ConflictError = exports.ForbiddenError = exports.UnauthorizedError = exports.NotFoundError = exports.ValidationError = void 0;
|
|
4
|
+
const global_1 = require("../config/global");
|
|
5
|
+
class ValidationError extends Error {
|
|
6
|
+
constructor(message, details) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.statusCode = 400;
|
|
9
|
+
this.code = 'VALIDATION_ERROR';
|
|
10
|
+
this.name = 'ValidationError';
|
|
11
|
+
this.details = details;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.ValidationError = ValidationError;
|
|
15
|
+
class NotFoundError extends Error {
|
|
16
|
+
constructor(resource = 'Resource') {
|
|
17
|
+
super(`${resource} not found`);
|
|
18
|
+
this.statusCode = 404;
|
|
19
|
+
this.code = 'NOT_FOUND';
|
|
20
|
+
this.name = 'NotFoundError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.NotFoundError = NotFoundError;
|
|
24
|
+
class UnauthorizedError extends Error {
|
|
25
|
+
constructor(message = 'Unauthorized access') {
|
|
26
|
+
super(message);
|
|
27
|
+
this.statusCode = 401;
|
|
28
|
+
this.code = 'UNAUTHORIZED';
|
|
29
|
+
this.name = 'UnauthorizedError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.UnauthorizedError = UnauthorizedError;
|
|
33
|
+
class ForbiddenError extends Error {
|
|
34
|
+
constructor(message = 'Access forbidden') {
|
|
35
|
+
super(message);
|
|
36
|
+
this.statusCode = 403;
|
|
37
|
+
this.code = 'FORBIDDEN';
|
|
38
|
+
this.name = 'ForbiddenError';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.ForbiddenError = ForbiddenError;
|
|
42
|
+
class ConflictError extends Error {
|
|
43
|
+
constructor(message = 'Resource conflict') {
|
|
44
|
+
super(message);
|
|
45
|
+
this.statusCode = 409;
|
|
46
|
+
this.code = 'CONFLICT';
|
|
47
|
+
this.name = 'ConflictError';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.ConflictError = ConflictError;
|
|
51
|
+
class RateLimitError extends Error {
|
|
52
|
+
constructor(message = 'Rate limit exceeded') {
|
|
53
|
+
super(message);
|
|
54
|
+
this.statusCode = 429;
|
|
55
|
+
this.code = 'RATE_LIMIT_EXCEEDED';
|
|
56
|
+
this.name = 'RateLimitError';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.RateLimitError = RateLimitError;
|
|
60
|
+
class InternalServerError extends Error {
|
|
61
|
+
constructor(message = 'Internal server error') {
|
|
62
|
+
super(message);
|
|
63
|
+
this.statusCode = 500;
|
|
64
|
+
this.code = 'INTERNAL_SERVER_ERROR';
|
|
65
|
+
this.name = 'InternalServerError';
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.InternalServerError = InternalServerError;
|
|
69
|
+
// Main error handler middleware
|
|
70
|
+
const errorHandler = (err, req, res, next) => {
|
|
71
|
+
// Default values
|
|
72
|
+
let statusCode = err.statusCode || 500;
|
|
73
|
+
let code = err.code || 'INTERNAL_SERVER_ERROR';
|
|
74
|
+
let message = err.message || 'An unexpected error occurred';
|
|
75
|
+
let details = err.details;
|
|
76
|
+
// Handle specific error types
|
|
77
|
+
if (err.name === 'ValidationError' || err.message.includes('validation')) {
|
|
78
|
+
statusCode = 400;
|
|
79
|
+
code = 'VALIDATION_ERROR';
|
|
80
|
+
}
|
|
81
|
+
else if (err.message.includes('not found') || err.message.includes('does not exist')) {
|
|
82
|
+
statusCode = 404;
|
|
83
|
+
code = 'NOT_FOUND';
|
|
84
|
+
}
|
|
85
|
+
else if (err.message.includes('unauthorized') || err.message.includes('authentication')) {
|
|
86
|
+
statusCode = 401;
|
|
87
|
+
code = 'UNAUTHORIZED';
|
|
88
|
+
}
|
|
89
|
+
else if (err.message.includes('forbidden') || err.message.includes('permission')) {
|
|
90
|
+
statusCode = 403;
|
|
91
|
+
code = 'FORBIDDEN';
|
|
92
|
+
}
|
|
93
|
+
else if (err.message.includes('conflict') || err.message.includes('already exists')) {
|
|
94
|
+
statusCode = 409;
|
|
95
|
+
code = 'CONFLICT';
|
|
96
|
+
}
|
|
97
|
+
else if (err.message.includes('rate limit')) {
|
|
98
|
+
statusCode = 429;
|
|
99
|
+
code = 'RATE_LIMIT_EXCEEDED';
|
|
100
|
+
}
|
|
101
|
+
// Handle JSON parsing errors
|
|
102
|
+
if (err instanceof SyntaxError && 'body' in err) {
|
|
103
|
+
statusCode = 400;
|
|
104
|
+
code = 'INVALID_JSON';
|
|
105
|
+
message = 'Invalid JSON in request body';
|
|
106
|
+
}
|
|
107
|
+
// Handle database constraint errors
|
|
108
|
+
if (err.message.includes('duplicate key') || err.message.includes('unique constraint')) {
|
|
109
|
+
statusCode = 409;
|
|
110
|
+
code = 'DUPLICATE_RESOURCE';
|
|
111
|
+
message = 'Resource already exists';
|
|
112
|
+
}
|
|
113
|
+
// Handle foreign key constraint errors
|
|
114
|
+
if (err.message.includes('foreign key constraint') || err.message.includes('violates foreign key')) {
|
|
115
|
+
statusCode = 400;
|
|
116
|
+
code = 'INVALID_REFERENCE';
|
|
117
|
+
message = 'Referenced resource does not exist';
|
|
118
|
+
}
|
|
119
|
+
// Prepare error response
|
|
120
|
+
const errorResponse = {
|
|
121
|
+
error: {
|
|
122
|
+
code,
|
|
123
|
+
message,
|
|
124
|
+
timestamp: new Date().toISOString(),
|
|
125
|
+
path: req.path,
|
|
126
|
+
method: req.method
|
|
127
|
+
},
|
|
128
|
+
success: false
|
|
129
|
+
};
|
|
130
|
+
// Add details in development/detailed error mode
|
|
131
|
+
if (global_1.config.validation.returnDetailedErrors) {
|
|
132
|
+
errorResponse.error.details = details || {
|
|
133
|
+
stack: err.stack,
|
|
134
|
+
originalError: err.message
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
// Log error (but not for client errors like 400, 401, 403, 404)
|
|
138
|
+
if (statusCode >= 500 && global_1.config.logging.enableErrorLogging) {
|
|
139
|
+
console.error(`🚨 Error ${statusCode} - ${code}:`, {
|
|
140
|
+
message: err.message,
|
|
141
|
+
stack: err.stack,
|
|
142
|
+
path: req.path,
|
|
143
|
+
method: req.method,
|
|
144
|
+
body: req.body,
|
|
145
|
+
query: req.query,
|
|
146
|
+
params: req.params,
|
|
147
|
+
headers: req.headers,
|
|
148
|
+
timestamp: new Date().toISOString()
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
else if (global_1.config.logging.level === 'debug') {
|
|
152
|
+
console.log(`⚠️ Client Error ${statusCode} - ${code}: ${message}`);
|
|
153
|
+
}
|
|
154
|
+
// Send error response
|
|
155
|
+
res.status(statusCode).json(errorResponse);
|
|
156
|
+
};
|
|
157
|
+
exports.errorHandler = errorHandler;
|
|
158
|
+
// 404 handler for undefined routes
|
|
159
|
+
const notFoundHandler = (req, res) => {
|
|
160
|
+
const errorResponse = {
|
|
161
|
+
error: {
|
|
162
|
+
code: 'ENDPOINT_NOT_FOUND',
|
|
163
|
+
message: `Endpoint ${req.method} ${req.path} not found`,
|
|
164
|
+
timestamp: new Date().toISOString(),
|
|
165
|
+
path: req.path,
|
|
166
|
+
method: req.method
|
|
167
|
+
},
|
|
168
|
+
success: false
|
|
169
|
+
};
|
|
170
|
+
res.status(404).json(errorResponse);
|
|
171
|
+
};
|
|
172
|
+
exports.notFoundHandler = notFoundHandler;
|
|
173
|
+
// Async error wrapper
|
|
174
|
+
const asyncHandler = (fn) => (req, res, next) => {
|
|
175
|
+
Promise.resolve(fn(req, res, next)).catch(next);
|
|
176
|
+
};
|
|
177
|
+
exports.asyncHandler = asyncHandler;
|
|
178
|
+
// Request validation helper
|
|
179
|
+
const validateRequired = (body, requiredFields) => {
|
|
180
|
+
const missing = requiredFields.filter(field => {
|
|
181
|
+
const value = body[field];
|
|
182
|
+
return value === undefined || value === null || value === '';
|
|
183
|
+
});
|
|
184
|
+
if (missing.length > 0) {
|
|
185
|
+
throw new ValidationError(`Missing required fields: ${missing.join(', ')}`, {
|
|
186
|
+
missingFields: missing,
|
|
187
|
+
providedFields: Object.keys(body)
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
exports.validateRequired = validateRequired;
|
|
192
|
+
// Type validation helper
|
|
193
|
+
const validateTypes = (body, fieldTypes) => {
|
|
194
|
+
const errors = [];
|
|
195
|
+
Object.entries(fieldTypes).forEach(([field, expectedType]) => {
|
|
196
|
+
const value = body[field];
|
|
197
|
+
if (value !== undefined && value !== null) {
|
|
198
|
+
const actualType = Array.isArray(value) ? 'array' : typeof value;
|
|
199
|
+
if (actualType !== expectedType) {
|
|
200
|
+
errors.push(`Field '${field}' must be of type ${expectedType}, got ${actualType}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
if (errors.length > 0) {
|
|
205
|
+
throw new ValidationError('Type validation failed', {
|
|
206
|
+
errors,
|
|
207
|
+
received: body
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
exports.validateTypes = validateTypes;
|
|
212
|
+
exports.default = {
|
|
213
|
+
errorHandler: exports.errorHandler,
|
|
214
|
+
notFoundHandler: exports.notFoundHandler,
|
|
215
|
+
asyncHandler: exports.asyncHandler,
|
|
216
|
+
validateRequired: exports.validateRequired,
|
|
217
|
+
validateTypes: exports.validateTypes,
|
|
218
|
+
ValidationError,
|
|
219
|
+
NotFoundError,
|
|
220
|
+
UnauthorizedError,
|
|
221
|
+
ForbiddenError,
|
|
222
|
+
ConflictError,
|
|
223
|
+
RateLimitError,
|
|
224
|
+
InternalServerError
|
|
225
|
+
};
|
|
226
|
+
//# sourceMappingURL=errorHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorHandler.js","sourceRoot":"","sources":["../../../src/middleware/errorHandler.ts"],"names":[],"mappings":";;;AACA,6CAA0C;AAQ1C,MAAa,eAAgB,SAAQ,KAAK;IAKxC,YAAY,OAAe,EAAE,OAAa;QACxC,KAAK,CAAC,OAAO,CAAC,CAAC;QALV,eAAU,GAAG,GAAG,CAAC;QACjB,SAAI,GAAG,kBAAkB,CAAC;QAK/B,IAAI,CAAC,IAAI,GAAG,iB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