Skip to main content

Hook Output

The HookOutput type defines the return type for BAML React hooks.

Usage​

function Component() {
const hook = useTestAws({ stream: true });

return (
<div>
{hook.error && <div>Error: {hook.error.message}</div>}
<button onClick={() => hook.mutate("test")} disabled={hook.isLoading}>
Submit
</button>
</div>
);
}

Type Definition​

type HookOutput<FunctionName, Options extends { stream?: boolean } = { stream?: true }> = {
data?: Options['stream'] extends false
? FinalDataType<FunctionName>
: FinalDataType<FunctionName> | StreamDataType<FunctionName>
finalData?: FinalDataType<FunctionName>
streamData?: Options['stream'] extends false ? never : StreamDataType<FunctionName>
error?: BamlErrors
isError: boolean
isLoading: boolean
isPending: boolean
isSuccess: boolean
isStreaming: Options['stream'] extends false ? never : boolean
status: HookStatus<Options>
mutate: (...args: Parameters<ServerAction>) => Options['stream'] extends false
? Promise<FinalDataType<FunctionName>>
: Promise<ReadableStream<Uint8Array>>
reset: () => void
}

type HookStatus<Options extends { stream?: boolean }> = Options['stream'] extends false
? 'idle' | 'pending' | 'success' | 'error'
: 'idle' | 'pending' | 'streaming' | 'success' | 'error'

Type Parameters​

ParameterTypeDescription
FunctionNamegenericBAML function name, used to infer types
Options{ stream?: boolean }Streaming behavior config. Defaults to { stream?: true }

Properties​

PropertyTypeDescription
dataFinalData | StreamData | undefinedCurrent response data
finalDataFinalData | undefinedFinal response when complete
streamDataStreamData | undefinedLatest streaming response (streaming only)
errorBamlErrors | undefinedError if request failed
isErrorbooleanRequest resulted in error
isLoadingbooleanRequest in progress (pending or streaming)
isPendingbooleanRequest pending (not yet streaming)
isSuccessbooleanRequest completed successfully
isStreamingbooleanCurrently streaming (streaming only)
statusHookStatusCurrent request state
mutate(...args) => PromiseExecute the BAML function
reset() => voidReset hook to initial state

Examples​

Streaming Result​

const streamingResult: HookOutput<'TestAws', { stream: true }> = {
data: "Any response",
finalData: "Final response",
streamData: "Streaming response...",
error: undefined,
isError: false,
isLoading: true,
isSuccess: false,
isStreaming: true,
isPending: false,
status: 'streaming',
mutate: async () => new ReadableStream(),
reset: () => {}
};

Non-Streaming Result​

const nonStreamingResult: HookOutput<'TestAws', { stream: false }> = {
data: "Final response",
finalData: "Final response",
// streamData and isStreaming not available
error: undefined,
isError: false,
isLoading: false,
isSuccess: true,
isPending: false,
status: 'success',
mutate: async () => "Final response",
reset: () => {}
};

See Also​