@instroc/client 1.0.0-alpha.2 → 1.0.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +47 -0
- package/dist/index.js +20 -15
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -65,6 +65,52 @@ declare function initInstroc(options: InitInstrocOptions): void;
|
|
|
65
65
|
* failures otherwise.
|
|
66
66
|
*/
|
|
67
67
|
declare function resetInstroc(): void;
|
|
68
|
+
/**
|
|
69
|
+
* Auto-init from the build-server bootstrap contract.
|
|
70
|
+
*
|
|
71
|
+
* The Instroc build server injects this snippet into the generated
|
|
72
|
+
* `index.html` before any module scripts:
|
|
73
|
+
*
|
|
74
|
+
* <script>
|
|
75
|
+
* window.__INSTROC_BOOTSTRAP__ = {
|
|
76
|
+
* projectId: "...",
|
|
77
|
+
* baseUrl: "...",
|
|
78
|
+
* apiKey: "...",
|
|
79
|
+
* };
|
|
80
|
+
* </script>
|
|
81
|
+
*
|
|
82
|
+
* The SDK reads it on module load. No `process.env` substitution, no
|
|
83
|
+
* bundler-specific magic, no manual `initInstroc()` call from app code.
|
|
84
|
+
*
|
|
85
|
+
* Why a window global instead of `process.env`:
|
|
86
|
+
* - Bundler-agnostic — works under vite, webpack, esbuild, swc
|
|
87
|
+
* - Visible in view-source — debuggable in 2 seconds
|
|
88
|
+
* - Loud failure — if the build is wrong the global is missing and
|
|
89
|
+
* hooks can throw a clear error rather than silently sitting at
|
|
90
|
+
* loading=true forever
|
|
91
|
+
* - Easy to test — set the global before importing the module
|
|
92
|
+
*
|
|
93
|
+
* Module-script timing: classic `<script>` tags in `<head>` execute
|
|
94
|
+
* before any deferred module script (`<script type="module">`), so the
|
|
95
|
+
* global is set before this IIFE runs.
|
|
96
|
+
*/
|
|
97
|
+
declare global {
|
|
98
|
+
interface Window {
|
|
99
|
+
__INSTROC_BOOTSTRAP__?: {
|
|
100
|
+
projectId?: unknown;
|
|
101
|
+
baseUrl?: unknown;
|
|
102
|
+
apiKey?: unknown;
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Throw a clear error if the SDK is asked to do something that requires a
|
|
108
|
+
* configured projectId but auto-init never seeded one. Used by hooks that
|
|
109
|
+
* make network requests; lets the failure surface immediately instead of
|
|
110
|
+
* leaving the UI stuck at `loading: true` forever when the build server
|
|
111
|
+
* forgot to inject the bootstrap.
|
|
112
|
+
*/
|
|
113
|
+
declare function assertInstrocConfigured(): void;
|
|
68
114
|
/**
|
|
69
115
|
* Read the current config synchronously. Use inside non-React code (event
|
|
70
116
|
* handlers, fetch wrappers, the auth bootstrap effect). For React
|
|
@@ -99,6 +145,7 @@ declare function useInstrocReady(): boolean;
|
|
|
99
145
|
export {
|
|
100
146
|
type InitInstrocOptions,
|
|
101
147
|
type InstrocConfig,
|
|
148
|
+
assertInstrocConfigured,
|
|
102
149
|
configStore,
|
|
103
150
|
getInstrocConfig,
|
|
104
151
|
initInstroc,
|
package/dist/index.js
CHANGED
|
@@ -19,22 +19,19 @@ function initInstroc(options) {
|
|
|
19
19
|
function resetInstroc() {
|
|
20
20
|
configStore.setState({ ...DEFAULT_CONFIG }, true);
|
|
21
21
|
}
|
|
22
|
-
function
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (typeof value === "string" && value.length > 0) {
|
|
26
|
-
return value;
|
|
27
|
-
}
|
|
28
|
-
} catch {
|
|
29
|
-
}
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
(function autoInitFromEnv() {
|
|
33
|
-
const projectId = readEnvVar("INSTROC_PROJECT_ID");
|
|
34
|
-
if (!projectId)
|
|
22
|
+
(function autoInitFromBootstrap() {
|
|
23
|
+
const bootstrap = globalThis.__INSTROC_BOOTSTRAP__;
|
|
24
|
+
if (!bootstrap)
|
|
35
25
|
return;
|
|
36
|
-
const
|
|
37
|
-
|
|
26
|
+
const projectId = typeof bootstrap.projectId === "string" && bootstrap.projectId.length > 0 ? bootstrap.projectId : null;
|
|
27
|
+
if (!projectId) {
|
|
28
|
+
console.error(
|
|
29
|
+
"[Instroc] window.__INSTROC_BOOTSTRAP__ is set but missing a non-empty projectId. Check the build-server's index.html generation."
|
|
30
|
+
);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const baseUrl = typeof bootstrap.baseUrl === "string" && bootstrap.baseUrl.length > 0 ? bootstrap.baseUrl : null;
|
|
34
|
+
const apiKey = typeof bootstrap.apiKey === "string" && bootstrap.apiKey.length > 0 ? bootstrap.apiKey : null;
|
|
38
35
|
configStore.setState((prev) => ({
|
|
39
36
|
...prev,
|
|
40
37
|
projectId,
|
|
@@ -42,6 +39,13 @@ function readEnvVar(name) {
|
|
|
42
39
|
...apiKey ? { apiKey } : {}
|
|
43
40
|
}));
|
|
44
41
|
})();
|
|
42
|
+
function assertInstrocConfigured() {
|
|
43
|
+
if (configStore.getState().projectId)
|
|
44
|
+
return;
|
|
45
|
+
throw new Error(
|
|
46
|
+
"Instroc SDK not configured. Expected `window.__INSTROC_BOOTSTRAP__` to be set by the build server before the app loads, or `initInstroc({ projectId })` to be called manually in tests / multi-tenant setups."
|
|
47
|
+
);
|
|
48
|
+
}
|
|
45
49
|
function getInstrocConfig() {
|
|
46
50
|
return configStore.getState();
|
|
47
51
|
}
|
|
@@ -63,6 +67,7 @@ function useInstrocReady() {
|
|
|
63
67
|
);
|
|
64
68
|
}
|
|
65
69
|
export {
|
|
70
|
+
assertInstrocConfigured,
|
|
66
71
|
configStore,
|
|
67
72
|
getInstrocConfig,
|
|
68
73
|
initInstroc,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@instroc/client",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.4",
|
|
4
4
|
"description": "Shared config store for Instroc Cloud SDK packages — initInstroc(), useInstrocConfig(), and the singleton store consumed by @instroc/auth, @instroc/data, @instroc/functions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|