import {z} from 'zod' export type GetApiEtfsListArgs = {symbol?: string | undefined, figi?: string | undefined, isin?: string | undefined, cusip?: string | undefined, cik?: string | undefined, country?: string | undefined, fund_family?: string | undefined, fund_type?: string | undefined, format?: ('JSON' | 'CSV') | undefined, delimiter?: string | undefined, dp?: number | undefined, page?: number | undefined, outputsize?: number | undefined}; export type GetApiEtfsListOptions = {baseUrl?: string | undefined, authorizationHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const eTFsListResponseItem = z.object({symbol: z.string(), name: z.string(), country: z.string(), mic_code: z.string(), fund_family: z.string(), fund_type: z.string()}); export const getApiEtfsListResponse = z.object({result: z.object({count: z.number().int(), list: z.array(eTFsListResponseItem)}), status: z.string()}); 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 }; /** * ETFs directory * * GET /etfs/list * * Also accepts auth: queryParameter */ export const getApiEtfsList = async (args: GetApiEtfsListArgs, options?: GetApiEtfsListOptions) => { const {baseUrl = "https://api.twelvedata.com", fetch: fetchFn = fetch, authorizationHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.figi != null) query.append("figi", String(args.figi)) if (args.isin != null) query.append("isin", String(args.isin)) if (args.cusip != null) query.append("cusip", String(args.cusip)) if (args.cik != null) query.append("cik", String(args.cik)) if (args.country != null) query.append("country", String(args.country)) if (args.fund_family != null) query.append("fund_family", String(args.fund_family)) if (args.fund_type != null) query.append("fund_type", String(args.fund_type)) if (args.format != null) query.append("format", String(args.format)) if (args.delimiter != null) query.append("delimiter", String(args.delimiter)) if (args.dp != null) query.append("dp", String(args.dp)) if (args.page != null) query.append("page", String(args.page)) if (args.outputsize != null) query.append("outputsize", String(args.outputsize)) if (authorizationHeader !== undefined) headers.set("Authorization", authorizationHeader) const url = `${baseUrl.replace(/\/+$/, '')}/etfs/list${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(getApiEtfsListResponse, await res.json()) };