import {z} from 'zod' export type GetApiIrSymbolAlertEventsArgs = {symbol: string, fromDate?: string | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiIrSymbolAlertEventsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fireAntApiModelsIRIRAlertEventItem = z.object({eventId: z.number().int().optional(), ruleId: z.number().int().optional(), ruleName: z.string().optional(), metric: z.string().optional(), companyId: z.number().int().optional(), symbol: z.string().optional(), companyName: z.string().optional(), triggeredDate: z.string().optional(), metricValue: z.number().optional(), baselineValue: z.number().optional(), deviationSigma: z.number().optional(), payloadJson: z.string().optional(), channels: z.string().optional(), recipients: z.string().optional(), notifiedCount: z.number().int().optional(), acknowledgedBy: z.string().optional(), acknowledgedDate: z.string().optional()}); export const fireAntApiModelsIRIRPagedResultFireAntApiModelsIRIRAlertEventItem = z.object({items: z.array(fireAntApiModelsIRIRAlertEventItem).optional(), totalCount: z.number().int().optional(), offset: z.number().int().optional(), limit: 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ịch sử cảnh báo đã kích hoạt * * GET /ir/{symbol}/alert-events */ export const getApiIrSymbolAlertEvents = async (args: GetApiIrSymbolAlertEventsArgs, options?: GetApiIrSymbolAlertEventsOptions) => { 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.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(/\/+$/, '')}/ir/${encodeURIComponent(String(args.symbol))}/alert-events${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(fireAntApiModelsIRIRPagedResultFireAntApiModelsIRIRAlertEventItem, await res.json()) };