Files
classroomio/apps/dashboard/src/lib/utils/services/api/README.md
T
Rotimi Best 6d68351a46 feat: end to end api type safety with redis rate limiting (#573)
* feat: support hono rpc

* add wrangler file

* update name of wrangler file

* add wrangler as a dependency

* add compatibility_flags

* trigger ci

* core: add health check to docker file

* expose port 80

* update custom healthcheck

* feat: implement openapi

* install dependencies in root

* debug yml

* update yml

* update port

* update dockerfile with envs

* completely move away from apiClient

* rename backend to api

* refactor: update server descriptions and comment out local spec saving

* update lock file

* use api.classroomio.com

* add support for redis

* fix: add missing line break in WelcomeModal.svelte

* refactor: remove wrangler configuration and update Vite config for API integration
2025-09-21 19:35:41 +01:00

1.7 KiB

API Client

Basic Usage

import { apiClient, safeRequest, classroomio } from '$lib/utils/services/api';

// GET request
const result = await safeRequest(async () => {
  return apiClient.request('/api/users', { method: 'GET' });
});

// POST request
const result = await safeRequest(async () => {
  return apiClient.request('/api/users', {
    method: 'POST',
    body: { name: 'John', email: 'john@example.com' }
  });
});

// Handle response
if (result.success) {
  console.log('Data:', result.data);
} else {
  console.error('Error:', result.error.message);
}

Error Handling

if (result.success) {
  // Success
  console.log(result.data);
} else {
  // Check error type
  if (result.error.error_code === 'auth') {
    window.location.href = '/login';
  } else if (result.error.error_code === 'client') {
    showError('Please check your input');
  } else if (result.error.error_code === 'server') {
    showError('Server error, please try again');
  }
}

Custom Fetch

import { createApiClient } from '$lib/utils/services/api';

const customClient = createApiClient({
  customFetch: (input, init) => {
    // Your custom fetch logic
    return fetch(input, init);
  }
});

const result = await safeRequest(async () => {
  return customClient.request('/api/data', { method: 'GET' });
});

RPC Usage

import { classroomio } from '$lib/utils/services/api';

// Use with Hono RPC
const data = await classroomio.users.$get();
const user = await classroomio.users.$post({ json: { name: 'John' } });

Error Codes

  • 'auth' - 401 Unauthorized
  • 'client' - 400-499 Client errors
  • 'server' - 500-599 Server errors
  • 'timeout' - 408 Request timeout
  • 'network' - 0 Network issues