import {z} from 'zod' export type PrashnaNumberInput = {system?: ('navamsa108' | 'ashtamangala') | undefined, number: number}; export type CreateApiVedicPrashnaNumberArgs = {fields?: string | undefined, precision?: number | undefined, 'Idempotency-Key'?: string | undefined, body: PrashnaNumberInput}; export type CreateApiVedicPrashnaNumberOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiVedicPrashnaNumberResponse = z.object({ok: z.boolean().optional(), data: z.object({system: z.string().nullable().optional(), number: z.number().nullable().optional(), sign: z.number().nullable().optional(), signName: z.string().nullable().optional(), navamsaInSign: z.number().nullable().optional(), navamsaSign: z.number().nullable().optional(), navamsaSignName: z.string().nullable().optional(), span: z.object({start: z.number().nullable().optional(), end: z.number().nullable().optional()}).nullable().optional(), arudha: z.string().nullable().optional(), kpNumbers: z.string().nullable().optional(), sources: z.object({navamsa108: z.object({text: z.string().nullable().optional(), ref: z.string().nullable().optional(), status: z.string().nullable().optional()}).nullable().optional()}).nullable().optional()}).optional()}); 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 }; /** * Prashna: number to sign * * POST /vedic/prashna/number */ export const createApiVedicPrashnaNumber = async (args: CreateApiVedicPrashnaNumberArgs, options?: CreateApiVedicPrashnaNumberOptions) => { const {baseUrl = "https://api.astroway.info/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.fields != null) query.append("fields", String(args.fields)) if (args.precision != null) query.append("precision", String(args.precision)) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/vedic/prashna/number${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiVedicPrashnaNumberResponse, await res.json()) };