@tonightpass/react 0.0.182 → 0.0.184

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { SWRConfiguration, SWRResponse } from 'swr';
2
- import { Client, PathsFor, Endpoints, TonightPassAPIError, APIRequestOptions, Query } from 'tonightpass';
2
+ import { Client, PathsFor, Endpoints, TonightPassAPIError, APIRequestOptions, Query, WebSocketPaths, WebSocketEndpoints } from 'tonightpass';
3
3
  import { SWRInfiniteConfiguration, SWRInfiniteResponse } from 'swr/infinite';
4
4
 
5
5
  declare const client: Client;
6
6
  type AnyEndpoint = Endpoints extends infer T ? T : never;
7
- type ForceAccept<T> = T extends never ? any : T;
8
- type ResponseType<Path extends PathsFor<"GET">> = ForceAccept<Extract<AnyEndpoint, {
7
+ type ForceAccept$1<T> = T extends never ? any : T;
8
+ type ResponseType<Path extends PathsFor<"GET">> = ForceAccept$1<Extract<AnyEndpoint, {
9
9
  path: Path;
10
10
  method: "GET";
11
11
  }>["res"]>;
@@ -20,4 +20,21 @@ interface UseAPIInfiniteConfig<Path extends PathsFor<"GET">> extends SWRInfinite
20
20
  }
21
21
  declare function useAPIInfinite<Path extends PathsFor<"GET">>(getKey: (pageIndex: number, previousPageData: ResponseType<Path> | null) => [Path, Query<Path> | undefined] | null, config?: UseAPIInfiniteConfig<Path>): SWRInfiniteResponse<ResponseType<Path>, ErrorType<Path>>;
22
22
 
23
- export { type ErrorType, type ResponseType, type UseAPIConfig, type UseAPIInfiniteConfig, client, useAPI, useAPIInfinite };
23
+ type AnyWebSocketEndpoint = WebSocketEndpoints extends infer T ? T : never;
24
+ type ForceAccept<T> = T extends never ? unknown : T;
25
+ type WebSocketOptionsType<Path extends WebSocketPaths> = ForceAccept<Extract<AnyWebSocketEndpoint, {
26
+ path: Path;
27
+ }>["options"]>;
28
+ interface UseWebSocketOptions {
29
+ maxReconnectAttempts?: number;
30
+ reconnectInterval?: number;
31
+ }
32
+ declare function useWebSocket<Path extends WebSocketPaths>(path: Path | null | undefined, options?: WebSocketOptionsType<Path>, config?: UseWebSocketOptions): {
33
+ data: any;
34
+ error: any;
35
+ isConnected: boolean;
36
+ isDisconnected: boolean;
37
+ message: any;
38
+ };
39
+
40
+ export { type ErrorType, type ResponseType, type UseAPIConfig, type UseAPIInfiniteConfig, type UseWebSocketOptions, type WebSocketOptionsType, client, useAPI, useAPIInfinite, useWebSocket };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { SWRConfiguration, SWRResponse } from 'swr';
2
- import { Client, PathsFor, Endpoints, TonightPassAPIError, APIRequestOptions, Query } from 'tonightpass';
2
+ import { Client, PathsFor, Endpoints, TonightPassAPIError, APIRequestOptions, Query, WebSocketPaths, WebSocketEndpoints } from 'tonightpass';
3
3
  import { SWRInfiniteConfiguration, SWRInfiniteResponse } from 'swr/infinite';
4
4
 
5
5
  declare const client: Client;
6
6
  type AnyEndpoint = Endpoints extends infer T ? T : never;
7
- type ForceAccept<T> = T extends never ? any : T;
8
- type ResponseType<Path extends PathsFor<"GET">> = ForceAccept<Extract<AnyEndpoint, {
7
+ type ForceAccept$1<T> = T extends never ? any : T;
8
+ type ResponseType<Path extends PathsFor<"GET">> = ForceAccept$1<Extract<AnyEndpoint, {
9
9
  path: Path;
10
10
  method: "GET";
11
11
  }>["res"]>;
@@ -20,4 +20,21 @@ interface UseAPIInfiniteConfig<Path extends PathsFor<"GET">> extends SWRInfinite
20
20
  }
