Edge SDKFeature ReferenceRecipes & Patterns

SDK Recipes

Common patterns and best practices for building with Aerostack SDKs.

👥 Authentication Patterns

Protected Routes (React)

Using the useAuth hook to protect sensitive UI components.

import { useAuth } from "@aerostack/react";
 
const ProtectedComponent = ({ children }) => {
  const { user, loading } = useAuth();
 
  if (loading) return <Spinner />;
  if (!user) return <Redirect to="/login" />;
 
  return children;
};

Social Auth with Logic Lab

Handling OAuth callbacks in a Hook.

// Logic Lab Hook (auth-callback)
export default async ({ sdk, body }) => {
  const { code } = body;
  const tokens = await sdk.secrets.get('OAUTH_CLIENT_SECRET');
  // ... process login
  return { success: true };
};

💾 Data Management

Optimistic UI Updates

Using useDb with temporary local state for instant feedback.

const { query } = useDb();
const [todos, setTodos] = useState([]);
 
const addTodo = async (text) => {
  const newTodo = { id: Date.now(), text, status: 'pending' };
  setTodos([...todos, newTodo]); // Optimistic
  
  await query("INSERT INTO todos (text) VALUES (?)", [text]);
  setTodos(prev => prev.map(t => t.id === newTodo.id ? { ...t, status: 'saved' } : t));
};

Edge-Cached API Responses

Combining sdk.db and sdk.cache in a Logic Hook for maximum performance.

export default async ({ sdk, body }) => {
  const cacheKey = "all_products";
  
  // 1. Try Cache
  const cached = await sdk.cache.get(cacheKey);
  if (cached) return cached;
 
  // 2. Fetch from DB
  const products = await sdk.db.query("SELECT * FROM products");
  
  // 3. Store in Cache for 5 mins
  await sdk.cache.set(cacheKey, products, { ttl: 300 });
 
  return products;
};

🤖 AI & Automation

Streaming AI Responses (Web)

Handling large AI generations.

const { chat } = useAI();
 
const handleAsk = async () => {
  const result = await chat([{ role: "user", content: "Write a poem" }]);
  // Note: Standard SDK methods currently return full responses.
  // Streaming support coming in v0.3.0
  setResponse(result);
};

Background Processing

Triggering long-running AI tasks via Queues.

// 1. Frontend enqueues a task
await sdk.queue.enqueue("generate-report", { userId: 123 });
 
// 2. Background Hook processes it
export default async ({ sdk, body }) => {
  const analysis = await sdk.ai.chat([{ content: `Analyze user ${body.userId}` }]);
  await sdk.db.query("UPDATE reports SET status='done' WHERE user_id=?", [body.userId]);
};