@butterbase/cli 0.1.0 → 0.1.2
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 +1 -1
- package/dist/bin/butterbase.js +7 -2
- package/dist/bin/butterbase.js.map +1 -1
- package/dist/src/commands/functions.d.ts +3 -0
- package/dist/src/commands/functions.d.ts.map +1 -1
- package/dist/src/commands/functions.js +15 -1
- package/dist/src/commands/functions.js.map +1 -1
- package/dist/src/commands/init.d.ts.map +1 -1
- package/dist/src/commands/init.js +17 -1
- package/dist/src/commands/init.js.map +1 -1
- package/dist/src/lib/api-client.d.ts +1 -0
- package/dist/src/lib/api-client.d.ts.map +1 -1
- package/dist/src/lib/api-client.js +3 -0
- package/dist/src/lib/api-client.js.map +1 -1
- package/package.json +7 -2
- package/templates/react-vite/package.json +1 -1
- package/bin/butterbase.ts +0 -137
- package/src/commands/apps.ts +0 -130
- package/src/commands/config.ts +0 -50
- package/src/commands/functions.ts +0 -116
- package/src/commands/init.ts +0 -151
- package/src/commands/schema.ts +0 -78
- package/src/commands/storage.ts +0 -117
- package/src/lib/api-client.ts +0 -176
- package/src/lib/config.ts +0 -112
- package/tsconfig.json +0 -20
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import ora from 'ora';
|
|
3
|
-
import fs from 'fs-extra';
|
|
4
|
-
import { deployFunction, listFunctions, getFunctionLogs } from '../lib/api-client.js';
|
|
5
|
-
import { getCurrentAppId } from '../lib/config.js';
|
|
6
|
-
|
|
7
|
-
async function requireAppId(appId?: string): Promise<string> {
|
|
8
|
-
if (appId) return appId;
|
|
9
|
-
|
|
10
|
-
const currentAppId = await getCurrentAppId();
|
|
11
|
-
if (!currentAppId) {
|
|
12
|
-
console.log(chalk.red('✗ No app specified and no current app set'));
|
|
13
|
-
console.log(chalk.gray('Use: butterbase apps use <app-id>'));
|
|
14
|
-
process.exit(1);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return currentAppId;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export async function functionsListCommand(options: { app?: string }) {
|
|
21
|
-
const appId = await requireAppId(options.app);
|
|
22
|
-
const spinner = ora('Fetching functions...').start();
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
const response: any = await listFunctions(appId);
|
|
26
|
-
spinner.stop();
|
|
27
|
-
|
|
28
|
-
if (!response.functions || response.functions.length === 0) {
|
|
29
|
-
console.log(chalk.yellow('No functions found'));
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
console.log(chalk.blue('\nDeployed functions:\n'));
|
|
34
|
-
for (const func of response.functions) {
|
|
35
|
-
console.log(chalk.bold(func.name));
|
|
36
|
-
console.log(chalk.gray(` Trigger: ${func.trigger_type}`));
|
|
37
|
-
if (func.description) {
|
|
38
|
-
console.log(chalk.gray(` Description: ${func.description}`));
|
|
39
|
-
}
|
|
40
|
-
console.log();
|
|
41
|
-
}
|
|
42
|
-
} catch (error) {
|
|
43
|
-
spinner.fail('Failed to fetch functions');
|
|
44
|
-
console.error(chalk.red((error as Error).message));
|
|
45
|
-
process.exit(1);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export async function functionsDeployCommand(file: string, options: {
|
|
50
|
-
app?: string;
|
|
51
|
-
name?: string;
|
|
52
|
-
trigger?: string;
|
|
53
|
-
description?: string;
|
|
54
|
-
}) {
|
|
55
|
-
const appId = await requireAppId(options.app);
|
|
56
|
-
|
|
57
|
-
if (!await fs.pathExists(file)) {
|
|
58
|
-
console.log(chalk.red(`✗ File not found: ${file}`));
|
|
59
|
-
process.exit(1);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const functionName = options.name || file.replace(/\.(ts|js)$/, '').split('/').pop()!;
|
|
63
|
-
const spinner = ora(`Deploying function "${functionName}"...`).start();
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
const code = await fs.readFile(file, 'utf-8');
|
|
67
|
-
|
|
68
|
-
const triggerType = options.trigger || 'http';
|
|
69
|
-
const trigger = { type: triggerType, config: {} };
|
|
70
|
-
|
|
71
|
-
await deployFunction(appId, {
|
|
72
|
-
name: functionName,
|
|
73
|
-
code,
|
|
74
|
-
description: options.description,
|
|
75
|
-
trigger,
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
spinner.succeed(`Deployed function "${functionName}"`);
|
|
79
|
-
console.log(chalk.green('\n✓ Function deployed successfully!'));
|
|
80
|
-
console.log(chalk.gray(` Invoke URL: /v1/${appId}/fn/${functionName}`));
|
|
81
|
-
} catch (error) {
|
|
82
|
-
spinner.fail('Failed to deploy function');
|
|
83
|
-
console.error(chalk.red((error as Error).message));
|
|
84
|
-
process.exit(1);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export async function functionsLogsCommand(functionName: string, options: {
|
|
89
|
-
app?: string;
|
|
90
|
-
level?: string;
|
|
91
|
-
limit?: number;
|
|
92
|
-
}) {
|
|
93
|
-
const appId = await requireAppId(options.app);
|
|
94
|
-
const spinner = ora('Fetching logs...').start();
|
|
95
|
-
|
|
96
|
-
try {
|
|
97
|
-
const response: any = await getFunctionLogs(appId, functionName, options.level, options.limit);
|
|
98
|
-
spinner.stop();
|
|
99
|
-
|
|
100
|
-
if (!response.logs || response.logs.length === 0) {
|
|
101
|
-
console.log(chalk.yellow('No logs found'));
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
console.log(chalk.blue(`\nLogs for ${functionName}:\n`));
|
|
106
|
-
for (const log of response.logs) {
|
|
107
|
-
const timestamp = new Date(log.timestamp).toLocaleString();
|
|
108
|
-
const levelColor = log.level === 'error' ? chalk.red : chalk.gray;
|
|
109
|
-
console.log(`${chalk.gray(timestamp)} ${levelColor(log.level.toUpperCase())} ${log.message}`);
|
|
110
|
-
}
|
|
111
|
-
} catch (error) {
|
|
112
|
-
spinner.fail('Failed to fetch logs');
|
|
113
|
-
console.error(chalk.red((error as Error).message));
|
|
114
|
-
process.exit(1);
|
|
115
|
-
}
|
|
116
|
-
}
|
package/src/commands/init.ts
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import ora from 'ora';
|
|
3
|
-
import prompts from 'prompts';
|
|
4
|
-
import fs from 'fs-extra';
|
|
5
|
-
import path from 'path';
|
|
6
|
-
import { fileURLToPath } from 'url';
|
|
7
|
-
import { getMergedConfig } from '../lib/config.js';
|
|
8
|
-
|
|
9
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
10
|
-
const __dirname = path.dirname(__filename);
|
|
11
|
-
|
|
12
|
-
interface TemplateVariables {
|
|
13
|
-
PROJECT_NAME: string;
|
|
14
|
-
APP_ID: string;
|
|
15
|
-
API_URL: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async function replaceVariables(content: string, variables: TemplateVariables): Promise<string> {
|
|
19
|
-
let result = content;
|
|
20
|
-
for (const [key, value] of Object.entries(variables)) {
|
|
21
|
-
result = result.replace(new RegExp(`{{${key}}}`, 'g'), value);
|
|
22
|
-
}
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async function copyTemplate(
|
|
27
|
-
templateDir: string,
|
|
28
|
-
targetDir: string,
|
|
29
|
-
variables: TemplateVariables
|
|
30
|
-
): Promise<void> {
|
|
31
|
-
const files = await fs.readdir(templateDir, { withFileTypes: true });
|
|
32
|
-
|
|
33
|
-
for (const file of files) {
|
|
34
|
-
const sourcePath = path.join(templateDir, file.name);
|
|
35
|
-
const targetPath = path.join(targetDir, file.name);
|
|
36
|
-
|
|
37
|
-
if (file.isDirectory()) {
|
|
38
|
-
await fs.ensureDir(targetPath);
|
|
39
|
-
await copyTemplate(sourcePath, targetPath, variables);
|
|
40
|
-
} else {
|
|
41
|
-
const content = await fs.readFile(sourcePath, 'utf-8');
|
|
42
|
-
const processedContent = await replaceVariables(content, variables);
|
|
43
|
-
await fs.writeFile(targetPath, processedContent);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export async function initCommand(template?: string) {
|
|
49
|
-
console.log(chalk.blue('🚀 Initialize Butterbase Project\n'));
|
|
50
|
-
|
|
51
|
-
// Get project name
|
|
52
|
-
const { projectName } = await prompts({
|
|
53
|
-
type: 'text',
|
|
54
|
-
name: 'projectName',
|
|
55
|
-
message: 'Project name:',
|
|
56
|
-
initial: 'my-butterbase-app',
|
|
57
|
-
validate: (value) => value.length > 0 || 'Project name is required',
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
if (!projectName) {
|
|
61
|
-
console.log(chalk.yellow('Cancelled'));
|
|
62
|
-
process.exit(0);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Select template
|
|
66
|
-
if (!template) {
|
|
67
|
-
const { selectedTemplate } = await prompts({
|
|
68
|
-
type: 'select',
|
|
69
|
-
name: 'selectedTemplate',
|
|
70
|
-
message: 'Select a template:',
|
|
71
|
-
choices: [
|
|
72
|
-
{ title: 'React + Vite', value: 'react-vite' },
|
|
73
|
-
{ title: 'Next.js (coming soon)', value: 'nextjs', disabled: true },
|
|
74
|
-
{ title: 'Vue + Vite (coming soon)', value: 'vue-vite', disabled: true },
|
|
75
|
-
],
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
if (!selectedTemplate) {
|
|
79
|
-
console.log(chalk.yellow('Cancelled'));
|
|
80
|
-
process.exit(0);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
template = selectedTemplate;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Get Butterbase configuration
|
|
87
|
-
const config = await getMergedConfig();
|
|
88
|
-
|
|
89
|
-
const { appId } = await prompts({
|
|
90
|
-
type: 'text',
|
|
91
|
-
name: 'appId',
|
|
92
|
-
message: 'Butterbase App ID (leave empty to create later):',
|
|
93
|
-
initial: config.currentApp || '',
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
const apiUrl = config.endpoint || 'http://localhost:4000';
|
|
97
|
-
|
|
98
|
-
// Create project directory
|
|
99
|
-
const targetDir = path.join(process.cwd(), projectName);
|
|
100
|
-
|
|
101
|
-
if (await fs.pathExists(targetDir)) {
|
|
102
|
-
console.log(chalk.red(`✗ Directory "${projectName}" already exists`));
|
|
103
|
-
process.exit(1);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const spinner = ora('Creating project...').start();
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
// Get template directory
|
|
110
|
-
const templateDir = path.join(__dirname, '../../templates', template!);
|
|
111
|
-
|
|
112
|
-
if (!await fs.pathExists(templateDir)) {
|
|
113
|
-
throw new Error(`Template "${template}" not found`);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// Create target directory
|
|
117
|
-
await fs.ensureDir(targetDir);
|
|
118
|
-
|
|
119
|
-
// Copy template with variable replacement
|
|
120
|
-
const variables: TemplateVariables = {
|
|
121
|
-
PROJECT_NAME: projectName,
|
|
122
|
-
APP_ID: appId || 'your-app-id',
|
|
123
|
-
API_URL: apiUrl,
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
await copyTemplate(templateDir, targetDir, variables);
|
|
127
|
-
|
|
128
|
-
// Create .env from .env.example
|
|
129
|
-
const envExamplePath = path.join(targetDir, '.env.example');
|
|
130
|
-
const envPath = path.join(targetDir, '.env');
|
|
131
|
-
if (await fs.pathExists(envExamplePath)) {
|
|
132
|
-
await fs.copy(envExamplePath, envPath);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
spinner.succeed('Project created!');
|
|
136
|
-
|
|
137
|
-
console.log(chalk.green('\n✓ Project initialized successfully!\n'));
|
|
138
|
-
console.log(chalk.gray('Next steps:\n'));
|
|
139
|
-
console.log(chalk.white(` cd ${projectName}`));
|
|
140
|
-
console.log(chalk.white(` npm install`));
|
|
141
|
-
console.log(chalk.white(` npm run dev`));
|
|
142
|
-
|
|
143
|
-
if (!appId) {
|
|
144
|
-
console.log(chalk.yellow('\n⚠ Remember to update .env with your Butterbase App ID'));
|
|
145
|
-
}
|
|
146
|
-
} catch (error) {
|
|
147
|
-
spinner.fail('Failed to create project');
|
|
148
|
-
console.error(chalk.red((error as Error).message));
|
|
149
|
-
process.exit(1);
|
|
150
|
-
}
|
|
151
|
-
}
|
package/src/commands/schema.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import ora from 'ora';
|
|
3
|
-
import fs from 'fs-extra';
|
|
4
|
-
import { getSchema, applySchema } from '../lib/api-client.js';
|
|
5
|
-
import { getCurrentAppId } from '../lib/config.js';
|
|
6
|
-
|
|
7
|
-
async function requireAppId(appId?: string): Promise<string> {
|
|
8
|
-
if (appId) return appId;
|
|
9
|
-
|
|
10
|
-
const currentAppId = await getCurrentAppId();
|
|
11
|
-
if (!currentAppId) {
|
|
12
|
-
console.log(chalk.red('✗ No app specified and no current app set'));
|
|
13
|
-
console.log(chalk.gray('Use: butterbase apps use <app-id>'));
|
|
14
|
-
console.log(chalk.gray('Or: butterbase schema get --app <app-id>'));
|
|
15
|
-
process.exit(1);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return currentAppId;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export async function schemaGetCommand(options: { app?: string; output?: string }) {
|
|
22
|
-
const appId = await requireAppId(options.app);
|
|
23
|
-
const spinner = ora('Fetching schema...').start();
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
const response: any = await getSchema(appId);
|
|
27
|
-
spinner.stop();
|
|
28
|
-
|
|
29
|
-
const schemaJson = JSON.stringify(response.schema, null, 2);
|
|
30
|
-
|
|
31
|
-
if (options.output) {
|
|
32
|
-
await fs.writeFile(options.output, schemaJson);
|
|
33
|
-
console.log(chalk.green(`✓ Schema saved to ${options.output}`));
|
|
34
|
-
} else {
|
|
35
|
-
console.log(schemaJson);
|
|
36
|
-
}
|
|
37
|
-
} catch (error) {
|
|
38
|
-
spinner.fail('Failed to fetch schema');
|
|
39
|
-
console.error(chalk.red((error as Error).message));
|
|
40
|
-
process.exit(1);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export async function schemaApplyCommand(file: string, options: { app?: string; dryRun?: boolean; name?: string }) {
|
|
45
|
-
const appId = await requireAppId(options.app);
|
|
46
|
-
|
|
47
|
-
if (!await fs.pathExists(file)) {
|
|
48
|
-
console.log(chalk.red(`✗ File not found: ${file}`));
|
|
49
|
-
process.exit(1);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const spinner = ora('Reading schema file...').start();
|
|
53
|
-
|
|
54
|
-
try {
|
|
55
|
-
const schemaContent = await fs.readFile(file, 'utf-8');
|
|
56
|
-
const schema = JSON.parse(schemaContent);
|
|
57
|
-
|
|
58
|
-
spinner.text = options.dryRun ? 'Running dry-run...' : 'Applying schema...';
|
|
59
|
-
|
|
60
|
-
const response: any = await applySchema(appId, schema, options.dryRun, options.name);
|
|
61
|
-
|
|
62
|
-
spinner.stop();
|
|
63
|
-
|
|
64
|
-
if (options.dryRun) {
|
|
65
|
-
console.log(chalk.blue('\n📋 Dry-run results:\n'));
|
|
66
|
-
console.log(JSON.stringify(response, null, 2));
|
|
67
|
-
} else {
|
|
68
|
-
console.log(chalk.green('\n✓ Schema applied successfully!'));
|
|
69
|
-
if (response.migration_id) {
|
|
70
|
-
console.log(chalk.gray(` Migration ID: ${response.migration_id}`));
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
} catch (error) {
|
|
74
|
-
spinner.fail(options.dryRun ? 'Dry-run failed' : 'Failed to apply schema');
|
|
75
|
-
console.error(chalk.red((error as Error).message));
|
|
76
|
-
process.exit(1);
|
|
77
|
-
}
|
|
78
|
-
}
|
package/src/commands/storage.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import ora from 'ora';
|
|
3
|
-
import fs from 'fs-extra';
|
|
4
|
-
import { generateUploadUrl, listStorageObjects, deleteStorageObject } from '../lib/api-client.js';
|
|
5
|
-
import { getCurrentAppId } from '../lib/config.js';
|
|
6
|
-
|
|
7
|
-
async function requireAppId(appId?: string): Promise<string> {
|
|
8
|
-
if (appId) return appId;
|
|
9
|
-
|
|
10
|
-
const currentAppId = await getCurrentAppId();
|
|
11
|
-
if (!currentAppId) {
|
|
12
|
-
console.log(chalk.red('✗ No app specified and no current app set'));
|
|
13
|
-
console.log(chalk.gray('Use: butterbase apps use <app-id>'));
|
|
14
|
-
process.exit(1);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return currentAppId;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export async function storageListCommand(options: { app?: string }) {
|
|
21
|
-
const appId = await requireAppId(options.app);
|
|
22
|
-
const spinner = ora('Fetching storage objects...').start();
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
const response: any = await listStorageObjects(appId);
|
|
26
|
-
spinner.stop();
|
|
27
|
-
|
|
28
|
-
if (!response.objects || response.objects.length === 0) {
|
|
29
|
-
console.log(chalk.yellow('No files found'));
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
console.log(chalk.blue('\nStorage objects:\n'));
|
|
34
|
-
for (const obj of response.objects) {
|
|
35
|
-
console.log(chalk.bold(obj.filename));
|
|
36
|
-
console.log(chalk.gray(` ID: ${obj.id}`));
|
|
37
|
-
console.log(chalk.gray(` Size: ${(obj.size_bytes / 1024).toFixed(2)} KB`));
|
|
38
|
-
console.log(chalk.gray(` Type: ${obj.content_type}`));
|
|
39
|
-
console.log();
|
|
40
|
-
}
|
|
41
|
-
} catch (error) {
|
|
42
|
-
spinner.fail('Failed to fetch storage objects');
|
|
43
|
-
console.error(chalk.red((error as Error).message));
|
|
44
|
-
process.exit(1);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export async function storageUploadCommand(file: string, options: { app?: string }) {
|
|
49
|
-
const appId = await requireAppId(options.app);
|
|
50
|
-
|
|
51
|
-
if (!await fs.pathExists(file)) {
|
|
52
|
-
console.log(chalk.red(`✗ File not found: ${file}`));
|
|
53
|
-
process.exit(1);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const spinner = ora('Uploading file...').start();
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
const stats = await fs.stat(file);
|
|
60
|
-
const filename = file.split('/').pop()!;
|
|
61
|
-
|
|
62
|
-
// Detect content type (basic implementation)
|
|
63
|
-
let contentType = 'application/octet-stream';
|
|
64
|
-
if (filename.endsWith('.png')) contentType = 'image/png';
|
|
65
|
-
else if (filename.endsWith('.jpg') || filename.endsWith('.jpeg')) contentType = 'image/jpeg';
|
|
66
|
-
else if (filename.endsWith('.pdf')) contentType = 'application/pdf';
|
|
67
|
-
else if (filename.endsWith('.txt')) contentType = 'text/plain';
|
|
68
|
-
|
|
69
|
-
// Get presigned upload URL
|
|
70
|
-
const uploadData: any = await generateUploadUrl(appId, filename, contentType, stats.size);
|
|
71
|
-
|
|
72
|
-
// Read file and upload to S3
|
|
73
|
-
const fileBuffer = await fs.readFile(file);
|
|
74
|
-
|
|
75
|
-
const uploadResponse = await fetch(uploadData.uploadUrl, {
|
|
76
|
-
method: 'PUT',
|
|
77
|
-
headers: {
|
|
78
|
-
'Content-Type': contentType,
|
|
79
|
-
},
|
|
80
|
-
body: fileBuffer,
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
if (!uploadResponse.ok) {
|
|
84
|
-
throw new Error('Failed to upload file to storage');
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
spinner.succeed('File uploaded');
|
|
88
|
-
console.log(chalk.green('\n✓ File uploaded successfully!'));
|
|
89
|
-
console.log(chalk.gray(` Object ID: ${uploadData.objectId}`));
|
|
90
|
-
console.log(chalk.gray(` Object Key: ${uploadData.objectKey}`));
|
|
91
|
-
} catch (error) {
|
|
92
|
-
spinner.fail('Failed to upload file');
|
|
93
|
-
console.error(chalk.red((error as Error).message));
|
|
94
|
-
process.exit(1);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export async function storageDeleteCommand(objectId: string, options: { app?: string }) {
|
|
99
|
-
const appId = await requireAppId(options.app);
|
|
100
|
-
|
|
101
|
-
if (!objectId) {
|
|
102
|
-
console.log(chalk.red('✗ Object ID is required'));
|
|
103
|
-
console.log(chalk.gray('Usage: butterbase storage delete <object-id>'));
|
|
104
|
-
process.exit(1);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const spinner = ora('Deleting object...').start();
|
|
108
|
-
|
|
109
|
-
try {
|
|
110
|
-
await deleteStorageObject(appId, objectId);
|
|
111
|
-
spinner.succeed('Object deleted');
|
|
112
|
-
} catch (error) {
|
|
113
|
-
spinner.fail('Failed to delete object');
|
|
114
|
-
console.error(chalk.red((error as Error).message));
|
|
115
|
-
process.exit(1);
|
|
116
|
-
}
|
|
117
|
-
}
|
package/src/lib/api-client.ts
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import { getMergedConfig } from './config.js';
|
|
2
|
-
|
|
3
|
-
export interface ApiError {
|
|
4
|
-
error: string;
|
|
5
|
-
details?: unknown;
|
|
6
|
-
hint?: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Get authorization headers
|
|
11
|
-
*/
|
|
12
|
-
async function getHeaders(): Promise<HeadersInit> {
|
|
13
|
-
const config = await getMergedConfig();
|
|
14
|
-
const headers: HeadersInit = {
|
|
15
|
-
'Content-Type': 'application/json',
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
if (config.apiKey) {
|
|
19
|
-
headers['Authorization'] = `Bearer ${config.apiKey}`;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return headers;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Get base URL from config
|
|
27
|
-
*/
|
|
28
|
-
async function getBaseUrl(): Promise<string> {
|
|
29
|
-
const config = await getMergedConfig();
|
|
30
|
-
return config.endpoint;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Make a GET request
|
|
35
|
-
*/
|
|
36
|
-
export async function apiGet<T>(path: string): Promise<T> {
|
|
37
|
-
const baseUrl = await getBaseUrl();
|
|
38
|
-
const headers = await getHeaders();
|
|
39
|
-
|
|
40
|
-
const res = await fetch(`${baseUrl}${path}`, { headers });
|
|
41
|
-
const body: any = await res.json();
|
|
42
|
-
|
|
43
|
-
if (!res.ok) {
|
|
44
|
-
throw new Error(body.error || body.message || 'Request failed');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return body as T;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Make a POST request
|
|
52
|
-
*/
|
|
53
|
-
export async function apiPost<T>(path: string, data: unknown): Promise<T> {
|
|
54
|
-
const baseUrl = await getBaseUrl();
|
|
55
|
-
const headers = await getHeaders();
|
|
56
|
-
|
|
57
|
-
const res = await fetch(`${baseUrl}${path}`, {
|
|
58
|
-
method: 'POST',
|
|
59
|
-
headers,
|
|
60
|
-
body: JSON.stringify(data),
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
const body: any = await res.json();
|
|
64
|
-
|
|
65
|
-
if (!res.ok) {
|
|
66
|
-
throw new Error(body.error || body.message || 'Request failed');
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return body as T;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Make a PATCH request
|
|
74
|
-
*/
|
|
75
|
-
export async function apiPatch<T>(path: string, data: unknown): Promise<T> {
|
|
76
|
-
const baseUrl = await getBaseUrl();
|
|
77
|
-
const headers = await getHeaders();
|
|
78
|
-
|
|
79
|
-
const res = await fetch(`${baseUrl}${path}`, {
|
|
80
|
-
method: 'PATCH',
|
|
81
|
-
headers,
|
|
82
|
-
body: JSON.stringify(data),
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
const body: any = await res.json();
|
|
86
|
-
|
|
87
|
-
if (!res.ok) {
|
|
88
|
-
throw new Error(body.error || body.message || 'Request failed');
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return body as T;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Make a DELETE request
|
|
96
|
-
*/
|
|
97
|
-
export async function apiDelete<T>(path: string): Promise<T> {
|
|
98
|
-
const baseUrl = await getBaseUrl();
|
|
99
|
-
const headers = await getHeaders();
|
|
100
|
-
|
|
101
|
-
const res = await fetch(`${baseUrl}${path}`, {
|
|
102
|
-
method: 'DELETE',
|
|
103
|
-
headers,
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
// Handle 204 No Content
|
|
107
|
-
if (res.status === 204) {
|
|
108
|
-
return {} as T;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const body: any = await res.json();
|
|
112
|
-
|
|
113
|
-
if (!res.ok) {
|
|
114
|
-
throw new Error(body.error || body.message || 'Request failed');
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return body as T;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// MCP tool wrappers
|
|
121
|
-
|
|
122
|
-
export async function initApp(name: string) {
|
|
123
|
-
return apiPost('/init', { name });
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export async function listApps() {
|
|
127
|
-
return apiGet('/apps');
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export async function deleteApp(appId: string) {
|
|
131
|
-
return apiDelete(`/apps/${appId}`);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export async function getSchema(appId: string) {
|
|
135
|
-
return apiGet(`/v1/${appId}/schema`);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export async function applySchema(appId: string, schema: any, dryRun?: boolean, name?: string) {
|
|
139
|
-
return apiPost(`/v1/${appId}/schema/apply`, { schema, dry_run: dryRun, name });
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export async function deployFunction(appId: string, data: {
|
|
143
|
-
name: string;
|
|
144
|
-
code: string;
|
|
145
|
-
description?: string;
|
|
146
|
-
envVars?: Record<string, string>;
|
|
147
|
-
timeoutMs?: number;
|
|
148
|
-
memoryLimitMb?: number;
|
|
149
|
-
trigger: { type: string; config?: any };
|
|
150
|
-
}) {
|
|
151
|
-
return apiPost(`/v1/${appId}/functions`, data);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export async function listFunctions(appId: string) {
|
|
155
|
-
return apiGet(`/v1/${appId}/functions`);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export async function getFunctionLogs(appId: string, functionName: string, level?: string, limit?: number) {
|
|
159
|
-
const params = new URLSearchParams();
|
|
160
|
-
if (level) params.set('level', level);
|
|
161
|
-
if (limit) params.set('limit', String(limit));
|
|
162
|
-
const query = params.toString();
|
|
163
|
-
return apiGet(`/v1/${appId}/functions/${functionName}/logs${query ? `?${query}` : ''}`);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export async function generateUploadUrl(appId: string, filename: string, contentType: string, sizeBytes: number) {
|
|
167
|
-
return apiPost(`/storage/${appId}/upload`, { filename, contentType, sizeBytes });
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export async function listStorageObjects(appId: string) {
|
|
171
|
-
return apiGet(`/storage/${appId}/objects`);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export async function deleteStorageObject(appId: string, objectId: string) {
|
|
175
|
-
return apiDelete(`/storage/${appId}/${objectId}`);
|
|
176
|
-
}
|