UseKy
guarahooks/use-kyFreeHook
A customizable hook for making HTTP requests using Ky
Install it
npx shadcn@latest add https://guarahooks.com/r/use-ky.jsonSource
1'use client';23import {4 createContext,5 useCallback,6 useContext,7 useEffect,8 useRef,9 useState,10 type ReactNode,11} from 'react';1213import ky, {14 HTTPError,15 KyInstance,16 KyResponse,17 Options,18 TimeoutError,19} from 'ky';2021// #region Types & Interfaces2223export interface KyPlugin {24 beforeRequest?: (url: string, options: Options) => void | Promise<void>;25 afterResponse?: (response: KyResponse) => void | Promise<void>;26 onError?: (error: HTTPError | TimeoutError | Error) => void | Promise<void>;27}2829export interface KyConfig extends Options {30 retries?: number;31 timeout?: number;32}3334export interface KyOptions extends Options {35 retries?: number;36 timeout?: number;37 immediate?: boolean;38 plugins?: KyPlugin[];39}4041export interface KyResult<T> {42 data: T | null;43 error: HTTPError | TimeoutError | Error | null;44 loading: boolean;45 refetch: () => Promise<T | null>;46 abort: () => void;47 aborted: boolean;48}4950export interface KyContextValue {51 config: KyConfig;52 updateConfig: (newConfig: Partial<KyConfig>) => void;53 instance: KyInstance;54 request: (url: string, options?: Options) => Promise<KyResponse>;55}5657export interface KyProviderProps {58 config?: KyConfig;59 children: ReactNode;60}6162// #endregion6364// #region Context6566const KyContext = createContext<KyContextValue | null>(null);6768export function KyProvider({ config = {}, children }: KyProviderProps) {69 const [currentConfig, setCurrentConfig] = useState<KyConfig>(config);70 const instanceRef = useRef<KyInstance | null>(ky.create(config));7172 const updateConfig = useCallback((newConfig: Partial<KyConfig>) => {73 setCurrentConfig((prev) => ({ ...prev, ...newConfig }));74 }, []);7576 // Create or update ky instance when config changes77 useEffect(() => {78 instanceRef.current = ky.create(currentConfig);79 }, [currentConfig]);8081 const enhancedRequest = useCallback(82 async (url: string, options: Options = {}): Promise<KyResponse> => {83 if (!instanceRef.current) {84 throw new Error('Ky instance not initialized');85 }8687 return instanceRef.current(url, options);88 },89 [],90 );9192 const contextValue: KyContextValue = {93 config: currentConfig,94 updateConfig,95 instance: instanceRef.current!,96 request: enhancedRequest,97 };9899 return (100 <KyContext.Provider value={contextValue}>{children}</KyContext.Provider>101 );102}103104export function useKyContext(): KyContextValue {105 const context = useContext(KyContext);106 if (!context) {107// … truncated
Files it writes
More from guarahooks
All 100 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| UseAbortControlleruse-abort-controller | guarahooks | Hooks | Free | no deps | |
| UseArrayStateuse-array-state | guarahooks | Hooks | Free | no deps | |
| UseAxiosuse-axios | guarahooks | Hooks | Free | no deps | |
| UseBatteryStatususe-battery-status | guarahooks | Hooks | Free | no deps | |
| UseBetterAuthuse-better-auth | guarahooks | Hooks | Free | no deps | |
| UseClickOutsideuse-click-outside | guarahooks | Hooks | Free | no deps | |
| UseConfirmuse-confirm | guarahooks | Hooks | Free | no deps | |
| UseCookieuse-cookie | guarahooks | Hooks | Free | no deps |
