UNPKG

mya-cli

Version:

MYA - AI-Powered Stock & Options Analysis CLI Tool

58 lines 2.73 kB
import * as stytch from 'stytch'; import { logger } from '../utils/logger.js'; import { inspect } from 'util'; /** * Returns a singleton Stytch client for the given environment. * Uses credentials from environment variables. */ let cachedClient = null; let cachedProjectId = ''; let cachedSecret = ''; export function getStytchClient(env) { if (!env.STYTCH_PROJECT_ID || !env.STYTCH_SECRET) { throw new Error('Missing STYTCH_PROJECT_ID or STYTCH_SECRET in the environment object.'); } try { // Only create a new client if credentials changed if (!cachedClient || cachedProjectId !== env.STYTCH_PROJECT_ID || cachedSecret !== env.STYTCH_SECRET) { logger.info('Initializing new Stytch client with:'); logger.info(` project_id: ${env.STYTCH_PROJECT_ID}`); logger.info(` secret: ${env.STYTCH_SECRET ? '[set]' : '[missing]'}`); // Log the first few characters of the secret to help with debugging // but avoid logging the entire secret for security reasons if (env.STYTCH_SECRET) { const secretPrefix = env.STYTCH_SECRET.substring(0, 8); logger.info(` secret prefix: ${secretPrefix}...`); } // Log if we're in test mode logger.info(` is test mode: ${env.IS_CLI || env.TEST_MODE ? 'true' : 'false'}`); // Log if the project_id starts with 'project-test-' to identify test credentials const isTestProject = env.STYTCH_PROJECT_ID.startsWith('project-test-'); logger.info(` using test project: ${isTestProject}`); // Initialize Stytch client without specifying the env parameter // The API will automatically use the correct environment based on the project ID cachedClient = new stytch.Client({ project_id: env.STYTCH_PROJECT_ID, secret: env.STYTCH_SECRET, }); cachedProjectId = env.STYTCH_PROJECT_ID; cachedSecret = env.STYTCH_SECRET; } return cachedClient || (() => { throw new Error('Failed to initialize Stytch client'); })(); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); logger.error('Error initializing Stytch client:', errorMessage); // Log more details about the error if available if (error instanceof Error) { logger.error('Error details:', inspect(error)); if ('error_type' in error) { logger.error('Stytch error type:', error.error_type); } } throw error; } } //# sourceMappingURL=stytch-client.js.map