feat: Обновление функции createTextDataPOST для поддержки нескольких типов входных данных (domains, ipRanges, items) с улучшенной валидацией и обработкой ошибок. Это улучшает обратную совместимость и гибкость API.
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 1m35s
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 1m35s
This commit is contained in:
@@ -100,22 +100,31 @@ function createTextDataGET(s3Key, mapLine, validate, cachePrefix) {
|
|||||||
*/
|
*/
|
||||||
function createTextDataPOST(s3Key, formatLine, validate, validateItem = null) {
|
function createTextDataPOST(s3Key, formatLine, validate, validateItem = null) {
|
||||||
return async (req, res) => {
|
return async (req, res) => {
|
||||||
const { domains, etag } = req.body; // Используем 'domains' для обратной совместимости
|
const { domains, ipRanges, items, etag } = req.body; // 'domains' основной ключ, но поддерживаем и альтернативные
|
||||||
|
|
||||||
if (!Array.isArray(domains)) {
|
// Поддержка обратной совместимости: принимаем domains | ipRanges | items
|
||||||
|
const payload = Array.isArray(domains)
|
||||||
|
? domains
|
||||||
|
: Array.isArray(ipRanges)
|
||||||
|
? ipRanges
|
||||||
|
: Array.isArray(items)
|
||||||
|
? items
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (!Array.isArray(payload)) {
|
||||||
return sendError(res, 400, 'Data must be an array', 'E_BAD_REQUEST');
|
return sendError(res, 400, 'Data must be an array', 'E_BAD_REQUEST');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Валидация с помощью AJV
|
// Валидация с помощью AJV
|
||||||
if (!validate(domains || [])) {
|
if (!validate(payload || [])) {
|
||||||
return sendError(res, 400, 'Invalid payload format', 'E_SCHEMA');
|
return sendError(res, 400, 'Invalid payload format', 'E_SCHEMA');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Дополнительная валидация элементов, если предоставлена
|
// Дополнительная валидация элементов, если предоставлена
|
||||||
if (validateItem) {
|
if (validateItem) {
|
||||||
const validationErrors = [];
|
const validationErrors = [];
|
||||||
for (let i = 0; i < domains.length; i++) {
|
for (let i = 0; i < payload.length; i++) {
|
||||||
const errors = validateItem(domains[i], i);
|
const errors = validateItem(payload[i], i);
|
||||||
if (errors.length > 0) {
|
if (errors.length > 0) {
|
||||||
validationErrors.push(...errors);
|
validationErrors.push(...errors);
|
||||||
}
|
}
|
||||||
@@ -128,7 +137,7 @@ function createTextDataPOST(s3Key, formatLine, validate, validateItem = null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileContent = (domains || []).map(formatLine).filter(Boolean).join('\n');
|
const fileContent = (payload || []).map(formatLine).filter(Boolean).join('\n');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Проверка ETag для optimistic concurrency
|
// Проверка ETag для optimistic concurrency
|
||||||
|
|||||||
Reference in New Issue
Block a user