| 1 |
# API & Data Management |
| 2 |
|
| 3 |
## API Refactoring Patterns |
| 4 |
- Extract API logic into pure functions that return Promises |
| 5 |
- Implement caching with search parameters as cache keys |
| 6 |
- Fix pagination by calculating page numbers based on `startIndex` and `stopIndex` |
| 7 |
|
| 8 |
## Centralized API Requests |
| 9 |
- Create `makeApiRequest()` static functions in Helper class |
| 10 |
- Include standard headers: site URL and API key |
| 11 |
- Maintain consistent API request patterns across the application |
| 12 |
|
| 13 |
## Race Condition Prevention |
| 14 |
- Use `AbortController` to cancel previous ongoing requests |
| 15 |
- Cancel ongoing requests before initiating new requests |
| 16 |
- Implement proper cleanup in component unmounting |
| 17 |
|
| 18 |
## Development API Configuration |
| 19 |
- Use `TEMPLATELY_DEV_API` constant for API endpoint selection |
| 20 |
- Simple boolean logic: `true` = dev server, `false`/`undefined` = live API |
| 21 |
- Avoid complex state tracking for API endpoint management |
| 22 |
|
| 23 |
## Error Handling in Async Functions |
| 24 |
- Throw errors instead of returning them in async functions and promises |
| 25 |
- This allows calling code to properly catch errors with `.catch()` |
| 26 |
- Follow JavaScript/Promise best practices for error propagation |
| 27 |
|
| 28 |
--- |
| 29 |
|
| 30 |
# Gateway Timeout Handling |
| 31 |
|
| 32 |
## Polling Implementation |
| 33 |
- Implement polling mechanism for 504 Gateway Timeout errors |
| 34 |
- Use appropriate intervals between polling attempts |
| 35 |
- Respect `AbortSignal` and clear intervals on component exit |
| 36 |
- Implement sequential calls with delays between them |
| 37 |
- Avoid fixed intervals; use dynamic timing based on response |
| 38 |
- Implement proper cleanup and cancellation |
| 39 |
|