UNPKG

eden-swr

Version:

React/NextJS SWR hooks for Elysia applications with fully typed Eden

58 lines 2.4 kB
import type { Elysia } from "elysia"; import type { SWRResponse, SWRConfiguration } from "swr"; import type { IsNever, IsUnknown, TreatyToPath } from "./types"; /** * Extract response type from an endpoint schema */ export type ExtractedResponse<Endpoints, P extends keyof Endpoints> = Endpoints[P] extends { get: { response: { 200: infer R; }; }; } ? R : never; /** * Extract options type from an endpoint schema */ export type OptionsForEndpoint<Endpoints, P extends keyof Endpoints> = Endpoints[P] extends { get: { params: infer Params; query: infer Query; headers: infer Headers; body: infer Body; }; } ? Omit<RequestInit, "body" | "method" | "headers"> & { method?: "GET"; } & (IsNever<keyof Params> extends true ? { params?: Record<never, string>; } : { params: Params; }) & (IsNever<keyof Query> extends true ? { query?: Record<never, string>; } : { query: Query; }) & (undefined extends Headers ? { headers?: Record<string, string>; } : { headers: Headers; }) & (IsUnknown<Body> extends false ? { body: Body; } : { body?: unknown; }) : any; /** * Create a typed SWR hook for Elysia applications * * @param fetch - The edenFetch function created for your Elysia app * @param specialPathHandlers - Optional custom handlers for specific paths (e.g. "/index" → "") * @returns A typed useEdenSWR hook with overloads for known endpoints and fallback for arbitrary endpoints */ export declare function createUseEdenSWR<App extends Elysia<any, any, any, any, any, any, any>>(fetch: any, specialPathHandlers?: Record<string, (path: string) => string>): <P extends [keyof TreatyToPath<App["_routes"]>] extends [never] ? string : keyof TreatyToPath<App["_routes"]>>(path: P, options?: P extends keyof TreatyToPath<App["_routes"]> ? OptionsForEndpoint<TreatyToPath<App["_routes"]>, P> : any, swrOptions?: SWRConfiguration & { cacheKey?: string; shouldFetch?: boolean; }) => SWRResponse<P extends keyof TreatyToPath<App["_routes"]> ? [ExtractedResponse<TreatyToPath<App["_routes"]>, P>] extends [never] ? any : ExtractedResponse<TreatyToPath<App["_routes"]>, P> : any, any>; /** * Default special path handlers with common transformations */ export declare const defaultSpecialPathHandlers: Record<string, (path: string) => string>; //# sourceMappingURL=index.d.ts.map