import type { ApiResponse, ColorInfoRequest, ColorInfoData, ConvertFormatRequest, ConvertFormatData, ColorManipulationRequest, ColorManipulationData, ColorMixRequest, ColorMixData, RandomColorsRequest, RandomColorsData, DistinctColorsRequest, DistinctColorsData, GradientRequest, GradientData, ColorDistanceRequest, ColorDistanceData, ColorSortRequest, ColorSortData, ColorBlindnessRequest, ColorBlindnessData, TextColorRequest, TextColorData, NamedColorsData, NamedColorSearchRequest, NamedColorSearchData, HealthData, CapabilitiesData, PaletteGenerateRequest, PaletteGenerateData, } from './types'; export class PastelAPIClient { private baseURL: string; constructor(baseURL?: string) { // Use the Next.js API proxy route for runtime configuration // This allows changing the backend API URL without rebuilding this.baseURL = baseURL || '/api/pastel'; } private async request( endpoint: string, options?: RequestInit ): Promise> { // Endpoint already includes /api/v1 prefix on backend, // but our proxy route expects paths after /api/v1/ const url = `${this.baseURL}${endpoint}`; try { const response = await fetch(url, { ...options, headers: { 'Content-Type': 'application/json', ...options?.headers, }, }); const data = await response.json(); if (!response.ok) { return { success: false, error: data.error || { code: 'INTERNAL_ERROR', message: 'An unknown error occurred', }, }; } return data; } catch (error) { return { success: false, error: { code: 'NETWORK_ERROR', message: error instanceof Error ? error.message : 'Network request failed', }, }; } } // Color Information async getColorInfo(request: ColorInfoRequest): Promise> { return this.request('/colors/info', { method: 'POST', body: JSON.stringify(request), }); } // Format Conversion async convertFormat(request: ConvertFormatRequest): Promise> { return this.request('/colors/convert', { method: 'POST', body: JSON.stringify(request), }); } // Color Manipulation async lighten(request: ColorManipulationRequest): Promise> { return this.request('/colors/lighten', { method: 'POST', body: JSON.stringify(request), }); } async darken(request: ColorManipulationRequest): Promise> { return this.request('/colors/darken', { method: 'POST', body: JSON.stringify(request), }); } async saturate(request: ColorManipulationRequest): Promise> { return this.request('/colors/saturate', { method: 'POST', body: JSON.stringify(request), }); } async desaturate(request: ColorManipulationRequest): Promise> { return this.request('/colors/desaturate', { method: 'POST', body: JSON.stringify(request), }); } async rotate(request: ColorManipulationRequest): Promise> { return this.request('/colors/rotate', { method: 'POST', body: JSON.stringify(request), }); } async complement(colors: string[]): Promise> { return this.request('/colors/complement', { method: 'POST', body: JSON.stringify({ colors }), }); } async grayscale(colors: string[]): Promise> { return this.request('/colors/grayscale', { method: 'POST', body: JSON.stringify({ colors }), }); } async mix(request: ColorMixRequest): Promise> { return this.request('/colors/mix', { method: 'POST', body: JSON.stringify(request), }); } // Color Generation async generateRandom(request: RandomColorsRequest): Promise> { return this.request('/colors/random', { method: 'POST', body: JSON.stringify(request), }); } async generateDistinct(request: DistinctColorsRequest): Promise> { return this.request('/colors/distinct', { method: 'POST', body: JSON.stringify(request), }); } async generateGradient(request: GradientRequest): Promise> { return this.request('/colors/gradient', { method: 'POST', body: JSON.stringify(request), }); } // Color Analysis async calculateDistance(request: ColorDistanceRequest): Promise> { return this.request('/colors/distance', { method: 'POST', body: JSON.stringify(request), }); } async sortColors(request: ColorSortRequest): Promise> { return this.request('/colors/sort', { method: 'POST', body: JSON.stringify(request), }); } // Accessibility async simulateColorBlindness(request: ColorBlindnessRequest): Promise> { return this.request('/colors/colorblind', { method: 'POST', body: JSON.stringify(request), }); } async getTextColor(request: TextColorRequest): Promise> { return this.request('/colors/textcolor', { method: 'POST', body: JSON.stringify(request), }); } // Named Colors async getNamedColors(): Promise> { return this.request('/colors/names', { method: 'GET', }); } async searchNamedColors(request: NamedColorSearchRequest): Promise> { return this.request('/colors/names/search', { method: 'POST', body: JSON.stringify(request), }); } // System async getHealth(): Promise> { return this.request('/health', { method: 'GET', }); } async getCapabilities(): Promise> { return this.request('/capabilities', { method: 'GET', }); } // Palette Generation async generatePalette(request: PaletteGenerateRequest): Promise> { return this.request('/palettes/generate', { method: 'POST', body: JSON.stringify(request), }); } } // Export singleton instance export const pastelAPI = new PastelAPIClient();