Skip to content

Commit 3128284

Browse files
committed
docs: Remove unverified Timeout Middleware example
Removed the Timeout Middleware example that was not found in the codebase: - The pattern was unverified and potentially had memory leaks (no timeout cleanup) - Used unnecessary middleware complexity Replaced with correct approach using signal option directly: - DevupApiRequestInit extends RequestInit, so signal is natively supported - Added two examples: wrapper function and direct signal usage - Includes proper timeout cleanup with clearTimeout() - Simpler, clearer, and verified approach This matches how React Query integration uses signal (see packages/react-query/src/query-client.ts:88-94)
1 parent 97970e6 commit 3128284

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

README.md

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -576,35 +576,67 @@ api.use({
576576
})
577577
```
578578

579-
#### Timeout Middleware
579+
#### Request Timeout
580+
581+
devup-api supports `signal` option from `RequestInit`, allowing you to implement timeouts easily:
580582

581583
```ts
582584
import { createApi } from '@devup-api/fetch'
583585

584586
const api = createApi({ baseUrl: 'https://api.example.com' })
585587

586-
api.use({
587-
onRequest: async ({ request }) => {
588-
const controller = new AbortController()
589-
const timeout = setTimeout(() => controller.abort(), 5000) // 5 second timeout
590-
591-
// Create new request with abort signal
592-
const modifiedRequest = new Request(request, { signal: controller.signal })
593-
594-
// Store timeout ID for cleanup
595-
;(modifiedRequest as any).__timeoutId = timeout
588+
// Simple timeout wrapper
589+
async function getWithTimeout<T>(
590+
api: ReturnType<typeof createApi>,
591+
path: string,
592+
options: any = {},
593+
timeoutMs = 5000
594+
) {
595+
const controller = new AbortController()
596+
const timeout = setTimeout(() => controller.abort(), timeoutMs)
596597

597-
return modifiedRequest
598+
try {
599+
const result = await api.get(path, {
600+
...options,
601+
signal: controller.signal
602+
})
603+
clearTimeout(timeout)
604+
return result
605+
} catch (error) {
606+
clearTimeout(timeout)
607+
throw error
598608
}
599-
})
609+
}
600610

601611
// Usage
602612
try {
603-
const result = await api.get('getUsers', {})
613+
const result = await getWithTimeout(api, 'getUsers', {}, 5000)
614+
if (result.data) {
615+
console.log(result.data)
616+
}
617+
} catch (error) {
618+
if (error.name === 'AbortError') {
619+
console.error('Request timed out')
620+
} else {
621+
console.error('Request failed:', error)
622+
}
623+
}
624+
625+
// Or use signal directly
626+
const controller = new AbortController()
627+
const timeout = setTimeout(() => controller.abort(), 5000)
628+
629+
try {
630+
const result = await api.get('getUsers', {
631+
signal: controller.signal
632+
})
633+
clearTimeout(timeout)
634+
604635
if (result.data) {
605636
console.log(result.data)
606637
}
607638
} catch (error) {
639+
clearTimeout(timeout)
608640
if (error.name === 'AbortError') {
609641
console.error('Request timed out')
610642
}

0 commit comments

Comments
 (0)