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 component = await contentTypeService.createContentType({
const contentType = await contentTypeService.createContentType({
contentType: body.contentType,
components: body.components,
});
if (_.isEmpty(strapi.api)) {
await strapi.telemetry.send('didCreateFirstContentType');
await strapi.telemetry.send('didCreateFirstContentType', { kind: contentType.kind });
} else {
await strapi.telemetry.send('didCreateContentType');
await strapi.telemetry.send('didCreateContentType', { kind: contentType.kind });
}
setImmediate(() => strapi.reload());
ctx.send({ data: { uid: component.uid } }, 201);
ctx.send({ data: { uid: contentType.uid } }, 201);
} catch (error) {
strapi.log.error(error);
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 replaceTmpUIDs = replaceTemporaryUIDs(uidMap);
const newComponent = builder.createComponent(replaceTmpUIDs(component));
const newComponent = await builder.createComponent(replaceTmpUIDs(component));
components.forEach(component => {
if (!_.has(component, 'uid')) {

View File

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

View File

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

View File

@ -11,18 +11,8 @@ const fetch = require('node-fetch');
const ciEnv = require('ci-info');
const { scheduleJob } = require('node-schedule');
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;
};
const createMiddleware = require('./middleware');
const isTruthyEnvVar = require('./truthy-var');
const createTelemetryInstance = strapi => {
const uuid = strapi.config.uuid;
@ -68,52 +58,18 @@ const createTelemetryInstance = strapi => {
}
};
const _state = {
currentDay: null,
counter: 0,
};
return {
initPing() {
const initPing = () => {
if (isDisabled) {
return;
}
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);
},
return {
initPing,
send: sendEvent,
middleware: createMiddleware({ sendEvent, isDisabled }),
};
};

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;