stream-hooks
Version:
<div align="center"> <a aria-label="NPM version" href="https://twitter.com/dimitrikennedy"> <img alt="stream-hooks" src="https://img.shields.io/twitter/follow/dimitrikennedy?style=social&labelColor=000000"> </a> <a aria-label="GH Issues" href="h
35 lines (32 loc) • 1.09 kB
TypeScript
import { UseJsonStreamProps, StartStream, StopStream } from '../types/index.js';
import z from 'zod';
/**
* `useJsonStream` is a custom React hook that extends the `useStream` hook to add JSON parsing functionality.
* It uses the `SchemaStream` to parse the incoming stream into JSON.
*
* @param {UseJsonStreamProps} props - The props for the hook include optional callback
* functions that will be invoked at different stages of the stream lifecycle, and a schema for the JSON data.
*
* @returns {
* startStream: StartStream,
* stopStream: StopStream,
* data: Partial<z.infer<T>>,
* loading: boolean
* }
*
* @example
* ```
* const {
* loading,
* startStream,
* stopStream,
* } = useJsonStream({ onBeforeStart: ..., onReceive: ..., onStop: ..., schema: ... });
* ```
*/
declare function useJsonStream<T extends z.AnyZodObject>({ onReceive, onEnd, schema, onBeforeStart, onStop, defaultData }: UseJsonStreamProps<T>): {
startStream: StartStream;
stopStream: StopStream;
data: Partial<z.infer<T>>;
loading: boolean;
};
export { useJsonStream };