import {z} from 'zod' export type GetApiMarketdataMarketsNameArgs = {name: string}; export type GetApiMarketdataMarketsNameOptions = {baseUrl?: string | undefined, Secured?: string | undefined, fetch?: typeof fetch | undefined}; export const holiday = z.string(); export const openTime = z.object({ticks: z.number().int().optional(), days: z.number().int().optional(), hours: z.number().int().optional(), milliseconds: z.number().int().optional(), minutes: z.number().int().optional(), seconds: z.number().int().optional(), totalDays: z.number().optional(), totalHours: z.number().optional(), totalMilliseconds: z.number().int().optional(), totalMinutes: z.number().int().optional(), totalSeconds: z.number().int().optional()}); export const market = z.object({description: z.unknown(), onlyWorkdays: z.boolean().optional(), holidays: z.array(holiday).optional(), recesses: z.array(z.unknown()).optional(), name: z.string().optional(), currency: z.string().optional(), specialHours: z.array(z.unknown()).optional(), openTime: openTime.optional(), closeTime: openTime.optional(), xmlOpenTime: z.string().optional(), xmlCloseTime: z.string().optional(), timeZoneId: z.string().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 }; /** * Get market by name * * GET /marketdata/markets/{name} */ export const getApiMarketdataMarketsName = async (args: GetApiMarketdataMarketsNameArgs, options?: GetApiMarketdataMarketsNameOptions) => { const {baseUrl = "https://api.tradematic.com", fetch: fetchFn = fetch, Secured} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Secured !== undefined) headers.set("X-API-Key", Secured) const url = `${baseUrl.replace(/\/+$/, '')}/marketdata/markets/${encodeURIComponent(String(args.name))}${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(market, await res.json()) };