|
| 1 | +## Astrology API TypeScript SDK |
| 2 | + |
| 3 | +Type-safe Node.js client for the [Astrology API v3.0.0](https://api.astrology-api.io/rapidoc). The package ships as an ESM build, exposes a modular architecture with dedicated sub-clients per endpoint family, and enforces 100 % test coverage. Publish-ready as `@procoders/astrology-api-client`. |
| 4 | + |
| 5 | +### Highlights |
| 6 | +- Axios-powered HTTP layer with retry/backoff, header normalization, and response unwrapping |
| 7 | +- Strong TypeScript types generated from the local OpenAPI spec (`docs-openapi.json`) |
| 8 | +- Exhaustive runtime validation via `src/utils/validators.ts` to prevent invalid API calls |
| 9 | +- Rich category coverage: data, charts, horoscopes, analysis, astrocartography, insights, SVG, enhanced traditional analytics, and more |
| 10 | +- Debug-friendly logging pipeline with configurable logger and opt-in verbosity |
| 11 | + |
| 12 | +### Installation |
| 13 | +```bash |
| 14 | +npm install @procoders/astrology-api-client |
| 15 | +``` |
| 16 | + |
| 17 | +### Environment Variables |
| 18 | +| Variable | Required | Description | |
| 19 | +| --- | --- | --- | |
| 20 | +| `ASTROLOGY_API_BASE_URL` | optional | Override for the default `https://api.astrology-api.io` endpoint | |
| 21 | +| `ASTROLOGY_DEBUG` | optional | Set to `true` to enable debug logging | |
| 22 | +| `RUN_ASTROLOGY_EXAMPLE` | optional | Set to `true` to execute `examples/usage.ts` | |
| 23 | + |
| 24 | +### Client Architecture |
| 25 | +```ts |
| 26 | +import { AstrologyClient } from '@procoders/astrology-api-client'; |
| 27 | + |
| 28 | +const client = new AstrologyClient({ |
| 29 | + apiKey: 'your-api-key-here', // optional for public endpoints |
| 30 | + baseUrl: process.env.ASTROLOGY_API_BASE_URL ?? 'https://api.astrology-api.io', |
| 31 | + retry: { attempts: 2, delayMs: 250 }, |
| 32 | + debug: process.env.ASTROLOGY_DEBUG === 'true', |
| 33 | +}); |
| 34 | + |
| 35 | +// Category sub-clients |
| 36 | +client.data.getPositions(...); |
| 37 | +client.charts.getNatalChart(...); |
| 38 | +client.analysis.getNatalReport(...); |
| 39 | +client.horoscope.getPersonalDailyHoroscope(...); |
| 40 | +client.insights.relationship.getCompatibility(...); |
| 41 | +client.svg.getNatalChartSvg(...); |
| 42 | +client.enhanced.getGlobalAnalysis(...); |
| 43 | +``` |
| 44 | + |
| 45 | +| Sub-client | Path prefix | Sample methods | |
| 46 | +| --- | --- | --- | |
| 47 | +| `data` | `/api/v3/data` | `getPositions`, `getGlobalPositions`, `getLunarMetrics` | |
| 48 | +| `charts` | `/api/v3/charts` | `getNatalChart`, `getTransitChart`, `getSolarReturnChart` | |
| 49 | +| `horoscope` | `/api/v3/horoscope` | `getPersonalDailyHoroscope`, `getSignWeeklyHoroscopeText` | |
| 50 | +| `analysis` | `/api/v3/analysis` | `getSynastryReport`, `getCompatibilityAnalysis`, `getProgressionReport` | |
| 51 | +| `glossary` | `/api/v3/glossary` | `getCities`, `getActivePoints`, `getHouseSystems` | |
| 52 | +| `astrocartography` | `/api/v3/astrocartography` | `getLines`, `getMap`, `getRelocationChart` | |
| 53 | +| `chinese` | `/api/v3/chinese` | `getBaZi`, `getYearlyForecast`, `getCompatibility` | |
| 54 | +| `eclipses` | `/api/v3/eclipses` | `getUpcoming`, `getNatalCheck`, `getInterpretation` | |
| 55 | +| `lunar` | `/api/v3/lunar` | `getCalendar`, `getPhases`, `getVoidOfCourse` | |
| 56 | +| `numerology` | `/api/v3/numerology` | `getCoreNumbers`, `getComprehensiveReport`, `getCompatibility` | |
| 57 | +| `tarot` | `/api/v3/tarot` | `drawCards`, `getTreeOfLife`, `getTimingAnalysis` | |
| 58 | +| `traditional` | `/api/v3/traditional` | `getAnalysis`, `getAnnualProfection`, `getLots` | |
| 59 | +| `fixedStars` | `/api/v3/fixed-stars` | `getPositions`, `getConjunctions`, `getReport` | |
| 60 | +| `insights` | `/api/v3/insights` | relationship/pet/wellness/financial/business suites | |
| 61 | +| `svg` | `/api/v3/svg` | `getNatalChartSvg`, `getSynastryChartSvg`, `getTransitChartSvg` | |
| 62 | +| `enhanced` | `/api/v3/enhanced*` | `getGlobalAnalysis`, `getPersonalAnalysis`, chart variants | |
| 63 | + |
| 64 | +Each category client inherits from a shared base that enforces the API prefix contract and provides a consistent `buildUrl` helper, so request construction stays uniform across the SDK. |
| 65 | + |
| 66 | +### Category Examples |
| 67 | +```ts |
| 68 | +// Data: planetary positions |
| 69 | +const subject = { |
| 70 | + name: 'Demo User', |
| 71 | + birth_data: { |
| 72 | + year: 1990, |
| 73 | + month: 5, |
| 74 | + day: 11, |
| 75 | + hour: 18, |
| 76 | + minute: 15, |
| 77 | + city: 'London', |
| 78 | + country_code: 'GB', |
| 79 | + }, |
| 80 | +}; |
| 81 | +const subjectA = subject; |
| 82 | +const subjectB = { ...subject, name: 'Partner' }; |
| 83 | + |
| 84 | +const positions = await client.data.getPositions({ subject }); |
| 85 | + |
| 86 | +// Charts: natal chart SVG export |
| 87 | +const natalSvg = await client.svg.getNatalChartSvg({ subject, svg_options: { theme: 'dark' } }); |
| 88 | + |
| 89 | +// Analysis: synastry report |
| 90 | +const synastry = await client.analysis.getSynastryReport({ subject1: subjectA, subject2: subjectB }); |
| 91 | + |
| 92 | +// Horoscope: personalized daily forecast |
| 93 | +const daily = await client.horoscope.getPersonalDailyHoroscope({ subject }); |
| 94 | + |
| 95 | +// Insights: relationship compatibility |
| 96 | +const compatibility = await client.insights.relationship.getCompatibility({ subjects: [subjectA, subjectB] }); |
| 97 | + |
| 98 | +// Enhanced: global traditional analysis |
| 99 | +const global = await client.enhanced.getGlobalAnalysis({ |
| 100 | + options: { house_system: 'W', zodiac_type: 'Tropic', active_points: ['Sun', 'Moon', 'Mercury'], precision: 3 }, |
| 101 | + orbs: { major_aspects_deg: 2 }, |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +See `examples/usage.ts` for a full script that conditionally runs when `RUN_ASTROLOGY_EXAMPLE=true`. |
| 106 | + |
| 107 | +### Debug Logging |
| 108 | + |
| 109 | +Pass `debug: true` and optionally supply a custom `logger` function. Setting `ASTROLOGY_DEBUG=true` enables the same behaviour globally. The client logs request metadata, retry attempts, and responses (status and url). |
| 110 | + |
| 111 | +### Testing & Linting |
| 112 | +```bash |
| 113 | +npm run lint # ESLint with @typescript-eslint + Prettier |
| 114 | +npm run test # Vitest watch mode |
| 115 | +npm run test:coverage # Vitest with V8 coverage (enforced at 100 %) |
| 116 | +npm run build # TypeScript build (emits ESM to dist/) |
| 117 | +``` |
| 118 | + |
| 119 | +### Project Structure |
| 120 | +``` |
| 121 | +├── src/ |
| 122 | +│ ├── categories/ # Modular sub-clients per API family |
| 123 | +│ ├── client.ts # Root client wiring sub-clients & interceptors |
| 124 | +│ ├── errors/AstrologyError.ts # Custom error hierarchy |
| 125 | +│ ├── types/ # Generated and hand-written typings |
| 126 | +│ └── utils/validators.ts # Runtime payload guards |
| 127 | +├── tests/unit/ # Vitest suites with axios-mock-adapter |
| 128 | +├── examples/usage.ts # Usage example (guarded by env flag) |
| 129 | +├── docs-openapi.json # Cached OpenAPI specification |
| 130 | +└── README.md |
| 131 | +``` |
| 132 | + |
| 133 | +### Publishing Checklist |
| 134 | +1. Ensure `npm run test:coverage` passes with 100 % coverage. |
| 135 | +2. Run `npm run build` and review the emitted `dist/` bundle. |
| 136 | +3. Update version, changelog, and documentation as needed. |
| 137 | + |
| 138 | +### License |
| 139 | +Released under the MIT License. See [`LICENSE`](./LICENSE) for details. |
| 140 | + |
0 commit comments