@trakit/react 0.0.11 → 0.0.12

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.
@@ -1,16 +1,20 @@
1
1
  import { guid, Machine, nothing, url, User } from '@trakit/objects';
2
- import { TrakitSyncCommander } from '@trakit/sync';
2
+ import { TrakitAuditCommander, TrakitSyncCommander } from '@trakit/sync';
3
3
  import { ReactNode } from 'react';
4
4
  /**
5
5
  * The result of the `useConnection` hook, providing the synchronizer instance and connection state.
6
6
  */
7
- export type ConnectionContextType = {
7
+ export type ApiContextType = {
8
8
  /**
9
9
  * The synchronizer instance used for commands.
10
10
  */
11
11
  readonly synchronizer: TrakitSyncCommander;
12
12
  /**
13
- * Indicates whether the connection is ready for use.
13
+ * The auditor instance used for history.
14
+ */
15
+ readonly auditor: TrakitAuditCommander;
16
+ /**
17
+ * Indicates whether the APIs are ready for use.
14
18
  * Will be `true` if there is {@link User|user session} or {@link Machine|API credentials}
15
19
  * available and the underlying WebSocket connection has been established,
16
20
  * or if there is no authentication details given.
@@ -35,30 +39,31 @@ export type ConnectionContextType = {
35
39
  };
36
40
  /**
37
41
  * Provider component that wraps the application with CookiesProvider.
38
- * Required for the `useConnection` hook to work properly.
42
+ * Required for the `useApi` hook to work properly.
39
43
  *
40
44
  * @example
41
45
  * ```tsx
42
- * import { ConnectionProvider, useConnection } from '@trakit/react';
46
+ * import { ApiProvider, useApi } from '@trakit/react';
43
47
  *
44
48
  * function App() {
45
49
  * return (
46
- * <ConnectionProvider>
50
+ * <ApiProvider>
47
51
  * <YourComponent />
48
- * </ConnectionProvider>
52
+ * </ApiProvider>
49
53
  * );
50
54
  * }
51
55
  * ```
52
56
  */
53
- export declare function ConnectionProvider({ children, restAddress, socketAddress }: {
57
+ export declare function ApiProvider({ children, restAddress, socketAddress, auditAddress, }: {
54
58
  children: ReactNode;
55
59
  restAddress?: URL | url | nothing;
56
60
  socketAddress?: URL | url | nothing;
61
+ auditAddress?: URL | url | nothing;
57
62
  }): import("react").JSX.Element;
58
63
  /**
59
- * A React hook that manages the connection to the Trak-iT synchronization service.
64
+ * A React hook that manages the connection to the Trak-iT synchronization service and APIs.
60
65
  * It initializes the synchronizer, listens for connection and account events,
61
- * and provides the current connection state and user/machine information.
66
+ * and provides the current connection state, API instances, and user/machine information.
62
67
  */
63
- export declare function useConnection(): ConnectionContextType;
64
- //# sourceMappingURL=connectionContext.d.ts.map
68
+ export declare function useApi(): ApiContextType;
69
+ //# sourceMappingURL=apiContext.d.ts.map
package/index.d.ts CHANGED
@@ -4,20 +4,19 @@
4
4
  * Last updated on Thu June 11 2026 15:43:01
5
5
  * @copyright Trak-iT Wireless Inc. 2026
6
6
  */
7
- import { ConnectionProvider, useConnection, type ConnectionContextType } from "./context/connectionContext";
8
- import { useAudit } from "./hooks/useAudit";
7
+ import { ApiProvider, useApi, type ApiContextType } from "./context/apiContext";
9
8
  import useIsOnline from "./hooks/useIsOnline";
10
9
  import { useSingle, useSync, UseSyncMultiple, UseSyncResult, UseSyncSingle } from "./hooks/useSync";
11
10
  /**
12
11
  * Version number for this release.
13
12
  */
14
- export declare const version = "0.0.11";
13
+ export declare const version = "0.0.12";
15
14
  /**
16
15
  * Hooks
17
16
  */
18
- export { useAudit, useConnection, useIsOnline, useSingle, useSync, type UseSyncMultiple, type UseSyncResult, type UseSyncSingle };
17
+ export { useApi, useIsOnline, useSingle, useSync, type UseSyncMultiple, type UseSyncResult, type UseSyncSingle };
19
18
  /**
20
- * Context provider for connection state and synchronizer instance.
19
+ * Context provider for connection state and API instances.
21
20
  */
22
- export { ConnectionProvider, type ConnectionContextType };
21
+ export { ApiProvider, type ApiContextType };
23
22
  //# sourceMappingURL=index.d.ts.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@trakit/react",
3
3
  "type": "module",
4
- "version": "0.0.11",
4
+ "version": "0.0.12",
5
5
  "main": "./trakit-react.min.js",
6
6
  "types": "./index.d.ts",
7
7
  "scripts": {
@@ -35,7 +35,7 @@
35
35
  "vitest": "^4.0.7"
36
36
  },
37
37
  "dependencies": {
38
- "@trakit/commands": "^0.1.7",
38
+ "@trakit/commands": "^0.1.8",
39
39
  "@trakit/objects": "^0.1.5",
40
40
  "@trakit/sync": "^0.1.10",
41
41
  "@types/react": "^19.2.17"
@@ -1,7 +1,7 @@
1
- function n({children:n,restAddress:t,socketAddress:r}){return d(I,{children:d(e,{restAddress:t,socketAddress:r,children:n})})}function e({children:n,restAddress:e,socketAddress:t}){function r(n){f(n.online)}function o(n){const{account:e}=n;e.user?.login&&e.expiry>new Date?(c(v,e.ghostId,{...C,expires:e.expiry}),g(e.ghostId||"")):u(v,C),b(e.user||null),e.machine?.key&&e.expiry>new Date?(c(D,e.machine.key,{...C,expires:e.expiry}),c(z,e.machine.secret,{...C,expires:e.expiry})):(u(D,C),u(z,C)),S(e.machine||null)}const i=y(new m(null,e||p.URI_PROD,t||w.URI_PROD)),[s,c,u]=j([v,D,z]),[l,f]=k(i.current.socketOnline),[h,g]=k(s[v]||""),[A,b]=k(i.current.account.user||null),[I,S]=k(i.current.account.machine||s[D]&&new a({key:s[D],secret:s[z]})||null),E=!(!h&&!I)==!(!i.current.account.user&&!i.current.account.machine);return x(()=>{i.current.on("open",r),i.current.on("error",r),i.current.on("close",r),i.current.on("account",o),(h||I)&&i.current.setAuth({ghostId:h,machine:I?.toJSON()})},[]),d(N,{value:{synchronizer:i.current,ready:E,ghostId:h,online:l,user:A,machine:I},children:n})}function t(){const n=A(N);if(!n)throw new Error("useConnection must be used within ConnectionProvider");return n}function r(n,e){const t=y(new h(null,e||h.URI_PROD)),r=!(!t.current.account.user&&!t.current.account.machine);return x(()=>t.current.setAuth(n),[n?.machine?.key||n?.user?.login||n?.key||n?.ghostId||n||null]),{auditor:t.current,ready:r}}function o(n){return window.addEventListener("online",n),window.addEventListener("offline",n),()=>{window.removeEventListener("online",n),window.removeEventListener("offline",n)}}function i(){return navigator.onLine}function s(){return!0}function c(){return b(o,i,s)}function u(n,e){const r=y(null),o=y(0),[i,s]=k(null),[c,u]=k({}),{synchronizer:l,ready:d,online:a,user:m,machine:p}=t();return x(()=>{function t(t,r){0!==o.current&&e===r&&n.includes(t)&&u(n=>({...n,[t]:[...f[t].values().filter(n=>n.companyId===e)]}))}function c(n){const{kind:e,companyId:r}=n;t(e,r)}function a(n){const{name:e,body:r}=n;t(w.msgNameToSyncName(e),r.company)}function m(){if(0===o.current){const n=i?.map(n=>n.syncName).filter(n=>!!n);n?.length&&l.desync(e,n),l.off("list",c),l.off("update",c),l.off("delete",c),l.off("message",a)}r.current=null}function p(r){s(r),n.forEach(n=>t(n,e))}if(d&&n?.length&&!isNaN(e))return o.current++,l.on("list",c),l.on("update",c),l.on("delete",c),l.on("message",a),r.current=r.current||l.sync(e,n),r.current.then(p,p),r.current.finally(m),()=>{o.current--,r.current||m()}},[e=e??m?.companyId??p?.companyId,n.sort().join(",")]),{loading:!(d&&a&&i&&!r.current),replies:i??[],...c}}function l(n,e){const{loading:t,replies:r,[n]:o}=u([n],e);return{loading:t,replies:r,objects:o??[]}}
1
+ function n({children:n,restAddress:t,socketAddress:r,auditAddress:i}){return d(b,{children:d(e,{restAddress:t,socketAddress:r,auditAddress:i,children:n})})}function e({children:n,restAddress:e,socketAddress:t,auditAddress:r}){function i(n){x(n.online)}function o(n){const{account:e}=n;e.user?.login&&e.expiry>new Date?(a(j,e.ghostId,{...z,expires:e.expiry}),S(e.ghostId||"")):h(j,z),K(e.user||null),e.machine?.key&&e.expiry>new Date?(a(v,e.machine.key,{...z,expires:e.expiry}),a(D,e.machine.secret,{...z,expires:e.expiry})):(h(v,z),h(D,z)),q(e.machine||null),c.current.setAuth(e)}const s=A(new f(null,e||m.URI_PROD,t||p.URI_PROD)),c=A(new w(null,r||w.URI_PROD)),[u,a,h]=I([j,v,D]),[y,x]=g(s.current.socketOnline),[b,S]=g(u[j]||""),[E,K]=g(s.current.account.user||null),[P,q]=g(s.current.account.machine||u[v]&&new l({key:u[v],secret:u[D]})||null),B=!(!b&&!P)==!(!s.current.account.user&&!s.current.account.machine);return k(()=>{s.current.on("open",i),s.current.on("error",i),s.current.on("close",i),s.current.on("account",o),(b||P)&&s.current.setAuth({ghostId:b,machine:P?.toJSON()})},[]),d(N,{value:{synchronizer:s.current,auditor:c.current,ready:B,ghostId:b,online:y,user:E,machine:P},children:n})}function t(){const n=y(N);if(!n)throw new Error("useApi must be used within ApiProvider");return n}function r(n){return window.addEventListener("online",n),window.addEventListener("offline",n),()=>{window.removeEventListener("online",n),window.removeEventListener("offline",n)}}function i(){return navigator.onLine}function o(){return!0}function s(){return x(r,i,o)}function c(n,e){const r=A(null),i=A(0),[o,s]=g(null),[c,u]=g({}),{synchronizer:d,ready:l,online:f,user:m,machine:w}=t();return k(()=>{function t(t,r){0!==i.current&&e===r&&n.includes(t)&&u(n=>({...n,[t]:[...a[t].values().filter(n=>n.companyId===e)]}))}function c(n){const{kind:e,companyId:r}=n;t(e,r)}function f(n){const{name:e,body:r}=n;t(p.msgNameToSyncName(e),r.company)}function m(){if(0===i.current){const n=o?.map(n=>n.syncName).filter(n=>!!n);n?.length&&d.desync(e,n),d.off("list",c),d.off("update",c),d.off("delete",c),d.off("message",f)}r.current=null}function w(r){s(r),n.forEach(n=>t(n,e))}if(l&&n?.length&&!isNaN(e))return i.current++,d.on("list",c),d.on("update",c),d.on("delete",c),d.on("message",f),r.current=r.current||d.sync(e,n),r.current.then(w,w),r.current.finally(m),()=>{i.current--,r.current||m()}},[e=e??m?.companyId??w?.companyId,n.sort().join(",")]),{loading:!(l&&f&&o&&!r.current),replies:o??[],...c}}function u(n,e){const{loading:t,replies:r,[n]:i}=c([n],e);return{loading:t,replies:r,objects:i??[]}}
2
2
  /**
3
3
  * ReactJS library.
4
4
  * {@link https://github.com/trakitwireless/trakit-ts-react|ReactJS controls.}
5
5
  * Last updated on Thu June 11 2026 15:43:01
6
6
  * @copyright Trak-iT Wireless Inc. 2026
7
- */import{jsx as d}from"react/jsx-runtime";import{Machine as a,storage as f}from"@trakit/objects";import{TrakitSyncCommander as m,TrakitRestfulCommander as p,TrakitSocketCommander as w,TrakitAuditCommander as h}from"@trakit/sync";import{createContext as g,useRef as y,useState as k,useEffect as x,useContext as A,useSyncExternalStore as b}from"react";import{CookiesProvider as I,useCookies as j}from"react-cookie";const v="ghostId",D="aK",z="aS",C={path:"/",secure:!0,sameSite:"strict",expires:new Date(0)},N=g(null),S="0.0.11";export{n as ConnectionProvider,r as useAudit,t as useConnection,c as useIsOnline,l as useSingle,u as useSync,S as version};
7
+ */import{jsx as d}from"react/jsx-runtime";import{Machine as l,storage as a}from"@trakit/objects";import{TrakitSyncCommander as f,TrakitRestfulCommander as m,TrakitSocketCommander as p,TrakitAuditCommander as w}from"@trakit/sync";import{createContext as h,useRef as A,useState as g,useEffect as k,useContext as y,useSyncExternalStore as x}from"react";import{CookiesProvider as b,useCookies as I}from"react-cookie";const j="ghostId",v="aK",D="aS",z={path:"/",secure:!0,sameSite:"strict",expires:new Date(0)},N=h(null),S="0.0.12";export{n as ApiProvider,t as useApi,s as useIsOnline,u as useSingle,c as useSync,S as version};
@@ -1,22 +0,0 @@
1
- import { RepSelfGet } from '@trakit/commands';
2
- import { guid, Machine, nothing, url } from '@trakit/objects';
3
- import { TrakitAuditCommander } from '@trakit/sync';
4
- /**
5
- *
6
- * @param account The account information used for authentication.
7
- * @param baseAddress The base address of the audit service.
8
- * @returns An object containing the auditor instance and its readiness state.
9
- */
10
- export declare function useAudit(account?: RepSelfGet | {
11
- machine: {
12
- key: string;
13
- };
14
- } | Machine | {
15
- key: string;
16
- } | {
17
- ghostId: guid;
18
- } | guid | nothing, baseAddress?: URL | url | nothing): {
19
- auditor: TrakitAuditCommander;
20
- ready: boolean;
21
- };
22
- //# sourceMappingURL=useAudit.d.ts.map
@@ -1,41 +0,0 @@
1
- import { guid, Machine, nothing, url, User } from '@trakit/objects';
2
- import { TrakitSyncCommander } from '@trakit/sync';
3
- /**
4
- * The result of the `useConnection` hook, providing the synchronizer instance and connection state.
5
- */
6
- export type UseConnectionResult = {
7
- /**
8
- * The synchronizer instance used for commands.
9
- */
10
- readonly synchronizer: TrakitSyncCommander;
11
- /**
12
- * Indicates whether the connection is ready for use.
13
- * Will be `true` if there is {@link User|user session} or {@link Machine|API credentials}
14
- * available and the underlying WebSocket connection has been established,
15
- * or if there is no authentication details given.
16
- */
17
- ready: boolean;
18
- /**
19
- * The session ID taken from a login command.
20
- */
21
- ghostId: guid;
22
- /**
23
- * Indicates whether the WebSocket connection is currently online.
24
- */
25
- online: boolean;
26
- /**
27
- * The {@link User} information associated with the current session, if available.
28
- */
29
- user: User | null;
30
- /**
31
- * The {@link Machine|API credentials} associated with the current session, if available.
32
- */
33
- machine: Machine | null;
34
- };
35
- /**
36
- * A React hook that manages the connection to the Trak-iT synchronization service.
37
- * It initializes the synchronizer, listens for connection and account events,
38
- * and provides the current connection state and user/machine information.
39
- */
40
- export declare function useConnection(restAddress?: URL | url | nothing, socketAddress?: URL | url | nothing): UseConnectionResult;
41
- //# sourceMappingURL=useConnection.d.ts.map
@@ -1,2 +0,0 @@
1
- export declare function useStateRef<T>(initial: T): readonly [T, import("react").Dispatch<import("react").SetStateAction<T>>, import("react").RefObject<T>];
2
- //# sourceMappingURL=useStateRef.d.ts.map