import {z} from 'zod' export type GetApiIrSymbolEventsArgs = {symbol: string, fromDate?: string | undefined, toDate?: string | undefined, status?: (0 | 1 | 2 | 3) | undefined}; export type GetApiIrSymbolEventsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fireAntApiModelsIRIREventItem = z.object({eventId: z.number().int().optional(), companyId: z.number().int().optional(), type: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7)]).optional(), title: z.string().optional(), description: z.string().optional(), startDate: z.string().optional(), endDate: z.string().optional(), location: z.string().optional(), onlineUrl: z.string().optional(), livestreamPostId: z.number().int().optional(), allowRegistration: z.boolean().optional(), registrations: z.number().int().optional(), publishToSymbolPage: z.boolean().optional(), publishedPostId: z.number().int().optional(), reminderEnabled: z.boolean().optional(), reminderLeadHours: z.number().int().optional(), reminderSentDate: z.string().optional(), status: z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3)]).optional(), createdBy: z.string().optional(), createdDate: z.string().optional(), modifiedBy: z.string().optional(), modifiedDate: z.string().optional()}); export const getApiIrSymbolEventsResponse = z.array(fireAntApiModelsIRIREventItem); 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ự kiện quan hệ nhà đầu tư * * GET /ir/{symbol}/events */ export const getApiIrSymbolEvents = async (args: GetApiIrSymbolEventsArgs, options?: GetApiIrSymbolEventsOptions) => { 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 (args.status != null) query.append("status", String(args.status)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ir/${encodeURIComponent(String(args.symbol))}/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(getApiIrSymbolEventsResponse, await res.json()) };