Loading...
+{:else if userProfile} +{userProfile.email}
+Failed to load profile
+{/if} +``` + +## Example 9: Form with Loading States + +Proper loading and error states: + +```svelte + + + +``` + +## Example 10: Environment-Specific API URL + +Override API URL for different environments: + +```svelte + +``` + +## Best Practices + +### 1. Always Check Browser Context + +```typescript +const apiClient = browser ? createClientApiClient() : null; +``` + +### 2. Handle Service Unavailability + +```typescript +if (!authService) { + toast.error('Service not available'); + return; +} +``` + +### 3. Use Loading States + +```typescript +let isLoading = $state(false); + +async function doSomething() { + isLoading = true; + try { + // ... + } finally { + isLoading = false; + } +} +``` + +### 4. Proper Error Handling + +```typescript +try { + const result = await authService.login(data); + if (result.success) { + // Success + } else { + // Handle API error + toast.error(result.error); + } +} catch (error) { + // Handle unexpected error + console.error(error); + toast.error('Unexpected error'); +} +``` + +### 5. Use TypeScript Types + +```typescript +import type { UserLoginDto } from '$lib/dto/user'; +import type { ApiResponse } from '$lib/dto/response'; +``` + +## Migration Checklist + +When migrating a feature from remote functions to client API: + +- [ ] Create/use appropriate client service +- [ ] Handle browser context check +- [ ] Implement loading states +- [ ] Add error handling +- [ ] Test success path +- [ ] Test error paths +- [ ] Test loading states +- [ ] Update navigation/redirects +- [ ] Remove old remote function (if fully migrated) +- [ ] Update documentation + +## Common Pitfalls + +### ❌ Don't: Create client on server + +```typescript +// This will error! +const apiClient = createClientApiClient(); +``` + +### ✅ Do: Check browser context + +```typescript +const apiClient = browser ? createClientApiClient() : null; +``` + +### ❌ Don't: Forget loading states + +```typescript +// Bad UX - no feedback +await authService.login(data); +``` + +### ✅ Do: Show loading states + +```typescript +isSubmitting = true; +try { + await authService.login(data); +} finally { + isSubmitting = false; +} +``` + +### ❌ Don't: Ignore errors + +```typescript +// Silent failure +const result = await authService.login(data); +``` + +### ✅ Do: Handle errors properly + +```typescript +const result = await authService.login(data); +if (!result.success) { + toast.error(result.error); +} +``` + +## Next Steps + +After implementing authentication with client API: + +1. Test thoroughly with backend +2. Monitor for errors in production +3. Migrate other features incrementally +4. Update team documentation +5. Share learnings with team diff --git a/src/lib/components/auth/ClientLoginForm.svelte b/src/lib/components/auth/ClientLoginForm.svelte index 5a38d3f..ea9446c 100644 --- a/src/lib/components/auth/ClientLoginForm.svelte +++ b/src/lib/components/auth/ClientLoginForm.svelte @@ -27,7 +27,7 @@ async function handleSubmit(event: Event) { event.preventDefault(); - + if (!authService) { toast.error('Authentication service not available'); return; diff --git a/src/lib/components/auth/ClientLogoutButton.svelte b/src/lib/components/auth/ClientLogoutButton.svelte index 60086a9..86318a3 100644 --- a/src/lib/components/auth/ClientLogoutButton.svelte +++ b/src/lib/components/auth/ClientLogoutButton.svelte @@ -12,7 +12,7 @@ async function handleLogout() { if (!browser) return; - + isLoading = true; try { await clientLogout(); @@ -25,12 +25,7 @@ } -