import {z} from 'zod' export type GetApiAdminFeedExplainArgs = {userId: string, postId: number, mode?: string | undefined}; export type GetApiAdminFeedExplainOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fireAntApiModelsFeedFeedExplanation = z.object({postId: z.number().int().optional(), mode: z.string().optional(), bucket: z.number().int().optional(), inPool: z.boolean().optional(), isCandidate: z.boolean().optional(), filteredBy: z.string().optional(), rank: z.number().int().optional(), score: z.number().optional(), r: z.number().optional(), h: z.number().optional(), q: z.number().optional(), f: z.number().optional(), m: z.number().optional(), n: z.number().optional(), a: z.number().optional(), y: z.number().optional(), g: z.number().optional(), ageHours: z.number().optional(), sources: z.string().optional(), reasonCode: z.string().optional(), reasonParam: z.string().optional(), candidateCount: z.number().int().optional(), snapshotSize: z.number().int().optional(), elapsedMs: z.number().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 }; /** * Giải thích điểm của một bài với một người dùng (R, H, Q, F, M, N, nguồn, lý do, vị trí, hoặc lý do bị lọc) * * GET /admin/feed/explain */ export const getApiAdminFeedExplain = async (args: GetApiAdminFeedExplainArgs, options?: GetApiAdminFeedExplainOptions) => { 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.userId != null) query.append("userId", String(args.userId)) if (args.postId != null) query.append("postId", String(args.postId)) if (args.mode != null) query.append("mode", String(args.mode)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/admin/feed/explain${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(fireAntApiModelsFeedFeedExplanation, await res.json()) };