Skip to content

Commit ac8e6dc

Browse files
committed
Refactor types and validators for improved type safety and clarity. Added DirectionReportRequest and ElementalDignitiesRequest types. Updated mergeConfigWithParams to use generic type T. Adjusted various interfaces in requests and responses for consistency and accuracy. Ensured all changes maintain strict TypeScript compliance.
1 parent b3a6857 commit ac8e6dc

File tree

6 files changed

+43
-122
lines changed

6 files changed

+43
-122
lines changed

src/categories/AnalysisClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { AxiosRequestConfig } from 'axios';
33
import {
44
CompatibilityRequest,
55
CompositeReportRequest,
6+
DirectionReportRequest,
67
LunarAnalysisRequest,
78
LunarReturnReportRequest,
89
LunarReturnTransitRequest,

src/categories/LunarClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import {
2525
} from '../utils/validators';
2626
import { BaseCategoryClient } from './BaseCategoryClient';
2727

28-
const mergeConfigWithParams = (
28+
const mergeConfigWithParams = <T extends object>(
2929
config: AxiosRequestConfig | undefined,
30-
params?: Record<string, unknown>,
30+
params?: T,
3131
): AxiosRequestConfig | undefined => {
3232
if (!params || Object.keys(params).length === 0) {
3333
return config;

src/categories/TarotClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ import {
5252
} from '../utils/validators';
5353
import { BaseCategoryClient } from './BaseCategoryClient';
5454

55-
const mergeConfigWithParams = (
55+
const mergeConfigWithParams = <T extends object>(
5656
config: AxiosRequestConfig | undefined,
57-
params?: Record<string, unknown> | null,
57+
params?: T | null,
5858
): AxiosRequestConfig | undefined => {
5959
if (!params || Object.keys(params).length === 0) {
6060
return config;

src/types/requests.ts

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,14 @@ export type ActivePointType =
134134
| 'asteroid'
135135
| 'fixed-star';
136136

137+
export type PrimaryActivePointType = Exclude<ActivePointType, 'fixed-star'>;
138+
137139
export interface ActivePointsQuery {
138140
type?: ActivePointType;
139141
}
140142

141143
export interface PrimaryActivePointsQuery {
142-
type?: Exclude<ActivePointType, 'fixed-star'>;
144+
type?: PrimaryActivePointType;
143145
}
144146

145147
export interface HousesRequest {
@@ -514,34 +516,6 @@ export interface PersonalTradingRequest {
514516
language?: Language | null;
515517
}
516518

517-
export type ActivePointType = 'planet' | 'lunar-node' | 'angle' | 'special-point' | 'asteroid' | 'fixed-star';
518-
519-
export interface ActivePointsQuery {
520-
type?: ActivePointType;
521-
}
522-
523-
export type PrimaryActivePointType = Exclude<ActivePointType, 'fixed-star'>;
524-
525-
export interface PrimaryActivePointsQuery {
526-
type?: PrimaryActivePointType;
527-
}
528-
529-
export interface HousesRequest {
530-
house_system?: HouseSystem;
531-
}
532-
533-
export type KeywordsCategory = 'planets' | 'lines' | 'houses' | 'aspects' | 'themes';
534-
535-
export interface KeywordsRequest {
536-
category?: KeywordsCategory;
537-
}
538-
539-
export type LifeAreasLanguage = 'en' | 'de' | 'fr' | 'es' | 'ru';
540-
541-
export interface LifeAreasRequest {
542-
language?: LifeAreasLanguage;
543-
}
544-
545519
export interface ProgressionReportRequest extends ProgressionRequest {
546520
report_options?: ReportOptions | null;
547521
}

src/types/responses.ts

Lines changed: 11 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { components } from './generated/api';
22
import type {
33
ActivePointType,
4-
KeywordCategory,
4+
KeywordsCategory,
55
LifeAreasLanguage,
66
Subject,
77
} from './requests';
@@ -149,7 +149,7 @@ export interface SynastryReportResponse {
149149
export interface ActivePoint {
150150
id: string;
151151
description: string;
152-
type: string;
152+
type: ActivePointType;
153153
strength?: number | null;
154154
[key: string]: unknown;
155155
}
@@ -232,7 +232,7 @@ export interface AvailableHouseSystem {
232232
}
233233

234234
export interface HousesResponse {
235-
house_system?: string;
235+
house_system: string;
236236
houses: HouseInfo[];
237237
available_systems?: AvailableHouseSystem[];
238238
[key: string]: unknown;
@@ -247,7 +247,7 @@ export interface KeywordCollection {
247247
}
248248

249249
export interface KeywordsResponse {
250-
category: string;
250+
category: KeywordsCategory;
251251
total_concepts?: number;
252252
keywords: KeywordCollection[];
253253
[key: string]: unknown;
@@ -270,7 +270,7 @@ export interface LifeArea {
270270
description: string;
271271
keywords?: string[];
272272
category?: string;
273-
emoji?: string;
273+
emoji?: string | null;
274274
primary_planets?: string[];
275275
secondary_planets?: string[];
276276
primary_houses?: number[];
@@ -281,8 +281,8 @@ export interface LifeArea {
281281
}
282282

283283
export interface LifeAreasResponse {
284-
language: string;
285-
total_areas?: number;
284+
language: LifeAreasLanguage;
285+
total_areas: number;
286286
life_areas: LifeArea[];
287287
[key: string]: unknown;
288288
}
@@ -402,85 +402,18 @@ export interface PaginatedResponse<T> {
402402
offset: number;
403403
}
404404

405-
export interface ActivePoint {
406-
id: string;
407-
description: string;
408-
type: ActivePointType;
409-
strength?: number;
410-
}
411-
412-
export interface CountryInfo {
413-
name: string;
414-
iso: string;
415-
capital?: string | null;
416-
population?: number | null;
417-
continent?: string | null;
418-
currency?: string | null;
419-
}
405+
export type CountryInfo = CountryReference;
420406

421407
export interface GlossaryItem {
422408
id: string;
423409
description: string;
424410
}
425411

426-
export interface HouseSystemInfo {
427-
code: string;
428-
name: string;
429-
description?: string;
430-
category?: string;
431-
usage?: string;
432-
is_default?: boolean;
433-
}
412+
export type HouseSystemInfo = AvailableHouseSystem;
434413

435-
export interface HouseInfo {
436-
number: number;
437-
name: string;
438-
themes?: string[];
439-
primary_theme?: string | null;
440-
life_areas?: string[];
441-
traditional_meaning?: string;
442-
modern_keywords?: string[];
443-
}
414+
export type KeywordEntry = KeywordCollection;
444415

445-
export interface HousesResponse {
446-
house_system: string;
447-
houses: HouseInfo[];
448-
available_systems?: HouseSystemInfo[];
449-
}
450-
451-
export interface KeywordEntry {
452-
id: string;
453-
name: string;
454-
keywords: string[];
455-
total_keywords?: number;
456-
}
457-
458-
export interface KeywordsResponse {
459-
category: KeywordCategory;
460-
total_concepts?: number;
461-
keywords: KeywordEntry[];
462-
}
463-
464-
export interface LifeAreaDetails {
465-
id: string;
466-
name: string;
467-
description: string;
468-
keywords?: string[];
469-
category?: string;
470-
emoji?: string | null;
471-
primary_planets?: string[];
472-
secondary_planets?: string[];
473-
primary_houses?: number[];
474-
secondary_houses?: number[];
475-
preferred_lines?: string[];
476-
strength_weight?: number;
477-
}
478-
479-
export interface LifeAreasResponse {
480-
language: LifeAreasLanguage;
481-
total_areas: number;
482-
life_areas: LifeAreaDetails[];
483-
}
416+
export type LifeAreaDetails = LifeArea;
484417

485418
export type EnhancedPlanetaryPosition = Schemas['EnhancedPlanetaryPosition'];
486419

src/utils/validators.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
DirectionReportRequest,
2929
DirectionType,
3030
DrawCardsRequest,
31+
ElementalDignitiesRequest,
3132
CryptoTimingRequest,
3233
ForexTimingRequest,
3334
FixedStarsConjunctionsRequest,
@@ -103,6 +104,7 @@ import {
103104
TarotNatalReportRequest,
104105
TarotReportRequest,
105106
TarotTransitReportRequest,
107+
TreeOfLifeRequest,
106108
TextHoroscopeRequest,
107109
TextMonthlyHoroscopeRequest,
108110
TextWeeklyHoroscopeRequest,
@@ -480,12 +482,12 @@ const validateChineseSubject = (subject: ChineseHoroscopeSubject | ChineseSubjec
480482
ensureIntegerInRange(birthData.minute ?? null, [0, 59], `${context}.birth_data.minute`);
481483
ensureIntegerInRange((birthData as { second?: number | null }).second ?? null, [0, 59], `${context}.birth_data.second`);
482484

483-
if (birthData.latitude !== undefined) {
484-
ensureNumberInRange(birthData.latitude ?? null, [-90, 90], `${context}.birth_data.latitude`);
485+
if ('latitude' in birthData && birthData.latitude !== undefined) {
486+
ensureNumberInRange((birthData.latitude as number | null) ?? null, [-90, 90], `${context}.birth_data.latitude`);
485487
}
486488

487-
if (birthData.longitude !== undefined) {
488-
ensureNumberInRange(birthData.longitude ?? null, [-180, 180], `${context}.birth_data.longitude`);
489+
if ('longitude' in birthData && birthData.longitude !== undefined) {
490+
ensureNumberInRange((birthData.longitude as number | null) ?? null, [-180, 180], `${context}.birth_data.longitude`);
489491
}
490492

491493
const gender = extractChineseGender(subject);
@@ -1539,7 +1541,10 @@ const validateBusinessOptions = (
15391541
}
15401542

15411543
if ('departments' in opts) {
1542-
validateDepartments(opts.departments as BusinessMultipleRequest['options']['departments'], subjectCount, `${context}.departments`);
1544+
const { departments } = opts as {
1545+
departments?: NonNullable<BusinessMultipleRequest['options']>['departments'] | null;
1546+
};
1547+
validateDepartments(departments ?? null, subjectCount, `${context}.departments`);
15431548
}
15441549
};
15451550

@@ -1682,10 +1687,16 @@ export const validatePersonalTradingRequest = (
16821687

16831688
const options = extractOptionsObject(request.options ?? null);
16841689
if (options) {
1685-
validateAnalysisDays(options.analysis_period_days ?? null, `${context}.options.analysis_period_days`);
1686-
ensureOptionalBoolean(options.include_lunar_cycles, `${context}.options.include_lunar_cycles`);
1687-
normalizeTradingStyle(options.trading_style as string | undefined, `${context}.options.trading_style`);
1688-
validateLanguageValue(options.language as string | undefined, `${context}.options.language`);
1690+
const typedOptions = options as {
1691+
analysis_period_days?: number | null;
1692+
include_lunar_cycles?: boolean | null;
1693+
trading_style?: string | null;
1694+
language?: string | null;
1695+
};
1696+
validateAnalysisDays(typedOptions.analysis_period_days ?? null, `${context}.options.analysis_period_days`);
1697+
ensureOptionalBoolean(typedOptions.include_lunar_cycles, `${context}.options.include_lunar_cycles`);
1698+
normalizeTradingStyle(typedOptions.trading_style, `${context}.options.trading_style`);
1699+
validateLanguageValue(typedOptions.language, `${context}.options.language`);
16891700
}
16901701
};
16911702

@@ -1962,8 +1973,10 @@ export const validateElementalDignitiesRequest = (
19621973
throw new AstrologyError(`${context}.cards must include at least one entry.`);
19631974
}
19641975

1965-
request.cards.forEach((card, index) => {
1966-
if (!card || typeof card !== 'object') {
1976+
const cards = request.cards as Array<NonNullable<ElementalDignitiesRequest['cards']>[number]>;
1977+
1978+
cards.forEach((card, index) => {
1979+
if (!card || typeof card !== 'object' || Array.isArray(card)) {
19671980
throw new AstrologyError(`${context}.cards[${index}] must be an object.`);
19681981
}
19691982

0 commit comments

Comments
 (0)