npm package: @happyspace/sdk
Description: HappySpace SDK for AI-generated websites - Restaurant, Ecommerce, and Data hooks
Author: HappySpace
Latest version: 1.0.2

# @happyspace/sdk

SDK for HappySpace AI-generated websites - Restaurant, Ecommerce, and Data operations.

## Installation

```bash
npm install @happyspace/sdk
```

## Quick Start

### 1. Initialize the SDK

```tsx
import { initSDK } from '@happyspace/sdk';

// Call at app startup
initSDK({
  businessId: 'your-business-id',
  apiBaseUrl: 'https://api.happyspace.one/api/v1',
  taxRate: 0.08,
});
```

Or use `window.SITE_CONFIG` (auto-detected):

```html
<script>
  window.SITE_CONFIG = {
    businessId: 'your-business-id',
    apiBaseUrl: 'https://api.happyspace.one/api/v1',
  };
</script>
```

### 2. Wrap your app with providers

```tsx
import { RestaurantCartProvider } from '@happyspace/sdk/restaurant';
// or
import { EcommerceCartProvider } from '@happyspace/sdk/ecommerce';
// or
import { DataProvider } from '@happyspace/sdk/data';

function App() {
  return (
    <RestaurantCartProvider>
      <YourApp />
    </RestaurantCartProvider>
  );
}
```

## Restaurant SDK

```tsx
import {
  useMenu,
  useRestaurantCart,
  RestaurantCartProvider,
  usePaymentConfig,
  checkoutClient,
  ordersClient,
} from '@happyspace/sdk/restaurant';

// Fetch menu
const { items, categories, loading } = useMenu();
const { items: filtered } = useMenu({ categoryId: 'appetizers' });
const { items: searched } = useMenu({ search: 'burger' });

// Cart operations
const { items, addItem, removeItem, updateQuantity, clearCart, total } = useRestaurantCart();
addItem(menuItem, selectedModifiers, quantity);

// Checkout
const { clientSecret } = await checkoutClient.createPaymentIntent({
  items: cartItems,
  customer: { name, email, phone },
  orderType: 'pickup',
});

// Submit order
const { order } = await checkoutClient.submitOrder({
  items: cartItems,
  customer: { name, email, phone },
  paymentIntentId,
  paymentMethod: 'stripe', // or 'pay_at_pickup'
});

// Track order
const { order } = await ordersClient.getOrderStatus({ orderId, email });
```

## Ecommerce SDK

```tsx
import {
  useProducts,
  useEcommerceCart,
  EcommerceCartProvider,
  checkoutClient,
  ordersClient,
} from '@happyspace/sdk/ecommerce';

// Fetch products
const { products, categories, loading } = useProducts();
const { products: featured } = useProducts({ featured: true });
const { products: searched } = useProducts({ search: 'shoes' });

// Cart operations
const { items, addItem, removeItem, updateQuantity, clearCart, total } = useEcommerceCart();
addItem(product, quantity, selectedVariants);

// Checkout
const { clientSecret } = await checkoutClient.createPaymentIntent({
  items: cartItems,
  shippingMethod: 'standard',
  shippingAddress: { street, city, state, zip },
});

// Submit order
const { order } = await checkoutClient.submitOrder({
  items: cartItems,
  customer: { name, email, phone },
  paymentIntentId,
  shippingAddress,
});
```

## Data SDK

For forms, user submissions, and data storage:

```tsx
import { useData, DataProvider, DataSDKError } from '@happyspace/sdk/data';

// Contact form
function ContactForm() {
  const { insert, loading, error } = useData('contact_submissions');

  const handleSubmit = async (e) => {
    e.preventDefault();
    try {
      await insert({ name, email, message });
      alert('Submitted!');
    } catch (err) {
      alert(err.message);
    }
  };

  return <form onSubmit={handleSubmit}>...</form>;
}

// Fetch and display data
function Testimonials() {
  const { rows, loading } = useData('testimonials', {
    autoFetch: true,
    where: { approved: true },
    orderBy: { createdAt: -1 },
    limit: 10,
  });

  if (loading) return <div>Loading...</div>;

  return rows.map(row => <div key={row._id}>{row.data.content}</div>);
}
```

## API Reference

### Shared

- `initSDK(config)` - Initialize SDK with configuration
- `getConfig()` - Get current configuration
- `isPreviewMode()` - Check if running in preview/sandbox mode

### Restaurant

Hooks:
- `useMenu(options?)` - Fetch menu items
- `useRestaurantCart()` - Cart state and actions
- `usePaymentConfig()` - Payment configuration
- `useLocations()` - Business locations

Clients:
- `checkoutClient` - Payment and order submission
- `ordersClient` - Order tracking
- `menuClient` - Imperative menu API
- `cartClient` - Server-side cart API
- `reservationClient` - Table reservations
- `locationsClient` - Multi-location support

### Ecommerce

Hooks:
- `useProducts(options?)` - Fetch products
- `useEcommerceCart()` - Cart state and actions

Clients:
- `checkoutClient` - Payment and order submission
- `ordersClient` - Order tracking

### Data

Hooks:
- `useData(tableName, options?)` - CRUD operations

Classes:
- `DataSDKError` - Error type for data operations

## License

MIT