import {z} from 'zod' export type GetApiV4UserNewsArgs = {divisionId: number, offset?: number | undefined, limit?: number | undefined, dateSortDirection?: ('asc' | 'desc') | undefined, from?: string | undefined, to?: string | undefined, language?: ('en' | 'ar' | 'de' | 'ru' | 'zh' | 'ja' | 'nl' | 'fa' | 'fr' | 'el' | 'it' | 'ko' | 'pt' | 'es' | 'tr' | 'pl') | undefined}; export type GetApiV4UserNewsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV4UserNewsResponse = z.object({data: z.array(z.object({id: z.number().int(), title: z.string(), content: z.string(), contentType: z.enum(["html", "plaintext"]), timestamp: z.string(), country: z.string().nullable().optional(), language_code: z.enum(["en", "ar", "de", "ru", "zh", "ja", "nl", "fa", "fr", "el", "it", "ko", "pt", "es", "tr", "pl"]).optional(), summary: z.string().optional(), url_link: z.string().nullable().optional(), tags: z.array(z.string()).optional(), source: z.string().nullable().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 }; /** * Get news articles * * GET /v4/user/news */ export const getApiV4UserNews = async (args: GetApiV4UserNewsArgs, options: GetApiV4UserNewsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.divisionId != null) query.append("divisionId", String(args.divisionId)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.dateSortDirection != null) query.append("dateSortDirection", String(args.dateSortDirection)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.language != null) query.append("language", String(args.language)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/user/news${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(getApiV4UserNewsResponse, await res.json()) };