@tonightpass/react
Version:
@tonightpass react sdk.
1 lines • 7.69 kB
Source Map (JSON)
{"version":3,"sources":["../src/hooks/use-api.ts","../src/hooks/use-api-infinite.ts","../src/hooks/use-web-socket.ts"],"names":["client","Client","DEFAULT_API_URL","useAPI","path","query","config","requestOptions","swrConfig","useSWR","fetchPath","fetchQuery","useAPIInfinite","getKey","useSWRInfinite","useWebSocket","options","data","error","useSWRSubscription","wsPath","wsOptions","next","WebSocketClient","isConnected","connect","err","event","connectionInterval","connected"],"mappings":"gJAWaA,IAAAA,CAAAA,CAAS,IAAIC,MAAAA,CAAO,CAAE,OAASC,CAAAA,eAAgB,CAAC,EAmBtD,SAASC,CAAAA,CACdC,CACAC,CAAAA,CAAAA,CACAC,CACkD,CAAA,CAClD,GAAM,CAAE,cAAAC,CAAAA,CAAAA,CAAgB,GAAGC,CAAU,EAAIF,CAAU,EAAA,EAUnD,CAAA,OAAOG,CAILL,CAAAA,CAAAA,CAAO,CAACA,CAAAA,CAAMC,CAAK,CAAA,CAAI,IAZT,CAAA,MAAO,CAACK,CAAAA,CAAWC,CAAU,CAAA,GAI1B,MAAMX,CAAO,CAAA,GAAA,CAAIU,CAAWC,CAAAA,CAAAA,CAAYJ,CAAc,CAAA,CAQjCC,CAAS,CACnD,CCrCO,SAASI,CAAAA,CACdC,CAIAP,CAAAA,CAAAA,CAC0D,CAC1D,GAAM,CAAE,cAAAC,CAAAA,CAAAA,CAAgB,GAAGC,CAAU,CAAIF,CAAAA,CAAAA,EAAU,EAAC,CAUpD,OAAOQ,CAAAA,CAAeD,CARN,CAAA,MAAO,CAACH,CAAAA,CAAWC,CAAU,CAI1B,GAAA,MAAMX,CAAO,CAAA,GAAA,CAAIU,CAAWC,CAAAA,CAAAA,CAAYJ,CAAc,CAAA,CAIlCC,CAAS,CAClD,CCVO,SAASO,CACdX,CAAAA,CAAAA,CACAY,CACAV,CAAAA,CAAAA,CACA,CACA,GAAM,CAAE,IAAA,CAAAW,CAAM,CAAA,KAAA,CAA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CAAA,CAE3D,OAAAG,CAAQ,EAAA,CAED,IAAM,CACX,aAAcG,CAAAA,CAAkB,CAC5BJ,CAAAA,CAAAA,EACFxB,CAAO,CAAA,UAAA,GAEX,CACF,CACF,CAAA,CAEA,OAAO,CACL,KAAAiB,CACA,CAAA,KAAA,CAAAC,CACA,CAAA,WAAA,CAAaD,CAAM,EAAA,IAAA,GAAS,WAC5B,CAAA,cAAA,CAAgBA,GAAM,IAAS,GAAA,cAAA,CAC/B,OAASA,CAAAA,CAAAA,EAAM,IAAS,GAAA,SAAA,CAAYA,CAAK,CAAA,IAAA,CAAO,IAClD,CACF","file":"index.mjs","sourcesContent":["import useSWR, { SWRConfiguration, SWRResponse } from \"swr\";\nimport {\n Endpoints,\n APIRequestOptions,\n Client,\n PathsFor,\n DEFAULT_API_URL,\n TonightPassAPIError,\n Query,\n} from \"tonightpass\";\n\nexport const client = new Client({ baseURL: DEFAULT_API_URL });\n\ntype AnyEndpoint = Endpoints extends infer T ? T : never;\n\ntype ForceAccept<T> = T extends never ? any : T;\n\nexport type ResponseType<Path extends PathsFor<\"GET\">> = ForceAccept<\n Extract<AnyEndpoint, { path: Path; method: \"GET\" }>[\"res\"]\n>;\n\nexport type ErrorType<Path extends PathsFor<\"GET\">> = TonightPassAPIError<\n ResponseType<Path>\n>;\n\nexport interface UseAPIConfig<Path extends PathsFor<\"GET\">>\n extends SWRConfiguration<ResponseType<Path>, ErrorType<Path>> {\n requestOptions?: APIRequestOptions;\n}\n\nexport function useAPI<Path extends PathsFor<\"GET\">>(\n path: Path | null | undefined,\n query?: Query<Path>,\n config?: UseAPIConfig<Path>,\n): SWRResponse<ResponseType<Path>, ErrorType<Path>> {\n const { requestOptions, ...swrConfig } = config || {};\n\n const fetcher = async ([fetchPath, fetchQuery]: [\n Path,\n Query<Path> | undefined,\n ]) => {\n const response = await client.get(fetchPath, fetchQuery, requestOptions);\n return response as unknown as ResponseType<Path>;\n };\n\n return useSWR<\n ResponseType<Path>,\n ErrorType<Path>,\n [Path, Query<Path> | undefined] | null\n >(path ? [path, query] : null, fetcher, swrConfig);\n}\n","import useSWRInfinite, {\n SWRInfiniteConfiguration,\n SWRInfiniteResponse,\n} from \"swr/infinite\";\nimport { APIRequestOptions, PathsFor, Query } from \"tonightpass\";\n\nimport { client, ResponseType, ErrorType } from \"./use-api\";\n\nexport interface UseAPIInfiniteConfig<Path extends PathsFor<\"GET\">>\n extends SWRInfiniteConfiguration<ResponseType<Path>, ErrorType<Path>> {\n requestOptions?: APIRequestOptions;\n}\n\nexport function useAPIInfinite<Path extends PathsFor<\"GET\">>(\n getKey: (\n pageIndex: number,\n previousPageData: ResponseType<Path> | null,\n ) => [Path, Query<Path> | undefined] | null,\n config?: UseAPIInfiniteConfig<Path>,\n): SWRInfiniteResponse<ResponseType<Path>, ErrorType<Path>> {\n const { requestOptions, ...swrConfig } = config || {};\n\n const fetcher = async ([fetchPath, fetchQuery]: [\n Path,\n Query<Path> | undefined,\n ]) => {\n const response = await client.get(fetchPath, fetchQuery, requestOptions);\n return response as unknown as ResponseType<Path>;\n };\n\n return useSWRInfinite(getKey, fetcher, swrConfig);\n}\n","import useSWRSubscription from \"swr/subscription\";\nimport {\n WebSocketClient,\n WebSocketPaths,\n DEFAULT_API_URL,\n WebSocketEndpoints,\n} from \"tonightpass\";\n\ntype AnyWebSocketEndpoint = WebSocketEndpoints extends infer T ? T : never;\n\ntype ForceAccept<T> = T extends never ? unknown : T;\n\nexport type WebSocketOptionsType<Path extends WebSocketPaths> = ForceAccept<\n Extract<AnyWebSocketEndpoint, { path: Path }>[\"options\"]\n>;\n\nexport interface UseWebSocketOptions {\n maxReconnectAttempts?: number;\n reconnectInterval?: number;\n}\n\nexport function useWebSocket<Path extends WebSocketPaths>(\n path: Path | null | undefined,\n options?: WebSocketOptionsType<Path>,\n config?: UseWebSocketOptions,\n) {\n const { data, error } = useSWRSubscription(\n path ? [path, options] : null,\n ([wsPath, wsOptions], { next }) => {\n const client = new WebSocketClient({\n baseURL: DEFAULT_API_URL.replace(/^http/, \"ws\"),\n maxReconnectAttempts: config?.maxReconnectAttempts ?? 3,\n reconnectInterval: config?.reconnectInterval ?? 1000,\n });\n\n let isConnected = false;\n\n const connect = async () => {\n try {\n await client.connect(wsPath, wsOptions ?? ({} as any));\n isConnected = true;\n } catch (err) {\n next(err instanceof Error ? err : new Error(\"Connection failed\"));\n }\n };\n\n client.on(\"*\", (event: unknown) => {\n next(null, { type: \"message\", data: event });\n });\n\n // Monitor connection state changes\n const checkConnection = () => {\n const connected = client.connected;\n if (connected && !isConnected) {\n isConnected = true;\n next(null, { type: \"connected\" });\n } else if (!connected && isConnected) {\n isConnected = false;\n next(null, { type: \"disconnected\" });\n }\n };\n\n const connectionInterval = setInterval(checkConnection, 100);\n\n connect();\n\n return () => {\n clearInterval(connectionInterval);\n if (isConnected) {\n client.disconnect();\n }\n };\n },\n );\n\n return {\n data,\n error,\n isConnected: data?.type === \"connected\",\n isDisconnected: data?.type === \"disconnected\",\n message: data?.type === \"message\" ? data.data : null,\n };\n}\n"]}