WeldResponse
Every request method returns a WeldResponse<T> object with three properties.
interface WeldResponse<T> { signal: WeldSignalState<T> promise: Promise<T> abort: () => void}signal
Section titled “signal”A reactive state container powered by @preact/signals-core. Use this with framework adapters or subscribe directly in vanilla JS.
interface WeldSignalState<T> { data: Signal<T | null> status: Signal<'idle' | 'loading' | 'success' | 'error'> error: Signal<Error | null>}Subscribing directly
Section titled “Subscribing directly”const { signal } = api.get('v1/products', Schema)
// Subscribe to any signal fieldconst unsubscribe = signal.data.subscribe((data) => { console.log('data changed:', data)})
// Unsubscribe when doneunsubscribe()Reading current value
Section titled “Reading current value”const { signal } = api.get('v1/products', Schema)console.log(signal.status.value) // 'loading'
// After promise resolves:console.log(signal.status.value) // 'success'console.log(signal.data.value) // Product[]promise
Section titled “promise”A standard Promise that resolves with the validated data or rejects with an error.
const { promise } = api.get('v1/products', Schema)
const products = await promise
// .then/.catchpromise .then((products) => console.log(products)) .catch((err) => console.error(err))Cancels the in-flight request. The promise will reject with an AbortError.
const { promise, abort } = api.get('v1/products', Schema)
// Cancel immediatelyabort()
try { await promise} catch (err) { console.log(err.name) // 'AbortError'}Status lifecycle
Section titled “Status lifecycle”idle → loading → success → error| Status | Meaning |
|---|---|
idle |
Initial state before any request |
loading |
Request is in-flight |
success |
Request completed and data is validated |
error |
Request failed or validation failed |