21
21
  declare function useAPIInfinite<Path extends PathsFor<"GET">>(getKey: (pageIndex: number, previousPageData: ResponseType<Path> | null) => [Path, Query<Path> | undefined] | null, config?: UseAPIInfiniteConfig<Path>): SWRInfiniteResponse<ResponseType<Path>, ErrorType<Path>>;
22
22
 
23
- export { type ErrorType, type ResponseType, type UseAPIConfig, type UseAPIInfiniteConfig, client, useAPI, useAPIInfinite };
23
+ type AnyWebSocketEndpoint = WebSocketEndpoints extends infer T ? T : never;
24
+ type ForceAccept<T> = T extends never ? unknown : T;
25
+ type WebSocketOptionsType<Path extends WebSocketPaths> = ForceAccept<Extract<AnyWebSocketEndpoint, {
26
+ path: Path;
27
+ }>["options"]>;
28
+ interface UseWebSocketOptions {
29
+ maxReconnectAttempts?: number;
30
+ reconnectInterval?: number;
31
+ }
32
+ declare function useWebSocket<Path extends WebSocketPaths>(path: Path | null | undefined, options?: WebSocketOptionsType<Path>, config?: UseWebSocketOptions): {
33
+ data: any;
34
+ error: any;
35
+ isConnected: boolean;
36
+ isDisconnected: boolean;
37
+ message: any;
38
+ };
39
+
40
+ export { type ErrorType, type ResponseType, type UseAPIConfig, type UseAPIInfiniteConfig, type UseWebSocketOptions, type WebSocketOptionsType, client, useAPI, useAPIInfinite, useWebSocket };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- 'use strict';var P=require('swr'),tonightpass=require('tonightpass'),f=require('swr/infinite');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var P__default=/*#__PURE__*/_interopDefault(P);var f__default=/*#__PURE__*/_interopDefault(f);var r=new tonightpass.Client({baseURL:tonightpass.DEFAULT_API_URL});function m(e,t,n){let{requestOptions:s,...p}=n||{};return P__default.default(e?[e,t]:null,async([o,i])=>await r.get(o,i,s),p)}function w(e,t){let{requestOptions:n,...s}=t||{};return f__default.default(e,async([a,o])=>await r.get(a,o,n),s)}exports.client=r;exports.useAPI=m;exports.useAPIInfinite=w;//# sourceMappingURL=index.js.map
1
+ 'use strict';var d=require('swr'),tonightpass=require('tonightpass'),l=require('swr/infinite'),R=require('swr/subscription');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var d__default=/*#__PURE__*/_interopDefault(d);var l__default=/*#__PURE__*/_interopDefault(l);var R__default=/*#__PURE__*/_interopDefault(R);var u=new tonightpass.Client({baseURL:tonightpass.DEFAULT_API_URL});function w(t,a,s){let{requestOptions:e,...i}=s||{};return d__default.default(t?[t,a]:null,async([p,n])=>await u.get(p,n,e),i)}function D(t,a){let{requestOptions:s,...e}=a||{};return l__default.default(t,async([P,p])=>await u.get(P,p,s),e)}function J(t,a,s){let{data:e,error:i}=R__default.default(t?[t,a]:null,([P,p],{next:n})=>{let c=new tonightpass.WebSocketClient({baseURL:tonightpass.DEFAULT_API_URL.replace(/^http/,"ws"),maxReconnectAttempts:s?.maxReconnectAttempts??3,reconnectInterval:s?.reconnectInterval??1e3}),r=false,h=async()=>{try{await c.connect(P,p??{}),r=!0;}catch(o){n(o instanceof Error?o:new Error("Connection failed"));}};c.on("*",o=>{n(null,{type:"message",data:o});});let f=setInterval(()=>{let o=c.connected;o&&!r?(r=true,n(null,{type:"connected"})):!o&&r&&(r=false,n(null,{type:"disconnected"}));},100);return h(),()=>{clearInterval(f),r&&c.disconnect();}});return {data:e,error:i,isConnected:e?.type==="connected",isDisconnected:e?.type==="disconnected",message:e?.type==="message"?e.data:null}}exports.client=u;exports.useAPI=w;exports.useAPIInfinite=D;exports.useWebSocket=J;//# sourceMappingURL=index.js.map
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hooks/useAPI.ts","../src/hooks/useAPIInfinite.ts"],"names":["client","Client","DEFAULT_API_URL","useAPI","path","query","config","requestOptions","swrConfig","useSWR","fetchPath","fetchQuery","useAPIInfinite","getKey","useSWRInfinite"],"mappings":"8PAWO,IAAMA,EAAS,IAAIC,kBAAAA,CAAO,CAAE,OAASC,CAAAA,2BAAgB,CAAC,EAmBtD,SAASC,EACdC,CACAC,CAAAA,CAAAA,CACAC,EACkD,CAClD,GAAM,CAAE,cAAAC,CAAAA,CAAAA,CAAgB,GAAGC,CAAU,CAAA,CAAIF,GAAU,EAAC,CAUpD,OAAOG,kBAILL,CAAAA,CAAAA,CAAO,CAACA,CAAMC,CAAAA,CAAK,EAAI,IAZT,CAAA,MAAO,CAACK,CAAWC,CAAAA,CAAU,CAI1B,GAAA,MAAMX,EAAO,GAAIU,CAAAA,CAAAA,CAAWC,EAAYJ,CAAc,CAAA,CAQjCC,CAAS,CACnD,CCrCO,SAASI,CACdC,CAAAA,CAAAA,CAIAP,EAC0D,CAC1D,GAAM,CAAE,cAAAC,CAAAA,CAAAA,CAAgB,GAAGC,CAAU,CAAA,CAAIF,GAAU,EAAC,CAUpD,OAAOQ,kBAAeD,CAAAA,CAAAA,CARN,MAAO,CAACH,CAAAA,CAAWC,CAAU,CAI1B,GAAA,MAAMX,EAAO,GAAIU,CAAAA,CAAAA,CAAWC,EAAYJ,CAAc,CAAA,CAIlCC,CAAS,CAClD","file":"index.js","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 \"./useAPI\";\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"]}
1
+ {"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":"2UAWaA,IAAAA,CAAAA,CAAS,IAAIC,kBAAAA,CAAO,CAAE,OAASC,CAAAA,2BAAgB,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,kBAILL,CAAAA,CAAAA,CAAO,CAACA,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CAAK,CACZJ,CAAAA,CAAKI,CAAe,YAAA,KAAA,CAAQA,CAAM,CAAA,IAAI,KAAM,CAAA,mBAAmB,CAAC,EAClE,CACF,CAAA,CAEA1B,EAAO,EAAG,CAAA,GAAA,CAAM2B,CAAmB,EAAA,CACjCL,CAAK,CAAA,IAAA,CAAM,CAAE,IAAA,CAAM,UAAW,IAAMK,CAAAA,CAAM,CAAC,EAC7C,CAAC,CAAA,CAcD,IAAMC,CAAAA,CAAqB,YAXH,IAAM,CAC5B,IAAMC,CAAAA,CAAY7B,CAAO,CAAA,SAAA,CACrB6B,CAAa,EAAA,CAACL,CAChBA,EAAAA,CAAAA,CAAc,IACdF,CAAAA,CAAAA,CAAK,IAAM,CAAA,CAAE,IAAM,CAAA,WAAY,CAAC,CACvB,EAAA,CAACO,CAAaL,EAAAA,CAAAA,GACvBA,CAAc,CAAA,KAAA,CACdF,CAAK,CAAA,IAAA,CAAM,CAAE,IAAA,CAAM,cAAe,CAAC,CAEvC,EAAA,CAAA,CAEwD,GAAG,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.js","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"]}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import P from'swr';import {Client,DEFAULT_API_URL}from'tonightpass';import f from'swr/infinite';var r=new Client({baseURL:DEFAULT_API_URL});function m(e,t,n){let{requestOptions:s,...p}=n||{};return P(e?[e,t]:null,async([o,i])=>await r.get(o,i,s),p)}function w(e,t){let{requestOptions:n,...s}=t||{};return f(e,async([a,o])=>await r.get(a,o,n),s)}export{r as client,m as useAPI,w as useAPIInfinite};//# sourceMappingURL=index.mjs.map
1
+ import d from'swr';import {Client,DEFAULT_API_URL,WebSocketClient}from'tonightpass';import l from'swr/infinite';import R from'swr/subscription';var u=new Client({baseURL:DEFAULT_API_URL});function w(t,a,s){let{requestOptions:e,...i}=s||{};return d(t?[t,a]:null,async([p,n])=>await u.get(p,n,e),i)}function D(t,a){let{requestOptions:s,...e}=a||{};return l(t,async([P,p])=>await u.get(P,p,s),e)}function J(t,a,s){let{data:e,error:i}=R(t?[t,a]:null,([P,p],{next:n})=>{let c=new WebSocketClient({baseURL:DEFAULT_API_URL.replace(/^http/,"ws"),maxReconnectAttempts:s?.maxReconnectAttempts??3,reconnectInterval:s?.reconnectInterval??1e3}),r=false,h=async()=>{try{await c.connect(P,p??{}),r=!0;}catch(o){n(o instanceof Error?o:new Error("Connection failed"));}};c.on("*",o=>{n(null,{type:"message",data:o});});let f=setInterval(()=>{let o=c.connected;o&&!r?(r=true,n(null,{type:"connected"})):!o&&r&&(r=false,n(null,{type:"disconnected"}));},100);return h(),()=>{clearInterval(f),r&&c.disconnect();}});return {data:e,error:i,isConnected:e?.type==="connected",isDisconnected:e?.type==="disconnected",message:e?.type==="message"?e.data:null}}export{u as client,w as useAPI,D as useAPIInfinite,J as useWebSocket};//# sourceMappingURL=index.mjs.map
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hooks/useAPI.ts","../src/hooks/useAPIInfinite.ts"],"names":["client","Client","DEFAULT_API_URL","useAPI","path","query","config","requestOptions","swrConfig","useSWR","fetchPath","fetchQuery","useAPIInfinite","getKey","useSWRInfinite"],"mappings":"gGAWO,IAAMA,EAAS,IAAIC,MAAAA,CAAO,CAAE,OAASC,CAAAA,eAAgB,CAAC,EAmBtD,SAASC,EACdC,CACAC,CAAAA,CAAAA,CACAC,EACkD,CAClD,GAAM,CAAE,cAAAC,CAAAA,CAAAA,CAAgB,GAAGC,CAAU,CAAA,CAAIF,GAAU,EAAC,CAUpD,OAAOG,CAILL,CAAAA,CAAAA,CAAO,CAACA,CAAMC,CAAAA,CAAK,EAAI,IAZT,CAAA,MAAO,CAACK,CAAWC,CAAAA,CAAU,CAI1B,GAAA,MAAMX,EAAO,GAAIU,CAAAA,CAAAA,CAAWC,EAAYJ,CAAc,CAAA,CAQjCC,CAAS,CACnD,CCrCO,SAASI,CACdC,CAAAA,CAAAA,CAIAP,EAC0D,CAC1D,GAAM,CAAE,cAAAC,CAAAA,CAAAA,CAAgB,GAAGC,CAAU,CAAA,CAAIF,GAAU,EAAC,CAUpD,OAAOQ,CAAeD,CAAAA,CAAAA,CARN,MAAO,CAACH,CAAAA,CAAWC,CAAU,CAI1B,GAAA,MAAMX,EAAO,GAAIU,CAAAA,CAAAA,CAAWC,EAAYJ,CAAc,CAAA,CAIlCC,CAAS,CAClD","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 \"./useAPI\";\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"]}
1
+ {"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,CAAAC,CAAM,CAAA,CAAIC,EACtBf,CAAO,CAAA,CAACA,CAAMY,CAAAA,CAAO,CAAI,CAAA,IAAA,CACzB,CAAC,CAACI,EAAQC,CA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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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tonightpass/react",
3
- "version": "0.0.182",
3
+ "version": "0.0.184",
4
4
  "description": "@tonightpass react sdk.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,7 +28,7 @@
28
28
  "types": "dist/index.d.ts",
29
29
  "dependencies": {
30
30
  "swr": "^2.2.5",
31
- "tonightpass": "^0.0.178"
31
+ "tonightpass": "^0.0.180"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "react": "*"