import {z} from 'zod' export type GetApiAstrologySynastryPlanetAspectArgs = {primary_profile: {datetime: string, coordinates: string, birth_time_unknown?: boolean | undefined}, secondary_profile: {datetime: string, coordinates: string, birth_time_unknown?: boolean | undefined}, house_system: string, chart_type: string, orb: string, birth_time_rectification?: string | undefined, la?: string | undefined, ayanamsa?: number | undefined}; export type GetApiAstrologySynastryPlanetAspectOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const westernPlanet = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const aspect = z.object({id: z.number().int(), name: z.string()}); export const planetAspect = z.object({planet_one: westernPlanet, planet_two: westernPlanet, aspect: aspect, orb: z.number()}); export const synastryPlanetAspectResponse = z.object({status: z.string(), data: z.object({synastry_aspects: z.array(planetAspect)})}); 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 }; /** * Synastry Planet Aspect * * GET /astrology/synastry-planet-aspect */ export const getApiAstrologySynastryPlanetAspect = async (args: GetApiAstrologySynastryPlanetAspectArgs, options?: GetApiAstrologySynastryPlanetAspectOptions) => { 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.primary_profile != null) query.append("primary_profile", JSON.stringify(args.primary_profile)) if (args.secondary_profile != null) query.append("secondary_profile", JSON.stringify(args.secondary_profile)) if (args.house_system != null) query.append("house_system", String(args.house_system)) if (args.chart_type != null) query.append("chart_type", String(args.chart_type)) if (args.orb != null) query.append("orb", String(args.orb)) if (args.birth_time_rectification != null) query.append("birth_time_rectification", String(args.birth_time_rectification)) if (args.la != null) query.append("la", String(args.la)) if (args.ayanamsa != null) query.append("ayanamsa", String(args.ayanamsa)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/astrology/synastry-planet-aspect${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(synastryPlanetAspectResponse, await res.json()) };