import {z} from 'zod' export type GetApiApiV1ExchangesArgs = {limit?: number | undefined, cursor?: string | undefined, country?: string | undefined, fields?: string | undefined, 'If-None-Match'?: string | undefined, 'If-Modified-Since'?: string | undefined}; export type GetApiApiV1ExchangesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const exchangeSummary = z.object({id: z.string(), name: z.string(), mic_code: z.string(), acronym: z.string().nullable().optional(), country: z.object({id: z.string().optional(), name: z.string().optional(), iso2_code: z.string().optional()}).optional()}); export const getApiApiV1ExchangesResponse = z.object({exchanges: z.array(exchangeSummary).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 Exchanges * * GET /api/v1/exchanges */ export const getApiApiV1Exchanges = async (args: GetApiApiV1ExchangesArgs, options?: GetApiApiV1ExchangesOptions) => { 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.country != null) query.append("country", String(args.country)) 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/exchanges${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(getApiApiV1ExchangesResponse, await res.json()) };