UNPKG

@posthog/wizard

Version:

The PostHog wizard helps you to configure your project

102 lines 3.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiProjectSchema = exports.ApiUserSchema = void 0; exports.fetchUserData = fetchUserData; exports.fetchProjectData = fetchProjectData; const axios_1 = __importDefault(require("axios")); const zod_1 = require("zod"); const analytics_1 = require("../utils/analytics"); exports.ApiUserSchema = zod_1.z.object({ distinct_id: zod_1.z.string(), organizations: zod_1.z.array(zod_1.z.object({ id: zod_1.z.string().uuid(), })), team: zod_1.z.object({ id: zod_1.z.number(), organization: zod_1.z.string().uuid(), }), organization: zod_1.z.object({ id: zod_1.z.string().uuid(), }), }); exports.ApiProjectSchema = zod_1.z.object({ id: zod_1.z.number(), uuid: zod_1.z.string().uuid(), organization: zod_1.z.string().uuid(), api_token: zod_1.z.string(), name: zod_1.z.string(), }); class ApiError extends Error { statusCode; endpoint; constructor(message, statusCode, endpoint) { super(message); this.statusCode = statusCode; this.endpoint = endpoint; this.name = 'ApiError'; } } async function fetchUserData(accessToken, baseUrl) { try { const response = await axios_1.default.get(`${baseUrl}/api/users/@me/`, { headers: { Authorization: `Bearer ${accessToken}`, }, }); return exports.ApiUserSchema.parse(response.data); } catch (error) { const apiError = handleApiError(error, 'fetch user data'); analytics_1.analytics.captureException(apiError, { endpoint: '/api/users/@me/', baseUrl, }); throw apiError; } } async function fetchProjectData(accessToken, projectId, baseUrl) { try { const response = await axios_1.default.get(`${baseUrl}/api/projects/${projectId}/`, { headers: { Authorization: `Bearer ${accessToken}`, }, }); return exports.ApiProjectSchema.parse(response.data); } catch (error) { const apiError = handleApiError(error, 'fetch project data'); analytics_1.analytics.captureException(apiError, { endpoint: `/api/projects/${projectId}/`, baseUrl, projectId, }); throw apiError; } } function handleApiError(error, operation) { if (axios_1.default.isAxiosError(error)) { const axiosError = error; const status = axiosError.response?.status; const detail = axiosError.response?.data?.detail; const endpoint = axiosError.config?.url; if (status === 401) { return new ApiError(`Authentication failed while trying to ${operation}`, status, endpoint); } if (status === 403) { return new ApiError(`Access denied while trying to ${operation}`, status, endpoint); } if (status === 404) { return new ApiError(`Resource not found while trying to ${operation}`, status, endpoint); } const message = detail || `Failed to ${operation}`; return new ApiError(message, status, endpoint); } if (error instanceof zod_1.z.ZodError) { return new ApiError(`Invalid response format while trying to ${operation}`); } return new ApiError(`Unexpected error while trying to ${operation}: ${error instanceof Error ? error.message : 'Unknown error'}`); } //# sourceMappingURL=api.js.map