import {z} from 'zod' export type GetApiAstrologyDashaPeriodsArgs = {ayanamsa: number, coordinates: string, datetime: string, la?: string | undefined, year_length?: number | undefined}; export type GetApiAstrologyDashaPeriodsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const pratyantarDasha = z.object({id: z.number().int(), name: z.string(), start: z.string(), end: z.string()}); export const antarDasha = z.object({id: z.number().int(), name: z.string(), start: z.string(), end: z.string(), pratyantardasha: z.array(pratyantarDasha)}); export const vimsottariDasha = z.object({id: z.number().int(), name: z.string(), start: z.string(), end: z.string(), antardasha: z.array(antarDasha)}); export const planet = z.object({id: z.number().int(), name: z.string(), vedic_name: z.string()}); export const dashaBalance = z.object({lord: planet, duration: z.string(), description: z.string()}); export const dashaPeriodResponse = z.object({status: z.string(), data: z.object({dasha_periods: z.array(vimsottariDasha), dasha_balance: dashaBalance})}); 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 }; /** * Dasha Periods * * GET /astrology/dasha-periods */ export const getApiAstrologyDashaPeriods = async (args: GetApiAstrologyDashaPeriodsArgs, options?: GetApiAstrologyDashaPeriodsOptions) => { 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 (args.year_length != null) query.append("year_length", String(args.year_length)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/astrology/dasha-periods${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(dashaPeriodResponse, await res.json()) };