bonescript-compiler 0.7.0 → 0.8.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/dist/cli.js +61 -4
- package/dist/cli.js.map +1 -1
- package/dist/emit_full.js +11 -1
- package/dist/emit_full.js.map +1 -1
- package/dist/emit_notify.js +49 -1
- package/dist/emit_notify.js.map +1 -1
- package/dist/emit_react.d.ts +24 -0
- package/dist/emit_react.js +222 -0
- package/dist/emit_react.js.map +1 -0
- package/dist/emit_sqlite.d.ts +33 -0
- package/dist/emit_sqlite.js +539 -0
- package/dist/emit_sqlite.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/test_notify.d.ts +11 -0
- package/dist/test_notify.js +220 -0
- package/dist/test_notify.js.map +1 -0
- package/dist/test_react.d.ts +10 -0
- package/dist/test_react.js +177 -0
- package/dist/test_react.js.map +1 -0
- package/dist/test_sqlite.d.ts +13 -0
- package/dist/test_sqlite.js +262 -0
- package/dist/test_sqlite.js.map +1 -0
- package/package.json +7 -4
- package/src/cli.ts +68 -5
- package/src/emit_full.ts +11 -1
- package/src/emit_notify.ts +49 -1
- package/src/emit_react.ts +236 -0
- package/src/emit_sqlite.ts +562 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BoneScript React Hooks Emitter
|
|
3
|
+
*
|
|
4
|
+
* Generates a typed React hooks file (sdk/react.ts) on top of the existing
|
|
5
|
+
* fetch SDK. Hooks are zero-dep — they use React's built-in useState +
|
|
6
|
+
* useEffect rather than pulling in @tanstack/react-query so consumers can
|
|
7
|
+
* decide their own data layer.
|
|
8
|
+
*
|
|
9
|
+
* For each entity:
|
|
10
|
+
* useList<Entity>() → { data, loading, error, refetch }
|
|
11
|
+
* use<Entity>(id) → { data, loading, error, refetch }
|
|
12
|
+
* useCreate<Entity>() → mutate(input) → Promise<Entity>
|
|
13
|
+
* useUpdate<Entity>() → mutate(id, patch) → Promise<Entity>
|
|
14
|
+
* useDelete<Entity>() → mutate(id) → Promise<void>
|
|
15
|
+
*
|
|
16
|
+
* For each capability:
|
|
17
|
+
* useCapability<Name>() → mutate(input) → Promise<unknown>
|
|
18
|
+
*
|
|
19
|
+
* Hooks are framework-agnostic in shape (no react-query, no SWR) so they work
|
|
20
|
+
* with Next.js, Vite, CRA, or any plain React app.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import * as IR from "./ir";
|
|
24
|
+
import { EmittedFile } from "./emitter";
|
|
25
|
+
|
|
26
|
+
function toSnakeCase(s: string): string {
|
|
27
|
+
return s.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function toPascalCase(s: string): string {
|
|
31
|
+
return s.replace(/(^|[-_\s])(\w)/g, (_, __, c: string) => c.toUpperCase());
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const TS_TYPE_MAP: Record<string, string> = {
|
|
35
|
+
string: "string", uint: "number", int: "number", float: "number",
|
|
36
|
+
bool: "boolean", timestamp: "string", uuid: "string", bytes: "string", json: "unknown",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function toTsType(irType: string): string {
|
|
40
|
+
if (TS_TYPE_MAP[irType]) return TS_TYPE_MAP[irType];
|
|
41
|
+
const m = irType.match(/^(list|set)<(.+)>$/);
|
|
42
|
+
if (m) return `${toTsType(m[2])}[]`;
|
|
43
|
+
const om = irType.match(/^optional<(.+)>$/);
|
|
44
|
+
if (om) return `${toTsType(om[1])} | null`;
|
|
45
|
+
return irType;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function emitReactHooks(system: IR.IRSystem): EmittedFile {
|
|
49
|
+
const lines: string[] = [];
|
|
50
|
+
|
|
51
|
+
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
52
|
+
lines.push(`// React hooks for the ${system.name} API.`);
|
|
53
|
+
lines.push(`//`);
|
|
54
|
+
lines.push(`// Pair with sdk/client.ts. Pass an instance of <System>Client to <ApiProvider>.`);
|
|
55
|
+
lines.push(``);
|
|
56
|
+
lines.push(`import { useCallback, useEffect, useState, createContext, useContext, ReactNode, createElement } from "react";`);
|
|
57
|
+
lines.push(``);
|
|
58
|
+
|
|
59
|
+
// Entity types (forward-declared so the hooks compile without importing the SDK)
|
|
60
|
+
// We re-declare them here to keep this file independent — the consumer can
|
|
61
|
+
// also import these types from sdk/client.ts if they prefer.
|
|
62
|
+
const apiModules = system.modules.filter(m => m.kind === "api_service" && m.models.length > 0);
|
|
63
|
+
for (const mod of apiModules) {
|
|
64
|
+
const model = mod.models[0];
|
|
65
|
+
lines.push(`export interface ${toPascalCase(model.name)} {`);
|
|
66
|
+
for (const field of model.fields) {
|
|
67
|
+
const optional = field.nullable ? "?" : "";
|
|
68
|
+
lines.push(` ${field.name}${optional}: ${toTsType(field.type)};`);
|
|
69
|
+
}
|
|
70
|
+
lines.push(`}`);
|
|
71
|
+
lines.push(``);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
lines.push(`export interface ApiClient {`);
|
|
75
|
+
lines.push(` baseUrl: string;`);
|
|
76
|
+
lines.push(` getToken: () => string | null;`);
|
|
77
|
+
lines.push(`}`);
|
|
78
|
+
lines.push(``);
|
|
79
|
+
lines.push(`async function apiFetch<T>(client: ApiClient, method: string, path: string, body?: unknown): Promise<T> {`);
|
|
80
|
+
lines.push(` const headers: Record<string, string> = { "Content-Type": "application/json" };`);
|
|
81
|
+
lines.push(` const token = client.getToken();`);
|
|
82
|
+
lines.push(` if (token) headers["Authorization"] = "Bearer " + token;`);
|
|
83
|
+
lines.push(` const res = await fetch(client.baseUrl + path, {`);
|
|
84
|
+
lines.push(` method,`);
|
|
85
|
+
lines.push(` headers,`);
|
|
86
|
+
lines.push(` body: body !== undefined ? JSON.stringify(body) : undefined,`);
|
|
87
|
+
lines.push(` });`);
|
|
88
|
+
lines.push(` if (!res.ok) {`);
|
|
89
|
+
lines.push(` let errMsg = "Request failed: " + res.status;`);
|
|
90
|
+
lines.push(` try { const j = await res.json() as { error?: { message?: string } }; if (j.error?.message) errMsg = j.error.message; } catch {}`);
|
|
91
|
+
lines.push(` throw new Error(errMsg);`);
|
|
92
|
+
lines.push(` }`);
|
|
93
|
+
lines.push(` if (res.status === 204) return undefined as unknown as T;`);
|
|
94
|
+
lines.push(` return await res.json() as T;`);
|
|
95
|
+
lines.push(`}`);
|
|
96
|
+
lines.push(``);
|
|
97
|
+
|
|
98
|
+
// Provider + context. createElement avoids needing JSX in this generated file.
|
|
99
|
+
lines.push(`const ApiContext = createContext<ApiClient | null>(null);`);
|
|
100
|
+
lines.push(``);
|
|
101
|
+
lines.push(`export interface ApiProviderProps {`);
|
|
102
|
+
lines.push(` client: ApiClient;`);
|
|
103
|
+
lines.push(` children: ReactNode;`);
|
|
104
|
+
lines.push(`}`);
|
|
105
|
+
lines.push(``);
|
|
106
|
+
lines.push(`export function ApiProvider(props: ApiProviderProps) {`);
|
|
107
|
+
lines.push(` return createElement(ApiContext.Provider, { value: props.client }, props.children);`);
|
|
108
|
+
lines.push(`}`);
|
|
109
|
+
lines.push(``);
|
|
110
|
+
lines.push(`function useApi(): ApiClient {`);
|
|
111
|
+
lines.push(` const ctx = useContext(ApiContext);`);
|
|
112
|
+
lines.push(` if (!ctx) throw new Error("useApi must be used inside <ApiProvider>");`);
|
|
113
|
+
lines.push(` return ctx;`);
|
|
114
|
+
lines.push(`}`);
|
|
115
|
+
lines.push(``);
|
|
116
|
+
|
|
117
|
+
// Generic shape helpers
|
|
118
|
+
lines.push(`export interface QueryState<T> {`);
|
|
119
|
+
lines.push(` data: T | null;`);
|
|
120
|
+
lines.push(` loading: boolean;`);
|
|
121
|
+
lines.push(` error: Error | null;`);
|
|
122
|
+
lines.push(` refetch: () => Promise<void>;`);
|
|
123
|
+
lines.push(`}`);
|
|
124
|
+
lines.push(``);
|
|
125
|
+
lines.push(`export interface MutationState<TInput, TOutput> {`);
|
|
126
|
+
lines.push(` mutate: (input: TInput) => Promise<TOutput>;`);
|
|
127
|
+
lines.push(` loading: boolean;`);
|
|
128
|
+
lines.push(` error: Error | null;`);
|
|
129
|
+
lines.push(` reset: () => void;`);
|
|
130
|
+
lines.push(`}`);
|
|
131
|
+
lines.push(``);
|
|
132
|
+
lines.push(`export interface PaginatedResponse<T> {`);
|
|
133
|
+
lines.push(` items: T[];`);
|
|
134
|
+
lines.push(` total: number;`);
|
|
135
|
+
lines.push(` page: number;`);
|
|
136
|
+
lines.push(` page_size: number;`);
|
|
137
|
+
lines.push(`}`);
|
|
138
|
+
lines.push(``);
|
|
139
|
+
|
|
140
|
+
// Generic primitive hooks. Specific entity / capability hooks below wrap these.
|
|
141
|
+
lines.push(`function useQueryGeneric<T>(method: string, path: string, deps: unknown[]): QueryState<T> {`);
|
|
142
|
+
lines.push(` const client = useApi();`);
|
|
143
|
+
lines.push(` const [data, setData] = useState<T | null>(null);`);
|
|
144
|
+
lines.push(` const [loading, setLoading] = useState(true);`);
|
|
145
|
+
lines.push(` const [error, setError] = useState<Error | null>(null);`);
|
|
146
|
+
lines.push(` const fetchOnce = useCallback(async () => {`);
|
|
147
|
+
lines.push(` setLoading(true); setError(null);`);
|
|
148
|
+
lines.push(` try { const result = await apiFetch<T>(client, method, path); setData(result); }`);
|
|
149
|
+
lines.push(` catch (e: unknown) { setError(e instanceof Error ? e : new Error(String(e))); }`);
|
|
150
|
+
lines.push(` finally { setLoading(false); }`);
|
|
151
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
152
|
+
lines.push(` }, [client, method, path]);`);
|
|
153
|
+
lines.push(` useEffect(() => { void fetchOnce(); /* eslint-disable-next-line react-hooks/exhaustive-deps */ }, deps);`);
|
|
154
|
+
lines.push(` return { data, loading, error, refetch: fetchOnce };`);
|
|
155
|
+
lines.push(`}`);
|
|
156
|
+
lines.push(``);
|
|
157
|
+
lines.push(`function useMutationGeneric<TInput, TOutput>(builder: (input: TInput) => { method: string; path: string; body?: unknown }): MutationState<TInput, TOutput> {`);
|
|
158
|
+
lines.push(` const client = useApi();`);
|
|
159
|
+
lines.push(` const [loading, setLoading] = useState(false);`);
|
|
160
|
+
lines.push(` const [error, setError] = useState<Error | null>(null);`);
|
|
161
|
+
lines.push(` const mutate = useCallback(async (input: TInput): Promise<TOutput> => {`);
|
|
162
|
+
lines.push(` setLoading(true); setError(null);`);
|
|
163
|
+
lines.push(` try { const req = builder(input); return await apiFetch<TOutput>(client, req.method, req.path, req.body); }`);
|
|
164
|
+
lines.push(` catch (e: unknown) { const err = e instanceof Error ? e : new Error(String(e)); setError(err); throw err; }`);
|
|
165
|
+
lines.push(` finally { setLoading(false); }`);
|
|
166
|
+
lines.push(` }, [client, builder]);`);
|
|
167
|
+
lines.push(` const reset = useCallback(() => setError(null), []);`);
|
|
168
|
+
lines.push(` return { mutate, loading, error, reset };`);
|
|
169
|
+
lines.push(`}`);
|
|
170
|
+
lines.push(``);
|
|
171
|
+
|
|
172
|
+
// Per-entity hooks
|
|
173
|
+
for (const mod of apiModules) {
|
|
174
|
+
const model = mod.models[0];
|
|
175
|
+
const entity = toPascalCase(model.name);
|
|
176
|
+
const tableName = toSnakeCase(model.name) + "s";
|
|
177
|
+
const route = `/${tableName}`;
|
|
178
|
+
|
|
179
|
+
lines.push(`// ─── ${entity} ───────────────────────────────────────────────────────────`);
|
|
180
|
+
lines.push(``);
|
|
181
|
+
|
|
182
|
+
// List
|
|
183
|
+
lines.push(`export function useList${entity}(): QueryState<PaginatedResponse<${entity}>> {`);
|
|
184
|
+
lines.push(` return useQueryGeneric<PaginatedResponse<${entity}>>("GET", "${route}", []);`);
|
|
185
|
+
lines.push(`}`);
|
|
186
|
+
lines.push(``);
|
|
187
|
+
|
|
188
|
+
// Read
|
|
189
|
+
lines.push(`export function use${entity}(id: string | null): QueryState<${entity}> {`);
|
|
190
|
+
lines.push(` return useQueryGeneric<${entity}>("GET", id ? \`${route}/\${id}\` : "${route}", [id]);`);
|
|
191
|
+
lines.push(`}`);
|
|
192
|
+
lines.push(``);
|
|
193
|
+
|
|
194
|
+
// Create
|
|
195
|
+
const createInputType = `Partial<${entity}>`;
|
|
196
|
+
lines.push(`export function useCreate${entity}(): MutationState<${createInputType}, ${entity}> {`);
|
|
197
|
+
lines.push(` return useMutationGeneric<${createInputType}, ${entity}>((input) => ({ method: "POST", path: "${route}", body: input }));`);
|
|
198
|
+
lines.push(`}`);
|
|
199
|
+
lines.push(``);
|
|
200
|
+
|
|
201
|
+
// Update
|
|
202
|
+
lines.push(`export function useUpdate${entity}(): MutationState<{ id: string; patch: Partial<${entity}> }, ${entity}> {`);
|
|
203
|
+
lines.push(` return useMutationGeneric<{ id: string; patch: Partial<${entity}> }, ${entity}>(({ id, patch }) => ({ method: "PUT", path: \`${route}/\${id}\`, body: patch }));`);
|
|
204
|
+
lines.push(`}`);
|
|
205
|
+
lines.push(``);
|
|
206
|
+
|
|
207
|
+
// Delete
|
|
208
|
+
lines.push(`export function useDelete${entity}(): MutationState<string, void> {`);
|
|
209
|
+
lines.push(` return useMutationGeneric<string, void>((id) => ({ method: "DELETE", path: \`${route}/\${id}\` }));`);
|
|
210
|
+
lines.push(`}`);
|
|
211
|
+
lines.push(``);
|
|
212
|
+
|
|
213
|
+
// Capability hooks
|
|
214
|
+
for (const iface of mod.interfaces) {
|
|
215
|
+
for (const method of iface.methods) {
|
|
216
|
+
if (["create", "read", "update", "delete", "list"].includes(method.name)) continue;
|
|
217
|
+
const capName = toPascalCase(method.name);
|
|
218
|
+
const inputType = method.input.length > 0
|
|
219
|
+
? `{ ${method.input.map(p => `${p.name}${p.nullable ? "?" : ""}: ${toTsType(p.type)}`).join("; ")} }`
|
|
220
|
+
: "Record<string, never>";
|
|
221
|
+
const endpoint = `${route}/${method.name.replace(/_/g, "-")}`;
|
|
222
|
+
lines.push(`export function useCapability${capName}(): MutationState<${inputType}, unknown> {`);
|
|
223
|
+
lines.push(` return useMutationGeneric<${inputType}, unknown>((input) => ({ method: "POST", path: "${endpoint}", body: input }));`);
|
|
224
|
+
lines.push(`}`);
|
|
225
|
+
lines.push(``);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
path: "sdk/react.ts",
|
|
232
|
+
content: lines.join("\n"),
|
|
233
|
+
language: "typescript",
|
|
234
|
+
source_module: "sdk",
|
|
235
|
+
};
|
|
236
|
+
}
|