|
| 1 | +# @nbw/api-client |
| 2 | + |
| 3 | +A TypeScript API client for the NoteBlockWorld backend API, built with axios. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Since this is a workspace package, you can import it in any other package: |
| 8 | + |
| 9 | +```typescript |
| 10 | +import { createApiClient, createDefaultConfig } from '@nbw/api-client'; |
| 11 | +// or |
| 12 | +import { NoteBlockWorldApiClient } from '@nbw/api-client'; |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +### Basic Setup |
| 18 | + |
| 19 | +```typescript |
| 20 | +import { createApiClient, createDefaultConfig } from '@nbw/api-client'; |
| 21 | + |
| 22 | +// Create a client with default configuration |
| 23 | +const client = createApiClient(createDefaultConfig('http://localhost:4000')); |
| 24 | + |
| 25 | +// Or create with custom configuration |
| 26 | +const client = createApiClient({ |
| 27 | + baseURL: 'http://localhost:4000/v1', |
| 28 | + timeout: 30000, |
| 29 | + headers: { |
| 30 | + 'Custom-Header': 'value', |
| 31 | + }, |
| 32 | +}); |
| 33 | +``` |
| 34 | + |
| 35 | +### Authentication |
| 36 | + |
| 37 | +```typescript |
| 38 | +// Set authentication tokens |
| 39 | +client.setAuthTokens({ |
| 40 | + accessToken: 'your-jwt-token', |
| 41 | + refreshToken: 'your-refresh-token', |
| 42 | +}); |
| 43 | + |
| 44 | +// Clear tokens |
| 45 | +client.clearAuthTokens(); |
| 46 | + |
| 47 | +// Get current tokens |
| 48 | +const tokens = client.getAuthTokens(); |
| 49 | +``` |
| 50 | + |
| 51 | +### Using Services |
| 52 | + |
| 53 | +The API client provides organized services for different modules: |
| 54 | + |
| 55 | +#### Auth Service |
| 56 | + |
| 57 | +```typescript |
| 58 | +// Send magic link email |
| 59 | +await client. auth. sendMagicLink({ destination: '[email protected]' }); |
| 60 | + |
| 61 | +// Verify token |
| 62 | +const verification = await client.auth.verifyToken(); |
| 63 | + |
| 64 | +// Get OAuth URLs |
| 65 | +const githubUrl = client.auth.getGitHubLoginUrl(); |
| 66 | +const googleUrl = client.auth.getGoogleLoginUrl(); |
| 67 | +const discordUrl = client.auth.getDiscordLoginUrl(); |
| 68 | +``` |
| 69 | + |
| 70 | +#### User Service |
| 71 | + |
| 72 | +```typescript |
| 73 | +// Get current user |
| 74 | +const user = await client.user.getMe(); |
| 75 | + |
| 76 | +// Get user by email or ID |
| 77 | +const user = await client. user. getUser({ email: '[email protected]' }); |
| 78 | + |
| 79 | +// Update username |
| 80 | +await client.user.updateUsername({ username: 'newusername' }); |
| 81 | + |
| 82 | +// Get paginated users |
| 83 | +const users = await client.user.getUsersPaginated({ page: 1, limit: 10 }); |
| 84 | +``` |
| 85 | + |
| 86 | +#### Song Service |
| 87 | + |
| 88 | +```typescript |
| 89 | +// Get songs with pagination and filtering |
| 90 | +const songs = await client.song.getSongs({ |
| 91 | + page: 1, |
| 92 | + limit: 20, |
| 93 | + sort: 'createdAt', |
| 94 | + order: 'desc', |
| 95 | +}); |
| 96 | + |
| 97 | +// Get featured songs |
| 98 | +const featured = await client.song.getSongs({ q: 'featured' }); |
| 99 | + |
| 100 | +// Get recent songs |
| 101 | +const recent = await client.song.getSongs({ q: 'recent' }); |
| 102 | + |
| 103 | +// Get categories |
| 104 | +const categories = await client.song.getSongs({ q: 'categories' }); |
| 105 | + |
| 106 | +// Get random songs |
| 107 | +const random = await client.song.getSongs({ |
| 108 | + q: 'random', |
| 109 | + count: 5, |
| 110 | + category: 'pop', |
| 111 | +}); |
| 112 | + |
| 113 | +// Search songs |
| 114 | +const searchResults = await client.song.searchSongs( |
| 115 | + { page: 1, limit: 10 }, |
| 116 | + 'my search query', |
| 117 | +); |
| 118 | + |
| 119 | +// Get specific song |
| 120 | +const song = await client.song.getSong('song-id'); |
| 121 | + |
| 122 | +// Get song for editing (requires auth) |
| 123 | +const editableSong = await client.song.getSongForEdit('song-id'); |
| 124 | + |
| 125 | +// Update song (requires auth) |
| 126 | +await client.song.updateSong('song-id', songData); |
| 127 | + |
| 128 | +// Upload new song (requires auth) |
| 129 | +const file = new File([nbsFileContent], 'song.nbs'); |
| 130 | +const uploadResult = await client.song.uploadSong(file, { |
| 131 | + title: 'My Song', |
| 132 | + description: 'A great song', |
| 133 | + // ... other song metadata |
| 134 | +}); |
| 135 | + |
| 136 | +// Get user's songs (requires auth) |
| 137 | +const mySongs = await client.song.getMySongs({ page: 1, limit: 10 }); |
| 138 | + |
| 139 | +// Delete song (requires auth) |
| 140 | +await client.song.deleteSong('song-id'); |
| 141 | + |
| 142 | +// Get download URL |
| 143 | +const downloadUrl = client.song.getSongDownloadUrl('song-id', 'source'); |
| 144 | +``` |
| 145 | + |
| 146 | +#### Seed Service |
| 147 | + |
| 148 | +```typescript |
| 149 | +// Seed development data |
| 150 | +await client.seed.seedDev(); |
| 151 | +``` |
| 152 | + |
| 153 | +### Error Handling |
| 154 | + |
| 155 | +All methods return promises that may reject with an `ApiError`: |
| 156 | + |
| 157 | +```typescript |
| 158 | +try { |
| 159 | + const songs = await client.song.getSongs(); |
| 160 | +} catch (error) { |
| 161 | + console.error('API Error:', error.message); |
| 162 | + console.error('Status:', error.status); |
| 163 | + console.error('Code:', error.code); |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +### Using Individual Services |
| 168 | + |
| 169 | +You can also import and use individual services: |
| 170 | + |
| 171 | +```typescript |
| 172 | +import { SongService, createDefaultConfig } from '@nbw/api-client'; |
| 173 | + |
| 174 | +const songService = new SongService( |
| 175 | + createDefaultConfig('http://localhost:4000'), |
| 176 | +); |
| 177 | +const songs = await songService.getSongs(); |
| 178 | +``` |
| 179 | + |
| 180 | +### Custom Extensions |
| 181 | + |
| 182 | +Extend the base client for custom functionality: |
| 183 | + |
| 184 | +```typescript |
| 185 | +import { BaseApiClient } from '@nbw/api-client'; |
| 186 | + |
| 187 | +class CustomApiClient extends BaseApiClient { |
| 188 | + async customEndpoint() { |
| 189 | + return this.get('/custom-endpoint'); |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +## API Response Format |
| 195 | + |
| 196 | +All API methods return an `ApiResponse<T>` object: |
| 197 | + |
| 198 | +```typescript |
| 199 | +interface ApiResponse<T> { |
| 200 | + data: T; |
| 201 | + status: number; |
| 202 | + statusText: string; |
| 203 | + headers: Record<string, string>; |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +## TypeScript Support |
| 208 | + |
| 209 | +The client is fully typed using the DTOs from `@nbw/database`. All request/response types are properly typed for excellent IDE support and type checking. |
| 210 | + |
0 commit comments