import {z} from 'zod' export type LookUpRequest = {source_language: string, target_language: string, word: string}; export type CreateApiDictionariesProviderLookupArgs = {provider: , body: LookUpRequest}; export type CreateApiDictionariesProviderLookupOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const textTranslation = z.object({detected_source_language: z.string().optional(), text: z.string(), audio_url: z.string().optional()}); export const wordTag = z.object({name: z.string()}); export const wordDefinition = z.object({type: z.string().optional(), name: z.string().optional(), values: z.array(z.string()).optional()}); export const wordPronunciation = z.object({type: z.string().optional(), phonetic_symbol: z.string().optional(), audio_url: z.string().optional()}); export const wordImage = z.object({url: z.string()}); export const wordPhrase = z.object({text: z.string(), translations: z.array(z.string())}); export const wordTense = z.object({type: z.string().optional(), name: z.string().optional(), values: z.array(z.string()).optional()}); export const wordSentence = z.object({text: z.string(), translations: z.array(z.string())}); export const lookUpResponse = z.object({translations: z.array(textTranslation), word: z.string().optional(), tip: z.string().optional(), tags: z.array(wordTag).optional(), definitions: z.array(wordDefinition).optional(), pronunciations: z.array(wordPronunciation).optional(), images: z.array(wordImage).optional(), phrases: z.array(wordPhrase).optional(), tenses: z.array(wordTense).optional(), sentences: z.array(wordSentence).optional()}); export const lookUpResponseEnvelope = z.object({success: z.literal(true), data: lookUpResponse}); 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 }; /** * Lookup a dictionary entry * * POST /dictionaries/{provider}/lookup */ export const createApiDictionariesProviderLookup = async (args: CreateApiDictionariesProviderLookupArgs, options: CreateApiDictionariesProviderLookupOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/dictionaries/${encodeURIComponent(String(args.provider))}/lookup${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(lookUpResponseEnvelope, await res.json()) };