diff --git a/packages/@sanity/cli/src/actions/telemetry/setTelemetryConsent.ts b/packages/@sanity/cli/src/actions/telemetry/setTelemetryConsent.ts index 2fbb7ed3b72..e633bde58d7 100644 --- a/packages/@sanity/cli/src/actions/telemetry/setTelemetryConsent.ts +++ b/packages/@sanity/cli/src/actions/telemetry/setTelemetryConsent.ts @@ -1,12 +1,11 @@ import {type ConsentStatus} from '@sanity/telemetry' -import {ClientError, ServerError} from '@sanity/client' import {type CliCommandAction} from '../../types' import {debug} from '../../debug' import {getUserConfig} from '../../util/getUserConfig' import { ConsentInformation, - TELEMETRY_CONSENT_CONFIG_KEY, resolveConsent, + TELEMETRY_CONSENT_CONFIG_KEY, } from '../../util/createTelemetryStore' import { telemetryLearnMoreMessage, @@ -15,17 +14,6 @@ import { type SettableConsentStatus = Extract -const MOCK_MODES = ['success', 'failure:server', 'failure:msa'] as const -type MockMode = (typeof MOCK_MODES)[number] - -interface SetConsentResponse { - type: Type - status: ConsentStatus - updatedAt: string -} - -type Mock = () => Promise> - interface ResultMessage { success: () => string failure: (message?: string) => string @@ -56,66 +44,6 @@ const resultMessages: Record = { }, } -const mocks: Record = { - success: () => - Promise.resolve({ - type: 'telemetry', - status: 'granted', - updatedAt: '2023-08-08T13:40:30.801847Z', - }), - 'failure:server': () => { - throw new ServerError({ - statusCode: 500, - headers: {}, - body: {}, - }) - }, - 'failure:msa': () => { - throw new ClientError({ - statusCode: 403, - headers: {}, - body: { - message: 'User cannot give consent', - }, - }) - }, -} - -function isMockMode(mode?: string): mode is MockMode { - return MOCK_MODES.includes(mode as MockMode) -} - -function validateMockMode() { - // eslint-disable-next-line no-process-env - if (process.env.MOCK_TELEMETRY_CONSENT_MODE) { - // eslint-disable-next-line no-process-env - const mode = process.env.MOCK_TELEMETRY_CONSENT_MODE.toLowerCase() - - if (!isMockMode(mode)) { - const validModes = new Intl.ListFormat('en-US', { - style: 'long', - type: 'disjunction', - }).format(MOCK_MODES.map((name) => `"${name}"`)) - - throw new Error( - `Invalid value provided for environment variable MOCK_TELEMETRY_CONSENT_MODE. Must be either ${validModes}`, - ) - } - } -} - -// eslint-disable-next-line consistent-return -function getMock(): Mock | undefined { - validateMockMode() - - // eslint-disable-next-line no-process-env - const mode = process.env.MOCK_TELEMETRY_CONSENT_MODE?.toLowerCase() - - if (isMockMode(mode)) { - return mocks[mode] - } -} - export function createSetTelemetryConsentAction(status: SettableConsentStatus): CliCommandAction { return async function setTelemetryConsentAction(_, context) { const {apiClient, output} = context @@ -128,9 +56,6 @@ export function createSetTelemetryConsentAction(status: SettableConsentStatus): apiVersion: '2023-12-18', useProjectHostname: false, }) - - const mock = getMock() - // eslint-disable-next-line no-process-env const currentInformation = await resolveConsent({env: process.env}) const isChanged = currentInformation.status !== status @@ -144,19 +69,13 @@ export function createSetTelemetryConsentAction(status: SettableConsentStatus): if (isChanged) { debug('Setting telemetry consent to "%s"', status) try { - if (mock) { - debug('Mocking telemetry consent request') - await mock() - } else { - // TODO: Finalise API request. - const uri = `/users/me/consents/telemetry/status/${status}` - debug('Sending telemetry consent status to %s', uri) + const uri = `/users/me/consents/telemetry/status/${status}` + debug('Sending telemetry consent status to %s', uri) - await client.request({ - method: 'PUT', - uri, - }) - } + await client.request({ + method: 'PUT', + uri, + }) // Clear cached telemetry consent config.delete(TELEMETRY_CONSENT_CONFIG_KEY) @@ -164,8 +83,15 @@ export function createSetTelemetryConsentAction(status: SettableConsentStatus): output.print(`${telemetryStatusMessage(status, context)}\n`) output.print(resultMessages[status].success()) } catch (err) { - err.message = resultMessages[status].failure(err?.responseBody?.message) - throw err + const errorMessage = resultMessages[status].failure(err.response?.body?.message) + if (err.statusCode === 403) { + // throw without stack trace from original error + throw new Error(errorMessage) + } else { + // if not 403, throw original error + err.message = errorMessage + throw err + } } }