import {z} from 'zod' export type GetApiApiV1ExchangesIdentifierArgs = {'If-None-Match'?: string | undefined, 'If-Modified-Since'?: string | undefined}; export type GetApiApiV1ExchangesIdentifierOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const exchangeDetail = 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(), operating_mic_code: z.string().nullable().optional(), currency: z.string().nullable().optional(), ticker_suffix: z.string().nullable().optional(), timezone: z.string().nullable().optional(), city: z.string().nullable().optional(), indices: z.array(z.object({id: z.string().optional(), name: z.string().optional(), symbol: z.string().optional()})).optional()}); export const getApiApiV1ExchangesIdentifierResponse = z.object({exchange: exchangeDetail}); 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 Exchange Details * * GET /api/v1/exchanges/{identifier} */ export const getApiApiV1ExchangesIdentifier = async (args: GetApiApiV1ExchangesIdentifierArgs, options?: GetApiApiV1ExchangesIdentifierOptions) => { 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['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/${encodeURIComponent(String(args.identifier))}${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(getApiApiV1ExchangesIdentifierResponse, await res.json()) };