|
| 1 | +--- |
| 2 | +title: API Request |
| 3 | +description: Make HTTP requests |
| 4 | +--- |
| 5 | + |
| 6 | +# API Request |
| 7 | + |
| 8 | +Make HTTP requests to backend endpoints. |
| 9 | + |
| 10 | +## Protocol |
| 11 | + |
| 12 | +```typescript |
| 13 | +{ |
| 14 | + type: 'api', |
| 15 | + api: string, // API endpoint |
| 16 | + method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE', |
| 17 | + data?: object | string, // Request body (template supported) |
| 18 | + headers?: object, // Request headers |
| 19 | + confirm?: string, // Confirmation message |
| 20 | + onSuccess?: Action, // Success handler |
| 21 | + onError?: Action, // Error handler |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +## Examples |
| 26 | + |
| 27 | +### Basic Request |
| 28 | + |
| 29 | +```json |
| 30 | +{ |
| 31 | + "type": "button", |
| 32 | + "label": "Save", |
| 33 | + "onClick": { |
| 34 | + "type": "api", |
| 35 | + "api": "/api/users/${userId}", |
| 36 | + "method": "PUT", |
| 37 | + "data": "${formData}", |
| 38 | + "confirm": "Save changes?", |
| 39 | + "onSuccess": { |
| 40 | + "type": "toast", |
| 41 | + "message": "Saved successfully!" |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### DELETE with Confirmation |
| 48 | + |
| 49 | +```json |
| 50 | +{ |
| 51 | + "type": "api", |
| 52 | + "api": "/api/users/${id}", |
| 53 | + "method": "DELETE", |
| 54 | + "confirm": "Are you sure you want to delete this user?", |
| 55 | + "onSuccess": [ |
| 56 | + { "type": "toast", "message": "User deleted" }, |
| 57 | + { "type": "reload" } |
| 58 | + ], |
| 59 | + "onError": { |
| 60 | + "type": "toast", |
| 61 | + "message": "Failed to delete user", |
| 62 | + "variant": "error" |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### POST with Custom Headers |
| 68 | + |
| 69 | +```json |
| 70 | +{ |
| 71 | + "type": "api", |
| 72 | + "api": "/api/upload", |
| 73 | + "method": "POST", |
| 74 | + "headers": { |
| 75 | + "Content-Type": "multipart/form-data" |
| 76 | + }, |
| 77 | + "data": "${fileData}" |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## HTTP Methods |
| 82 | + |
| 83 | +- **GET** - Retrieve data |
| 84 | +- **POST** - Create new resources |
| 85 | +- **PUT** - Update entire resources |
| 86 | +- **PATCH** - Partial updates |
| 87 | +- **DELETE** - Remove resources |
| 88 | + |
| 89 | +## Template Variables |
| 90 | + |
| 91 | +Use template syntax in `api`, `data`, and other string fields: |
| 92 | + |
| 93 | +```json |
| 94 | +{ |
| 95 | + "api": "/api/users/${userId}/posts/${postId}", |
| 96 | + "data": { |
| 97 | + "title": "${formData.title}", |
| 98 | + "content": "${formData.content}" |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## Action Chaining |
| 104 | + |
| 105 | +Chain multiple actions on success or error: |
| 106 | + |
| 107 | +```json |
| 108 | +{ |
| 109 | + "type": "api", |
| 110 | + "api": "/api/submit", |
| 111 | + "method": "POST", |
| 112 | + "onSuccess": [ |
| 113 | + { "type": "toast", "message": "Success!" }, |
| 114 | + { "type": "navigate", "url": "/dashboard" } |
| 115 | + ] |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Confirmation Dialogs |
| 120 | + |
| 121 | +Add a confirmation prompt before executing: |
| 122 | + |
| 123 | +```json |
| 124 | +{ |
| 125 | + "type": "api", |
| 126 | + "api": "/api/delete-all", |
| 127 | + "method": "POST", |
| 128 | + "confirm": "This will delete all data. Are you sure?" |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +## Related |
| 133 | + |
| 134 | +- **[Form](/docs/03-objectui/container-components/form)** - Form submission |
| 135 | +- **[Toast](/docs/03-objectui/actions/toast)** - User feedback |
| 136 | +- **[Navigate](/docs/03-objectui/actions/navigate)** - Redirect after API calls |
0 commit comments