|
| 1 | +# Shared Directory |
| 2 | + |
| 3 | +This directory contains shared types, schemas, constants, and utilities that are used across both the AI backend and web frontend components of the Sport Scribe platform. |
| 4 | + |
| 5 | +## Directory Structure |
| 6 | + |
| 7 | +``` |
| 8 | +shared/ |
| 9 | +├── types/ # TypeScript type definitions |
| 10 | +├── schemas/ # Data schemas and validation |
| 11 | +├── constants/ # Application constants |
| 12 | +└── README.md # This file |
| 13 | +``` |
| 14 | + |
| 15 | +## Types (`/types`) |
| 16 | + |
| 17 | +Contains TypeScript interface definitions and types used throughout the application: |
| 18 | + |
| 19 | +- **`article.ts`** - Article-related types, statuses, and metadata |
| 20 | +- **`game.ts`** - Game data, statistics, and moment types |
| 21 | +- **`player.ts`** - Player profiles, statistics, and injury types |
| 22 | +- **`team.ts`** - Team information, roster, and history types |
| 23 | +- **`agent.ts`** - AI agent configuration and task types |
| 24 | +- **`api.ts`** - API request/response types and authentication |
| 25 | + |
| 26 | +### Usage Example |
| 27 | + |
| 28 | +```typescript |
| 29 | +// In AI backend (Python can access via code generation) |
| 30 | +import { Article, ArticleStatus } from '../../shared/types/article'; |
| 31 | + |
| 32 | +// In web frontend |
| 33 | +import { Game, GameStatus } from '@/shared/types/game'; |
| 34 | +import { Player, PlayerStats } from '@/shared/types/player'; |
| 35 | +``` |
| 36 | + |
| 37 | +## Schemas (`/schemas`) |
| 38 | + |
| 39 | +Contains data validation schemas and database definitions: |
| 40 | + |
| 41 | +### Database Schemas (`/schemas/database`) |
| 42 | +- **`articles.sql`** - Article tables, metadata, and relationships |
| 43 | +- **`games.sql`** - Game data, statistics, and player performance |
| 44 | +- **`users.sql`** - User profiles, permissions, and authentication |
| 45 | +- **`init.sql`** - Database initialization and system configuration |
| 46 | + |
| 47 | +### API Schemas (`/schemas/api`) |
| 48 | +- **`article-endpoints.json`** - REST API endpoint definitions |
| 49 | +- **`webhook-payloads.json`** - Webhook event payload schemas |
| 50 | +- **`openapi.yaml`** - Complete OpenAPI specification |
| 51 | + |
| 52 | +### Validation Schemas (`/schemas/validation`) |
| 53 | +- **`article-schema.json`** - JSON Schema for article validation |
| 54 | +- **`game-schema.json`** - JSON Schema for game data validation |
| 55 | + |
| 56 | +### Usage Example |
| 57 | + |
| 58 | +```sql |
| 59 | +-- Run database initialization |
| 60 | +\i shared/schemas/database/init.sql |
| 61 | +\i shared/schemas/database/users.sql |
| 62 | +\i shared/schemas/database/games.sql |
| 63 | +\i shared/schemas/database/articles.sql |
| 64 | +``` |
| 65 | + |
| 66 | +```typescript |
| 67 | +// Validate article data |
| 68 | +import articleSchema from '@/shared/schemas/validation/article-schema.json'; |
| 69 | +import Ajv from 'ajv'; |
| 70 | + |
| 71 | +const ajv = new Ajv(); |
| 72 | +const validate = ajv.compile(articleSchema); |
| 73 | +const isValid = validate(articleData); |
| 74 | +``` |
| 75 | + |
| 76 | +## Constants (`/constants`) |
| 77 | + |
| 78 | +Contains application-wide constants and configuration: |
| 79 | + |
| 80 | +- **`sports.ts`** - Sports categories, positions, statistics, and metadata |
| 81 | +- **`leagues.ts`** - League information, teams, and competition structures |
| 82 | +- **`api-endpoints.ts`** - API endpoint URLs and configuration |
| 83 | + |
| 84 | +### Usage Example |
| 85 | + |
| 86 | +```typescript |
| 87 | +import { SPORTS, SPORT_NAMES } from '@/shared/constants/sports'; |
| 88 | +import { LEAGUES, LEAGUE_INFO } from '@/shared/constants/leagues'; |
| 89 | +import { API_ENDPOINTS } from '@/shared/constants/api-endpoints'; |
| 90 | + |
| 91 | +// Get all supported sports |
| 92 | +const supportedSports = Object.values(SPORTS); |
| 93 | + |
| 94 | +// Get league information |
| 95 | +const nflInfo = LEAGUE_INFO[LEAGUES.NFL]; |
| 96 | + |
| 97 | +// Build API URL |
| 98 | +const articlesUrl = API_ENDPOINTS.ARTICLES.LIST; |
| 99 | +``` |
| 100 | + |
| 101 | +## Integration Patterns |
| 102 | + |
| 103 | +### Web Frontend Integration |
| 104 | + |
| 105 | +```typescript |
| 106 | +// Import shared types |
| 107 | +import type { Article, Game, Player } from '@/shared/types'; |
| 108 | + |
| 109 | +// Use constants |
| 110 | +import { SPORTS, LEAGUES } from '@/shared/constants'; |
| 111 | + |
| 112 | +// Validate data |
| 113 | +import { validateArticle } from '@/shared/validation'; |
| 114 | +``` |
| 115 | + |
| 116 | +### AI Backend Integration |
| 117 | + |
| 118 | +```python |
| 119 | +# Access shared schemas |
| 120 | +import json |
| 121 | +with open('../shared/schemas/validation/article-schema.json') as f: |
| 122 | + article_schema = json.load(f) |
| 123 | + |
| 124 | +# Use shared constants (via generated Python constants) |
| 125 | +from shared.constants import SPORTS, LEAGUES |
| 126 | +``` |
| 127 | + |
| 128 | +## Data Flow |
| 129 | + |
| 130 | +```mermaid |
| 131 | +graph LR |
| 132 | + A[AI Backend] --> B[Shared Types] |
| 133 | + C[Web Frontend] --> B |
| 134 | + B --> D[Database Schema] |
| 135 | + B --> E[API Validation] |
| 136 | + B --> F[Type Safety] |
| 137 | +``` |
| 138 | + |
| 139 | +## Validation Rules |
| 140 | + |
| 141 | +### Article Validation |
| 142 | +- Title: 10-500 characters, non-empty |
| 143 | +- Content: 100-50,000 characters |
| 144 | +- Sport: Must be from supported sports list |
| 145 | +- Tags: Maximum 20, each 2-50 characters |
| 146 | +- Reading time: 1-120 minutes |
| 147 | + |
| 148 | +### Game Validation |
| 149 | +- Teams: Home and away teams must be different |
| 150 | +- Scores: 0-999 range |
| 151 | +- Status: Must be valid game status |
| 152 | +- Season: Format 'YYYY' or 'YYYY-YYYY' |
| 153 | + |
| 154 | +### Player Validation |
| 155 | +- Position: Must be valid for the sport |
| 156 | +- Status: Active, injured, suspended, etc. |
| 157 | +- Statistics: Sport-specific validation rules |
| 158 | + |
| 159 | +## Database Design Principles |
| 160 | + |
| 161 | +1. **Row Level Security (RLS)** - All tables have appropriate RLS policies |
| 162 | +2. **Audit Trails** - Created/updated timestamps on all tables |
| 163 | +3. **Soft Deletes** - Important data is marked as deleted, not removed |
| 164 | +4. **Indexing** - Performance indexes on frequently queried columns |
| 165 | +5. **Constraints** - Data integrity enforced at database level |
| 166 | + |
| 167 | +## API Design Principles |
| 168 | + |
| 169 | +1. **RESTful** - Standard HTTP methods and status codes |
| 170 | +2. **Consistent** - Uniform response format across all endpoints |
| 171 | +3. **Paginated** - Large result sets include pagination metadata |
| 172 | +4. **Versioned** - API version in URL path (/api/v1/) |
| 173 | +5. **Documented** - OpenAPI specification for all endpoints |
| 174 | + |
| 175 | +## Contributing |
| 176 | + |
| 177 | +When adding new shared resources: |
| 178 | + |
| 179 | +1. **Types** - Add TypeScript interfaces with proper JSDoc comments |
| 180 | +2. **Schemas** - Include validation rules and error messages |
| 181 | +3. **Constants** - Group related constants logically |
| 182 | +4. **Documentation** - Update this README with usage examples |
| 183 | + |
| 184 | +### Adding New Types |
| 185 | + |
| 186 | +```typescript |
| 187 | +// Example: shared/types/venue.ts |
| 188 | +export interface Venue { |
| 189 | + id: string; |
| 190 | + name: string; |
| 191 | + city: string; |
| 192 | + capacity: number; |
| 193 | + surface_type: VenueSurfaceType; |
| 194 | + is_dome: boolean; |
| 195 | +} |
| 196 | + |
| 197 | +export enum VenueSurfaceType { |
| 198 | + GRASS = 'grass', |
| 199 | + ARTIFICIAL_TURF = 'artificial_turf', |
| 200 | + CLAY = 'clay', |
| 201 | + HARDCOURT = 'hardcourt' |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +### Adding New Constants |
| 206 | + |
| 207 | +```typescript |
| 208 | +// Example: shared/constants/venues.ts |
| 209 | +export const VENUE_TYPES = { |
| 210 | + STADIUM: 'stadium', |
| 211 | + ARENA: 'arena', |
| 212 | + FIELD: 'field', |
| 213 | + COURT: 'court', |
| 214 | + TRACK: 'track' |
| 215 | +} as const; |
| 216 | + |
| 217 | +export const VENUE_CAPACITIES = { |
| 218 | + SMALL: { min: 0, max: 20000 }, |
| 219 | + MEDIUM: { min: 20001, max: 50000 }, |
| 220 | + LARGE: { min: 50001, max: 100000 }, |
| 221 | + MEGA: { min: 100001, max: 200000 } |
| 222 | +}; |
| 223 | +``` |
| 224 | + |
| 225 | +## Best Practices |
| 226 | + |
| 227 | +1. **Type Safety** - Use strict TypeScript types with proper constraints |
| 228 | +2. **Validation** - Validate all data at API boundaries |
| 229 | +3. **Documentation** - Include JSDoc comments for all public interfaces |
| 230 | +4. **Consistency** - Follow established naming conventions |
| 231 | +5. **Performance** - Consider database indexes and query patterns |
| 232 | +6. **Security** - Implement proper RLS policies and input validation |
| 233 | + |
| 234 | +## Related Documentation |
| 235 | + |
| 236 | +- [API Documentation](../docs/api/) |
| 237 | +- [Database Schema](../docs/architecture/database-schema.md) |
| 238 | +- [Development Guidelines](../docs/development/coding-standards.md) |
| 239 | +- [Deployment Guide](../docs/deployment/) |
0 commit comments