@theruntimehq/react 0.1.0 → 0.2.0

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
@@ -112,7 +112,7 @@ function IndependentWidget() {
112
112
 
113
113
  ## Convenience Helpers
114
114
 
115
- Quickly determine states using utility checkers. These accept either the full `RuntimeResponse` object, the `RuntimeState` string, or `null`/`undefined`.
115
+ Quickly determine states using utility checkers. These accept either the full `RuntimeResponse` object, a `CapabilityState` object, the `RuntimeState` string, or `null`/`undefined`.
116
116
 
117
117
  ```typescript
118
118
  import {
@@ -197,6 +197,36 @@ export default function ClientSidePoller({ initialData }: ClientSidePollerProps)
197
197
  }
198
198
  ```
199
199
 
200
+ ## Examples Directory
201
+
202
+ Check out the [examples directory](./examples) for common integration patterns:
203
+
204
+ | File Name | Business Outcome |
205
+ | :--- | :--- |
206
+ | `01-application-wide-banner.tsx` | Inform all users when the application is degraded, under maintenance, or unavailable. |
207
+ | `02-capability-outage-tooltip.tsx` | Explain why a specific feature is unavailable. |
208
+ | `03-feature-gating.tsx` | Hide unavailable functionality from users. |
209
+ | `04-disable-checkout-when-payments-down.tsx` | Prevent purchases when the payments capability is unavailable. |
210
+ | `05-disable-document-upload-during-maintenance.tsx` | Prevent uploads while the upload capability is under maintenance. |
211
+ | `06-read-only-mode-during-outage.tsx` | Keep the application usable while blocking write operations. |
212
+ | `07-degraded-experience-message.tsx` | Communicate reduced functionality without blocking users. |
213
+ | `08-runtime-status-indicator.tsx` | Show operational status in the application header, footer, or navigation. |
214
+ | `09-customer-facing-status-page.tsx` | Build a public status page powered by RuntimeHQ. |
215
+ | `10-runtime-aware-navigation.tsx` | Hide or disable navigation items for unavailable capabilities. |
216
+ | `11-runtime-aware-routing.tsx` | Prevent access to routes backed by unavailable capabilities. |
217
+ | `12-runtime-aware-redirection.tsx` | Automatically redirect users when a capability becomes unavailable. |
218
+ | `13-runtime-aware-fallback-route.tsx` | Redirect users to an alternative workflow when the primary capability is unavailable. |
219
+ | `14-outage-toast-notifications.tsx` | Notify users immediately when runtime state changes occur. |
220
+ | `15-runtime-dashboard.tsx` | Build an operational dashboard displaying application health and capability health. |
221
+ | `16-multi-application-operations-center.tsx` | Monitor multiple applications from a unified screen. |
222
+ | `17-runtime-aware-error-page.tsx` | Replace generic errors with operationally-aware messaging. |
223
+ | `18-runtime-aware-empty-state.tsx` | Show meaningful empty states when functionality is unavailable. |
224
+ | `19-maintenance-lock-screen.tsx` | Restrict access to workflows during active maintenance windows. |
225
+ | `20-live-system-health-widget.tsx` | Embed a reusable health widget anywhere in the application. |
226
+ | `21-production-ready-provider.tsx` | Complete production integration including provider setup, refresh handling, and resilience patterns. |
227
+
228
+ ---
229
+
200
230
  ## License
201
231
 
202
232
  MIT
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
- import { RuntimeResponse, RuntimeState } from '@theruntimehq/js';
3
- export { RuntimeHQClientOptions, RuntimeResponse, RuntimeSourceType, RuntimeState, WatchRuntimeOptions } from '@theruntimehq/js';
2
+ import { RuntimeResponse, CapabilityState, RuntimeState } from '@theruntimehq/js';
3
+ export { CapabilityState, RuntimeHQClientOptions, RuntimeResponse, RuntimeState, WatchRuntimeOptions } from '@theruntimehq/js';
4
4
 
5
5
  interface RuntimeHQProviderProps {
6
6
  runtimeKey: string;
@@ -17,6 +17,8 @@ interface RuntimeHQContextValue {
17
17
  runtime: RuntimeResponse | null;
18
18
  loading: boolean;
19
19
  error: Error | null;
20
+ hasCapability: (name: string) => boolean;
21
+ getCapabilityState: (name: string) => CapabilityState | undefined;
20
22
  }
21
23
 
22
24
  /**
@@ -34,21 +36,22 @@ interface UseRuntimeHQStateOptions {
34
36
  */
35
37
  declare function useRuntimeHQState(options: UseRuntimeHQStateOptions): RuntimeHQContextValue;
36
38
 
39
+ type StateInput = RuntimeResponse | CapabilityState | RuntimeState | null | undefined;
37
40
  /**
38
41
  * Checks if the application runtime status is OPERATIONAL.
39
42
  */
40
- declare function isOperational(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean;
43
+ declare function isOperational(input: StateInput): boolean;
41
44
  /**
42
45
  * Checks if the application runtime status is MAINTENANCE.
43
46
  */
44
- declare function isMaintenance(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean;
47
+ declare function isMaintenance(input: StateInput): boolean;
45
48
  /**
46
49
  * Checks if the application runtime status is DEGRADED.
47
50
  */
48
- declare function isDegraded(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean;
51
+ declare function isDegraded(input: StateInput): boolean;
49
52
  /**
50
53
  * Checks if the application runtime status is OUTAGE.
51
54
  */
52
- declare function isOutage(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean;
55
+ declare function isOutage(input: StateInput): boolean;
53
56
 
54
57
  export { type RuntimeHQContextValue, RuntimeHQProvider, type RuntimeHQProviderProps, type UseRuntimeHQStateOptions, isDegraded, isMaintenance, isOperational, isOutage, useRuntimeHQ, useRuntimeHQState };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
- import { RuntimeResponse, RuntimeState } from '@theruntimehq/js';
3
- export { RuntimeHQClientOptions, RuntimeResponse, RuntimeSourceType, RuntimeState, WatchRuntimeOptions } from '@theruntimehq/js';
2
+ import { RuntimeResponse, CapabilityState, RuntimeState } from '@theruntimehq/js';
3
+ export { CapabilityState, RuntimeHQClientOptions, RuntimeResponse, RuntimeState, WatchRuntimeOptions } from '@theruntimehq/js';
4
4
 
5
5
  interface RuntimeHQProviderProps {
6
6
  runtimeKey: string;
@@ -17,6 +17,8 @@ interface RuntimeHQContextValue {
17
17
  runtime: RuntimeResponse | null;
18
18
  loading: boolean;
19
19
  error: Error | null;
20
+ hasCapability: (name: string) => boolean;
21
+ getCapabilityState: (name: string) => CapabilityState | undefined;
20
22
  }
21
23
 
22
24
  /**
@@ -34,21 +36,22 @@ interface UseRuntimeHQStateOptions {
34
36
  */
35
37
  declare function useRuntimeHQState(options: UseRuntimeHQStateOptions): RuntimeHQContextValue;
36
38
 
39
+ type StateInput = RuntimeResponse | CapabilityState | RuntimeState | null | undefined;
37
40
  /**
38
41
  * Checks if the application runtime status is OPERATIONAL.
39
42
  */
40
- declare function isOperational(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean;
43
+ declare function isOperational(input: StateInput): boolean;
41
44
  /**
42
45
  * Checks if the application runtime status is MAINTENANCE.
43
46
  */
44
- declare function isMaintenance(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean;
47
+ declare function isMaintenance(input: StateInput): boolean;
45
48
  /**
46
49
  * Checks if the application runtime status is DEGRADED.
47
50
  */
48
- declare function isDegraded(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean;
51
+ declare function isDegraded(input: StateInput): boolean;
49
52
  /**
50
53
  * Checks if the application runtime status is OUTAGE.
51
54
  */
52
- declare function isOutage(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean;
55
+ declare function isOutage(input: StateInput): boolean;
53
56
 
54
57
  export { type RuntimeHQContextValue, RuntimeHQProvider, type RuntimeHQProviderProps, type UseRuntimeHQStateOptions, isDegraded, isMaintenance, isOperational, isOutage, useRuntimeHQ, useRuntimeHQState };
package/dist/index.js CHANGED
@@ -55,10 +55,18 @@ function useRuntimeHQState(options) {
55
55
  unsubscribe();
56
56
  };
57
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]);
58
64
  return {
59
65
  runtime,
60
66
  loading,
61
- error
67
+ error,
68
+ hasCapability,
69
+ getCapabilityState
62
70
  };
63
71
  }
64
72
  function RuntimeHQProvider({
@@ -78,22 +86,22 @@ function useRuntimeHQ() {
78
86
  }
79
87
 
80
88
  // src/helpers/index.ts
81
- function getState(runtime) {
82
- if (!runtime) return null;
83
- if (typeof runtime === "string") return runtime;
84
- return runtime.state || null;
89
+ function getState(input) {
90
+ if (!input) return null;
91
+ if (typeof input === "string") return input;
92
+ return input.state || null;
85
93
  }
86
- function isOperational(runtime) {
87
- return getState(runtime) === "OPERATIONAL";
94
+ function isOperational(input) {
95
+ return getState(input) === "OPERATIONAL";
88
96
  }
89
- function isMaintenance(runtime) {
90
- return getState(runtime) === "MAINTENANCE";
97
+ function isMaintenance(input) {
98
+ return getState(input) === "MAINTENANCE";
91
99
  }
92
- function isDegraded(runtime) {
93
- return getState(runtime) === "DEGRADED";
100
+ function isDegraded(input) {
101
+ return getState(input) === "DEGRADED";
94
102
  }
95
- function isOutage(runtime) {
96
- return getState(runtime) === "OUTAGE";
103
+ function isOutage(input) {
104
+ return getState(input) === "OUTAGE";
97
105
  }
98
106
 
99
107
  exports.RuntimeHQProvider = RuntimeHQProvider;
package/dist/index.js.map CHANGED
@@ -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":["createContext","useMemo","RuntimeHQClient","useState","useEffect","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,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AO,QAAA,CAAS,OAAO,CAAA,KAAM,QAAA;AAC/B","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 } 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 return {\n runtime,\n loading,\n error,\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 } from \"../types\";\n\nfunction getState(runtime: RuntimeResponse | RuntimeState | null | undefined): RuntimeState | null {\n if (!runtime) return null;\n if (typeof runtime === \"string\") return runtime;\n return runtime.state || null;\n}\n\n/**\n * Checks if the application runtime status is OPERATIONAL.\n */\nexport function isOperational(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean {\n return getState(runtime) === \"OPERATIONAL\";\n}\n\n/**\n * Checks if the application runtime status is MAINTENANCE.\n */\nexport function isMaintenance(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean {\n return getState(runtime) === \"MAINTENANCE\";\n}\n\n/**\n * Checks if the application runtime status is DEGRADED.\n */\nexport function isDegraded(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean {\n return getState(runtime) === \"DEGRADED\";\n}\n\n/**\n * Checks if the application runtime status is OUTAGE.\n */\nexport function isOutage(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean {\n return getState(runtime) === \"OUTAGE\";\n}\n"]}
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,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,GAAIC,eAAiC,IAAI,CAAA;AACnE,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAIA,cAAA,CAAuB,mBAAmB,IAAI,CAAA;AACxE,EAAA,MAAM,CAAC,OAAA,EAAS,UAAU,CAAA,GAAIA,cAAA,CAAkB,CAAC,eAAe,CAAA;AAEhE,EAAAC,eAAA,CAAU,MAAM;AACd,IAAA,IAAI,eAAA,EAAiB;AACnB,MAAA,QAAA,CAAS,eAAe,CAAA;AACxB,MAAA,UAAA,C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C,OAAO,CAAC,CAAA;AAEZ,EAAA,OAAO;AAAA,IACL,OAAA;AAAA,IACA,OAAA;AAAA,IACA,KAAA;AAAA,IACA,aAAA;AAAA,IACA;AAAA,GACF;AACF;ACzEO,SAAS,iBAAA,CAAkB;AAAA,EAChC,UAAA;AAAA,EACA,eAAA;AAAA,EACA;AACF,CAAA,EAA2B;AACzB,EAAA,MAAM,KAAA,GAAQ,iBAAA,CAAkB,EAAE,UAAA,EAAY,iBAAiB,CAAA;AAE/D,EAAA,uBACEC,cAAA,CAAC,gBAAA,CAAiB,QAAA,EAAjB,EAA0B,OACxB,QAAA,EACH,CAAA;AAEJ;AClBO,SAAS,YAAA,GAAsC;AACpD,EAAA,MAAM,OAAA,GAAUC,iBAAW,gBAAgB,CAAA;AAC3C,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,MAAM,sDAAsD,CAAA;AAAA,EACxE;AACA,EAAA,OAAO,OAAA;AACT;;;ACZA,SAAS,SAAS,KAAA,EAAwC;AACxD,EAAA,IAAI,CAAC,OAAO,OAAO,IAAA;AACnB,EAAA,IAAI,OAAO,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"]}
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createContext, useMemo, useState, useEffect, useContext } from 'react';
1
+ import { createContext, useMemo, useState, useEffect, useCallback, useContext } from 'react';
2
2
  import { RuntimeHQClient } from '@theruntimehq/js';
3
3
  import { jsx } from 'react/jsx-runtime';
4
4
 
@@ -53,10 +53,18 @@ function useRuntimeHQState(options) {
53
53
  unsubscribe();
54
54
  };
55
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]);
56
62
  return {
57
63
  runtime,
58
64
  loading,
59
- error
65
+ error,
66
+ hasCapability,
67
+ getCapabilityState
60
68
  };
61
69
  }
62
70
  function RuntimeHQProvider({
@@ -76,22 +84,22 @@ function useRuntimeHQ() {
76
84
  }
77
85
 
78
86
  // src/helpers/index.ts
79
- function getState(runtime) {
80
- if (!runtime) return null;
81
- if (typeof runtime === "string") return runtime;
82
- return runtime.state || null;
87
+ function getState(input) {
88
+ if (!input) return null;
89
+ if (typeof input === "string") return input;
90
+ return input.state || null;
83
91
  }
84
- function isOperational(runtime) {
85
- return getState(runtime) === "OPERATIONAL";
92
+ function isOperational(input) {
93
+ return getState(input) === "OPERATIONAL";
86
94
  }
87
- function isMaintenance(runtime) {
88
- return getState(runtime) === "MAINTENANCE";
95
+ function isMaintenance(input) {
96
+ return getState(input) === "MAINTENANCE";
89
97
  }
90
- function isDegraded(runtime) {
91
- return getState(runtime) === "DEGRADED";
98
+ function isDegraded(input) {
99
+ return getState(input) === "DEGRADED";
92
100
  }
93
- function isOutage(runtime) {
94
- return getState(runtime) === "OUTAGE";
101
+ function isOutage(input) {
102
+ return getState(input) === "OUTAGE";
95
103
  }
96
104
 
97
105
  export { RuntimeHQProvider, isDegraded, isMaintenance, isOperational, isOutage, useRuntimeHQ, useRuntimeHQState };
@@ -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,KAAA,EAAO,QAAQ,CAAA,GAAI,QAAA,CAAuB,mBAAmB,IAAI,CAAA;AACxE,EAAA,MAAM,CAAC,OAAA,EAAS,UAAU,CAAA,GAAI,QAAA,CAAkB,CAAC,eAAe,CAAA;AAEhE,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,IAAI,eAAA,EAAiB;AACnB,MAAA,QAAA,CAAS,eAAe,CAAA;AACxB,MAAA,UAAA,CAAW,KAAK,CAAA;AAChB,MAAA;AAAA,IACF;AAEA,IAAA,IAAI,CAAC,MAAA,EA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AC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,MAAM,sDAAsD,CAAA;AAAA,EACxE;AACA,EAAA,OAAO,OAAA;AACT;;;ACdA,SAAS,SAAS,OAAA,EAAiF;AACjG,EAAA,IAAI,CAAC,SAAS,OAAO,IAAA;AACrB,EAAA,IAAI,OAAO,OAAA,KAAY,QAAA,EAAU,OAAO,OAAA;AACxC,EAAA,OAAO,QAAQ,KAAA,IAAS,IAAA;AAC1B;AAKO,SAAS,cAAc,OAAA,EAAqE;AACjG,EAAA,OAAO,QAAA,CAAS,OAAO,CAAA,KAAM,aAAA;AAC/B;AAKO,SAAS,cAAc,OAAA,EAAqE;AACjG,EAAA,OAAO,QAAA,CAAS,OAAO,CAAA,KAAM,aAAA;AAC/B;AAKO,SAAS,WAAW,OAAA,EAAqE;AAC9F,EAAA,OAAO,QAAA,CAAS,OAAO,CAAA,KAAM,UAAA;AAC/B;AAKO,SAAS,SAAS,OAAA,EAAqE;AAC5F,EAAA,OAAO,QAAA,CAAS,OAAO,CAAA,KAAM,QAAA;AAC/B","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 } 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 return {\n runtime,\n loading,\n error,\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 } from \"../types\";\n\nfunction getState(runtime: RuntimeResponse | RuntimeState | null | undefined): RuntimeState | null {\n if (!runtime) return null;\n if (typeof runtime === \"string\") return runtime;\n return runtime.state || null;\n}\n\n/**\n * Checks if the application runtime status is OPERATIONAL.\n */\nexport function isOperational(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean {\n return getState(runtime) === \"OPERATIONAL\";\n}\n\n/**\n * Checks if the application runtime status is MAINTENANCE.\n */\nexport function isMaintenance(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean {\n return getState(runtime) === \"MAINTENANCE\";\n}\n\n/**\n * Checks if the application runtime status is DEGRADED.\n */\nexport function isDegraded(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean {\n return getState(runtime) === \"DEGRADED\";\n}\n\n/**\n * Checks if the application runtime status is OUTAGE.\n */\nexport function isOutage(runtime: RuntimeResponse | RuntimeState | null | undefined): boolean {\n return getState(runtime) === \"OUTAGE\";\n}\n"]}
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,EAA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,IAAI,OAAO,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.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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theruntimehq/react",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Official React SDK for RuntimeHQ status checking and monitoring.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -18,7 +18,8 @@
18
18
  "scripts": {
19
19
  "build": "tsup",
20
20
  "test": "vitest run",
21
- "prepublishOnly": "npm run build"
21
+ "prepublishOnly": "npm run build",
22
+ "postversion": "git push && git push --tags"
22
23
  },
23
24
  "keywords": [
24
25
  "runtimehq",
@@ -39,7 +40,7 @@
39
40
  "react-dom": ">=18.0.0"
40
41
  },
41
42
  "dependencies": {
42
- "@theruntimehq/js": "^0.1.0"
43
+ "@theruntimehq/js": "^0.2.0"
43
44
  },
44
45
  "devDependencies": {
45
46
  "@testing-library/react": "^15.0.7",
@@ -50,6 +51,6 @@
50
51
  "react-dom": "^18.2.0",
51
52
  "tsup": "^8.0.2",
52
53
  "typescript": "^5.4.5",
53
- "vitest": "^1.6.0"
54
+ "vitest": "^4.1.8"
54
55
  }
55
56
  }