/** * Price Alerts Resource * * Manage price alert configurations for automated notifications. */ import type { OilPriceAPI } from "../client.js"; /** * Valid condition operators for price alerts */ export type AlertOperator = "greater_than" | "less_than" | "equals" | "greater_than_or_equal" | "less_than_or_equal"; /** * Price alert configuration */ export interface PriceAlert { /** Unique alert identifier */ id: string; /** User-friendly alert name */ name: string; /** Commodity code to monitor (e.g., "BRENT_CRUDE_USD") */ commodity_code: string; /** Comparison operator for alert condition */ condition_operator: AlertOperator; /** Price threshold value in USD */ condition_value: number; /** Optional webhook URL for notifications */ webhook_url?: string | null; /** Whether the alert is active */ enabled: boolean; /** Minimum minutes between alert triggers (0-1440) */ cooldown_minutes: number; /** Optional metadata for custom use */ metadata?: Record | null; /** Number of times this alert has triggered */ trigger_count: number; /** ISO timestamp of last trigger, or null if never triggered */ last_triggered_at: string | null; /** ISO timestamp when alert was created */ created_at: string; /** ISO timestamp when alert was last updated */ updated_at: string; } /** * Parameters for creating a new price alert */ export interface CreateAlertParams { /** User-friendly alert name */ name: string; /** Commodity code to monitor (e.g., "BRENT_CRUDE_USD") */ commodity_code: string; /** Comparison operator for alert condition */ condition_operator: AlertOperator; /** Price threshold value in USD (must be > 0 and <= 1,000,000) */ condition_value: number; /** Optional webhook URL for POST notifications */ webhook_url?: string; /** Whether to enable the alert immediately (default: true) */ enabled?: boolean; /** Minimum minutes between triggers (0-1440, default: 60) */ cooldown_minutes?: number; /** Optional metadata for custom use */ metadata?: Record; } /** * Parameters for updating an existing price alert */ export interface UpdateAlertParams { /** User-friendly alert name */ name?: string; /** Commodity code to monitor */ commodity_code?: string; /** Comparison operator for alert condition */ condition_operator?: AlertOperator; /** Price threshold value in USD */ condition_value?: number; /** Webhook URL for notifications */ webhook_url?: string | null; /** Whether the alert is active */ enabled?: boolean; /** Minimum minutes between triggers (0-1440) */ cooldown_minutes?: number; /** Metadata for custom use */ metadata?: Record | null; } /** * Response from webhook test endpoint */ export interface WebhookTestResponse { /** Test result status */ success: boolean; /** HTTP status code from webhook endpoint */ status_code: number; /** Response time in milliseconds */ response_time_ms: number; /** Response body from webhook endpoint */ response_body?: string; /** Error message if test failed */ error?: string; } /** A record returned by the deprecated trigger-history endpoint. */ export type AlertTrigger = Record; /** One triggered analytics alert returned by analytics history. */ export interface AlertAnalyticsEntry { id: string | number; name: string; commodity_code: string; analytics_type: string; analytics_period: string | null; analytics_config: Record | null; condition: string; trigger_count: number; last_triggered_at: string; cooldown_minutes: number; enabled: boolean; metadata: Record | null; } /** Pagination metadata returned with analytics history. */ export interface AlertAnalyticsPagination { page: number; per_page: number; total: number; } /** Typed response from the analytics-history endpoint. */ export interface AlertAnalyticsHistory { triggered_alerts: AlertAnalyticsEntry[]; pagination: AlertAnalyticsPagination; } /** * Price Alerts Resource * * Manage automated price alert configurations with webhook notifications. * * **Features:** * - Create alerts with customizable conditions * - Monitor commodity prices automatically * - Webhook notifications when conditions are met * - Cooldown periods to prevent spam * - 100 alerts per user soft limit * * **Example:** * ```typescript * import { OilPriceAPI } from 'oilpriceapi'; * * const client = new OilPriceAPI({ apiKey: 'your_key' }); * * // Create a price alert * const alert = await client.alerts.create({ * name: 'Brent High Price Alert', * commodity_code: 'BRENT_CRUDE_USD', * condition_operator: 'greater_than', * condition_value: 85.00, * webhook_url: 'https://your-app.com/webhooks/price-alert', * enabled: true, * cooldown_minutes: 60 * }); * * console.log(`Alert created: ${alert.name} (ID: ${alert.id})`); * * // List all alerts * const alerts = await client.alerts.list(); * console.log(`You have ${alerts.length} active alerts`); * * // Update an alert * const updated = await client.alerts.update(alert.id, { * condition_value: 90.00, * enabled: false * }); * * // Delete an alert * await client.alerts.delete(alert.id); * ``` */ export declare class AlertsResource { private client; constructor(client: OilPriceAPI); /** * List all price alerts for the authenticated user * * Returns all configured price alerts, including disabled ones. * Alerts are sorted by creation date (newest first). * * @returns Array of all price alerts * * @throws {OilPriceAPIError} If API request fails * @throws {AuthenticationError} If API key is invalid * @throws {RateLimitError} If rate limit exceeded * * @example * ```typescript * const alerts = await client.alerts.list(); * * alerts.forEach(alert => { * console.log(`${alert.name}: ${alert.commodity_code} ${alert.condition_operator} ${alert.condition_value}`); * console.log(` Status: ${alert.enabled ? 'Active' : 'Disabled'}`); * console.log(` Triggers: ${alert.trigger_count}`); * }); * ``` */ list(): Promise; /** * Get a specific price alert by ID * * @param id - The alert ID to retrieve * @returns The price alert details * * @throws {OilPriceAPIError} If API request fails * @throws {DataNotFoundError} If alert ID not found * @throws {AuthenticationError} If API key is invalid * * @example * ```typescript * const alert = await client.alerts.get('550e8400-e29b-41d4-a716-446655440000'); * console.log(`Alert: ${alert.name}`); * console.log(`Condition: ${alert.commodity_code} ${alert.condition_operator} ${alert.condition_value}`); * console.log(`Last triggered: ${alert.last_triggered_at || 'Never'}`); * ``` */ get(id: string): Promise; /** * Create a new price alert * * Creates a price alert that monitors a commodity and triggers when * the price meets the specified condition. Optionally sends webhook * notifications when triggered. * * **Validation:** * - name: 1-100 characters * - commodity_code: Must be a valid commodity code * - condition_value: Must be > 0 and <= 1,000,000 * - cooldown_minutes: Must be 0-1440 (24 hours) * - webhook_url: Must be valid HTTPS URL if provided * * **Soft Limit:** 100 alerts per user * * @param params - Alert configuration parameters * @returns The created price alert * * @throws {ValidationError} If parameters are invalid * @throws {OilPriceAPIError} If API request fails * @throws {AuthenticationError} If API key is invalid * * @example * ```typescript * // Alert when Brent crude exceeds $85 * const alert = await client.alerts.create({ * name: 'Brent $85 Alert', * commodity_code: 'BRENT_CRUDE_USD', * condition_operator: 'greater_than', * condition_value: 85.00, * webhook_url: 'https://myapp.com/webhook', * enabled: true, * cooldown_minutes: 120 // 2 hours between triggers * }); * ``` */ create(params: CreateAlertParams): Promise; /** * Update an existing price alert * * Updates one or more fields of an existing alert. Only provided * fields will be updated; others remain unchanged. * * @param id - The alert ID to update * @param params - Fields to update (partial update supported) * @returns The updated price alert * * @throws {ValidationError} If parameters are invalid * @throws {DataNotFoundError} If alert ID not found * @throws {OilPriceAPIError} If API request fails * * @example * ```typescript * // Disable an alert * await client.alerts.update(alertId, { enabled: false }); * * // Change threshold and cooldown * await client.alerts.update(alertId, { * condition_value: 90.00, * cooldown_minutes: 180 * }); * * // Update webhook URL * await client.alerts.update(alertId, { * webhook_url: 'https://newapp.com/webhook' * }); * ``` */ update(id: string, params: UpdateAlertParams): Promise; /** * Delete a price alert * * Permanently deletes a price alert. This action cannot be undone. * * @param id - The alert ID to delete * * @throws {DataNotFoundError} If alert ID not found * @throws {OilPriceAPIError} If API request fails * * @example * ```typescript * await client.alerts.delete(alertId); * console.log('Alert deleted successfully'); * ``` */ delete(id: string): Promise; /** * Test an alert * * Triggers a test run of the alert to verify it's working correctly. * Does not affect the alert's cooldown or trigger count. * * @param alertId - The alert ID to test * @returns Test results * * @throws {NotFoundError} If alert not found * @throws {OilPriceAPIError} If API request fails * * @example * ```typescript * const result = await client.alerts.test(alertId); * console.log(`Test ${result.success ? 'passed' : 'failed'}`); * if (result.response_time_ms) { * console.log(`Webhook response time: ${result.response_time_ms}ms`); * } * ``` */ test(alertId: string): Promise; /** * Get available alert triggers * * Returns list of supported trigger conditions and event types. * * @returns Array of available triggers * * @throws {OilPriceAPIError} If API request fails * * @example * ```typescript * const triggers = await client.alerts.triggers(); * triggers.forEach(trigger => { * console.log(`${trigger.name}: ${trigger.description}`); * }); * ``` */ triggers(): Promise; /** * Get alert analytics history * * Returns triggered analytics alerts and pagination metadata. The total * number of matching alerts is available as `pagination.total`. * * @returns Analytics history data * * @throws {OilPriceAPIError} If API request fails * * @example * ```typescript * const analytics = await client.alerts.analyticsHistory(); * console.log(`Triggered alerts: ${analytics.pagination.total}`); * console.log(analytics.triggered_alerts[0]?.last_triggered_at); * ``` */ analyticsHistory(): Promise; }