AgentSkillsCN

api-integrations-sunnahsleep

管理 SunnahSleep 的外部 API 集成。适用于与 Aladhan、ipwho.is、Nominatim、Open-Meteo,或伊斯兰网络 CDN 等服务协作时使用。涵盖错误处理、超时控制、故障降级,以及速率限制等关键环节。

SKILL.md
--- frontmatter
name: api-integrations-sunnahsleep
description: Manages external API integrations for SunnahSleep. Use when working with Aladhan, ipwho.is, Nominatim, Open-Meteo, or Islamic Network CDN. Covers error handling, timeouts, fallbacks, and rate limiting.

API Integrations

APIs Used by SunnahSleep

APIPurposeEndpointKey Required
AladhanPrayer timesapi.aladhan.com/v1/timingsNo
ipwho.isIP geolocationipwho.isNo
NominatimCity searchnominatim.openstreetmap.orgNo
Open-MeteoCity search fallbackgeocoding-api.open-meteo.comNo
Islamic NetworkQuran audio CDNcdn.islamic.networkNo

Requirements

1. Error Handling

  • Wrap fetch in try/catch
  • Check response.ok before parsing
  • Handle network errors, 4xx/5xx, JSON parse errors
  • Provide user-facing error messages
  • Log errors for debugging (avoid leaking sensitive data)

2. Timeouts

  • Add AbortController with timeout (e.g., 10s) for fetch
  • Prevent hanging requests
  • Clear timeout on success

3. Fallbacks

  • Location: Fallback to Mecca if IP detection fails
  • City search: Try Nominatim first, then Open-Meteo
  • Prayer times: Show error state; do not corrupt UI

4. User-Agent

  • Set User-Agent for Nominatim: SunnahSleep/1.0 (https://sunnahsleep.app)
  • Some APIs require identifiable User-Agent

5. Rate Limiting

  • Nominatim: 1 req/sec; cache results
  • Aladhan: Cache prayer times by date/location
  • Avoid repeated calls for same input

Example: Safe Fetch with Timeout

ts
async function fetchWithTimeout(url: string, options: RequestInit = {}, timeoutMs = 10000) {
  const controller = new AbortController();
  const id = setTimeout(() => controller.abort(), timeoutMs);
  try {
    const res = await fetch(url, { ...options, signal: controller.signal });
    clearTimeout(id);
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    return await res.json();
  } catch (err) {
    clearTimeout(id);
    if (err instanceof Error) throw err;
    throw new Error('Network request failed');
  }
}

Checklist

  • All fetch calls have try/catch
  • response.ok checked before JSON parse
  • Timeout on long-running requests
  • Fallback for location detection
  • User-Agent where required
  • Prayer times cached (localStorage) by date+coords