|
| 1 | +# JAVASCRIPT CLIENT - AI AGENT INSTRUCTIONS |
| 2 | + |
| 3 | +## ⚠️ CRITICAL: CHECK YOUR REPOSITORY FIRST |
| 4 | + |
| 5 | +Before making ANY changes, verify you're in the correct repository: |
| 6 | + |
| 7 | +```bash |
| 8 | +git remote -v |
| 9 | +``` |
| 10 | + |
| 11 | +- ✅ **CORRECT**: `origin .../algolia/api-clients-automation.git` → You may proceed |
| 12 | +- ❌ **WRONG**: `origin .../algolia/algoliasearch-client-javascript.git` → STOP! This is the PUBLIC repository |
| 13 | + |
| 14 | +**If you're in `algoliasearch-client-javascript`**: Do NOT make changes here. All changes must go through `api-clients-automation`. PRs and commits made directly to the public repo will be discarded on next release. |
| 15 | + |
| 16 | +## ⚠️ BEFORE ANY EDIT: Check If File Is Generated |
| 17 | + |
| 18 | +Before editing ANY file, verify it's hand-written by checking `config/generation.config.mjs`: |
| 19 | + |
| 20 | +```javascript |
| 21 | +// In generation.config.mjs - patterns WITHOUT '!' are GENERATED (do not edit) |
| 22 | +'clients/algoliasearch-client-javascript/packages/**/package.json', // Generated |
| 23 | +'!clients/algoliasearch-client-javascript/packages/client-common/**', // Hand-written ✓ |
| 24 | +``` |
| 25 | + |
| 26 | +**Hand-written (safe to edit):** |
| 27 | + |
| 28 | +- `packages/client-common/**` - Core transport, types, utilities |
| 29 | +- `packages/requester-*/**` - HTTP requesters (browser-xhr, node-http, fetch) |
| 30 | +- `packages/logger-console/**` - Logging utilities |
| 31 | +- `packages/algoliasearch/__tests__/**` - Tests |
| 32 | +- `scripts/**` - Build scripts |
| 33 | + |
| 34 | +**Generated (DO NOT EDIT):** |
| 35 | + |
| 36 | +- `packages/*/model/**` - API models |
| 37 | +- `packages/*/src/**` (except client-common) - API clients |
| 38 | +- `packages/**/package.json` - Package configs |
| 39 | + |
| 40 | +## Language Conventions |
| 41 | + |
| 42 | +### Naming |
| 43 | + |
| 44 | +- **Files**: `camelCase.ts` for source, `camelCase.test.ts` for tests |
| 45 | +- **Variables/Functions**: `camelCase` |
| 46 | +- **Types/Interfaces**: `PascalCase` |
| 47 | +- **Constants**: `UPPER_SNAKE_CASE` or `camelCase` depending on scope |
| 48 | + |
| 49 | +### Formatting |
| 50 | + |
| 51 | +- Prettier with project config (`.prettierrc`) |
| 52 | +- 120 char line width |
| 53 | +- Single quotes, trailing commas |
| 54 | +- Run: `yarn cli format javascript clients/algoliasearch-client-javascript` |
| 55 | + |
| 56 | +### TypeScript Patterns |
| 57 | + |
| 58 | +- Strict mode enabled |
| 59 | +- No `any` - use `unknown` or proper types |
| 60 | +- No `@ts-ignore` or `@ts-expect-error` |
| 61 | +- Prefer `type` over `interface` for object shapes |
| 62 | +- Use `readonly` for immutable properties |
| 63 | + |
| 64 | +### Dependencies |
| 65 | + |
| 66 | +- **HTTP**: Custom requesters (not axios/fetch directly) |
| 67 | +- **Build**: tsup for bundling |
| 68 | +- **Test**: Vitest |
| 69 | +- **Monorepo**: Yarn workspaces + Lerna |
| 70 | + |
| 71 | +## Client Patterns |
| 72 | + |
| 73 | +### Transporter Architecture |
| 74 | + |
| 75 | +```typescript |
| 76 | +// Core transport in packages/client-common/src/transporter/ |
| 77 | +createTransporter({ |
| 78 | + hosts, // Host[] - API hosts with failover |
| 79 | + hostsCache, // Cache for host state |
| 80 | + requester, // Pluggable HTTP implementation |
| 81 | + requestsCache, // Request deduplication |
| 82 | + responsesCache, // Response caching |
| 83 | +}); |
| 84 | +``` |
| 85 | + |
| 86 | +### Retry Strategy |
| 87 | + |
| 88 | +- Hosts tracked as `up`, `down`, or `timedOut` |
| 89 | +- Automatic failover to next host on failure |
| 90 | +- Timeout increases with retry count |
| 91 | +- Network errors are retryable, 4xx errors are not |
| 92 | + |
| 93 | +### Cache System |
| 94 | + |
| 95 | +- `createMemoryCache()` - In-memory, serializable |
| 96 | +- `createBrowserLocalStorageCache()` - Browser persistence |
| 97 | +- `createNullCache()` - No-op for servers |
| 98 | +- `createFallbackableCache()` - Chain caches |
| 99 | + |
| 100 | +## Common Gotchas |
| 101 | + |
| 102 | +### Browser vs Node |
| 103 | + |
| 104 | +- Different requesters for each environment |
| 105 | +- Use `requester-browser-xhr` for browsers |
| 106 | +- Use `requester-node-http` for Node.js |
| 107 | +- Check bundle targets in `tsup.config.ts` |
| 108 | + |
| 109 | +### Async Patterns |
| 110 | + |
| 111 | +- All API methods return Promises |
| 112 | +- No callback-style APIs |
| 113 | +- Use `async/await`, avoid `.then()` chains in new code |
| 114 | + |
| 115 | +### Type Imports |
| 116 | + |
| 117 | +```typescript |
| 118 | +// Use 'import type' for type-only imports |
| 119 | +import type { SearchResponse } from './types'; |
| 120 | +``` |
| 121 | + |
| 122 | +### Environment Detection |
| 123 | + |
| 124 | +```typescript |
| 125 | +// Avoid direct process/window checks |
| 126 | +// Use requester abstraction instead |
| 127 | +``` |
| 128 | + |
| 129 | +## Build & Test Commands |
| 130 | + |
| 131 | +```bash |
| 132 | +# From repo root (api-clients-automation) |
| 133 | +yarn cli build clients javascript # Build all JS packages |
| 134 | +yarn cli cts generate javascript # Generate CTS tests |
| 135 | +yarn cli cts run javascript # Run CTS tests |
| 136 | +yarn cli playground javascript node search # Interactive playground |
| 137 | +yarn cli format javascript clients/algoliasearch-client-javascript |
| 138 | + |
| 139 | +# From client directory (for package-specific work) |
| 140 | +cd clients/algoliasearch-client-javascript |
| 141 | +yarn build # Build packages |
| 142 | +yarn test # Run tests |
| 143 | +``` |
0 commit comments