bigblocks
Version:
Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React
71 lines (70 loc) • 2.35 kB
JavaScript
// BigBlocks Astro Adapter
// Wraps the framework-agnostic core with Astro specific functionality
// Works without Astro installed (graceful fallbacks)
import { createBigBlocksAuth, } from "../bigblocks-core.js";
/**
* Creates BigBlocks auth handlers for Astro
* Works with Astro API routes (/src/pages/api/auth/[...bigblocks].ts)
*/
export function createAstroBigBlocks(options = {}) {
const core = createBigBlocksAuth(options);
// Astro API route handler
const handler = async (context) => {
try {
// Convert Astro request to standard Request
const webRequest = context.request;
const response = await core.handler(webRequest);
return response;
}
catch (error) {
console.error("BigBlocks Astro API error:", error);
return new globalThis.Response(JSON.stringify({ error: "Internal server error" }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
};
// Middleware helper for protecting routes
const middleware = async (context, next) => {
const session = await core.api.getSession(context.request.headers);
if (!session) {
return new globalThis.Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: { "Content-Type": "application/json" },
});
}
// Add user info to context
context.locals.user =
session.user;
return next();
};
// Helper to get session from Astro context
const getSession = async (context) => {
return await core.api.getSession(context.request.headers);
};
return {
// API Route handlers
GET: handler,
POST: handler,
PUT: handler,
DELETE: handler,
// Middleware
middleware,
// Utilities
getSession,
// Direct API access
api: core.api,
// Config
options: core.options,
};
}
// Convenience function for API route files
export function createBigBlocksAPIRoute(options) {
const handlers = createAstroBigBlocks(options);
return {
GET: handlers.GET,
POST: handlers.POST,
PUT: handlers.PUT,
DELETE: handlers.DELETE,
};
}