Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ResourceTemplates = map[string]string{
"router.ts": `/**
* router.ts — generated by Basalt
* Express Router for the {{.Resource.Name}} resource.
* Docs: https://basalt.sh/docs/templates/router
* Express Router: https://expressjs.com/en/guide/routing.html
*/
import { Router } from 'express';
import * as handlers from './handlers';
const router = Router();
{{range .Resource.Endpoints}}router.{{printf "%s" .Method | lower}}('{{trimResourcePrefix .Path $.Resource.Name}}', handlers.{{handlerName .Method .Path $.Resource.Name}});
{{end}}
export default router;
`,
"handlers.ts": `/**
* handlers.ts — generated by Basalt
* Request handlers for the {{.Resource.Name}} resource.
* Docs: https://basalt.sh/docs/templates/handlers
*/
import { Request, Response } from 'express';
import * as db from './supabase';
{{range .Resource.Endpoints}}
export const {{handlerName .Method .Path $.Resource.Name}} = async (req: Request, res: Response) => {
try {
// TODO: Implement {{.Method}} {{.Path}}
res.json({ message: '{{.Method}} {{.Path}}' });
} catch (err: any) {
res.status(500).json({ error: err.message });
}
};
{{end}}`,
"model.ts": `/**
* model.ts — generated by Basalt
* TypeScript interfaces for the {{.Resource.Name}} resource.
* Docs: https://basalt.sh/docs/templates/model
*/
export interface {{printf "%s" .Resource.Name | capitalize}} {
{{range .Table.Fields}} {{.Name}}: {{.Type}};
{{end}}}
`,
"supabase.ts": `/**
* supabase.ts — generated by Basalt
* Supabase database queries for the {{.Resource.Name}} resource.
* Docs: https://basalt.sh/docs/templates/supabase
* Supabase JS: https://supabase.com/docs/reference/javascript
*/
import { supabase } from '../lib/supabase';
import { {{printf "%s" .Resource.Name | capitalize}} } from './model';
export const getAll = async () => {
return await supabase().from('{{.Table.Name}}').select('*');
};
export const getById = async (id: string) => {
return await supabase().from('{{.Table.Name}}').select('*').eq('id', id).single();
};
export const create = async (data: Partial<{{printf "%s" .Resource.Name | capitalize}}>) => {
return await supabase().from('{{.Table.Name}}').insert(data).select().single();
};
export const update = async (id: string, data: Partial<{{printf "%s" .Resource.Name | capitalize}}>) => {
return await supabase().from('{{.Table.Name}}').update(data).eq('id', id).select().single();
};
export const remove = async (id: string) => {
return await supabase().from('{{.Table.Name}}').delete().eq('id', id);
};
`,
}
View Source
"lib/supabase.ts": `/**
* lib/supabase.ts — generated by Basalt
* Shared Supabase client (lazy initialization).
* Supabase JS: https://supabase.com/docs/reference/javascript
*/
import { createClient, SupabaseClient } from '@supabase/supabase-js';
let _client: SupabaseClient | null = null;
export const supabase = (): SupabaseClient => {
if (!_client) {
const url = process.env.SUPABASE_URL;
const key = process.env.SUPABASE_ANON_KEY;
if (!url || !key) throw new Error('Missing SUPABASE_URL or SUPABASE_ANON_KEY');
_client = createClient(url, key);
}
return _client;
};
`,
"lib/auth.ts": `/**
* lib/auth.ts — generated by Basalt
* Handles authentication for the API.
* Docs: https://basalt.sh/docs/templates/auth
*/
import { Request, Response, NextFunction } from 'express';
export const authMiddleware = (req: Request, res: Response, next: NextFunction) => {
const authHeader = req.headers['authorization'];
if (!authHeader) {
return res.status(401).json({ error: 'No authorization header' });
}
// JWT or API Key logic should be here
next();
};
`,
"middleware/error.ts": `/**
* middleware/error.ts — generated by Basalt
* Global error handler for the API.
*/
import { Request, Response, NextFunction } from 'express';
export const errorHandler = (err: any, req: Request, res: Response, next: NextFunction) => {
console.error(err.stack);
res.status(500).json({ error: err.message || 'Internal Server Error' });
};
`,
"middleware/logger.ts": `/**
* middleware/logger.ts — generated by Basalt
* Request logger middleware.
* Docs: https://basalt.sh/docs/templates/logger
*/
import { Request, Response, NextFunction } from 'express';
export const loggerMiddleware = (req: Request, res: Response, next: NextFunction) => {
console.log(` + "`" + `[${new Date().toISOString()}] ${req.method} ${req.path}` + "`" + `);
next();
};
`,
"index.ts": `/**
* index.ts — generated by Basalt
* Entry point for the generated API.
* Docs: https://basalt.sh/docs/templates/index
*/
import dotenv from 'dotenv';
dotenv.config();
import express from 'express';
import { errorHandler } from './middleware/error';
import { loggerMiddleware } from './middleware/logger';
// Import routers
{{range .Resources}}import {{.Name}}Router from './{{.Name}}/router';
{{end}}
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
app.use(loggerMiddleware);
// Mount routers
{{range .Resources}}app.use('/{{.Name}}', {{.Name}}Router);
{{end}}
app.use(errorHandler);
app.listen(port, () => {
console.log(` + "`" + `Server running at http://localhost:${port}` + "`" + `);
});
`,
"package.json": `{
"name": "my-api",
"version": "1.0.0",
"main": "index.ts",
"scripts": {
"dev": "tsx watch index.ts"
},
"dependencies": {
"express": "^4.18.2",
"@supabase/supabase-js": "^2.39.0",
"dotenv": "^16.3.1"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/node": "^20.10.0",
"tsx": "^4.6.1",
"typescript": "^5.3.2"
}
}`,
"tsconfig.json": `{
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist"
}
}`,
".env.example": `PORT=3000
SUPABASE_URL=your-supabase-url
SUPABASE_ANON_KEY=your-supabase-anon-key
`,
"schema.sql": `{{range .Tables}}
-- Table: {{.Name}}
CREATE TABLE {{.Name}} (
{{range $i, $f := .Fields}} {{$f.Name}} {{$f.Type}}{{if $i}},{{end}}
{{end}});
{{end}}
`,
}
Functions ¶
This section is empty.
Types ¶
Click to show internal directories.
Click to hide internal directories.