import {z} from 'zod' export type GetApiTarotRiderWaiteSuitsSuitArgs = {suit: 'wands' | 'cups' | 'swords' | 'pentacles', fields?: string | undefined, precision?: number | undefined}; export type GetApiTarotRiderWaiteSuitsSuitOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiTarotRiderWaiteSuitsSuitResponse = z.object({ok: z.boolean().optional(), data: z.object({suit: z.string().nullable().optional(), items: z.array(z.object({slug: z.string().nullable().optional(), number: z.number().nullable().optional(), name: z.string().nullable().optional(), category: z.string().nullable().optional(), suit: z.string().nullable().optional(), rank: z.string().nullable().optional(), element: z.string().nullable().optional(), upright: z.object({keywords: z.array(z.string().nullable()).nullable().optional(), meaning: z.string().nullable().optional()}).nullable().optional(), reversed: z.object({keywords: z.array(z.string().nullable()).nullable().optional(), meaning: z.string().nullable().optional()}).nullable().optional(), astrology: z.string().nullable().optional(), numerology: z.number().nullable().optional(), yes: 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 }; /** * RWS: Suit * * GET /tarot/rider-waite/suits/{suit} */ export const getApiTarotRiderWaiteSuitsSuit = async (args: GetApiTarotRiderWaiteSuitsSuitArgs, options?: GetApiTarotRiderWaiteSuitsSuitOptions) => { 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(/\/+$/, '')}/tarot/rider-waite/suits/${encodeURIComponent(String(args.suit))}${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(getApiTarotRiderWaiteSuitsSuitResponse, await res.json()) };