import {z} from 'zod' export type GetApiSymbolsSymbolHistoricalQuotesArgs = {symbol: string, startDate: string, endDate: string, offset?: number | undefined, limit?: number | undefined}; export type GetApiSymbolsSymbolHistoricalQuotesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fireAntApiModelsQuoteHistoricalQuote = z.object({date: z.string().optional(), symbol: z.string().optional(), priceHigh: z.number().optional(), priceLow: z.number().optional(), priceOpen: z.number().optional(), priceAverage: z.number().optional(), priceClose: z.number().optional(), priceBasic: z.number().optional(), totalVolume: z.number().optional(), dealVolume: z.number().optional(), putthroughVolume: z.number().optional(), totalValue: z.number().optional(), putthroughValue: z.number().optional(), buyForeignQuantity: z.number().optional(), buyForeignValue: z.number().optional(), sellForeignQuantity: z.number().optional(), sellForeignValue: z.number().optional(), buyCount: z.number().optional(), buyQuantity: z.number().optional(), sellCount: z.number().optional(), sellQuantity: z.number().optional(), adjRatio: z.number().optional(), currentForeignRoom: z.number().optional(), propTradingNetDealValue: z.number().optional(), propTradingNetPTValue: z.number().optional(), propTradingNetValue: z.number().optional(), unit: z.number().optional()}); export const getApiSymbolsSymbolHistoricalQuotesResponse = z.array(fireAntApiModelsQuoteHistoricalQuote); 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 }; /** * Lấy giá quá khứ của Mã chứng khoán * * GET /symbols/{symbol}/historical-quotes */ export const getApiSymbolsSymbolHistoricalQuotes = async (args: GetApiSymbolsSymbolHistoricalQuotesArgs, options?: GetApiSymbolsSymbolHistoricalQuotesOptions) => { const {baseUrl = "https://api.fireant.vn", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/symbols/${encodeURIComponent(String(args.symbol))}/historical-quotes${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(getApiSymbolsSymbolHistoricalQuotesResponse, await res.json()) };