import {z} from 'zod' export type GetApiEsotericCrystalsByPlanetPlanetArgs = {planet: 'sun' | 'moon' | 'mercury' | 'venus' | 'mars' | 'jupiter' | 'saturn' | 'uranus' | 'neptune' | 'pluto' | 'earth', fields?: string | undefined, precision?: number | undefined}; export type GetApiEsotericCrystalsByPlanetPlanetOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiEsotericCrystalsByPlanetPlanetResponse = z.object({ok: z.boolean().optional(), data: z.object({planet: z.string().nullable().optional(), items: z.array(z.object({name: z.string().nullable().optional(), color: z.string().nullable().optional(), chakra: z.array(z.string().nullable()).nullable().optional(), zodiacSigns: z.array(z.string().nullable()).nullable().optional(), planet: z.array(z.string().nullable()).nullable().optional(), element: z.string().nullable().optional(), hardness: z.number().nullable().optional(), purposes: z.array(z.string().nullable()).nullable().optional(), description: z.string().nullable().optional()}).nullable()).nullable().optional(), count: z.number().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 }; /** * Crystals by Planet * * GET /esoteric/crystals/by-planet/{planet} */ export const getApiEsotericCrystalsByPlanetPlanet = async (args: GetApiEsotericCrystalsByPlanetPlanetArgs, options?: GetApiEsotericCrystalsByPlanetPlanetOptions) => { const {baseUrl = "https://api.astroway.info/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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 (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/esoteric/crystals/by-planet/${encodeURIComponent(String(args.planet))}${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(getApiEsotericCrystalsByPlanetPlanetResponse, await res.json()) };