import {z} from 'zod' export type GetApiV1FndMarketsStatusArgs = {region?: ('north_america' | 'europe' | 'asia_pacific' | 'latam' | 'global') | undefined}; export type GetApiV1FndMarketsStatusOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const marketStats = z.object({market_cap_usd: z.number().int().optional(), listed_companies: z.number().int().optional(), currency: z.string().optional(), as_of: z.string().optional()}); export const marketStatus = z.object({market: z.string().optional(), type: z.string().optional(), is_open: z.boolean().optional(), state: z.string().optional(), session: z.string().optional(), next_open: z.string().optional(), next_close: z.string().optional(), timezone: z.string().optional(), stats: marketStats.optional()}); export const marketsStatusAllResponse = z.object({data: z.array(marketStatus).optional(), meta: z.record(z.string(), z.unknown()).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 }; /** * All market statuses * * GET /v1/fnd/markets/status * * Also accepts auth: ApiKeyQuery */ export const getApiV1FndMarketsStatus = async (args: GetApiV1FndMarketsStatusArgs, options?: GetApiV1FndMarketsStatusOptions) => { const {baseUrl = "https://api.sifting.io", fetch: fetchFn = fetch, ApiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.region != null) query.append("region", String(args.region)) if (ApiKeyHeader !== undefined) headers.set("X-API-Key", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/fnd/markets/status${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(marketsStatusAllResponse, await res.json()) };