import {z} from 'zod' export type GetApiAstrologyKundliArgs = {ayanamsa: number, coordinates: string, datetime: string, la?: string | undefined}; export type GetApiAstrologyKundliOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const lord = z.object({id: z.number().int(), name: z.string(), vedic_name: z.string()}); export const nakshatra = z.object({id: z.number().int(), name: z.string(), lord: lord, pada: z.number().int().optional()}); export const rasi = z.object({id: z.number().int(), name: z.string(), lord: lord}); export const zodiac = z.object({id: z.number().int(), name: z.string()}); export const nakshatraAdditionalInfo = z.object({deity: z.string(), ganam: z.string(), symbol: z.string(), animal_sign: z.string(), nadi: z.string(), color: z.string(), best_direction: z.string(), syllables: z.string(), birth_stone: z.string(), gender: z.string(), planet: z.string(), enemy_yoni: z.string()}); export const birthDetails = z.object({nakshatra: nakshatra, chandra_rasi: rasi, soorya_rasi: rasi, zodiac: zodiac, additional_info: nakshatraAdditionalInfo}); export const mangalDosha = z.object({has_dosha: z.boolean(), description: z.string()}); export const kundliYogaDetails = z.object({name: z.string(), description: z.string()}); export const kundli = z.object({nakshatra_details: birthDetails, mangal_dosha: mangalDosha, yoga_details: z.array(kundliYogaDetails)}); export const kundliResponse = z.object({status: z.string(), data: kundli}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Kundli * * GET /astrology/kundli */ export const getApiAstrologyKundli = async (args: GetApiAstrologyKundliArgs, options?: GetApiAstrologyKundliOptions) => { const {baseUrl = "https://api.prokerala.com/v2", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ayanamsa != null) query.append("ayanamsa", String(args.ayanamsa)) if (args.coordinates != null) query.append("coordinates", String(args.coordinates)) if (args.datetime != null) query.append("datetime", String(args.datetime)) if (args.la != null) query.append("la", String(args.la)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/astrology/kundli${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(kundliResponse, await res.json()) };