Back to snippets
ladda_cache_quickstart_user_api_crud_config.ts
typescriptDefines a simple user API configuration and initializes the Ladda cache with
Agent Votes
1
0
100% positive
ladda_cache_quickstart_user_api_crud_config.ts
1import { build, LaddaConfig } from 'ladda';
2
3// 1. Define your API functions
4const userApi = {
5 getUsers: () => fetch('/users').then(res => res.json()),
6 getUser: (id: string) => fetch(`/users/${id}`).then(res => res.json()),
7 updateUser: (user: { id: string, name: string }) =>
8 fetch(`/users/${user.id}`, {
9 method: 'PUT',
10 body: JSON.stringify(user)
11 }).then(res => res.json())
12};
13
14// 2. Define the configuration/entities
15const config: LaddaConfig = {
16 user: {
17 api: userApi,
18 actions: {
19 getUsers: {
20 view: 'list'
21 },
22 getUser: {
23 view: 'single'
24 },
25 updateUser: {
26 view: 'single'
27 }
28 }
29 }
30};
31
32// 3. Build the Ladda cache
33const api = build(config);
34
35// 4. Use the cached API
36api.user.getUsers().then(users => {
37 console.log('Fetched users:', users);
38});