import {z} from 'zod' export type GetApiApiV1SourcesArgs = {limit?: number | undefined, cursor?: string | undefined, fields?: string | undefined, 'If-None-Match'?: string | undefined, 'If-Modified-Since'?: string | undefined}; export type GetApiApiV1SourcesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sourceSummary = z.object({id: z.string(), name: z.string(), domain: z.string(), country: z.string().nullable().optional(), source_type: z.enum(["news_publication", "wire_service", "press_release", "government", "trade_publication", "blog", "stock_exchange", "research"])}); export const getApiApiV1SourcesResponse = z.object({sources: z.array(sourceSummary).optional(), 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 Sources * * GET /api/v1/sources */ export const getApiApiV1Sources = async (args: GetApiApiV1SourcesArgs, options?: GetApiApiV1SourcesOptions) => { 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['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/sources${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(getApiApiV1SourcesResponse, await res.json()) };