tthr 0.3.19 → 0.3.21
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/{chunk-DR6CWO75.js → chunk-5S4QNCO2.js} +1 -1
- package/dist/{generate-26HERX3T.js → generate-FEUPHOUR.js} +1 -1
- package/dist/index.js +41 -23
- package/package.json +3 -3
- package/dist/chunk-2IDRK6NG.js +0 -416
- package/dist/chunk-2OI6PDXE.js +0 -491
- package/dist/chunk-2QPN6NDY.js +0 -721
- package/dist/chunk-6PDXNJZH.js +0 -721
- package/dist/chunk-CPBP6GSK.js +0 -684
- package/dist/chunk-E5LJW52C.js +0 -465
- package/dist/chunk-EIJEC3SG.js +0 -468
- package/dist/chunk-FBTKIMH3.js +0 -546
- package/dist/chunk-FUIPVG5Y.js +0 -692
- package/dist/chunk-GKELUQ44.js +0 -502
- package/dist/chunk-GYMF5VG7.js +0 -617
- package/dist/chunk-HNRJ3DYB.js +0 -542
- package/dist/chunk-JPZQRYD3.js +0 -502
- package/dist/chunk-LXODXQ5V.js +0 -418
- package/dist/chunk-MCKGQKYU.js +0 -15
- package/dist/chunk-NDEYB3IH.js +0 -538
- package/dist/chunk-QAZWXLPE.js +0 -491
- package/dist/chunk-QNUE3MEY.js +0 -709
- package/dist/chunk-QOJ5HQFG.js +0 -481
- package/dist/chunk-QUS263RB.js +0 -653
- package/dist/chunk-T4NH3NQS.js +0 -471
- package/dist/chunk-TU5LOAQ5.js +0 -418
- package/dist/chunk-VBD4DOWS.js +0 -649
- package/dist/chunk-WVCYYQ3X.js +0 -649
- package/dist/chunk-Z6EHWFL2.js +0 -469
- package/dist/chunk-ZWLVHKNL.js +0 -414
- package/dist/generate-56PV7NJX.js +0 -8
- package/dist/generate-5AY6JNCG.js +0 -8
- package/dist/generate-7DINPVQ5.js +0 -8
- package/dist/generate-BBHML7RQ.js +0 -8
- package/dist/generate-C3VMXUXE.js +0 -8
- package/dist/generate-CQIJJTKG.js +0 -8
- package/dist/generate-DYQ67MHG.js +0 -8
- package/dist/generate-F7VDD6JW.js +0 -8
- package/dist/generate-FTLMBVLH.js +0 -8
- package/dist/generate-HRIVNCH3.js +0 -8
- package/dist/generate-IJY2CQM4.js +0 -8
- package/dist/generate-KAPPABOL.js +0 -8
- package/dist/generate-KZ2UQ2E6.js +0 -8
- package/dist/generate-LVU4IEMB.js +0 -8
- package/dist/generate-M7G5AXMR.js +0 -8
- package/dist/generate-O66KTG5U.js +0 -8
- package/dist/generate-OAXWQ64W.js +0 -8
- package/dist/generate-OIXJM3VV.js +0 -8
- package/dist/generate-OKE6YGA3.js +0 -8
- package/dist/generate-PQY2ELXC.js +0 -8
- package/dist/generate-Q4MUBHHO.js +0 -8
- package/dist/generate-RVYWR72S.js +0 -8
- package/dist/generate-YQ4QRPXF.js +0 -8
- package/dist/generate-ZHAYHYVW.js +0 -8
- package/dist/generate-ZY23LDYI.js +0 -8
- package/dist/open-OC46UHP7.js +0 -344
|
@@ -86,7 +86,7 @@ async function resolveEnvironmentFromApiKey(cwd = process.cwd()) {
|
|
|
86
86
|
return prefix;
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
|
-
var DEFAULT_API_URL = "https://api.
|
|
89
|
+
var DEFAULT_API_URL = "https://api.tetherdb.co.uk";
|
|
90
90
|
function getApiUrl(config) {
|
|
91
91
|
if (process.env.TETHER_API_URL) {
|
|
92
92
|
return process.env.TETHER_API_URL.replace(/\/+$/, "");
|
package/dist/index.js
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
resolveEnvironmentFromApiKey,
|
|
14
14
|
resolvePath,
|
|
15
15
|
saveCredentials
|
|
16
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-5S4QNCO2.js";
|
|
17
17
|
|
|
18
18
|
// src/index.ts
|
|
19
19
|
import { Command } from "commander";
|
|
@@ -35,6 +35,42 @@ import ora from "ora";
|
|
|
35
35
|
import fs from "fs-extra";
|
|
36
36
|
import path from "path";
|
|
37
37
|
import * as esbuild from "esbuild";
|
|
38
|
+
async function describeHttpError(response, action) {
|
|
39
|
+
const status = response.status;
|
|
40
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
41
|
+
const text = await response.text().catch(() => "");
|
|
42
|
+
if (contentType.includes("application/json")) {
|
|
43
|
+
try {
|
|
44
|
+
const json = JSON.parse(text);
|
|
45
|
+
if (typeof json?.error === "string") return json.error;
|
|
46
|
+
} catch {
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const looksLikeHtml = contentType.includes("text/html") || /^\s*<(!doctype|html)/i.test(text);
|
|
50
|
+
if (looksLikeHtml) {
|
|
51
|
+
const hint = (() => {
|
|
52
|
+
switch (status) {
|
|
53
|
+
case 401:
|
|
54
|
+
case 403:
|
|
55
|
+
return "Authentication failed or insufficient permissions. Try `tthr login` first.";
|
|
56
|
+
case 404:
|
|
57
|
+
return `Route or resource not found. Common causes: wrong project ID, environment doesn't exist on this server, or TETHER_API_URL points at the wrong host.`;
|
|
58
|
+
case 429:
|
|
59
|
+
return "Rate-limited by the server or upstream proxy. Wait a moment and retry.";
|
|
60
|
+
case 502:
|
|
61
|
+
case 503:
|
|
62
|
+
case 504:
|
|
63
|
+
return "Server is unreachable, restarting, or under load. Check status and retry.";
|
|
64
|
+
default:
|
|
65
|
+
return `Upstream proxy returned a generic ${status} page (likely a Cloudflare / nginx fallback).`;
|
|
66
|
+
}
|
|
67
|
+
})();
|
|
68
|
+
return `Failed to ${action}: ${hint} (HTTP ${status})`;
|
|
69
|
+
}
|
|
70
|
+
const trimmed = text.trim();
|
|
71
|
+
if (trimmed) return `${trimmed.slice(0, 300)}${trimmed.length > 300 ? "\u2026" : ""} (HTTP ${status})`;
|
|
72
|
+
return `HTTP ${status}: ${response.statusText || "request failed"}`;
|
|
73
|
+
}
|
|
38
74
|
async function deployCommand(options) {
|
|
39
75
|
const credentials = await requireAuth();
|
|
40
76
|
const configPath = path.resolve(process.cwd(), "tether.config.ts");
|
|
@@ -146,16 +182,7 @@ async function deploySchemaToServer(projectId, token, schemaPath, environment, d
|
|
|
146
182
|
console.log(chalk.dim(` Response: ${response.status} ${response.statusText}`));
|
|
147
183
|
console.log(chalk.dim(` Content-Type: ${response.headers.get("content-type")}`));
|
|
148
184
|
if (!response.ok) {
|
|
149
|
-
|
|
150
|
-
console.log(chalk.dim(` Body: ${text.slice(0, 500)}`));
|
|
151
|
-
let errorMessage;
|
|
152
|
-
try {
|
|
153
|
-
const error = JSON.parse(text);
|
|
154
|
-
errorMessage = error.error || `HTTP ${response.status}`;
|
|
155
|
-
} catch {
|
|
156
|
-
errorMessage = text || `HTTP ${response.status}: ${response.statusText}`;
|
|
157
|
-
}
|
|
158
|
-
throw new Error(errorMessage);
|
|
185
|
+
throw new Error(await describeHttpError(response, "deploy schema"));
|
|
159
186
|
}
|
|
160
187
|
spinner.succeed(`Schema deployed (${tables.length} table(s))`);
|
|
161
188
|
} catch (error) {
|
|
@@ -269,16 +296,7 @@ ${helperSource}`;
|
|
|
269
296
|
console.log(chalk.dim(` Response: ${response.status} ${response.statusText}`));
|
|
270
297
|
console.log(chalk.dim(` Content-Type: ${response.headers.get("content-type")}`));
|
|
271
298
|
if (!response.ok) {
|
|
272
|
-
|
|
273
|
-
console.log(chalk.dim(` Body: ${text.slice(0, 500)}`));
|
|
274
|
-
let errorMessage;
|
|
275
|
-
try {
|
|
276
|
-
const error = JSON.parse(text);
|
|
277
|
-
errorMessage = error.error || `HTTP ${response.status}`;
|
|
278
|
-
} catch {
|
|
279
|
-
errorMessage = text || `HTTP ${response.status}: ${response.statusText}`;
|
|
280
|
-
}
|
|
281
|
-
throw new Error(errorMessage);
|
|
299
|
+
throw new Error(await describeHttpError(response, "deploy functions"));
|
|
282
300
|
}
|
|
283
301
|
spinner.succeed(`Functions deployed (${functions.length} function(s))`);
|
|
284
302
|
const queries = functions.filter((f) => f.type === "query");
|
|
@@ -1705,7 +1723,7 @@ async function runGenerate(spinner) {
|
|
|
1705
1723
|
}
|
|
1706
1724
|
isGenerating = true;
|
|
1707
1725
|
try {
|
|
1708
|
-
const { generateTypes: generateTypes2 } = await import("./generate-
|
|
1726
|
+
const { generateTypes: generateTypes2 } = await import("./generate-FEUPHOUR.js");
|
|
1709
1727
|
spinner.text = "Regenerating types...";
|
|
1710
1728
|
await generateTypes2({ silent: true });
|
|
1711
1729
|
spinner.succeed("Types regenerated");
|
|
@@ -1977,7 +1995,7 @@ async function devCommand(options) {
|
|
|
1977
1995
|
}
|
|
1978
1996
|
}
|
|
1979
1997
|
spinner.text = "Generating types...";
|
|
1980
|
-
const { generateTypes: generateTypes2 } = await import("./generate-
|
|
1998
|
+
const { generateTypes: generateTypes2 } = await import("./generate-FEUPHOUR.js");
|
|
1981
1999
|
await generateTypes2({ silent: true });
|
|
1982
2000
|
spinner.succeed("Types generated");
|
|
1983
2001
|
if (config.projectId) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tthr",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.21",
|
|
4
4
|
"description": "Tether CLI - project scaffolding and deployment",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
20
|
"scripts": {
|
|
21
|
-
"build": "tsup src/index.ts --format esm --dts",
|
|
22
|
-
"dev": "tsup src/index.ts --format esm --watch",
|
|
21
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
22
|
+
"dev": "tsup src/index.ts --format esm --watch --clean",
|
|
23
23
|
"typecheck": "tsc --noEmit",
|
|
24
24
|
"start": "TETHER_DEV=true node dist/index.js"
|
|
25
25
|
},
|
package/dist/chunk-2IDRK6NG.js
DELETED
|
@@ -1,416 +0,0 @@
|
|
|
1
|
-
// src/commands/generate.ts
|
|
2
|
-
import chalk2 from "chalk";
|
|
3
|
-
import ora from "ora";
|
|
4
|
-
import fs3 from "fs-extra";
|
|
5
|
-
import path3 from "path";
|
|
6
|
-
|
|
7
|
-
// src/utils/auth.ts
|
|
8
|
-
import chalk from "chalk";
|
|
9
|
-
import fs from "fs-extra";
|
|
10
|
-
import path from "path";
|
|
11
|
-
var CONFIG_DIR = path.join(process.env.HOME || process.env.USERPROFILE || "", ".tether");
|
|
12
|
-
var CREDENTIALS_FILE = path.join(CONFIG_DIR, "credentials.json");
|
|
13
|
-
async function getCredentials() {
|
|
14
|
-
try {
|
|
15
|
-
if (await fs.pathExists(CREDENTIALS_FILE)) {
|
|
16
|
-
return await fs.readJSON(CREDENTIALS_FILE);
|
|
17
|
-
}
|
|
18
|
-
} catch {
|
|
19
|
-
}
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
async function requireAuth() {
|
|
23
|
-
const credentials = await getCredentials();
|
|
24
|
-
if (!credentials) {
|
|
25
|
-
console.error(chalk.red("\n\u2717 Not logged in\n"));
|
|
26
|
-
console.log(chalk.dim("Run `tthr login` to authenticate\n"));
|
|
27
|
-
process.exit(1);
|
|
28
|
-
}
|
|
29
|
-
if (credentials.expiresAt) {
|
|
30
|
-
const expiresAt = new Date(credentials.expiresAt);
|
|
31
|
-
if (/* @__PURE__ */ new Date() > expiresAt) {
|
|
32
|
-
console.error(chalk.red("\n\u2717 Session expired\n"));
|
|
33
|
-
console.log(chalk.dim("Run `tthr login` to authenticate\n"));
|
|
34
|
-
process.exit(1);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return credentials;
|
|
38
|
-
}
|
|
39
|
-
async function saveCredentials(credentials) {
|
|
40
|
-
await fs.ensureDir(CONFIG_DIR);
|
|
41
|
-
await fs.writeJSON(CREDENTIALS_FILE, credentials, { spaces: 2, mode: 384 });
|
|
42
|
-
}
|
|
43
|
-
async function clearCredentials() {
|
|
44
|
-
try {
|
|
45
|
-
await fs.remove(CREDENTIALS_FILE);
|
|
46
|
-
} catch {
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// src/utils/config.ts
|
|
51
|
-
import fs2 from "fs-extra";
|
|
52
|
-
import path2 from "path";
|
|
53
|
-
var DEFAULT_CONFIG = {
|
|
54
|
-
schema: "./tether/schema.ts",
|
|
55
|
-
functions: "./tether/functions",
|
|
56
|
-
output: "./tether/_generated",
|
|
57
|
-
dev: {
|
|
58
|
-
port: 3001,
|
|
59
|
-
host: "localhost"
|
|
60
|
-
},
|
|
61
|
-
database: {
|
|
62
|
-
walMode: true
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
async function loadConfig(cwd = process.cwd()) {
|
|
66
|
-
const configPath = path2.resolve(cwd, "tether.config.ts");
|
|
67
|
-
if (!await fs2.pathExists(configPath)) {
|
|
68
|
-
return DEFAULT_CONFIG;
|
|
69
|
-
}
|
|
70
|
-
const configSource = await fs2.readFile(configPath, "utf-8");
|
|
71
|
-
const config = {};
|
|
72
|
-
const schemaMatch = configSource.match(/schema\s*:\s*['"]([^'"]+)['"]/);
|
|
73
|
-
if (schemaMatch) {
|
|
74
|
-
config.schema = schemaMatch[1];
|
|
75
|
-
}
|
|
76
|
-
const functionsMatch = configSource.match(/functions\s*:\s*['"]([^'"]+)['"]/);
|
|
77
|
-
if (functionsMatch) {
|
|
78
|
-
config.functions = functionsMatch[1];
|
|
79
|
-
}
|
|
80
|
-
const outputMatch = configSource.match(/output\s*:\s*['"]([^'"]+)['"]/);
|
|
81
|
-
if (outputMatch) {
|
|
82
|
-
config.output = outputMatch[1];
|
|
83
|
-
}
|
|
84
|
-
const envMatch = configSource.match(/environment\s*:\s*['"]([^'"]+)['"]/);
|
|
85
|
-
if (envMatch) {
|
|
86
|
-
config.environment = envMatch[1];
|
|
87
|
-
}
|
|
88
|
-
const portMatch = configSource.match(/port\s*:\s*(\d+)/);
|
|
89
|
-
if (portMatch) {
|
|
90
|
-
config.dev = { ...config.dev, port: parseInt(portMatch[1], 10) };
|
|
91
|
-
}
|
|
92
|
-
const hostMatch = configSource.match(/host\s*:\s*['"]([^'"]+)['"]/);
|
|
93
|
-
if (hostMatch) {
|
|
94
|
-
config.dev = { ...config.dev, host: hostMatch[1] };
|
|
95
|
-
}
|
|
96
|
-
return {
|
|
97
|
-
...DEFAULT_CONFIG,
|
|
98
|
-
...config,
|
|
99
|
-
dev: { ...DEFAULT_CONFIG.dev, ...config.dev },
|
|
100
|
-
database: { ...DEFAULT_CONFIG.database, ...config.database }
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
function resolvePath(configPath, cwd = process.cwd()) {
|
|
104
|
-
const normalised = configPath.replace(/^\.\//, "");
|
|
105
|
-
return path2.resolve(cwd, normalised);
|
|
106
|
-
}
|
|
107
|
-
async function detectFramework(cwd = process.cwd()) {
|
|
108
|
-
const packageJsonPath = path2.resolve(cwd, "package.json");
|
|
109
|
-
if (!await fs2.pathExists(packageJsonPath)) {
|
|
110
|
-
return "unknown";
|
|
111
|
-
}
|
|
112
|
-
try {
|
|
113
|
-
const packageJson = await fs2.readJson(packageJsonPath);
|
|
114
|
-
const deps = {
|
|
115
|
-
...packageJson.dependencies,
|
|
116
|
-
...packageJson.devDependencies
|
|
117
|
-
};
|
|
118
|
-
if (deps.nuxt || deps["@nuxt/kit"]) {
|
|
119
|
-
return "nuxt";
|
|
120
|
-
}
|
|
121
|
-
if (deps.next) {
|
|
122
|
-
return "next";
|
|
123
|
-
}
|
|
124
|
-
if (deps["@sveltejs/kit"]) {
|
|
125
|
-
return "sveltekit";
|
|
126
|
-
}
|
|
127
|
-
if (deps.vite && !deps.nuxt && !deps.next && !deps["@sveltejs/kit"]) {
|
|
128
|
-
return "vite";
|
|
129
|
-
}
|
|
130
|
-
return "vanilla";
|
|
131
|
-
} catch {
|
|
132
|
-
return "unknown";
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
function getFrameworkDevCommand(framework) {
|
|
136
|
-
switch (framework) {
|
|
137
|
-
case "nuxt":
|
|
138
|
-
return "nuxt dev";
|
|
139
|
-
case "next":
|
|
140
|
-
return "next dev";
|
|
141
|
-
case "sveltekit":
|
|
142
|
-
return "vite dev";
|
|
143
|
-
case "vite":
|
|
144
|
-
return "vite dev";
|
|
145
|
-
case "vanilla":
|
|
146
|
-
return null;
|
|
147
|
-
// No default dev server for vanilla
|
|
148
|
-
default:
|
|
149
|
-
return null;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// src/commands/generate.ts
|
|
154
|
-
function parseSchemaFile(source) {
|
|
155
|
-
const tables = [];
|
|
156
|
-
const schemaMatch = source.match(/defineSchema\s*\(\s*\{([\s\S]*)\}\s*\)/);
|
|
157
|
-
if (!schemaMatch) return tables;
|
|
158
|
-
const schemaContent = schemaMatch[1];
|
|
159
|
-
const tableStartRegex = /(\w+)\s*:\s*\{/g;
|
|
160
|
-
let match;
|
|
161
|
-
while ((match = tableStartRegex.exec(schemaContent)) !== null) {
|
|
162
|
-
const tableName = match[1];
|
|
163
|
-
const startOffset = match.index + match[0].length;
|
|
164
|
-
let braceCount = 1;
|
|
165
|
-
let endOffset = startOffset;
|
|
166
|
-
for (let i = startOffset; i < schemaContent.length && braceCount > 0; i++) {
|
|
167
|
-
const char = schemaContent[i];
|
|
168
|
-
if (char === "{") braceCount++;
|
|
169
|
-
else if (char === "}") braceCount--;
|
|
170
|
-
endOffset = i;
|
|
171
|
-
}
|
|
172
|
-
const columnsContent = schemaContent.slice(startOffset, endOffset);
|
|
173
|
-
const columns = parseColumns(columnsContent);
|
|
174
|
-
tables.push({ name: tableName, columns });
|
|
175
|
-
}
|
|
176
|
-
return tables;
|
|
177
|
-
}
|
|
178
|
-
function parseColumns(content) {
|
|
179
|
-
const columns = [];
|
|
180
|
-
const columnRegex = /(\w+)\s*:\s*(\w+)\s*\(\s*\)([^,\n}]*)/g;
|
|
181
|
-
let match;
|
|
182
|
-
while ((match = columnRegex.exec(content)) !== null) {
|
|
183
|
-
const name = match[1];
|
|
184
|
-
const schemaType = match[2];
|
|
185
|
-
const modifiers = match[3] || "";
|
|
186
|
-
const nullable = !modifiers.includes(".notNull()");
|
|
187
|
-
columns.push({
|
|
188
|
-
name,
|
|
189
|
-
type: schemaTypeToTS(schemaType),
|
|
190
|
-
nullable
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
return columns;
|
|
194
|
-
}
|
|
195
|
-
function schemaTypeToTS(schemaType) {
|
|
196
|
-
switch (schemaType) {
|
|
197
|
-
case "text":
|
|
198
|
-
return "string";
|
|
199
|
-
case "integer":
|
|
200
|
-
return "number";
|
|
201
|
-
case "real":
|
|
202
|
-
return "number";
|
|
203
|
-
case "boolean":
|
|
204
|
-
return "boolean";
|
|
205
|
-
case "timestamp":
|
|
206
|
-
return "string";
|
|
207
|
-
case "json":
|
|
208
|
-
return "unknown";
|
|
209
|
-
case "blob":
|
|
210
|
-
return "Uint8Array";
|
|
211
|
-
default:
|
|
212
|
-
return "unknown";
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
function tableNameToInterface(tableName) {
|
|
216
|
-
let name = tableName;
|
|
217
|
-
if (name.endsWith("ies")) {
|
|
218
|
-
name = name.slice(0, -3) + "y";
|
|
219
|
-
} else if (name.endsWith("s") && !name.endsWith("ss")) {
|
|
220
|
-
name = name.slice(0, -1);
|
|
221
|
-
}
|
|
222
|
-
return name.charAt(0).toUpperCase() + name.slice(1);
|
|
223
|
-
}
|
|
224
|
-
function generateDbFile(tables) {
|
|
225
|
-
const lines = [
|
|
226
|
-
"// Auto-generated by Tether CLI - do not edit manually",
|
|
227
|
-
`// Generated at: ${(/* @__PURE__ */ new Date()).toISOString()}`,
|
|
228
|
-
"",
|
|
229
|
-
"import { createDatabaseProxy, type TetherDatabase } from '@tthr/client';",
|
|
230
|
-
""
|
|
231
|
-
];
|
|
232
|
-
for (const table of tables) {
|
|
233
|
-
const interfaceName = tableNameToInterface(table.name);
|
|
234
|
-
lines.push(`export interface ${interfaceName} {`);
|
|
235
|
-
for (const col of table.columns) {
|
|
236
|
-
const typeStr = col.nullable ? `${col.type} | null` : col.type;
|
|
237
|
-
lines.push(` ${col.name}: ${typeStr};`);
|
|
238
|
-
}
|
|
239
|
-
lines.push("}");
|
|
240
|
-
lines.push("");
|
|
241
|
-
}
|
|
242
|
-
lines.push("export interface Schema {");
|
|
243
|
-
for (const table of tables) {
|
|
244
|
-
const interfaceName = tableNameToInterface(table.name);
|
|
245
|
-
lines.push(` ${table.name}: ${interfaceName};`);
|
|
246
|
-
}
|
|
247
|
-
lines.push("}");
|
|
248
|
-
lines.push("");
|
|
249
|
-
lines.push("// Database client with typed tables");
|
|
250
|
-
lines.push("// This is a proxy that will be populated by the Tether runtime");
|
|
251
|
-
lines.push("export const db: TetherDatabase<Schema> = createDatabaseProxy<Schema>();");
|
|
252
|
-
lines.push("");
|
|
253
|
-
return lines.join("\n");
|
|
254
|
-
}
|
|
255
|
-
async function parseFunctionsDir(functionsDir) {
|
|
256
|
-
const functions = [];
|
|
257
|
-
if (!await fs3.pathExists(functionsDir)) {
|
|
258
|
-
return functions;
|
|
259
|
-
}
|
|
260
|
-
const files = await fs3.readdir(functionsDir);
|
|
261
|
-
for (const file of files) {
|
|
262
|
-
if (!file.endsWith(".ts") && !file.endsWith(".js")) continue;
|
|
263
|
-
const filePath = path3.join(functionsDir, file);
|
|
264
|
-
const stat = await fs3.stat(filePath);
|
|
265
|
-
if (!stat.isFile()) continue;
|
|
266
|
-
const moduleName = file.replace(/\.(ts|js)$/, "");
|
|
267
|
-
const source = await fs3.readFile(filePath, "utf-8");
|
|
268
|
-
const exportRegex = /export\s+const\s+(\w+)\s*=\s*(query|mutation|action)\s*\(/g;
|
|
269
|
-
let match;
|
|
270
|
-
while ((match = exportRegex.exec(source)) !== null) {
|
|
271
|
-
functions.push({
|
|
272
|
-
name: match[1],
|
|
273
|
-
moduleName
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return functions;
|
|
278
|
-
}
|
|
279
|
-
async function generateApiFile(functionsDir) {
|
|
280
|
-
const functions = await parseFunctionsDir(functionsDir);
|
|
281
|
-
const moduleMap = /* @__PURE__ */ new Map();
|
|
282
|
-
for (const fn of functions) {
|
|
283
|
-
if (!moduleMap.has(fn.moduleName)) {
|
|
284
|
-
moduleMap.set(fn.moduleName, []);
|
|
285
|
-
}
|
|
286
|
-
moduleMap.get(fn.moduleName).push(fn.name);
|
|
287
|
-
}
|
|
288
|
-
const lines = [
|
|
289
|
-
"// Auto-generated by Tether CLI - do not edit manually",
|
|
290
|
-
`// Generated at: ${(/* @__PURE__ */ new Date()).toISOString()}`,
|
|
291
|
-
"",
|
|
292
|
-
"import { createApiProxy } from '@tthr/client';",
|
|
293
|
-
"",
|
|
294
|
-
"/**",
|
|
295
|
-
" * API function reference type for useQuery/useMutation.",
|
|
296
|
-
' * The _name property contains the function path (e.g., "users.list").',
|
|
297
|
-
" */",
|
|
298
|
-
"export interface ApiFunction<TArgs = unknown, TResult = unknown> {",
|
|
299
|
-
" _name: string;",
|
|
300
|
-
" _args?: TArgs;",
|
|
301
|
-
" _result?: TResult;",
|
|
302
|
-
"}",
|
|
303
|
-
""
|
|
304
|
-
];
|
|
305
|
-
if (moduleMap.size > 0) {
|
|
306
|
-
for (const [moduleName, fnNames] of moduleMap) {
|
|
307
|
-
const interfaceName = moduleName.charAt(0).toUpperCase() + moduleName.slice(1) + "Api";
|
|
308
|
-
lines.push(`export interface ${interfaceName} {`);
|
|
309
|
-
for (const fnName of fnNames) {
|
|
310
|
-
lines.push(` ${fnName}: ApiFunction;`);
|
|
311
|
-
}
|
|
312
|
-
lines.push("}");
|
|
313
|
-
lines.push("");
|
|
314
|
-
}
|
|
315
|
-
lines.push("export interface Api {");
|
|
316
|
-
for (const moduleName of moduleMap.keys()) {
|
|
317
|
-
const interfaceName = moduleName.charAt(0).toUpperCase() + moduleName.slice(1) + "Api";
|
|
318
|
-
lines.push(` ${moduleName}: ${interfaceName};`);
|
|
319
|
-
}
|
|
320
|
-
lines.push("}");
|
|
321
|
-
} else {
|
|
322
|
-
lines.push("/**");
|
|
323
|
-
lines.push(" * Flexible API type that allows access to any function path.");
|
|
324
|
-
lines.push(" * Functions are accessed as api.moduleName.functionName");
|
|
325
|
-
lines.push(" * The actual types depend on your function definitions.");
|
|
326
|
-
lines.push(" */");
|
|
327
|
-
lines.push("export type Api = {");
|
|
328
|
-
lines.push(" [module: string]: {");
|
|
329
|
-
lines.push(" [fn: string]: ApiFunction;");
|
|
330
|
-
lines.push(" };");
|
|
331
|
-
lines.push("};");
|
|
332
|
-
}
|
|
333
|
-
lines.push("");
|
|
334
|
-
lines.push("// API client proxy - provides typed access to your functions");
|
|
335
|
-
lines.push("// On the client: returns { _name } references for useQuery/useMutation");
|
|
336
|
-
lines.push("// In Tether functions: calls the actual function implementation");
|
|
337
|
-
lines.push("export const api = createApiProxy<Api>();");
|
|
338
|
-
lines.push("");
|
|
339
|
-
return lines.join("\n");
|
|
340
|
-
}
|
|
341
|
-
async function generateTypes(options = {}) {
|
|
342
|
-
const config = await loadConfig();
|
|
343
|
-
const schemaPath = resolvePath(config.schema);
|
|
344
|
-
const outputDir = resolvePath(config.output);
|
|
345
|
-
const functionsDir = resolvePath(config.functions);
|
|
346
|
-
if (!await fs3.pathExists(schemaPath)) {
|
|
347
|
-
throw new Error(`Schema file not found: ${schemaPath}`);
|
|
348
|
-
}
|
|
349
|
-
const schemaSource = await fs3.readFile(schemaPath, "utf-8");
|
|
350
|
-
const tables = parseSchemaFile(schemaSource);
|
|
351
|
-
await fs3.ensureDir(outputDir);
|
|
352
|
-
await fs3.writeFile(
|
|
353
|
-
path3.join(outputDir, "db.ts"),
|
|
354
|
-
generateDbFile(tables)
|
|
355
|
-
);
|
|
356
|
-
await fs3.writeFile(
|
|
357
|
-
path3.join(outputDir, "api.ts"),
|
|
358
|
-
await generateApiFile(functionsDir)
|
|
359
|
-
);
|
|
360
|
-
await fs3.writeFile(
|
|
361
|
-
path3.join(outputDir, "index.ts"),
|
|
362
|
-
`// Auto-generated by Tether CLI - do not edit manually
|
|
363
|
-
export * from './db';
|
|
364
|
-
export * from './api';
|
|
365
|
-
`
|
|
366
|
-
);
|
|
367
|
-
return { tables, outputDir };
|
|
368
|
-
}
|
|
369
|
-
async function generateCommand() {
|
|
370
|
-
await requireAuth();
|
|
371
|
-
const configPath = path3.resolve(process.cwd(), "tether.config.ts");
|
|
372
|
-
if (!await fs3.pathExists(configPath)) {
|
|
373
|
-
console.log(chalk2.red("\nError: Not a Tether project"));
|
|
374
|
-
console.log(chalk2.dim("Run `tthr init` to create a new project\n"));
|
|
375
|
-
process.exit(1);
|
|
376
|
-
}
|
|
377
|
-
console.log(chalk2.bold("\n\u26A1 Generating types from schema\n"));
|
|
378
|
-
const spinner = ora("Reading schema...").start();
|
|
379
|
-
try {
|
|
380
|
-
spinner.text = "Generating types...";
|
|
381
|
-
const { tables, outputDir } = await generateTypes();
|
|
382
|
-
if (tables.length === 0) {
|
|
383
|
-
spinner.warn("No tables found in schema");
|
|
384
|
-
console.log(chalk2.dim(" Make sure your schema uses defineSchema({ ... })\n"));
|
|
385
|
-
return;
|
|
386
|
-
}
|
|
387
|
-
spinner.succeed(`Types generated for ${tables.length} table(s)`);
|
|
388
|
-
console.log("\n" + chalk2.green("\u2713") + " Tables:");
|
|
389
|
-
for (const table of tables) {
|
|
390
|
-
console.log(chalk2.dim(` - ${table.name} (${table.columns.length} columns)`));
|
|
391
|
-
}
|
|
392
|
-
const relativeOutput = path3.relative(process.cwd(), outputDir);
|
|
393
|
-
console.log("\n" + chalk2.green("\u2713") + " Generated files:");
|
|
394
|
-
console.log(chalk2.dim(` ${relativeOutput}/db.ts`));
|
|
395
|
-
console.log(chalk2.dim(` ${relativeOutput}/api.ts`));
|
|
396
|
-
console.log(chalk2.dim(` ${relativeOutput}/index.ts
|
|
397
|
-
`));
|
|
398
|
-
} catch (error) {
|
|
399
|
-
spinner.fail("Failed to generate types");
|
|
400
|
-
console.error(chalk2.red(error instanceof Error ? error.message : "Unknown error"));
|
|
401
|
-
process.exit(1);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
export {
|
|
406
|
-
getCredentials,
|
|
407
|
-
requireAuth,
|
|
408
|
-
saveCredentials,
|
|
409
|
-
clearCredentials,
|
|
410
|
-
loadConfig,
|
|
411
|
-
resolvePath,
|
|
412
|
-
detectFramework,
|
|
413
|
-
getFrameworkDevCommand,
|
|
414
|
-
generateTypes,
|
|
415
|
-
generateCommand
|
|
416
|
-
};
|