@shopify/cli-kit
Version:
A set of utilities, interfaces, and models that are common across all the platform features
69 lines • 2.82 kB
JavaScript
import { fetch } from './http.js';
import { outputDebug, outputContent, outputToken } from '../../public/node/output.js';
const url = 'https://monorail-edge.shopifysvc.com/v1/produce';
// This is the topic name of the main event we log to Monorail, the command tracker
export const MONORAIL_COMMAND_TOPIC = 'app_cli3_command/1.20';
const publishedCommandNames = new Set();
/**
* Publishes an event to Monorail.
*
* @param schemaId - The schema ID of the event to publish.
* @param publicData - The public data to publish.
* @param sensitiveData - The sensitive data to publish.
* @returns A result indicating whether the event was successfully published.
*/
export async function publishMonorailEvent(schemaId, publicData, sensitiveData) {
// If a command has already been logged, never re-log it. This is to prevent duplication caused by unexpected errors.
const commandName = publicData.command;
if (commandName && typeof commandName === 'string') {
if (publishedCommandNames.has(commandName)) {
return { type: 'ok' };
}
publishedCommandNames.add(commandName);
}
try {
const currentTime = new Date().getTime();
const payload = { ...publicData, ...sensitiveData };
const body = JSON.stringify({ schema_id: schemaId, payload });
const headers = buildHeaders(currentTime);
const response = await fetch(url, { method: 'POST', body, headers }, 'non-blocking');
if (response.status === 200) {
outputDebug(outputContent `Analytics event sent: ${outputToken.json(sanitizePayload(payload))}`);
return { type: 'ok' };
}
else {
outputDebug(`Failed to report usage analytics: ${response.statusText}`);
return { type: 'error', message: response.statusText };
}
// eslint-disable-next-line no-catch-all/no-catch-all
}
catch (error) {
let message = 'Failed to report usage analytics';
if (error instanceof Error) {
message = message.concat(`: ${error.message}`);
}
outputDebug(message);
return { type: 'error', message };
}
}
/**
* Sanitizies the api_key from the payload and returns a new hash.
*
* @param payload - The public and sensitive data.
* @returns A copy of the payload with the api_key sanitized.
*/
function sanitizePayload(payload) {
const result = { ...payload };
if ('api_key' in result) {
result.api_key = '****';
}
return result;
}
const buildHeaders = (currentTime) => {
return {
'Content-Type': 'application/json; charset=utf-8',
'X-Monorail-Edge-Event-Created-At-Ms': currentTime.toString(),
'X-Monorail-Edge-Event-Sent-At-Ms': currentTime.toString(),
};
};
//# sourceMappingURL=monorail.js.map