import {z} from 'zod' export type GetApiAstrologyNakshatraPoruthamArgs = {girl_nakshatra: number, girl_nakshatra_pada: number, boy_nakshatra: number, boy_nakshatra_pada: number, la?: string | undefined}; export type GetApiAstrologyNakshatraPoruthamOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const message = z.object({type: z.string().nullable(), description: z.string()}); export const poruthamCheck = z.object({id: z.number().int(), name: z.string(), has_porutham: z.boolean()}); export const nakshatraPorutham = z.object({maximum_points: z.number(), obtained_points: z.number(), message: message, matches: z.array(poruthamCheck)}); export const nakshatraPoruthamResponse = z.object({status: z.string(), data: nakshatraPorutham}); 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 }; /** * Nakshatra Porutham * * GET /astrology/nakshatra-porutham */ export const getApiAstrologyNakshatraPorutham = async (args: GetApiAstrologyNakshatraPoruthamArgs, options?: GetApiAstrologyNakshatraPoruthamOptions) => { 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.girl_nakshatra != null) query.append("girl_nakshatra", String(args.girl_nakshatra)) if (args.girl_nakshatra_pada != null) query.append("girl_nakshatra_pada", String(args.girl_nakshatra_pada)) if (args.boy_nakshatra != null) query.append("boy_nakshatra", String(args.boy_nakshatra)) if (args.boy_nakshatra_pada != null) query.append("boy_nakshatra_pada", String(args.boy_nakshatra_pada)) if (args.la != null) query.append("la", String(args.la)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/astrology/nakshatra-porutham${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(nakshatraPoruthamResponse, await res.json()) };