import _ from 'lodash'; import { getService } from '../utils/index.mjs'; import { validateUpdateContentTypeInput, validateContentTypeInput, validateKind } from './validation/content-type.mjs'; import { isContentTypeVisible } from '../services/content-types.mjs'; /** * Telemetry uses a bounded HTTP timeout (~1s in the metrics sender); cap how long we defer * dev reload so a stuck client cannot block restart indefinitely. */ const RELOAD_AFTER_TELEMETRY_MAX_MS = 2500; const scheduleReloadAfterOutboundTelemetry = (sendPromise)=>{ setImmediate(()=>{ const settled = sendPromise.catch(()=>{}); Promise.race([ settled, new Promise((resolve)=>{ setTimeout(resolve, RELOAD_AFTER_TELEMETRY_MAX_MS); }) ]).catch(()=>{}).finally(()=>{ strapi.reload(); }); }); }; var contentTypes = { async getContentTypes (ctx) { const { kind } = ctx.query; try { await validateKind(kind); } catch (error) { return ctx.send({ error }, 400); } const contentTypeService = getService('content-types'); const contentTypes = Object.keys(strapi.contentTypes).filter((uid)=>!kind || _.get(strapi.contentTypes[uid], 'kind', 'collectionType') === kind).map((uid)=>contentTypeService.formatContentType(strapi.contentTypes[uid])); ctx.send({ data: contentTypes }); }, getContentType (ctx) { const { uid } = ctx.params; const contentType = strapi.contentTypes[uid]; if (!contentType) { return ctx.send({ error: 'contentType.notFound' }, 404); } if (!isContentTypeVisible(contentType)) { return ctx.send({ error: 'contentType.notFound' }, 404); } const contentTypeService = getService('content-types'); ctx.send({ data: contentTypeService.formatContentType(contentType) }); }, async createContentType (ctx) { const body = ctx.request.body; try { await validateContentTypeInput(body); } catch (error) { return ctx.send({ error }, 400); } try { strapi.reload.isWatching = false; const contentTypeService = getService('content-types'); const contentType = await contentTypeService.createContentType({ contentType: body.contentType, components: body.components }); const metricsPayload = { eventProperties: { kind: contentType.kind } }; const telemetryPromise = _.isEmpty(strapi.apis) ? strapi.telemetry.send('didCreateFirstContentType', metricsPayload) : strapi.telemetry.send('didCreateContentType', metricsPayload); scheduleReloadAfterOutboundTelemetry(telemetryPromise); ctx.send({ data: { uid: contentType.uid } }, 201); } catch (err) { strapi.log.error(err); strapi.telemetry.send('didNotCreateContentType', { eventProperties: { error: err.message || err } }).catch(()=>{}); ctx.send({ error: err.message || 'Unknown error' }, 400); } }, async updateContentType (ctx) { const { uid } = ctx.params; const body = ctx.request.body; if (!_.has(strapi.contentTypes, uid)) { return ctx.send({ error: 'contentType.notFound' }, 404); } try { await validateUpdateContentTypeInput(body); } catch (error) { return ctx.send({ error }, 400); } try { strapi.reload.isWatching = false; const contentTypeService = getService('content-types'); const component = await contentTypeService.editContentType(uid, { contentType: body.contentType, components: body.components }); setImmediate(()=>strapi.reload()); ctx.send({ data: { uid: component.uid } }, 201); } catch (error) { strapi.log.error(error); ctx.send({ error: error?.message || 'Unknown error' }, 400); } }, async deleteContentType (ctx) { const { uid } = ctx.params; if (!_.has(strapi.contentTypes, uid)) { return ctx.send({ error: 'contentType.notFound' }, 404); } try { strapi.reload.isWatching = false; const contentTypeService = getService('content-types'); const component = await contentTypeService.deleteContentType(uid); setImmediate(()=>strapi.reload()); ctx.send({ data: { uid: component.uid } }); } catch (error) { strapi.log.error(error); ctx.send({ error: error?.message || 'Unknown error' }, 400); } } }; export { contentTypes as default }; //# sourceMappingURL=content-types.mjs.map