fetch-data
Skill3 starsChanged 2 days agoMITView on GitHub
Charger des données depuis une API (liste, détail, recherche) avec chargement, erreurs et cache. À utiliser dès qu'une page affiche des données distantes.
- Reads credentials
- Installs packages
In this repository12 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- add-componentSkill
- add-formSkill
- add-motionSkill
- add-pageSkill
- add-testSkill
- deploySkill
- design-systemSkill
- from-figmaSkill
- polish-uiSkill
- rebrandSkill
The file
Files
Charger des données
Deux options selon le besoin.
Option A — loader de route (simple, sans dépendance)
Pour des données liées à une page, TanStack Router charge avant l'affichage :
import { createFileRoute } from '@tanstack/react-router'
type Repo = { id: number; name: string; stargazers_count: number }
export const Route = createFileRoute('/repos')({
loader: async () => {
const res = await fetch('https://api.github.com/users/vitejs/repos')
if (!res.ok) throw new Error('Impossible de charger les dépôts')
return (await res.json()) as Repo[]
},
pendingComponent: () => (
<p className="p-8 text-muted-foreground">Chargement…</p>
),
errorComponent: ({ error }) => (
<p className="p-8 text-destructive">{error.message}</p>
),
component: ReposPage,
})
function ReposPage() {
const repos = Route.useLoaderData()
return (
<ul className="mx-auto max-w-6xl px-4 py-16 sm:px-6">
{repos.map((r) => (
<li key={r.id}>
{r.name} · ★ {r.stargazers_count}
</li>
))}
</ul>
)
}Option B — TanStack Query (cache, rafraîchissement, mutations)
Quand les données sont partagées entre pages, rafraîchies ou modifiées :
npm install @tanstack/react-queryDans src/main.tsx, envelopper l'app :
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
const queryClient = new QueryClient()
// …
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>Puis dans un composant :
const { data, isPending, error } = useQuery({
queryKey: ['repos', user],
queryFn: () =>
fetch(`https://api.github.com/users/${user}/repos`).then((r) => r.json()),
})Règles
- Toujours gérer les trois états : chargement, erreur, données (et liste vide).
- Typer la réponse (
type Repo = …) ; pour une API externe peu fiable, valider avec zod. - Les clés d'API secrètes ne vont jamais dans le code du site (il est public). Variables publiques uniquement via
import.meta.env.VITE_*dans un fichier.env.local(ignoré par git).
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

