import {z} from 'zod' export type GetApiApiV1ArticlesArgs = {limit?: number | undefined, cursor?: string | undefined, fields?: string | undefined, updated_after?: string | undefined, q?: string | undefined, sort?: ('relevance' | 'latest') | undefined, mode?: 'semantic' | undefined, country?: string | undefined, category?: string | undefined, sources?: string | undefined, source_type?: string | undefined, ticker?: string | undefined, company?: string | undefined, exchange?: string | undefined, market?: string | undefined, domicile?: string | undefined, index?: string | undefined, sector?: string | undefined, market_segment?: string | undefined, content_type?: string | undefined, published_after?: string | undefined, published_before?: string | undefined, listed?: string | undefined, watchlist?: string | undefined, primary_only?: boolean | undefined, include_peripheral?: boolean | undefined, ids?: string | undefined, 'If-None-Match'?: string | undefined, 'If-Modified-Since'?: string | undefined}; export type GetApiApiV1ArticlesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const articleSummary = z.object({id: z.string(), title: z.string(), article_url: z.string(), content_type: z.enum(["news", "analysis", "press_release", "market_commentary", "market_news", "trading_halt", "trading_event", "other"]), published_at: z.string(), updated_at: z.string(), category: z.object({id: z.string().optional(), name: z.string().optional()}), source: z.object({id: z.string().optional(), name: z.string().optional(), domain: z.string().optional(), source_type: z.enum(["news_publication", "wire_service", "press_release", "government", "trade_publication", "blog", "stock_exchange", "research"]).optional()}), company_ids: z.array(z.string()), country: z.string().nullable(), event_id: z.string().nullable().optional()}); export const getApiApiV1ArticlesResponse = z.object({articles: z.array(articleSummary), pagination: z.object({count: z.number().int().optional(), next_cursor: z.string().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 }; /** * List Articles * * GET /api/v1/articles */ export const getApiApiV1Articles = async (args: GetApiApiV1ArticlesArgs, options?: GetApiApiV1ArticlesOptions) => { const {baseUrl = "https://nordicfinancialnews.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.fields != null) query.append("fields", String(args.fields)) if (args.updated_after != null) query.append("updated_after", String(args.updated_after)) if (args.q != null) query.append("q", String(args.q)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.country != null) query.append("country", String(args.country)) if (args.category != null) query.append("category", String(args.category)) if (args.sources != null) query.append("sources", String(args.sources)) if (args.source_type != null) query.append("source_type", String(args.source_type)) if (args.ticker != null) query.append("ticker", String(args.ticker)) if (args.company != null) query.append("company", String(args.company)) if (args.exchange != null) query.append("exchange", String(args.exchange)) if (args.market != null) query.append("market", String(args.market)) if (args.domicile != null) query.append("domicile", String(args.domicile)) if (args.index != null) query.append("index", String(args.index)) if (args.sector != null) query.append("sector", String(args.sector)) if (args.market_segment != null) query.append("market_segment", String(args.market_segment)) if (args.content_type != null) query.append("content_type", String(args.content_type)) if (args.published_after != null) query.append("published_after", String(args.published_after)) if (args.published_before != null) query.append("published_before", String(args.published_before)) if (args.listed != null) query.append("listed", String(args.listed)) if (args.watchlist != null) query.append("watchlist", String(args.watchlist)) if (args.primary_only != null) query.append("primary_only", String(args.primary_only)) if (args.include_peripheral != null) query.append("include_peripheral", String(args.include_peripheral)) if (args.ids != null) query.append("ids", String(args.ids)) if (args['If-None-Match'] != null) headers.set("If-None-Match", String(args['If-None-Match'])) if (args['If-Modified-Since'] != null) headers.set("If-Modified-Since", String(args['If-Modified-Since'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/articles${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(getApiApiV1ArticlesResponse, await res.json()) };