import {z} from 'zod' export type GetApiLocaleMeetingsJsonArgs = {locale: 'ar' | 'zh' | 'en' | 'fr' | 'ru' | 'es', q?: string | undefined, ft?: '1' | undefined, category?: string | undefined, date?: string | undefined, from?: string | undefined, to?: string | undefined, sort?: ('date_desc' | 'date_asc' | 'title_asc' | 'title_desc') | undefined, page?: number | undefined, text?: (Array<'transcript' | 'pv' | 'sr'>) | undefined, xlang?: '1' | undefined}; export type GetApiLocaleMeetingsJsonOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const meetingsResponse = z.object({meetings: z.array(z.object({title: z.string().nullable(), date: z.string().nullable(), body: z.string().nullable(), category: z.string().nullable(), slug: z.string(), duration: z.number().nullable(), hasTranscript: z.boolean(), pageUrl: z.string(), jsonUrl: z.string(), textUrl: z.string().nullable(), matches: z.object({count: z.number().int().gte(-9007199254740991).lte(9007199254740991), statements: z.array(z.object({speaker: z.object({affiliation: z.string().nullable(), affiliation_full: z.string().nullable(), group: z.string().nullable(), function: z.string().nullable()}).nullable(), text: z.string(), start: z.number().int().gte(-9007199254740991).lte(9007199254740991), pageUrl: z.string()}))}).optional()})), total: z.number().int().gte(-9007199254740991).lte(9007199254740991), totalIncludingOther: z.number().int().gte(-9007199254740991).lte(9007199254740991), hasMore: z.boolean(), page: z.number().int().gte(-9007199254740991).lte(9007199254740991), pageSize: z.number().int().gte(-9007199254740991).lte(9007199254740991), statementTotal: z.number().int().gte(-9007199254740991).lte(9007199254740991).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 }; /** * Browse or search meetings (JSON) * * GET /{locale}/meetings.json */ export const getApiLocaleMeetingsJson = async (args: GetApiLocaleMeetingsJsonArgs, options: GetApiLocaleMeetingsJsonOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.ft != null) query.append("ft", String(args.ft)) if (args.category != null) query.append("category", String(args.category)) if (args.date != null) query.append("date", String(args.date)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.page != null) query.append("page", String(args.page)) for (const item of args.text ?? []) query.append("text", String(item)) if (args.xlang != null) query.append("xlang", String(args.xlang)) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.locale))}/meetings.json${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(meetingsResponse, await res.json()) };