import {z} from 'zod' export type GetApiIrSymbolPageStatsArgs = {symbol: string, fromDate?: string | undefined, toDate?: string | undefined}; export type GetApiIrSymbolPageStatsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fireAntApiModelsIRIRPageStatsDay = z.object({date: z.string().optional(), platform: z.number().int().optional(), views: z.number().int().optional(), visitors: z.number().int().optional()}); export const fireAntApiModelsIRIRPageTabViews = z.object({tab: z.string().optional(), views: z.number().int().optional()}); export const fireAntApiModelsIRIRPageTopDocument = z.object({documentId: z.number().int().optional(), title: z.string().optional(), downloads: z.number().int().optional()}); export const fireAntApiModelsIRIRPageStats = z.object({days: z.array(fireAntApiModelsIRIRPageStatsDay).optional(), tabs: z.array(fireAntApiModelsIRIRPageTabViews).optional(), topDocuments: z.array(fireAntApiModelsIRIRPageTopDocument).optional(), followers: z.number().int().optional(), newFollowers: z.number().int().optional(), upcomingRegistrations: z.number().int().optional(), questionsAsked: z.number().int().optional(), questionsAnswered: z.number().int().optional(), pendingQuestions: z.number().int().optional(), oldestPendingDays: z.number().int().optional(), downloads: z.number().int().optional(), newMessages: z.number().int().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 }; /** * Lượt xem theo ngày / nền tảng / tab, người theo dõi, hỏi đáp, lượt tải tài liệu * * GET /ir/{symbol}/page/stats */ export const getApiIrSymbolPageStats = async (args: GetApiIrSymbolPageStatsArgs, options?: GetApiIrSymbolPageStatsOptions) => { 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.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ir/${encodeURIComponent(String(args.symbol))}/page/stats${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(fireAntApiModelsIRIRPageStats, await res.json()) };