/
githubmirror
/
strapi
Обзор
Документация
Войти
/
githubmirror
/
strapi
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/plugins/i18n/server/src/controllers/validate-locale-creation.ts
59 строк
1 KB
Nico André
chore(lint): add non-blocking oxlint setup (#26923)
31 июл 2026, 16:56
Не верифицирован
31 июл 2026, 16:56
6469af3
Код
Авторство
О чём код?
import { get } from 'lodash/fp'; import { errors } from '@strapi/utils'; import type { Core, Struct } from '@strapi/types'; import { getService } from '../utils'; const { ApplicationError } = errors; // TODO: v5 if implemented in the CM => delete this middleware const validateLocaleCreation: Core.MiddlewareHandler = async (ctx, next) => { const { model } = ctx.params; const { query } = ctx.request; // Prevent empty body if (!ctx.request.body) { ctx.request.body = {}; } const body = ctx.request.body as any; const { getValidLocale, isLocalizedContentType } = getService('content-types'); const modelDef = strapi.getModel(model) as Struct.ContentTypeSchema; if (!isLocalizedContentType(modelDef)) { return next(); } // Prevent empty string locale const locale = get('locale', query) || get('locale', body) || undefined; // cleanup to avoid creating duplicates in single types ctx.request.query = {}; let entityLocale; try { entityLocale = await getValidLocale(locale); } catch { throw new ApplicationError("This locale doesn't exist"); } body.locale = entityLocale; if (modelDef.kind === 'singleType') { const entity = await strapi.entityService.findMany(modelDef.uid, { locale: entityLocale, } as any); // TODO: add this type to entityService ctx.request.query.locale = body.locale; // updating if (entity) { return next(); } } return next(); }; export default validateLocaleCreation;