@theruntimehq/react 0.2.0 → 0.2.2

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/README.md CHANGED
@@ -14,6 +14,20 @@ This SDK is a **pure state management SDK** with:
14
14
 
15
15
  ---
16
16
 
17
+ ## Table of Contents
18
+
19
+ - [Installation](#installation)
20
+ - [Core Hooks & Provider APIs](#core-hooks--provider-apis)
21
+ - [1. Global Provider (\`RuntimeHQProvider\`)](#1-global-provider-runtimehqprovider)
22
+ - [2. Context Hook (\`useRuntimeHQ\`)](#2-context-hook-useruntimehq)
23
+ - [3. Standalone Direct Hook (\`useRuntimeHQState\`)](#3-standalone-direct-hook-useruntimehqstate)
24
+ - [Convenience Helpers](#convenience-helpers)
25
+ - [Server Rendering (SSR) & Next.js App Router Integration](#server-rendering-ssr--nextjs-app-router-integration)
26
+ - [Examples Directory](#examples-directory)
27
+ - [License](#license)
28
+
29
+ ---
30
+
17
31
  ## Installation
18
32
 
19
33
  Ensure you have `@theruntimehq/js` and React installed in your project:
@@ -37,7 +51,7 @@ export default function Root() {
37
51
  return (
38
52
  <RuntimeHQProvider
39
53
  runtimeKey="rt_prod_your_key_here"
40
- intervalSeconds={15}
54
+ intervalSeconds={60}
41
55
  >
42
56
  <App />
43
57
  </RuntimeHQProvider>
@@ -100,7 +114,7 @@ import { useRuntimeHQState } from "@theruntimehq/react";
100
114
  function IndependentWidget() {
101
115
  const { runtime, loading, error } = useRuntimeHQState({
102
116
  runtimeKey: "rt_prod_your_key_here",
103
- intervalSeconds: 30,
117
+ intervalSeconds: 60,
104
118
  });
105
119
 
106
120
  if (loading) return <Spinner />;
@@ -181,7 +195,7 @@ interface ClientSidePollerProps {
181
195
  export default function ClientSidePoller({ initialData }: ClientSidePollerProps) {
182
196
  const { runtime } = useRuntimeHQState({
183
197
  runtimeKey: "rt_prod_your_key_here",
184
- intervalSeconds: 15,
198
+ intervalSeconds: 60,
185
199
  });
186
200
 
187
201
  // Hydrate client-side with server-fetched data initially
package/dist/index.cjs ADDED
@@ -0,0 +1,173 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) {
14
+ __defProp(to, key, {
15
+ get: ((k) => from[k]).bind(null, key),
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ }
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
+ value: mod,
25
+ enumerable: true
26
+ }) : target, mod));
27
+
28
+ //#endregion
29
+ let react = require("react");
30
+ react = __toESM(react);
31
+ let _theruntimehq_js = require("@theruntimehq/js");
32
+ let react_jsx_runtime = require("react/jsx-runtime");
33
+
34
+ //#region src/context/index.ts
35
+ const RuntimeHQContext = (0, react.createContext)(null);
36
+
37
+ //#endregion
38
+ //#region src/hooks/useRuntimeHQState.ts
39
+ /**
40
+ * A direct React hook to fetch and watch RuntimeHQ status without using a Context Provider.
41
+ */
42
+ function useRuntimeHQState(options) {
43
+ const { runtimeKey, intervalSeconds = 60 } = options;
44
+ const [client, clientInitError] = (0, react.useMemo)(() => {
45
+ if (!runtimeKey) return [null, /* @__PURE__ */ new Error("runtimeKey is required")];
46
+ try {
47
+ return [new _theruntimehq_js.RuntimeHQClient({ runtimeKey }), null];
48
+ } catch (err) {
49
+ return [null, err instanceof Error ? err : new Error(String(err))];
50
+ }
51
+ }, [runtimeKey]);
52
+ const [runtime, setRuntime] = (0, react.useState)(null);
53
+ const [error, setError] = (0, react.useState)(clientInitError || null);
54
+ const [loading, setLoading] = (0, react.useState)(!clientInitError);
55
+ (0, react.useEffect)(() => {
56
+ if (clientInitError) {
57
+ setError(clientInitError);
58
+ setLoading(false);
59
+ return;
60
+ }
61
+ if (!client) return;
62
+ setRuntime(null);
63
+ setError(null);
64
+ setLoading(true);
65
+ let active = true;
66
+ const unsubscribe = client.watchRuntime({
67
+ intervalSeconds,
68
+ onUpdate: (data) => {
69
+ if (active) {
70
+ setRuntime(data);
71
+ setError(null);
72
+ setLoading(false);
73
+ }
74
+ },
75
+ onError: (err) => {
76
+ if (active) {
77
+ setError(err);
78
+ setLoading(false);
79
+ }
80
+ }
81
+ });
82
+ return () => {
83
+ active = false;
84
+ unsubscribe();
85
+ };
86
+ }, [
87
+ client,
88
+ clientInitError,
89
+ intervalSeconds
90
+ ]);
91
+ return {
92
+ runtime,
93
+ loading,
94
+ error,
95
+ hasCapability: (0, react.useCallback)((name) => {
96
+ return runtime ? runtime.hasCapability(name) : false;
97
+ }, [runtime]),
98
+ getCapabilityState: (0, react.useCallback)((name) => {
99
+ return runtime ? runtime.getCapabilityState(name) : void 0;
100
+ }, [runtime])
101
+ };
102
+ }
103
+
104
+ //#endregion
105
+ //#region src/provider/index.tsx
106
+ /**
107
+ * Context Provider that manages a global RuntimeHQ client subscription and polling loop,
108
+ * making the status state available to all child components using `useRuntimeHQ()`.
109
+ */
110
+ function RuntimeHQProvider({ runtimeKey, intervalSeconds, children }) {
111
+ const value = useRuntimeHQState({
112
+ runtimeKey,
113
+ intervalSeconds
114
+ });
115
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RuntimeHQContext.Provider, {
116
+ value,
117
+ children
118
+ });
119
+ }
120
+
121
+ //#endregion
122
+ //#region src/hooks/useRuntimeHQ.ts
123
+ /**
124
+ * Accesses the global RuntimeHQ status check context.
125
+ * Must be used within a `<RuntimeHQProvider>`.
126
+ */
127
+ function useRuntimeHQ() {
128
+ const context = (0, react.useContext)(RuntimeHQContext);
129
+ if (!context) throw new Error("useRuntimeHQ must be used within a RuntimeHQProvider");
130
+ return context;
131
+ }
132
+
133
+ //#endregion
134
+ //#region src/helpers/index.ts
135
+ function getState(input) {
136
+ if (!input) return null;
137
+ if (typeof input === "string") return input;
138
+ return input.state || null;
139
+ }
140
+ /**
141
+ * Checks if the application runtime status is OPERATIONAL.
142
+ */
143
+ function isOperational(input) {
144
+ return getState(input) === "OPERATIONAL";
145
+ }
146
+ /**
147
+ * Checks if the application runtime status is MAINTENANCE.
148
+ */
149
+ function isMaintenance(input) {
150
+ return getState(input) === "MAINTENANCE";
151
+ }
152
+ /**
153
+ * Checks if the application runtime status is DEGRADED.
154
+ */
155
+ function isDegraded(input) {
156
+ return getState(input) === "DEGRADED";
157
+ }
158
+ /**
159
+ * Checks if the application runtime status is OUTAGE.
160
+ */
161
+ function isOutage(input) {
162
+ return getState(input) === "OUTAGE";
163
+ }
164
+
165
+ //#endregion
166
+ exports.RuntimeHQProvider = RuntimeHQProvider;
167
+ exports.isDegraded = isDegraded;
168
+ exports.isMaintenance = isMaintenance;
169
+ exports.isOperational = isOperational;
170
+ exports.isOutage = isOutage;
171
+ exports.useRuntimeHQ = useRuntimeHQ;
172
+ exports.useRuntimeHQState = useRuntimeHQState;
173
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["RuntimeHQClient"],"sources":["../src/context/index.ts","../src/hooks/useRuntimeHQState.ts","../src/provider/index.tsx","../src/hooks/useRuntimeHQ.ts","../src/helpers/index.ts"],"sourcesContent":["\"use client\";\n\nimport { createContext } from \"react\";\nimport { RuntimeHQContextValue } from \"../types\";\n\nexport const RuntimeHQContext = createContext<RuntimeHQContextValue | null>(null);\n","\"use client\";\n\nimport { useState, useEffect, useMemo, useCallback } from \"react\";\nimport { RuntimeHQClient } from \"@theruntimehq/js\";\nimport { RuntimeResponse, RuntimeHQContextValue } from \"../types\";\n\nexport interface UseRuntimeHQStateOptions {\n runtimeKey: string;\n intervalSeconds?: number;\n}\n\n/**\n * A direct React hook to fetch and watch RuntimeHQ status without using a Context Provider.\n */\nexport function useRuntimeHQState(options: UseRuntimeHQStateOptions): RuntimeHQContextValue {\n const { runtimeKey, intervalSeconds = 60 } = options;\n\n // Safely instantiate RuntimeHQClient\n const [client, clientInitError] = useMemo(() => {\n if (!runtimeKey) {\n return [null, new Error(\"runtimeKey is required\")] as const;\n }\n try {\n return [new RuntimeHQClient({ runtimeKey }), null] as const;\n } catch (err) {\n return [null, err instanceof Error ? err : new Error(String(err))] as const;\n }\n }, [runtimeKey]);\n\n const [runtime, setRuntime] = useState<RuntimeResponse | null>(null);\n const [error, setError] = useState<Error | null>(clientInitError || null);\n const [loading, setLoading] = useState<boolean>(!clientInitError);\n\n useEffect(() => {\n if (clientInitError) {\n setError(clientInitError);\n setLoading(false);\n return;\n }\n\n if (!client) {\n return;\n }\n\n // Reset states when the client or key changes\n setRuntime(null);\n setError(null);\n setLoading(true);\n\n let active = true;\n\n const unsubscribe = client.watchRuntime({\n intervalSeconds,\n onUpdate: (data) => {\n if (active) {\n setRuntime(data);\n setError(null);\n setLoading(false);\n }\n },\n onError: (err) => {\n if (active) {\n setError(err);\n setLoading(false);\n }\n },\n });\n\n return () => {\n active = false;\n unsubscribe();\n };\n }, [client, clientInitError, intervalSeconds]);\n\n const hasCapability = useCallback((name: string) => {\n return runtime ? runtime.hasCapability(name) : false;\n }, [runtime]);\n\n const getCapabilityState = useCallback((name: string) => {\n return runtime ? runtime.getCapabilityState(name) : undefined;\n }, [runtime]);\n\n return {\n runtime,\n loading,\n error,\n hasCapability,\n getCapabilityState,\n };\n}\n","\"use client\";\n\nimport React from \"react\";\nimport { RuntimeHQContext } from \"../context\";\nimport { useRuntimeHQState } from \"../hooks/useRuntimeHQState\";\n\nexport interface RuntimeHQProviderProps {\n runtimeKey: string;\n intervalSeconds?: number;\n children: React.ReactNode;\n}\n\n/**\n * Context Provider that manages a global RuntimeHQ client subscription and polling loop,\n * making the status state available to all child components using `useRuntimeHQ()`.\n */\nexport function RuntimeHQProvider({\n runtimeKey,\n intervalSeconds,\n children,\n}: RuntimeHQProviderProps) {\n const value = useRuntimeHQState({ runtimeKey, intervalSeconds });\n\n return (\n <RuntimeHQContext.Provider value={value}>\n {children}\n </RuntimeHQContext.Provider>\n );\n}\n","\"use client\";\n\nimport { useContext } from \"react\";\nimport { RuntimeHQContext } from \"../context\";\nimport { RuntimeHQContextValue } from \"../types\";\n\n/**\n * Accesses the global RuntimeHQ status check context.\n * Must be used within a `<RuntimeHQProvider>`.\n */\nexport function useRuntimeHQ(): RuntimeHQContextValue {\n const context = useContext(RuntimeHQContext);\n if (!context) {\n throw new Error(\"useRuntimeHQ must be used within a RuntimeHQProvider\");\n }\n return context;\n}\n","import { RuntimeResponse, RuntimeState, CapabilityState } from \"../types\";\n\ntype StateInput = RuntimeResponse | CapabilityState | RuntimeState | null | undefined;\n\nfunction getState(input: StateInput): RuntimeState | null {\n if (!input) return null;\n if (typeof input === \"string\") return input;\n return input.state || null;\n}\n\n/**\n * Checks if the application runtime status is OPERATIONAL.\n */\nexport function isOperational(input: StateInput): boolean {\n return getState(input) === \"OPERATIONAL\";\n}\n\n/**\n * Checks if the application runtime status is MAINTENANCE.\n */\nexport function isMaintenance(input: StateInput): boolean {\n return getState(input) === \"MAINTENANCE\";\n}\n\n/**\n * Checks if the application runtime status is DEGRADED.\n */\nexport function isDegraded(input: StateInput): boolean {\n return getState(input) === \"DEGRADED\";\n}\n\n/**\n * Checks if the application runtime status is OUTAGE.\n */\nexport function isOutage(input: StateInput): boolean {\n return getState(input) === \"OUTAGE\";\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,MAAa,4CAA+D,IA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F;;;;;;;;ACzEA,SAAgB,kBAAkB,EAChC,YACA,iBACA,YACyB;CACzB,MAAM,QAAQ,kBAAkB;EAAE;EAAY;CAAgB,CAAC;CAE/D,OACE,2CAAC,iBAAiB,UAAlB;EAAkC;EAC/B;CACwB;AAE/B;;;;;;;;AClBA,SAAgB,eAAsC;CACpD,MAAM,gCAAqB,gBAAgB;CAC3C,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,sDAAsD;CAExE,OAAO;AACT;;;;ACZA,SAAS,SAAS,OAAwC;CACxD,IAAI,CAAC,OAAO,OAAO;CACnB,IAAI,OAAO,UAAU,UAAU,OAAO;CACtC,OAAO,MAAM,SAAS;AACxB;;;;AAKA,SAAgB,cAAc,OAA4B;CACxD,OAAO,SAAS,KAAK,MAAM;AAC7B;;;;AAKA,SAAgB,cAAc,OAA4B;CACxD,OAAO,SAAS,KAAK,MAAM;AAC7B;;;;AAKA,SAAgB,WAAW,OAA4B;CACrD,OAAO,SAAS,KAAK,MAAM;AAC7B;;;;AAKA,SAAgB,SAAS,OAA4B;CACnD,OAAO,SAAS,KAAK,MAAM;AAC7B"}
@@ -0,0 +1,66 @@
1
+ import React from "react";
2
+ import { CapabilityState, CapabilityState as CapabilityState$1, RuntimeHQClientOptions, RuntimeResponse, RuntimeResponse as RuntimeResponse$1, RuntimeState, WatchRuntimeOptions } from "@theruntimehq/js";
3
+
4
+ //#region src/provider/index.d.ts
5
+ interface RuntimeHQProviderProps {
6
+ runtimeKey: string;
7
+ intervalSeconds?: number;
8
+ children: React.ReactNode;
9
+ }
10
+ /**
11
+ * Context Provider that manages a global RuntimeHQ client subscription and polling loop,
12
+ * making the status state available to all child components using `useRuntimeHQ()`.
13
+ */
14
+ declare function RuntimeHQProvider({
15
+ runtimeKey,
16
+ intervalSeconds,
17
+ children
18
+ }: RuntimeHQProviderProps): React.JSX.Element;
19
+ //#endregion
20
+ //#region src/types/index.d.ts
21
+ interface RuntimeHQContextValue {
22
+ runtime: RuntimeResponse$1 | null;
23
+ loading: boolean;
24
+ error: Error | null;
25
+ hasCapability: (name: string) => boolean;
26
+ getCapabilityState: (name: string) => CapabilityState$1 | undefined;
27
+ }
28
+ //#endregion
29
+ //#region src/hooks/useRuntimeHQ.d.ts
30
+ /**
31
+ * Accesses the global RuntimeHQ status check context.
32
+ * Must be used within a `<RuntimeHQProvider>`.
33
+ */
34
+ declare function useRuntimeHQ(): RuntimeHQContextValue;
35
+ //#endregion
36
+ //#region src/hooks/useRuntimeHQState.d.ts
37
+ interface UseRuntimeHQStateOptions {
38
+ runtimeKey: string;
39
+ intervalSeconds?: number;
40
+ }
41
+ /**
42
+ * A direct React hook to fetch and watch RuntimeHQ status without using a Context Provider.
43
+ */
44
+ declare function useRuntimeHQState(options: UseRuntimeHQStateOptions): RuntimeHQContextValue;
45
+ //#endregion
46
+ //#region src/helpers/index.d.ts
47
+ type StateInput = RuntimeResponse | CapabilityState | RuntimeState | null | undefined;
48
+ /**
49
+ * Checks if the application runtime status is OPERATIONAL.
50
+ */
51
+ declare function isOperational(input: StateInput): boolean;
52
+ /**
53
+ * Checks if the application runtime status is MAINTENANCE.
54
+ */
55
+ declare function isMaintenance(input: StateInput): boolean;
56
+ /**
57
+ * Checks if the application runtime status is DEGRADED.
58
+ */
59
+ declare function isDegraded(input: StateInput): boolean;
60
+ /**
61
+ * Checks if the application runtime status is OUTAGE.
62
+ */
63
+ declare function isOutage(input: StateInput): boolean;
64
+ //#endregion
65
+ export { type CapabilityState, type RuntimeHQClientOptions, RuntimeHQContextValue, RuntimeHQProvider, type RuntimeHQProviderProps, type RuntimeResponse, type RuntimeState, type UseRuntimeHQStateOptions, type WatchRuntimeOptions, isDegraded, isMaintenance, isOperational, isOutage, useRuntimeHQ, useRuntimeHQState };
66
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/provider/index.tsx","../src/types/index.ts","../src/hooks/useRuntimeHQ.ts","../src/hooks/useRuntimeHQState.ts","../src/helpers/index.ts"],"mappings":";;;;UAMiB,sBAAA;EACf,UAAA;EACA,eAAA;EACA,QAAA,EAAU,KAAA,CAAM,SAAS;AAAA;;;;;iBAOX,iBAAA;EACd,UAAA;EACA,eAAA;EACA;AAAA,GACC,sBAAA,GAAsB,KAAA,CAAA,GAAA,CAAA,OAAA;;;UCVR,qBAAA;EACf,OAAA,EAAS,iBAAA;EACT,OAAA;EACA,KAAA,EAAO,KAAA;EACP,aAAA,GAAgB,IAAA;EAChB,kBAAA,GAAqB,IAAA,aAAiB,iBAAA;AAAA;;;;;;ADTxC;iBEIgB,YAAA,IAAgB,qBAAqB;;;UCJpC,wBAAA;EACf,UAAA;EACA,eAAe;AAAA;;;;iBAMD,iBAAA,CAAkB,OAAA,EAAS,wBAAA,GAA2B,qBAAqB;;;KCZtF,UAAA,GAAa,eAAA,GAAkB,eAAA,GAAkB,YAAA;;;AJItD;iBIOgB,aAAA,CAAc,KAAiB,EAAV,UAAU;;;;iBAO/B,aAAA,CAAc,KAAiB,EAAV,UAAU;;;;iBAO/B,UAAA,CAAW,KAAiB,EAAV,UAAU;AJlBjB;AAO3B;;AAP2B,iBIyBX,QAAA,CAAS,KAAiB,EAAV,UAAU"}
package/dist/index.d.mts CHANGED
@@ -1,41 +1,49 @@
1
- import React from 'react';
2
- import { RuntimeResponse, CapabilityState, RuntimeState } from '@theruntimehq/js';
3
- export { CapabilityState, RuntimeHQClientOptions, RuntimeResponse, RuntimeState, WatchRuntimeOptions } from '@theruntimehq/js';
1
+ import React from "react";
2
+ import { CapabilityState, CapabilityState as CapabilityState$1, RuntimeHQClientOptions, RuntimeResponse, RuntimeResponse as RuntimeResponse$1, RuntimeState, WatchRuntimeOptions } from "@theruntimehq/js";
4
3
 
4
+ //#region src/provider/index.d.ts
5
5
  interface RuntimeHQProviderProps {
6
- runtimeKey: string;
7
- intervalSeconds?: number;
8
- children: React.ReactNode;
6
+ runtimeKey: string;
7
+ intervalSeconds?: number;
8
+ children: React.ReactNode;
9
9
  }
10
10
  /**
11
11
  * Context Provider that manages a global RuntimeHQ client subscription and polling loop,
12
12
  * making the status state available to all child components using `useRuntimeHQ()`.
13
13
  */
14
- declare function RuntimeHQProvider({ runtimeKey, intervalSeconds, children, }: RuntimeHQProviderProps): React.JSX.Element;
15
-
14
+ declare function RuntimeHQProvider({
15
+ runtimeKey,
16
+ intervalSeconds,
17
+ children
18
+ }: RuntimeHQProviderProps): React.JSX.Element;
19
+ //#endregion
20
+ //#region src/types/index.d.ts
16
21
  interface RuntimeHQContextValue {
17
- runtime: RuntimeResponse | null;
18
- loading: boolean;
19
- error: Error | null;
20
- hasCapability: (name: string) => boolean;
21
- getCapabilityState: (name: string) => CapabilityState | undefined;
22
+ runtime: RuntimeResponse$1 | null;
23
+ loading: boolean;
24
+ error: Error | null;
25
+ hasCapability: (name: string) => boolean;
26
+ getCapabilityState: (name: string) => CapabilityState$1 | undefined;
22
27
  }
23
-
28
+ //#endregion
29
+ //#region src/hooks/useRuntimeHQ.d.ts
24
30
  /**
25
31
  * Accesses the global RuntimeHQ status check context.
26
32
  * Must be used within a `<RuntimeHQProvider>`.
27
33
  */
28
34
  declare function useRuntimeHQ(): RuntimeHQContextValue;
29
-
35
+ //#endregion
36
+ //#region src/hooks/useRuntimeHQState.d.ts
30
37
  interface UseRuntimeHQStateOptions {
31
- runtimeKey: string;
32
- intervalSeconds?: number;
38
+ runtimeKey: string;
39
+ intervalSeconds?: number;
33
40
  }
34
41
  /**
35
42
  * A direct React hook to fetch and watch RuntimeHQ status without using a Context Provider.
36
43
  */
37
44
  declare function useRuntimeHQState(options: UseRuntimeHQStateOptions): RuntimeHQContextValue;
38
-
45
+ //#endregion
46
+ //#region src/helpers/index.d.ts
39
47
  type StateInput = RuntimeResponse | CapabilityState | RuntimeState | null | undefined;
40
48
  /**
41
49
  * Checks if the application runtime status is OPERATIONAL.
@@ -53,5 +61,6 @@ declare function isDegraded(input: StateInput): boolean;
53
61
  * Checks if the application runtime status is OUTAGE.
54
62
  */
55
63
  declare function isOutage(input: StateInput): boolean;
56
-
57
- export { type RuntimeHQContextValue, RuntimeHQProvider, type RuntimeHQProviderProps, type UseRuntimeHQStateOptions, isDegraded, isMaintenance, isOperational, isOutage, useRuntimeHQ, useRuntimeHQState };
64
+ //#endregion
65
+ export { type CapabilityState, type RuntimeHQClientOptions, RuntimeHQContextValue, RuntimeHQProvider, type RuntimeHQProviderProps, type RuntimeResponse, type RuntimeState, type UseRuntimeHQStateOptions, type WatchRuntimeOptions, isDegraded, isMaintenance, isOperational, isOutage, useRuntimeHQ, useRuntimeHQState };
66
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/provider/index.tsx","../src/types/index.ts","../src/hooks/useRuntimeHQ.ts","../src/hooks/useRuntimeHQState.ts","../src/helpers/index.ts"],"mappings":";;;;UAMiB,sBAAA;EACf,UAAA;EACA,eAAA;EACA,QAAA,EAAU,KAAA,CAAM,SAAS;AAAA;;;;;iBAOX,iBAAA;EACd,UAAA;EACA,eAAA;EACA;AAAA,GACC,sBAAA,GAAsB,KAAA,CAAA,GAAA,CAAA,OAAA;;;UCVR,qBAAA;EACf,OAAA,EAAS,iBAAA;EACT,OAAA;EACA,KAAA,EAAO,KAAA;EACP,aAAA,GAAgB,IAAA;EAChB,kBAAA,GAAqB,IAAA,aAAiB,iBAAA;AAAA;;;;;;ADTxC;iBEIgB,YAAA,IAAgB,qBAAqB;;;UCJpC,wBAAA;EACf,UAAA;EACA,eAAe;AAAA;;;;iBAMD,iBAAA,CAAkB,OAAA,EAAS,wBAAA,GAA2B,qBAAqB;;;KCZtF,UAAA,GAAa,eAAA,GAAkB,eAAA,GAAkB,YAAA;;;AJItD;iBIOgB,aAAA,CAAc,KAAiB,EAAV,UAAU;;;;iBAO/B,aAAA,CAAc,KAAiB,EAAV,UAAU;;;;iBAO/B,UAAA,CAAW,KAAiB,EAAV,UAAU;AJlBjB;AAO3B;;AAP2B,iBIyBX,QAAA,CAAS,KAAiB,EAAV,UAAU"}
package/dist/index.mjs CHANGED
@@ -1,107 +1,138 @@
1
- import { createContext, useMemo, useState, useEffect, useCallback, useContext } from 'react';
2
- import { RuntimeHQClient } from '@theruntimehq/js';
3
- import { jsx } from 'react/jsx-runtime';
1
+ import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
2
+ import { RuntimeHQClient } from "@theruntimehq/js";
3
+ import { jsx } from "react/jsx-runtime";
4
4
 
5
- // src/context/index.ts
6
- var RuntimeHQContext = createContext(null);
5
+ //#region src/context/index.ts
6
+ const RuntimeHQContext = createContext(null);
7
+
8
+ //#endregion
9
+ //#region src/hooks/useRuntimeHQState.ts
10
+ /**
11
+ * A direct React hook to fetch and watch RuntimeHQ status without using a Context Provider.
12
+ */
7
13
  function useRuntimeHQState(options) {
8
- const { runtimeKey, intervalSeconds = 15 } = options;
9
- const [client, clientInitError] = useMemo(() => {
10
- if (!runtimeKey) {
11
- return [null, new Error("runtimeKey is required")];
12
- }
13
- try {
14
- return [new RuntimeHQClient({ runtimeKey }), null];
15
- } catch (err) {
16
- return [null, err instanceof Error ? err : new Error(String(err))];
17
- }
18
- }, [runtimeKey]);
19
- const [runtime, setRuntime] = useState(null);
20
- const [error, setError] = useState(clientInitError || null);
21
- const [loading, setLoading] = useState(!clientInitError);
22
- useEffect(() => {
23
- if (clientInitError) {
24
- setError(clientInitError);
25
- setLoading(false);
26
- return;
27
- }
28
- if (!client) {
29
- return;
30
- }
31
- setRuntime(null);
32
- setError(null);
33
- setLoading(true);
34
- let active = true;
35
- const unsubscribe = client.watchRuntime({
36
- intervalSeconds,
37
- onUpdate: (data) => {
38
- if (active) {
39
- setRuntime(data);
40
- setError(null);
41
- setLoading(false);
42
- }
43
- },
44
- onError: (err) => {
45
- if (active) {
46
- setError(err);
47
- setLoading(false);
48
- }
49
- }
50
- });
51
- return () => {
52
- active = false;
53
- unsubscribe();
54
- };
55
- }, [client, clientInitError, intervalSeconds]);
56
- const hasCapability = useCallback((name) => {
57
- return runtime ? runtime.hasCapability(name) : false;
58
- }, [runtime]);
59
- const getCapabilityState = useCallback((name) => {
60
- return runtime ? runtime.getCapabilityState(name) : void 0;
61
- }, [runtime]);
62
- return {
63
- runtime,
64
- loading,
65
- error,
66
- hasCapability,
67
- getCapabilityState
68
- };
14
+ const { runtimeKey, intervalSeconds = 60 } = options;
15
+ const [client, clientInitError] = useMemo(() => {
16
+ if (!runtimeKey) return [null, /* @__PURE__ */ new Error("runtimeKey is required")];
17
+ try {
18
+ return [new RuntimeHQClient({ runtimeKey }), null];
19
+ } catch (err) {
20
+ return [null, err instanceof Error ? err : new Error(String(err))];
21
+ }
22
+ }, [runtimeKey]);
23
+ const [runtime, setRuntime] = useState(null);
24
+ const [error, setError] = useState(clientInitError || null);
25
+ const [loading, setLoading] = useState(!clientInitError);
26
+ useEffect(() => {
27
+ if (clientInitError) {
28
+ setError(clientInitError);
29
+ setLoading(false);
30
+ return;
31
+ }
32
+ if (!client) return;
33
+ setRuntime(null);
34
+ setError(null);
35
+ setLoading(true);
36
+ let active = true;
37
+ const unsubscribe = client.watchRuntime({
38
+ intervalSeconds,
39
+ onUpdate: (data) => {
40
+ if (active) {
41
+ setRuntime(data);
42
+ setError(null);
43
+ setLoading(false);
44
+ }
45
+ },
46
+ onError: (err) => {
47
+ if (active) {
48
+ setError(err);
49
+ setLoading(false);
50
+ }
51
+ }
52
+ });
53
+ return () => {
54
+ active = false;
55
+ unsubscribe();
56
+ };
57
+ }, [
58
+ client,
59
+ clientInitError,
60
+ intervalSeconds
61
+ ]);
62
+ return {
63
+ runtime,
64
+ loading,
65
+ error,
66
+ hasCapability: useCallback((name) => {
67
+ return runtime ? runtime.hasCapability(name) : false;
68
+ }, [runtime]),
69
+ getCapabilityState: useCallback((name) => {
70
+ return runtime ? runtime.getCapabilityState(name) : void 0;
71
+ }, [runtime])
72
+ };
69
73
  }
70
- function RuntimeHQProvider({
71
- runtimeKey,
72
- intervalSeconds,
73
- children
74
- }) {
75
- const value = useRuntimeHQState({ runtimeKey, intervalSeconds });
76
- return /* @__PURE__ */ jsx(RuntimeHQContext.Provider, { value, children });
74
+
75
+ //#endregion
76
+ //#region src/provider/index.tsx
77
+ /**
78
+ * Context Provider that manages a global RuntimeHQ client subscription and polling loop,
79
+ * making the status state available to all child components using `useRuntimeHQ()`.
80
+ */
81
+ function RuntimeHQProvider({ runtimeKey, intervalSeconds, children }) {
82
+ const value = useRuntimeHQState({
83
+ runtimeKey,
84
+ intervalSeconds
85
+ });
86
+ return /* @__PURE__ */ jsx(RuntimeHQContext.Provider, {
87
+ value,
88
+ children
89
+ });
77
90
  }
91
+
92
+ //#endregion
93
+ //#region src/hooks/useRuntimeHQ.ts
94
+ /**
95
+ * Accesses the global RuntimeHQ status check context.
96
+ * Must be used within a `<RuntimeHQProvider>`.
97
+ */
78
98
  function useRuntimeHQ() {
79
- const context = useContext(RuntimeHQContext);
80
- if (!context) {
81
- throw new Error("useRuntimeHQ must be used within a RuntimeHQProvider");
82
- }
83
- return context;
99
+ const context = useContext(RuntimeHQContext);
100
+ if (!context) throw new Error("useRuntimeHQ must be used within a RuntimeHQProvider");
101
+ return context;
84
102
  }
85
103
 
86
- // src/helpers/index.ts
104
+ //#endregion
105
+ //#region src/helpers/index.ts
87
106
  function getState(input) {
88
- if (!input) return null;
89
- if (typeof input === "string") return input;
90
- return input.state || null;
107
+ if (!input) return null;
108
+ if (typeof input === "string") return input;
109
+ return input.state || null;
91
110
  }
111
+ /**
112
+ * Checks if the application runtime status is OPERATIONAL.
113
+ */
92
114
  function isOperational(input) {
93
- return getState(input) === "OPERATIONAL";
115
+ return getState(input) === "OPERATIONAL";
94
116
  }
117
+ /**
118
+ * Checks if the application runtime status is MAINTENANCE.
119
+ */
95
120
  function isMaintenance(input) {
96
- return getState(input) === "MAINTENANCE";
121
+ return getState(input) === "MAINTENANCE";
97
122
  }
123
+ /**
124
+ * Checks if the application runtime status is DEGRADED.
125
+ */
98
126
  function isDegraded(input) {
99
- return getState(input) === "DEGRADED";
127
+ return getState(input) === "DEGRADED";
100
128
  }
129
+ /**
130
+ * Checks if the application runtime status is OUTAGE.
131
+ */
101
132
  function isOutage(input) {
102
- return getState(input) === "OUTAGE";
133
+ return getState(input) === "OUTAGE";
103
134
  }
104
135
 
136
+ //#endregion
105
137
  export { RuntimeHQProvider, isDegraded, isMaintenance, isOperational, isOutage, useRuntimeHQ, useRuntimeHQState };
106
- //# sourceMappingURL=index.mjs.map
107
138
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/context/index.ts","../src/hooks/useRuntimeHQState.ts","../src/provider/index.tsx","../src/hooks/useRuntimeHQ.ts","../src/helpers/index.ts"],"names":[],"mappings":";;;;;AAKO,IAAM,gBAAA,GAAmB,cAA4C,IAAI,CAAA;ACSzE,SAAS,kBAAkB,OAAA,EAA0D;AAC1F,EAAA,MAAM,EAAE,UAAA,EAAY,eAAA,GAAkB,EAAA,EAAG,GAAI,OAAA;AAG7C,EAAA,MAAM,CAAC,MAAA,EAAQ,eAAe,CAAA,GAAI,QAAQ,MAAM;AAC9C,IAAA,IAAI,CAAC,UAAA,EAAY;AACf,MAAA,OAAO,CAAC,IAAA,EAAM,IAAI,KAAA,CAAM,wBAAwB,CAAC,CAAA;AAAA,IACnD;AACA,IAAA,IAAI;AACF,MAAA,OAAO,CAAC,IAAI,eAAA,CAAgB,EAAE,UAAA,EAAY,GAAG,IAAI,CAAA;AAAA,IACnD,SAAS,GAAA,EAAK;AACZ,MAAA,OAAO,CAAC,IAAA,EAAM,GAAA,YAAe,KAAA,GAAQ,GAAA,GAAM,IAAI,KAAA,CAAM,MAAA,CAAO,GAAG,CAAC,CAAC,CAAA;AAAA,IACnE;AAAA,EACF,CAAA,EAAG,CAAC,UAAU,CAAC,CAAA;AAEf,EAAA,MAAM,CAAC,OAAA,EAAS,UAAU,CAAA,GAAI,SAAiC,IAAI,CAAA;AACnE,EAAA,MAAM,CAAC,KA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SAAS,WAAW,KAAA,EAA4B;AACrD,EAAA,OAAO,QAAA,CAAS,KAAK,CAAA,KAAM,UAAA;AAC7B;AAKO,SAAS,SAAS,KAAA,EAA4B;AACnD,EAAA,OAAO,QAAA,CAAS,KAAK,CAAA,KAAM,QAAA;AAC7B","file":"index.mjs","sourcesContent":["\"use client\";\n\nimport { createContext } from \"react\";\nimport { RuntimeHQContextValue } from \"../types\";\n\nexport const RuntimeHQContext = createContext<RuntimeHQContextValue | null>(null);\n","\"use client\";\n\nimport { useState, useEffect, useMemo, useCallback } from \"react\";\nimport { RuntimeHQClient } from \"@theruntimehq/js\";\nimport { RuntimeResponse, RuntimeHQContextValue } from \"../types\";\n\nexport interface UseRuntimeHQStateOptions {\n runtimeKey: string;\n intervalSeconds?: number;\n}\n\n/**\n * A direct React hook to fetch and watch RuntimeHQ status without using a Context Provider.\n */\nexport function useRuntimeHQState(options: UseRuntimeHQStateOptions): RuntimeHQContextValue {\n const { runtimeKey, intervalSeconds = 15 } = options;\n\n // Safely instantiate RuntimeHQClient\n const [client, clientInitError] = useMemo(() => {\n if (!runtimeKey) {\n return [null, new Error(\"runtimeKey is required\")] as const;\n }\n try {\n return [new RuntimeHQClient({ runtimeKey }), null] as const;\n } catch (err) {\n return [null, err instanceof Error ? err : new Error(String(err))] as const;\n }\n }, [runtimeKey]);\n\n const [runtime, setRuntime] = useState<RuntimeResponse | null>(null);\n const [error, setError] = useState<Error | null>(clientInitError || null);\n const [loading, setLoading] = useState<boolean>(!clientInitError);\n\n useEffect(() => {\n if (clientInitError) {\n setError(clientInitError);\n setLoading(false);\n return;\n }\n\n if (!client) {\n return;\n }\n\n // Reset states when the client or key changes\n setRuntime(null);\n setError(null);\n setLoading(true);\n\n let active = true;\n\n const unsubscribe = client.watchRuntime({\n intervalSeconds,\n onUpdate: (data) => {\n if (active) {\n setRuntime(data);\n setError(null);\n setLoading(false);\n }\n },\n onError: (err) => {\n if (active) {\n setError(err);\n setLoading(false);\n }\n },\n });\n\n return () => {\n active = false;\n unsubscribe();\n };\n }, [client, clientInitError, intervalSeconds]);\n\n const hasCapability = useCallback((name: string) => {\n return runtime ? runtime.hasCapability(name) : false;\n }, [runtime]);\n\n const getCapabilityState = useCallback((name: string) => {\n return runtime ? runtime.getCapabilityState(name) : undefined;\n }, [runtime]);\n\n return {\n runtime,\n loading,\n error,\n hasCapability,\n getCapabilityState,\n };\n}\n","\"use client\";\n\nimport React from \"react\";\nimport { RuntimeHQContext } from \"../context\";\nimport { useRuntimeHQState } from \"../hooks/useRuntimeHQState\";\n\nexport interface RuntimeHQProviderProps {\n runtimeKey: string;\n intervalSeconds?: number;\n children: React.ReactNode;\n}\n\n/**\n * Context Provider that manages a global RuntimeHQ client subscription and polling loop,\n * making the status state available to all child components using `useRuntimeHQ()`.\n */\nexport function RuntimeHQProvider({\n runtimeKey,\n intervalSeconds,\n children,\n}: RuntimeHQProviderProps) {\n const value = useRuntimeHQState({ runtimeKey, intervalSeconds });\n\n return (\n <RuntimeHQContext.Provider value={value}>\n {children}\n </RuntimeHQContext.Provider>\n );\n}\n","\"use client\";\n\nimport { useContext } from \"react\";\nimport { RuntimeHQContext } from \"../context\";\nimport { RuntimeHQContextValue } from \"../types\";\n\n/**\n * Accesses the global RuntimeHQ status check context.\n * Must be used within a `<RuntimeHQProvider>`.\n */\nexport function useRuntimeHQ(): RuntimeHQContextValue {\n const context = useContext(RuntimeHQContext);\n if (!context) {\n throw new Error(\"useRuntimeHQ must be used within a RuntimeHQProvider\");\n }\n return context;\n}\n","import { RuntimeResponse, RuntimeState, CapabilityState } from \"../types\";\n\ntype StateInput = RuntimeResponse | CapabilityState | RuntimeState | null | undefined;\n\nfunction getState(input: StateInput): RuntimeState | null {\n if (!input) return null;\n if (typeof input === \"string\") return input;\n return input.state || null;\n}\n\n/**\n * Checks if the application runtime status is OPERATIONAL.\n */\nexport function isOperational(input: StateInput): boolean {\n return getState(input) === \"OPERATIONAL\";\n}\n\n/**\n * Checks if the application runtime status is MAINTENANCE.\n */\nexport function isMaintenance(input: StateInput): boolean {\n return getState(input) === \"MAINTENANCE\";\n}\n\n/**\n * Checks if the application runtime status is DEGRADED.\n */\nexport function isDegraded(input: StateInput): boolean {\n return getState(input) === \"DEGRADED\";\n}\n\n/**\n * Checks if the application runtime status is OUTAGE.\n */\nexport function isOutage(input: StateInput): boolean {\n return getState(input) === \"OUTAGE\";\n}\n"]}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/context/index.ts","../src/hooks/useRuntimeHQState.ts","../src/provider/index.tsx","../src/hooks/useRuntimeHQ.ts","../src/helpers/index.ts"],"sourcesContent":["\"use client\";\n\nimport { createContext } from \"react\";\nimport { RuntimeHQContextValue } from \"../types\";\n\nexport const RuntimeHQContext = createContext<RuntimeHQContextValue | null>(null);\n","\"use client\";\n\nimport { useState, useEffect, useMemo, useCallback } from \"react\";\nimport { RuntimeHQClient } from \"@theruntimehq/js\";\nimport { RuntimeResponse, RuntimeHQContextValue } from \"../types\";\n\nexport interface UseRuntimeHQStateOptions {\n runtimeKey: string;\n intervalSeconds?: number;\n}\n\n/**\n * A direct React hook to fetch and watch RuntimeHQ status without using a Context Provider.\n */\nexport function useRuntimeHQState(options: UseRuntimeHQStateOptions): RuntimeHQContextValue {\n const { runtimeKey, intervalSeconds = 60 } = options;\n\n // Safely instantiate RuntimeHQClient\n const [client, clientInitError] = useMemo(() => {\n if (!runtimeKey) {\n return [null, new Error(\"runtimeKey is required\")] as const;\n }\n try {\n return [new RuntimeHQClient({ runtimeKey }), null] as const;\n } catch (err) {\n return [null, err instanceof Error ? err : new Error(String(err))] as const;\n }\n }, [runtimeKey]);\n\n const [runtime, setRuntime] = useState<RuntimeResponse | null>(null);\n const [error, setError] = useState<Error | null>(clientInitError || null);\n const [loading, setLoading] = useState<boolean>(!clientInitError);\n\n useEffect(() => {\n if (clientInitError) {\n setError(clientInitError);\n setLoading(false);\n return;\n }\n\n if (!client) {\n return;\n }\n\n // Reset states when the client or key changes\n setRuntime(null);\n setError(null);\n setLoading(true);\n\n let active = true;\n\n const unsubscribe = client.watchRuntime({\n intervalSeconds,\n onUpdate: (data) => {\n if (active) {\n setRuntime(data);\n setError(null);\n setLoading(false);\n }\n },\n onError: (err) => {\n if (active) {\n setError(err);\n setLoading(false);\n }\n },\n });\n\n return () => {\n active = false;\n unsubscribe();\n };\n }, [client, clientInitError, intervalSeconds]);\n\n const hasCapability = useCallback((name: string) => {\n return runtime ? runtime.hasCapability(name) : false;\n }, [runtime]);\n\n const getCapabilityState = useCallback((name: string) => {\n return runtime ? runtime.getCapabilityState(name) : undefined;\n }, [runtime]);\n\n return {\n runtime,\n loading,\n error,\n hasCapability,\n getCapabilityState,\n };\n}\n","\"use client\";\n\nimport React from \"react\";\nimport { RuntimeHQContext } from \"../context\";\nimport { useRuntimeHQState } from \"../hooks/useRuntimeHQState\";\n\nexport interface RuntimeHQProviderProps {\n runtimeKey: string;\n intervalSeconds?: number;\n children: React.ReactNode;\n}\n\n/**\n * Context Provider that manages a global RuntimeHQ client subscription and polling loop,\n * making the status state available to all child components using `useRuntimeHQ()`.\n */\nexport function RuntimeHQProvider({\n runtimeKey,\n intervalSeconds,\n children,\n}: RuntimeHQProviderProps) {\n const value = useRuntimeHQState({ runtimeKey, intervalSeconds });\n\n return (\n <RuntimeHQContext.Provider value={value}>\n {children}\n </RuntimeHQContext.Provider>\n );\n}\n","\"use client\";\n\nimport { useContext } from \"react\";\nimport { RuntimeHQContext } from \"../context\";\nimport { RuntimeHQContextValue } from \"../types\";\n\n/**\n * Accesses the global RuntimeHQ status check context.\n * Must be used within a `<RuntimeHQProvider>`.\n */\nexport function useRuntimeHQ(): RuntimeHQContextValue {\n const context = useContext(RuntimeHQContext);\n if (!context) {\n throw new Error(\"useRuntimeHQ must be used within a RuntimeHQProvider\");\n }\n return context;\n}\n","import { RuntimeResponse, RuntimeState, CapabilityState } from \"../types\";\n\ntype StateInput = RuntimeResponse | CapabilityState | RuntimeState | null | undefined;\n\nfunction getState(input: StateInput): RuntimeState | null {\n if (!input) return null;\n if (typeof input === \"string\") return input;\n return input.state || null;\n}\n\n/**\n * Checks if the application runtime status is OPERATIONAL.\n */\nexport function isOperational(input: StateInput): boolean {\n return getState(input) === \"OPERATIONAL\";\n}\n\n/**\n * Checks if the application runtime status is MAINTENANCE.\n */\nexport function isMaintenance(input: StateInput): boolean {\n return getState(input) === \"MAINTENANCE\";\n}\n\n/**\n * Checks if the application runtime status is DEGRADED.\n */\nexport function isDegraded(input: StateInput): boolean {\n return getState(input) === \"DEGRADED\";\n}\n\n/**\n * Checks if the application runtime status is OUTAGE.\n */\nexport function isOutage(input: StateInput): boolean {\n return getState(input) === \"OUTAGE\";\n}\n"],"mappings":";;;;;AAKA,MAAa,mBAAmB,cAA4C,IAAI;;;;;;;ACShF,SAAgB,kBAAkB,SAA0D;CAC1F,MAAM,EAAE,YAAY,kBAAkB,OAAO;CAG7C,MAAM,CAAC,QAAQ,mBAAmB,cAAc;EAC9C,IAAI,CAAC,YACH,OAAO,CAAC,sBAAM,IAAI,MAAM,wBAAwB,CAAC;EAEnD,IAAI;GACF,OAAO,CAAC,IAAI,gBAAgB,EAAE,WAAW,CAAC,GAAG,IAAI;EACnD,SAAS,KAAK;GACZ,OAAO,CAAC,MAAM,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC,CAAC;EACnE;CACF,GAAG,CAAC,UAAU,CAAC;CAEf,MAAM,CAAC,SAAS,cAAc,SAAiC,IAAI;CACnE,MAAM,CAAC,OAAO,YAAY,SAAuB,mBAAmB,IAAI;CACxE,MAAM,CAAC,SAAS,cAAc,SAAkB,CAAC,eAAe;CAEhE,gBAAgB;EACd,IAAI,iBAAiB;GACnB,SAAS,eAAe;GACxB,WAAW,KAAK;GAChB;EACF;EAEA,IAAI,CAAC,QACH;EAIF,WAAW,IAAI;EACf,SAAS,IAAI;EACb,WAAW,IAAI;EAEf,IAAI,SAAS;EAEb,MAAM,cAAc,OAAO,aAAa;GACtC;GACA,WAAW,SA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  {
2
2
  "name": "@theruntimehq/react",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Official React SDK for RuntimeHQ status checking and monitoring.",
5
- "main": "./dist/index.js",
5
+ "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
7
- "types": "./dist/index.d.ts",
7
+ "types": "./dist/index.d.cts",
8
8
  "exports": {
9
9
  ".": {
10
- "types": "./dist/index.d.ts",
11
- "require": "./dist/index.js",
10
+ "types": {
11
+ "import": "./dist/index.d.mts",
12
+ "require": "./dist/index.d.cts"
13
+ },
14
+ "require": "./dist/index.cjs",
12
15
  "import": "./dist/index.mjs"
13
16
  }
14
17
  },
@@ -16,7 +19,7 @@
16
19
  "dist"
17
20
  ],
18
21
  "scripts": {
19
- "build": "tsup",
22
+ "build": "tsdown",
20
23
  "test": "vitest run",
21
24
  "prepublishOnly": "npm run build",
22
25
  "postversion": "git push && git push --tags"
@@ -40,7 +43,7 @@
40
43
  "react-dom": ">=18.0.0"
41
44
  },
42
45
  "dependencies": {
43
- "@theruntimehq/js": "^0.2.0"
46
+ "@theruntimehq/js": "^0.2.2"
44
47
  },
45
48
  "devDependencies": {
46
49
  "@testing-library/react": "^15.0.7",
@@ -49,8 +52,8 @@
49
52
  "jsdom": "^24.0.0",
50
53
  "react": "^18.2.0",
51
54
  "react-dom": "^18.2.0",
52
- "tsup": "^8.0.2",
55
+ "tsdown": "^0.22.3",
53
56
  "typescript": "^5.4.5",
54
- "vitest": "^4.1.8"
57
+ "vitest": "^4.1.9"
55
58
  }
56
59
  }
package/dist/index.d.ts DELETED
@@ -1,57 +0,0 @@
1
- import React from 'react';
2
- import { RuntimeResponse, CapabilityState, RuntimeState } from '@theruntimehq/js';
3
- export { CapabilityState, RuntimeHQClientOptions, RuntimeResponse, RuntimeState, WatchRuntimeOptions } from '@theruntimehq/js';
4
-
5
- interface RuntimeHQProviderProps {
6
- runtimeKey: string;
7
- intervalSeconds?: number;
8
- children: React.ReactNode;
9
- }
10
- /**
11
- * Context Provider that manages a global RuntimeHQ client subscription and polling loop,
12
- * making the status state available to all child components using `useRuntimeHQ()`.
13
- */
14
- declare function RuntimeHQProvider({ runtimeKey, intervalSeconds, children, }: RuntimeHQProviderProps): React.JSX.Element;
15
-
16
- interface RuntimeHQContextValue {
17
- runtime: RuntimeResponse | null;
18
- loading: boolean;
19
- error: Error | null;
20
- hasCapability: (name: string) => boolean;
21
- getCapabilityState: (name: string) => CapabilityState | undefined;
22
- }
23
-
24
- /**
25
- * Accesses the global RuntimeHQ status check context.
26
- * Must be used within a `<RuntimeHQProvider>`.
27
- */
28
- declare function useRuntimeHQ(): RuntimeHQContextValue;
29
-
30
- interface UseRuntimeHQStateOptions {
31
- runtimeKey: string;
32
- intervalSeconds?: number;
33
- }
34
- /**
35
- * A direct React hook to fetch and watch RuntimeHQ status without using a Context Provider.
36
- */
37
- declare function useRuntimeHQState(options: UseRuntimeHQStateOptions): RuntimeHQContextValue;
38
-
39
- type StateInput = RuntimeResponse | CapabilityState | RuntimeState | null | undefined;
40
- /**
41
- * Checks if the application runtime status is OPERATIONAL.
42
- */
43
- declare function isOperational(input: StateInput): boolean;
44
- /**
45
- * Checks if the application runtime status is MAINTENANCE.
46
- */
47
- declare function isMaintenance(input: StateInput): boolean;
48
- /**
49
- * Checks if the application runtime status is DEGRADED.
50
- */
51
- declare function isDegraded(input: StateInput): boolean;
52
- /**
53
- * Checks if the application runtime status is OUTAGE.
54
- */
55
- declare function isOutage(input: StateInput): boolean;
56
-
57
- export { type RuntimeHQContextValue, RuntimeHQProvider, type RuntimeHQProviderProps, type UseRuntimeHQStateOptions, isDegraded, isMaintenance, isOperational, isOutage, useRuntimeHQ, useRuntimeHQState };
package/dist/index.js DELETED
@@ -1,115 +0,0 @@
1
- 'use strict';
2
-
3
- var react = require('react');
4
- var js = require('@theruntimehq/js');
5
- var jsxRuntime = require('react/jsx-runtime');
6
-
7
- // src/context/index.ts
8
- var RuntimeHQContext = react.createContext(null);
9
- function useRuntimeHQState(options) {
10
- const { runtimeKey, intervalSeconds = 15 } = options;
11
- const [client, clientInitError] = react.useMemo(() => {
12
- if (!runtimeKey) {
13
- return [null, new Error("runtimeKey is required")];
14
- }
15
- try {
16
- return [new js.RuntimeHQClient({ runtimeKey }), null];
17
- } catch (err) {
18
- return [null, err instanceof Error ? err : new Error(String(err))];
19
- }
20
- }, [runtimeKey]);
21
- const [runtime, setRuntime] = react.useState(null);
22
- const [error, setError] = react.useState(clientInitError || null);
23
- const [loading, setLoading] = react.useState(!clientInitError);
24
- react.useEffect(() => {
25
- if (clientInitError) {
26
- setError(clientInitError);
27
- setLoading(false);
28
- return;
29
- }
30
- if (!client) {
31
- return;
32
- }
33
- setRuntime(null);
34
- setError(null);
35
- setLoading(true);
36
- let active = true;
37
- const unsubscribe = client.watchRuntime({
38
- intervalSeconds,
39
- onUpdate: (data) => {
40
- if (active) {
41
- setRuntime(data);
42
- setError(null);
43
- setLoading(false);
44
- }
45
- },
46
- onError: (err) => {
47
- if (active) {
48
- setError(err);
49
- setLoading(false);
50
- }
51
- }
52
- });
53
- return () => {
54
- active = false;
55
- unsubscribe();
56
- };
57
- }, [client, clientInitError, intervalSeconds]);
58
- const hasCapability = react.useCallback((name) => {
59
- return runtime ? runtime.hasCapability(name) : false;
60
- }, [runtime]);
61
- const getCapabilityState = react.useCallback((name) => {
62
- return runtime ? runtime.getCapabilityState(name) : void 0;
63
- }, [runtime]);
64
- return {
65
- runtime,
66
- loading,
67
- error,
68
- hasCapability,
69
- getCapabilityState
70
- };
71
- }
72
- function RuntimeHQProvider({
73
- runtimeKey,
74
- intervalSeconds,
75
- children
76
- }) {
77
- const value = useRuntimeHQState({ runtimeKey, intervalSeconds });
78
- return /* @__PURE__ */ jsxRuntime.jsx(RuntimeHQContext.Provider, { value, children });
79
- }
80
- function useRuntimeHQ() {
81
- const context = react.useContext(RuntimeHQContext);
82
- if (!context) {
83
- throw new Error("useRuntimeHQ must be used within a RuntimeHQProvider");
84
- }
85
- return context;
86
- }
87
-
88
- // src/helpers/index.ts
89
- function getState(input) {
90
- if (!input) return null;
91
- if (typeof input === "string") return input;
92
- return input.state || null;
93
- }
94
- function isOperational(input) {
95
- return getState(input) === "OPERATIONAL";
96
- }
97
- function isMaintenance(input) {
98
- return getState(input) === "MAINTENANCE";
99
- }
100
- function isDegraded(input) {
101
- return getState(input) === "DEGRADED";
102
- }
103
- function isOutage(input) {
104
- return getState(input) === "OUTAGE";
105
- }
106
-
107
- exports.RuntimeHQProvider = RuntimeHQProvider;
108
- exports.isDegraded = isDegraded;
109
- exports.isMaintenance = isMaintenance;
110
- exports.isOperational = isOperational;
111
- exports.isOutage = isOutage;
112
- exports.useRuntimeHQ = useRuntimeHQ;
113
- exports.useRuntimeHQState = useRuntimeHQState;
114
- //# sourceMappingURL=index.js.map
115
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/context/index.ts","../src/hooks/useRuntimeHQState.ts","../src/provider/index.tsx","../src/hooks/useRuntimeHQ.ts","../src/helpers/index.ts"],"names":["createContext","useMemo","RuntimeHQClient","useState","useEffect","useCallback","jsx","useContext"],"mappings":";;;;;;;AAKO,IAAM,gBAAA,GAAmBA,oBAA4C,IAAI,CAAA;ACSzE,SAAS,kBAAkB,OAAA,EAA0D;AAC1F,EAAA,MAAM,EAAE,UAAA,EAAY,eAAA,GAAkB,EAAA,EAAG,GAAI,OAAA;AAG7C,EAAA,MAAM,CAAC,MAAA,EAAQ,eAAe,CAAA,GAAIC,cAAQ,MAAM;AAC9C,IAAA,IAAI,CAAC,UAAA,EAAY;AACf,MAAA,OAAO,CAAC,IAAA,EAAM,IAAI,KAAA,CAAM,wBAAwB,CAAC,CAAA;AAAA,IACnD;AACA,IAAA,IAAI;AACF,MAAA,OAAO,CAAC,IAAIC,kBAAA,CAAgB,EAAE,UAAA,EAAY,GAAG,IAAI,CAAA;AAAA,IACnD,SAAS,GAAA,EAAK;AACZ,MAAA,OAAO,CAA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O,KAAA,KAAU,QAAA,EAAU,OAAO,KAAA;AACtC,EAAA,OAAO,MAAM,KAAA,IAAS,IAAA;AACxB;AAKO,SAAS,cAAc,KAAA,EAA4B;AACxD,EAAA,OAAO,QAAA,CAAS,KAAK,CAAA,KAAM,aAAA;AAC7B;AAKO,SAAS,cAAc,KAAA,EAA4B;AACxD,EAAA,OAAO,QAAA,CAAS,KAAK,CAAA,KAAM,aAAA;AAC7B;AAKO,SAAS,WAAW,KAAA,EAA4B;AACrD,EAAA,OAAO,QAAA,CAAS,KAAK,CAAA,KAAM,UAAA;AAC7B;AAKO,SAAS,SAAS,KAAA,EAA4B;AACnD,EAAA,OAAO,QAAA,CAAS,KAAK,CAAA,KAAM,QAAA;AAC7B","file":"index.js","sourcesContent":["\"use client\";\n\nimport { createContext } from \"react\";\nimport { RuntimeHQContextValue } from \"../types\";\n\nexport const RuntimeHQContext = createContext<RuntimeHQContextValue | null>(null);\n","\"use client\";\n\nimport { useState, useEffect, useMemo, useCallback } from \"react\";\nimport { RuntimeHQClient } from \"@theruntimehq/js\";\nimport { RuntimeResponse, RuntimeHQContextValue } from \"../types\";\n\nexport interface UseRuntimeHQStateOptions {\n runtimeKey: string;\n intervalSeconds?: number;\n}\n\n/**\n * A direct React hook to fetch and watch RuntimeHQ status without using a Context Provider.\n */\nexport function useRuntimeHQState(options: UseRuntimeHQStateOptions): RuntimeHQContextValue {\n const { runtimeKey, intervalSeconds = 15 } = options;\n\n // Safely instantiate RuntimeHQClient\n const [client, clientInitError] = useMemo(() => {\n if (!runtimeKey) {\n return [null, new Error(\"runtimeKey is required\")] as const;\n }\n try {\n return [new RuntimeHQClient({ runtimeKey }), null] as const;\n } catch (err) {\n return [null, err instanceof Error ? err : new Error(String(err))] as const;\n }\n }, [runtimeKey]);\n\n const [runtime, setRuntime] = useState<RuntimeResponse | null>(null);\n const [error, setError] = useState<Error | null>(clientInitError || null);\n const [loading, setLoading] = useState<boolean>(!clientInitError);\n\n useEffect(() => {\n if (clientInitError) {\n setError(clientInitError);\n setLoading(false);\n return;\n }\n\n if (!client) {\n return;\n }\n\n // Reset states when the client or key changes\n setRuntime(null);\n setError(null);\n setLoading(true);\n\n let active = true;\n\n const unsubscribe = client.watchRuntime({\n intervalSeconds,\n onUpdate: (data) => {\n if (active) {\n setRuntime(data);\n setError(null);\n setLoading(false);\n }\n },\n onError: (err) => {\n if (active) {\n setError(err);\n setLoading(false);\n }\n },\n });\n\n return () => {\n active = false;\n unsubscribe();\n };\n }, [client, clientInitError, intervalSeconds]);\n\n const hasCapability = useCallback((name: string) => {\n return runtime ? runtime.hasCapability(name) : false;\n }, [runtime]);\n\n const getCapabilityState = useCallback((name: string) => {\n return runtime ? runtime.getCapabilityState(name) : undefined;\n }, [runtime]);\n\n return {\n runtime,\n loading,\n error,\n hasCapability,\n getCapabilityState,\n };\n}\n","\"use client\";\n\nimport React from \"react\";\nimport { RuntimeHQContext } from \"../context\";\nimport { useRuntimeHQState } from \"../hooks/useRuntimeHQState\";\n\nexport interface RuntimeHQProviderProps {\n runtimeKey: string;\n intervalSeconds?: number;\n children: React.ReactNode;\n}\n\n/**\n * Context Provider that manages a global RuntimeHQ client subscription and polling loop,\n * making the status state available to all child components using `useRuntimeHQ()`.\n */\nexport function RuntimeHQProvider({\n runtimeKey,\n intervalSeconds,\n children,\n}: RuntimeHQProviderProps) {\n const value = useRuntimeHQState({ runtimeKey, intervalSeconds });\n\n return (\n <RuntimeHQContext.Provider value={value}>\n {children}\n </RuntimeHQContext.Provider>\n );\n}\n","\"use client\";\n\nimport { useContext } from \"react\";\nimport { RuntimeHQContext } from \"../context\";\nimport { RuntimeHQContextValue } from \"../types\";\n\n/**\n * Accesses the global RuntimeHQ status check context.\n * Must be used within a `<RuntimeHQProvider>`.\n */\nexport function useRuntimeHQ(): RuntimeHQContextValue {\n const context = useContext(RuntimeHQContext);\n if (!context) {\n throw new Error(\"useRuntimeHQ must be used within a RuntimeHQProvider\");\n }\n return context;\n}\n","import { RuntimeResponse, RuntimeState, CapabilityState } from \"../types\";\n\ntype StateInput = RuntimeResponse | CapabilityState | RuntimeState | null | undefined;\n\nfunction getState(input: StateInput): RuntimeState | null {\n if (!input) return null;\n if (typeof input === \"string\") return input;\n return input.state || null;\n}\n\n/**\n * Checks if the application runtime status is OPERATIONAL.\n */\nexport function isOperational(input: StateInput): boolean {\n return getState(input) === \"OPERATIONAL\";\n}\n\n/**\n * Checks if the application runtime status is MAINTENANCE.\n */\nexport function isMaintenance(input: StateInput): boolean {\n return getState(input) === \"MAINTENANCE\";\n}\n\n/**\n * Checks if the application runtime status is DEGRADED.\n */\nexport function isDegraded(input: StateInput): boolean {\n return getState(input) === \"DEGRADED\";\n}\n\n/**\n * Checks if the application runtime status is OUTAGE.\n */\nexport function isOutage(input: StateInput): boolean {\n return getState(input) === \"OUTAGE\";\n}\n"]}