@instroc/client 1.0.0-alpha.1

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.
@@ -0,0 +1,99 @@
1
+ import * as zustand_vanilla from 'zustand/vanilla';
2
+
3
+ /**
4
+ * Shared config consumed by every @instroc/* SDK package. Lives in a single
5
+ * Zustand store so that one initInstroc() call configures auth, data,
6
+ * functions, and storage in one shot — no provider tree required.
7
+ */
8
+ interface InstrocConfig {
9
+ /**
10
+ * BaaS project id — required for any SDK call that hits the backend.
11
+ * Null means the SDK is uninitialized; hooks return safe defaults
12
+ * (logged-out / loading=false / no project) until set.
13
+ */
14
+ projectId: string | null;
15
+ /**
16
+ * Base URL the SDK appends `${projectId}/...` paths to.
17
+ * Defaults to `/api/baas` so apps served behind the published-app router
18
+ * just work.
19
+ */
20
+ baseUrl: string;
21
+ /**
22
+ * If true, auth persists the session in localStorage and rehydrates on
23
+ * boot. Generated apps almost always want this; set false in tests or
24
+ * embed contexts where you don't want the session to leak across runs.
25
+ */
26
+ persistSession: boolean;
27
+ /**
28
+ * Optional API key for server-to-server data calls. Null in browser apps
29
+ * (auth is per-user via cookies/tokens). Reserved for SSR / Worker
30
+ * scenarios that may show up post-launch.
31
+ */
32
+ apiKey: string | null;
33
+ }
34
+ /**
35
+ * Caller-supplied init shape. projectId is required; everything else has a
36
+ * sensible default. Extending `Partial<InstrocConfig>` keeps the surface
37
+ * forward-compatible — adding a new optional field never breaks callers.
38
+ */
39
+ interface InitInstrocOptions extends Partial<InstrocConfig> {
40
+ projectId: string;
41
+ }
42
+
43
+ /**
44
+ * Module-level singleton. Every @instroc/* package imports this store, so
45
+ * one initInstroc() call configures all of them. Vanilla (non-React) store
46
+ * so it can be read from non-component code (auth's bootstrap effect, the
47
+ * legacy Provider shims, tests).
48
+ *
49
+ * The same instance is shared across packages because Node/Bun module
50
+ * resolution dedups `@instroc/client` to one copy in the user's tree.
51
+ */
52
+ declare const configStore: zustand_vanilla.StoreApi<InstrocConfig>;
53
+ /**
54
+ * Configure the SDK. Call once at app boot (typically in main.tsx) before
55
+ * rendering. Calling again is allowed and overwrites the existing config —
56
+ * useful for multi-tenant scenarios or runtime project switching.
57
+ *
58
+ * Fields not passed retain their default. projectId is required.
59
+ */
60
+ declare function initInstroc(options: InitInstrocOptions): void;
61
+ /**
62
+ * Reset the store to defaults. Test-only escape hatch — production code
63
+ * should never call this. Exposed because vitest workers share module
64
+ * state across files and config leaks would cause flakey cross-test
65
+ * failures otherwise.
66
+ */
67
+ declare function resetInstroc(): void;
68
+ /**
69
+ * Read the current config synchronously. Use inside non-React code (event
70
+ * handlers, fetch wrappers, the auth bootstrap effect). For React
71
+ * components, prefer `useInstrocConfig()` so re-renders fire when config
72
+ * changes.
73
+ */
74
+ declare function getInstrocConfig(): InstrocConfig;
75
+ /**
76
+ * Update projectId at runtime. Used by the legacy `<AuthProvider projectId>`
77
+ * shim and by apps that switch tenants without a full reload.
78
+ */
79
+ declare function setInstrocProjectId(projectId: string | null): void;
80
+ /**
81
+ * React hook — returns the current config and re-renders the component
82
+ * when any field changes. Most app code never needs this; SDK packages use
83
+ * it internally to pick up runtime projectId/baseUrl changes.
84
+ *
85
+ * Uses React's `useSyncExternalStore` directly against the vanilla zustand
86
+ * store so we don't pull in zustand's React adapter — that adapter binds to
87
+ * a single React copy at install time and creates the classic
88
+ * "Cannot read properties of null (reading 'useCallback')" error in
89
+ * monorepos / mixed-React-version setups.
90
+ */
91
+ declare function useInstrocConfig(): InstrocConfig;
92
+ /**
93
+ * React hook — returns true once a projectId has been set. Useful for
94
+ * gating SDK calls or showing a "configuring..." state during the brief
95
+ * window between createRoot() and initInstroc() resolving.
96
+ */
97
+ declare function useInstrocReady(): boolean;
98
+
99
+ export { type InitInstrocOptions, type InstrocConfig, configStore, getInstrocConfig, initInstroc, resetInstroc, setInstrocProjectId, useInstrocConfig, useInstrocReady };
package/dist/index.js ADDED
@@ -0,0 +1,50 @@
1
+ // src/config-store.ts
2
+ import { useSyncExternalStore } from "react";
3
+ import { createStore } from "zustand/vanilla";
4
+ var DEFAULT_CONFIG = {
5
+ projectId: null,
6
+ baseUrl: "/api/baas",
7
+ persistSession: true,
8
+ apiKey: null
9
+ };
10
+ var configStore = createStore(() => ({
11
+ ...DEFAULT_CONFIG
12
+ }));
13
+ function initInstroc(options) {
14
+ configStore.setState((prev) => ({
15
+ ...prev,
16
+ ...options
17
+ }));
18
+ }
19
+ function resetInstroc() {
20
+ configStore.setState({ ...DEFAULT_CONFIG }, true);
21
+ }
22
+ function getInstrocConfig() {
23
+ return configStore.getState();
24
+ }
25
+ function setInstrocProjectId(projectId) {
26
+ configStore.setState({ projectId });
27
+ }
28
+ function useInstrocConfig() {
29
+ return useSyncExternalStore(
30
+ configStore.subscribe,
31
+ configStore.getState,
32
+ configStore.getState
33
+ );
34
+ }
35
+ function useInstrocReady() {
36
+ return useSyncExternalStore(
37
+ configStore.subscribe,
38
+ () => configStore.getState().projectId !== null,
39
+ () => configStore.getState().projectId !== null
40
+ );
41
+ }
42
+ export {
43
+ configStore,
44
+ getInstrocConfig,
45
+ initInstroc,
46
+ resetInstroc,
47
+ setInstrocProjectId,
48
+ useInstrocConfig,
49
+ useInstrocReady
50
+ };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@instroc/client",
3
+ "version": "1.0.0-alpha.1",
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
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "tsup",
19
+ "dev": "tsup --watch"
20
+ },
21
+ "peerDependencies": {
22
+ "react": "^18.0.0 || ^19.0.0"
23
+ },
24
+ "dependencies": {
25
+ "zustand": "^5.0.0"
26
+ },
27
+ "devDependencies": {
28
+ "tsup": "^8.0.0",
29
+ "typescript": "^5.3.0"
30
+ },
31
+ "license": "MIT",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/Olaide-EO/robobuild",
35
+ "directory": "packages/client"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ }
40
+ }