# API Migration to @xtablo/shared-types

## Summary

Successfully migrated the API app (`apps/api`) to use the `@xtablo/shared-types` package instead of local type definitions. This provides a single source of truth for all type definitions across the monorepo.

## Changes Made

### 1. Added Dependency

Added `@xtablo/shared-types` to the API's dependencies:

```json
{
  "dependencies": {
    "@xtablo/shared-types": "workspace:*"
  }
}
```

### 2. Updated Imports

Replaced all local type imports with imports from `@xtablo/shared-types`:

#### Database Types
```typescript
// Before
import type { Database, Tables, TablesInsert } from "../types/database.types.js";

// After
import type { Database, Tables, TablesInsert } from "@xtablo/shared-types";
```

#### Domain Types
```typescript
// Before
import type { EventInsertInTablo, TabloInsert, EventAndTablo } from "../types/types.js";

// After
import type { EventInsertInTablo, TabloInsert, EventAndTablo } from "@xtablo/shared-types";
```

### 3. Files Updated

**Total: 13 files updated**

- ✅ `src/routers/user.ts`
- ✅ `src/routers/tablo.ts`
- ✅ `src/routers/invite.ts`
- ✅ `src/routers/public.ts`
- ✅ `src/routers/notes.ts`
- ✅ `src/routers/maybeAuthRouter.ts`
- ✅ `src/routers/index.ts`
- ✅ `src/routers/stripe.ts`
- ✅ `src/routers/tasks.ts`
- ✅ `src/routers/tablo_data.ts`
- ✅ `src/helpers/helpers.ts`
- ✅ `src/helpers/slots.ts`
- ✅ `src/__tests__/slots.test.ts`

### 4. Files Removed

Deleted local type files that are now provided by the shared package:

- ❌ `src/types/database.types.ts` (855 lines - now in @xtablo/shared-types)
- ❌ `src/types/types.ts` (34 lines - now in @xtablo/shared-types)

### 5. Files Kept

Kept API-specific type definitions:

- ✅ `src/types/app.types.ts` - Contains API-specific environment types:
  - `BaseEnv` - Base environment with Supabase, S3, Stripe, etc.
  - `AuthEnv` - Environment with authenticated user
  - `MaybeAuthEnv` - Environment with optional authentication

### 6. Fixed Import Extensions

Updated `@xtablo/shared-types` package to use `.js` extensions for ESM compatibility:

```typescript
// All internal imports in shared-types now use .js extensions
export type { Database } from "./database.types.js";
export type { Event } from "./events.types.js";
// etc.
```

## Verification

All checks pass successfully:

### ✅ Type Checking
```bash
turbo run typecheck --filter=@xtablo/api
# 0 errors
```

### ✅ Linting
```bash
turbo run lint --filter=@xtablo/api
# 0 errors (after auto-fix)
```

### ✅ Tests
```bash
turbo run test --filter=@xtablo/api
# 67 tests passing
```

### ✅ Build
```bash
turbo run build --filter=@xtablo/api
# Successful
```

## Benefits

1. **Single Source of Truth**: All type definitions centralized in one package
2. **Consistency**: Same types used across API, frontend, and mobile apps
3. **Easy Updates**: Update database types once, all apps get the changes
4. **Zero Duplication**: Removed ~900 lines of duplicate type code
5. **Better Maintainability**: Changes to types only need to happen in one place

## Type Mapping

| Old Import | New Import | Type Examples |
|-----------|-----------|---------------|
| `../types/database.types.js` | `@xtablo/shared-types` | `Database`, `Tables`, `TablesInsert`, `TablesUpdate` |
| `../types/types.js` | `@xtablo/shared-types` | `Event`, `Tablo`, `TabloInsert`, `EventInsertInTablo`, `EventAndTablo` |
| `../types/app.types.js` | (kept local) | `BaseEnv`, `AuthEnv`, `MaybeAuthEnv` |

## Import Examples

### Before
```typescript
import type { Database } from "../types/database.types.js";
import type { EventInsertInTablo, TabloInsert } from "../types/types.js";
import type { AuthEnv } from "../types/app.types.js";
```

### After
```typescript
import type { Database, EventInsertInTablo, TabloInsert } from "@xtablo/shared-types";
import type { AuthEnv } from "../types/app.types.js";
```

## Package Structure

### Before
```
apps/api/src/types/
├── app.types.ts        (API-specific)
├── database.types.ts   (855 lines - Supabase types)
└── types.ts            (34 lines - Domain types)
```

### After
```
apps/api/src/types/
└── app.types.ts        (API-specific only)

packages/shared-types/src/
├── database.types.ts   (855 lines)
├── events.types.ts     (Event types)
├── tablos.types.ts     (Tablo types)
├── stripe.types.ts     (Stripe types)
├── kanban.types.ts     (Kanban types)
├── utils.ts            (Utility types)
└── index.ts            (Main export)
```

## Next Steps

The API is now fully integrated with `@xtablo/shared-types`. Other apps can follow the same pattern:

1. Add `@xtablo/shared-types` to dependencies
2. Replace local type imports with shared types
3. Keep only app-specific types locally
4. Run tests to verify everything works

## Additional Notes

- **ESM Compatibility**: All imports use `.js` extensions for proper ESM module resolution
- **Tree-shaking**: Package supports granular imports (e.g., `@xtablo/shared-types/events`)
- **Zero Dependencies**: The shared-types package has no runtime dependencies
- **TypeScript Strict Mode**: All types are checked with strict mode enabled

