import {z} from 'zod' export type GetApiGatewayV1EquityEtfHoldingsArgs = {symbol: string, as_of?: string | undefined, asset_class?: ('equity' | 'bond') | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiGatewayV1EquityEtfHoldingsOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const equityETFHoldingsPeriod = z.object({as_of: z.string(), quarter: z.number().int().gte(1).lte(4), year: z.number().int().gte(1900)}); export const equityETFFund = z.object({as_of: z.string(), asset_class: z.string(), cik: z.string(), filing_date: z.string(), name: z.string(), offset: z.number().int(), returned: z.number().int(), source: z.string(), total_holdings: z.number().int(), total_net_assets: z.number().nullable()}); export const equityETFHolding = z.object({asset_class: z.string().nullable(), cusip: z.string().nullable(), isin: z.string().nullable(), logo_url: z.string().nullable(), market_value: z.number().nullable(), name: z.string().nullable(), shares: z.number().nullable(), ticker: z.string().nullable(), weight: z.number().nullable()}); export const equityETFHoldingsSnapshot = z.object({fund: equityETFFund, holdings: z.array(equityETFHolding).nullable()}); export const equityETFHoldingsResponse = z.object({available_periods: z.array(equityETFHoldingsPeriod).nullable(), snapshots: z.array(equityETFHoldingsSnapshot).nullable(), symbol: z.string()}); export const objectResponseMeta = z.object({cached: z.boolean(), credits_used: z.number().int(), empty_reason: z.string().optional()}); export const dataObjectResponseEquityETFHoldingsResponse = z.object({$schema: z.string().optional(), data: equityETFHoldingsResponse, meta: objectResponseMeta}); 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 }; /** * ETF Holdings * * GET /gateway/v1/equity/etf/holdings */ export const getApiGatewayV1EquityEtfHoldings = async (args: GetApiGatewayV1EquityEtfHoldingsArgs, options?: GetApiGatewayV1EquityEtfHoldingsOptions) => { const {baseUrl = "https://api.asksurf.ai", fetch: fetchFn = fetch, AccessToken} = 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.as_of != null) query.append("as_of", String(args.as_of)) if (args.asset_class != null) query.append("asset_class", String(args.asset_class)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (AccessToken !== undefined) headers.set('authorization', `Bearer ${AccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/gateway/v1/equity/etf/holdings${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(dataObjectResponseEquityETFHoldingsResponse, await res.json()) };