Add content-type kind property

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-03-27 12:14:12 +01:00
parent a8f909f4e3
commit 86312ecbaf
7 changed files with 76 additions and 67 deletions

View File

@ -66,20 +66,20 @@ module.exports = {
const contentTypeService = strapi.plugins['content-type-builder'].services.contenttypes; const contentTypeService = strapi.plugins['content-type-builder'].services.contenttypes;
const component = await contentTypeService.createContentType({ const contentType = await contentTypeService.createContentType({
contentType: body.contentType, contentType: body.contentType,
components: body.components, components: body.components,
}); });
if (_.isEmpty(strapi.api)) { if (_.isEmpty(strapi.api)) {
await strapi.telemetry.send('didCreateFirstContentType'); await strapi.telemetry.send('didCreateFirstContentType', { kind: contentType.kind });
} else { } else {
await strapi.telemetry.send('didCreateContentType'); await strapi.telemetry.send('didCreateContentType', { kind: contentType.kind });
} }
setImmediate(() => strapi.reload()); setImmediate(() => strapi.reload());
ctx.send({ data: { uid: component.uid } }, 201); ctx.send({ data: { uid: contentType.uid } }, 201);
} catch (error) { } catch (error) {
strapi.log.error(error); strapi.log.error(error);
await strapi.telemetry.send('didNotCreateContentType', { error: error.message }); await strapi.telemetry.send('didNotCreateContentType', { error: error.message });

View File

@ -41,7 +41,7 @@ const createComponent = async ({ component, components = [] }) => {
const uidMap = builder.createNewComponentUIDMap(components); const uidMap = builder.createNewComponentUIDMap(components);
const replaceTmpUIDs = replaceTemporaryUIDs(uidMap); const replaceTmpUIDs = replaceTemporaryUIDs(uidMap);
const newComponent = builder.createComponent(replaceTmpUIDs(component)); const newComponent = await builder.createComponent(replaceTmpUIDs(component));
components.forEach(component => { components.forEach(component => {
if (!_.has(component, 'uid')) { if (!_.has(component, 'uid')) {

View File

@ -30,7 +30,7 @@ module.exports = function createComponentBuilder() {
/** /**
* create a component in the tmpComponent map * create a component in the tmpComponent map
*/ */
async createComponent(infos) { createComponent(infos) {
const uid = this.createComponentUID(infos); const uid = this.createComponentUID(infos);
if (this.components.has(uid)) { if (this.components.has(uid)) {

View File

@ -43,6 +43,10 @@ module.exports = function createSchemaHandler(infos) {
return initialState.category; return initialState.category;
}, },
get kind() {
return _.get(state.schema, 'kind', 'collectionType');
},
get uid() { get uid() {
return state.uid; return state.uid;
}, },
@ -162,9 +166,7 @@ module.exports = function createSchemaHandler(infos) {
Array.isArray(attr.components) && Array.isArray(attr.components) &&
attr.components.includes(uid) attr.components.includes(uid)
) { ) {
const updatedComponentList = attributes[key].components.filter( const updatedComponentList = attributes[key].components.filter(val => val !== uid);
val => val !== uid
);
this.set(['attributes', key, 'components'], updatedComponentList); this.set(['attributes', key, 'components'], updatedComponentList);
} }
}); });
@ -187,9 +189,7 @@ module.exports = function createSchemaHandler(infos) {
Array.isArray(attr.components) && Array.isArray(attr.components) &&
attr.components.includes(uid) attr.components.includes(uid)
) { ) {
const updatedComponentList = attr.components.map(val => const updatedComponentList = attr.components.map(val => (val === uid ? newUID : val));
val === uid ? newUID : val
);
this.set(['attributes', key, 'components'], updatedComponentList); this.set(['attributes', key, 'components'], updatedComponentList);
} }

View File

@ -11,18 +11,8 @@ const fetch = require('node-fetch');
const ciEnv = require('ci-info'); const ciEnv = require('ci-info');
const { scheduleJob } = require('node-schedule'); const { scheduleJob } = require('node-schedule');
const isTruthyEnvVar = val => { const createMiddleware = require('./middleware');
if (val === null || val === undefined) return false; const isTruthyEnvVar = require('./truthy-var');
if (val === true) return true;
if (val.toString().toLowerCase() === 'true') return true;
if (val.toString().toLowerCase() === 'false') return false;
if (val === 1) return true;
return false;
};
const createTelemetryInstance = strapi => { const createTelemetryInstance = strapi => {
const uuid = strapi.config.uuid; const uuid = strapi.config.uuid;
@ -68,52 +58,18 @@ const createTelemetryInstance = strapi => {
} }
}; };
const _state = { const initPing = () => {
currentDay: null, if (isDisabled) {
counter: 0, return;
}
scheduleJob('0 0 12 * * *', () => sendEvent('ping'));
}; };
return { return {
initPing() { initPing,
if (isDisabled) { send: sendEvent,
return; middleware: createMiddleware({ sendEvent, isDisabled }),
}
scheduleJob('0 0 12 * * *', () => sendEvent('ping'));
},
middleware: async (ctx, next) => {
if (isDisabled) {
return next();
}
const { url, method } = ctx.request;
if (!url.includes('.') && ['GET', 'PUT', 'POST', 'DELETE'].includes(method)) {
const dayOfMonth = new Date().getDate();
if (dayOfMonth !== _state.currentDay) {
_state.currentDay = dayOfMonth;
_state.counter = 0;
}
// Send max. 1000 events per day.
if (_state.counter < 1000) {
await sendEvent('didReceiveRequest', { url: ctx.request.url });
// Increase counter.
_state.counter++;
}
}
await next();
},
async send(event, properties) {
if (isDisabled) {
return true;
}
await sendEvent(event, properties);
},
}; };
}; };

View File

@ -0,0 +1,37 @@
'use strict';
const createMiddleware = ({ sendEvent, isDisabled }) => {
const _state = {
currentDay: null,
counter: 0,
};
return async (ctx, next) => {
if (isDisabled) {
return next();
}
const { url, method } = ctx.request;
if (!url.includes('.') && ['GET', 'PUT', 'POST', 'DELETE'].includes(method)) {
const dayOfMonth = new Date().getDate();
if (dayOfMonth !== _state.currentDay) {
_state.currentDay = dayOfMonth;
_state.counter = 0;
}
// Send max. 1000 events per day.
if (_state.counter < 1000) {
await sendEvent('didReceiveRequest', { url: ctx.request.url });
// Increase counter.
_state.counter++;
}
}
await next();
};
};
module.exports = createMiddleware;

View File

@ -0,0 +1,16 @@
'use strict';
const isTruthyEnvVar = val => {
if (val === null || val === undefined) return false;
if (val === true) return true;
if (val.toString().toLowerCase() === 'true') return true;
if (val.toString().toLowerCase() === 'false') return false;
if (val === 1) return true;
return false;
};
module.exports = isTruthyEnvVar;