|
| 1 | +# Tidal API TypeScript Bulk Operations Client |
| 2 | + |
| 3 | +A TypeScript client library for performing bulk operations on Tidal API resources with authentication, error handling, and comprehensive logging. |
| 4 | + |
| 5 | +## ✅ Features |
| 6 | + |
| 7 | +- ✅ **Authentication Service**: Complete authentication flow with token refresh using `/authenticate` endpoint |
| 8 | +- ✅ **API Client**: HTTP client with automatic token management |
| 9 | +- ✅ **Error Handling**: Comprehensive error types and handling |
| 10 | +- ✅ **Logging**: Configurable logging with multiple levels |
| 11 | +- ✅ **Configuration**: Environment-based configuration management |
| 12 | +- ✅ **TypeScript**: Full type safety and IntelliSense support |
| 13 | +- ✅ **Testing**: Comprehensive unit tests with >80% coverage |
| 14 | + |
| 15 | +## 📦 Installation |
| 16 | + |
| 17 | +```bash |
| 18 | +npm install |
| 19 | +``` |
| 20 | + |
| 21 | +## 🔧 Configuration |
| 22 | + |
| 23 | +Create a `.env` file based on `.env.example`: |
| 24 | + |
| 25 | +```bash |
| 26 | +cp .env.example .env |
| 27 | +``` |
| 28 | + |
| 29 | +Configure your environment variables: |
| 30 | + |
| 31 | +```env |
| 32 | +TIDAL_WORKSPACE=your_workspace_name |
| 33 | +TIDAL_USERNAME=your_username |
| 34 | +TIDAL_PASSWORD=your_password |
| 35 | +LOG_LEVEL=info |
| 36 | +``` |
| 37 | + |
| 38 | +**Note**: The base URL is automatically generated as `https://{workspace}.tidal.cloud/api/v1` based on your workspace name. You can override this by setting `TIDAL_BASE_URL` if needed. |
| 39 | + |
| 40 | +## 🚀 Quick Start |
| 41 | + |
| 42 | +### Basic Usage |
| 43 | + |
| 44 | +```typescript |
| 45 | +import { TidalApiClient } from './src/api/client'; |
| 46 | + |
| 47 | +// Create client |
| 48 | +const client = new TidalApiClient({ |
| 49 | + workspace: 'your-workspace' |
| 50 | + // baseUrl is auto-generated as https://your-workspace.tidal.cloud/api/v1 |
| 51 | +}); |
| 52 | + |
| 53 | +// Authenticate |
| 54 | +await client.authenticate('username', 'password'); |
| 55 | + |
| 56 | +// Make API calls |
| 57 | +const response = await client.get('/servers'); |
| 58 | +console.log(response.data); |
| 59 | +``` |
| 60 | + |
| 61 | +### Using Environment Configuration |
| 62 | + |
| 63 | +```typescript |
| 64 | +import { createAuthenticatedClient } from './src/index'; |
| 65 | + |
| 66 | +// Automatically loads from environment variables |
| 67 | +const client = await createAuthenticatedClient(); |
| 68 | + |
| 69 | +// Client is ready to use |
| 70 | +const servers = await client.get('/servers'); |
| 71 | +``` |
| 72 | + |
| 73 | +### Manual Authentication Service |
| 74 | + |
| 75 | +```typescript |
| 76 | +import { AuthService } from './src/api/auth'; |
| 77 | + |
| 78 | +const auth = new AuthService('https://your-workspace.tidal.cloud/api/v1'); |
| 79 | + |
| 80 | +// Authenticate |
| 81 | +const tokens = await auth.authenticate({ |
| 82 | + username: 'your-username', |
| 83 | + password: 'your-password' |
| 84 | +}); |
| 85 | + |
| 86 | +// Check token validity |
| 87 | +if (auth.isTokenValid()) { |
| 88 | + const token = auth.getAccessToken(); |
| 89 | + // Use token for API calls |
| 90 | +} |
| 91 | + |
| 92 | +// Refresh token when needed |
| 93 | +if (!auth.isTokenValid()) { |
| 94 | + await auth.refreshAccessToken(); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## 🔍 API Reference |
| 99 | + |
| 100 | +### TidalApiClient |
| 101 | + |
| 102 | +```typescript |
| 103 | +class TidalApiClient { |
| 104 | + constructor(config: ClientConfig) |
| 105 | + |
| 106 | + // Authentication |
| 107 | + authenticate(username: string, password: string): Promise<AuthResponse> |
| 108 | + isAuthenticated(): boolean |
| 109 | + |
| 110 | + // HTTP Methods |
| 111 | + get<T>(endpoint: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>> |
| 112 | + post<T>(endpoint: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>> |
| 113 | + put<T>(endpoint: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>> |
| 114 | + patch<T>(endpoint: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse<T>> |
| 115 | + delete<T>(endpoint: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>> |
| 116 | + |
| 117 | + // Utility |
| 118 | + getWorkspace(): string |
| 119 | + getBaseUrl(): string |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### AuthService |
| 124 | + |
| 125 | +```typescript |
| 126 | +class AuthService { |
| 127 | + constructor(baseUrl: string) |
| 128 | + |
| 129 | + authenticate(credentials: AuthCredentials): Promise<AuthResponse> |
| 130 | + isTokenValid(): boolean |
| 131 | + getAccessToken(): string | null |
| 132 | + refreshAccessToken(): Promise<AuthResponse> |
| 133 | + clearTokens(): void |
| 134 | + ensureValidToken(credentials?: AuthCredentials): Promise<string> |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +## 🧪 Testing |
| 139 | + |
| 140 | +Run the test suite: |
| 141 | + |
| 142 | +```bash |
| 143 | +# Run all tests |
| 144 | +npm test |
| 145 | + |
| 146 | +# Run tests in watch mode |
| 147 | +npm run test:watch |
| 148 | + |
| 149 | +# Run tests with coverage |
| 150 | +npm test -- --coverage |
| 151 | +``` |
| 152 | + |
| 153 | +## 📝 Development |
| 154 | + |
| 155 | +### Build |
| 156 | + |
| 157 | +```bash |
| 158 | +npm run build |
| 159 | +``` |
| 160 | + |
| 161 | +### Linting |
| 162 | + |
| 163 | +```bash |
| 164 | +npm run lint |
| 165 | +``` |
| 166 | + |
| 167 | +### Development Mode |
| 168 | + |
| 169 | +```bash |
| 170 | +npm run dev |
| 171 | +``` |
| 172 | + |
| 173 | +## 🔧 Error Handling |
| 174 | + |
| 175 | +The client provides comprehensive error handling: |
| 176 | + |
| 177 | +```typescript |
| 178 | +import { TidalApiError, AuthenticationError, ConfigurationError } from './src/utils/errors'; |
| 179 | + |
| 180 | +try { |
| 181 | + await client.authenticate('invalid', 'credentials'); |
| 182 | +} catch (error) { |
| 183 | + if (error instanceof AuthenticationError) { |
| 184 | + console.error('Authentication failed:', error.message); |
| 185 | + } else if (error instanceof TidalApiError) { |
| 186 | + console.error('API error:', error.status, error.message); |
| 187 | + } |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +## 📊 Logging |
| 192 | + |
| 193 | +Configure logging levels: |
| 194 | + |
| 195 | +```typescript |
| 196 | +import { logger, LogLevel } from './src/utils/logger'; |
| 197 | + |
| 198 | +// Set log level |
| 199 | +logger.setLevel(LogLevel.DEBUG); |
| 200 | + |
| 201 | +// Log messages |
| 202 | +logger.info('Client initialized'); |
| 203 | +logger.debug('Making API request', { endpoint: '/servers' }); |
| 204 | +logger.error('Request failed', { error: 'Network timeout' }); |
| 205 | +``` |
| 206 | + |
| 207 | +## 🤝 Contributing |
| 208 | + |
| 209 | +1. Fork the repository |
| 210 | +2. Create a feature branch |
| 211 | +3. Make your changes |
| 212 | +4. Add tests |
| 213 | +5. Run the test suite |
| 214 | +6. Submit a pull request |
0 commit comments