Files
dofus-manager/docs/architecture/7-external-apis.md
2026-01-19 08:52:38 +01:00

1.7 KiB

7. External APIs

DofusDB API Integration

Base URL: https://api.dofusdb.fr

Endpoints Used

Endpoint Purpose
GET /classes Liste des classes de personnages
GET /servers Liste des serveurs
GET /quests Liste des quêtes
GET /dungeons Liste des donjons
GET /achievements Liste des succès

Integration Service

// src/lib/server/dofusdb.ts
import NodeCache from 'node-cache';

const cache = new NodeCache({ stdTTL: 3600 }); // 1 hour cache
const BASE_URL = 'https://api.dofusdb.fr';

interface DofusDbOptions {
  lang?: 'fr' | 'en';
  limit?: number;
}

export async function fetchFromDofusDb<T>(
  endpoint: string,
  options: DofusDbOptions = {}
): Promise<T> {
  const { lang = 'fr', limit = 100 } = options;
  const cacheKey = `dofusdb:${endpoint}:${lang}:${limit}`;

  // Check cache
  const cached = cache.get<T>(cacheKey);
  if (cached) return cached;

  // Fetch from API
  const url = new URL(endpoint, BASE_URL);
  url.searchParams.set('$lang', lang);
  url.searchParams.set('$limit', limit.toString());

  const response = await fetch(url.toString(), {
    headers: { 'Accept': 'application/json' },
  });

  if (!response.ok) {
    throw new Error(`DofusDB API error: ${response.status}`);
  }

  const data = await response.json();
  cache.set(cacheKey, data);

  return data as T;
}

// Specific fetchers
export const getClasses = () => fetchFromDofusDb<DofusClass[]>('/classes');
export const getServers = () => fetchFromDofusDb<DofusServer[]>('/servers');
export const getQuests = () => fetchFromDofusDb<DofusQuest[]>('/quests');
export const getDungeons = () => fetchFromDofusDb<DofusDungeon[]>('/dungeons');