import {z} from 'zod' export type GetApiTopicsTopicIdImportsArgs = {topicId: number, status?: ('Pending' | 'Processing' | 'Completed' | 'Failed' | 'Aborted') | undefined}; export type GetApiTopicsTopicIdImportsOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const importStatus = z.object({importId: z.number().int().optional(), name: z.string().nullable().optional(), totalMentions: z.number().int().optional(), savedCount: z.number().int().optional(), failedCount: z.number().int().optional(), status: z.enum(["Pending", "Processing", "Completed", "Failed", "Aborted"]).optional(), createdAt: z.string().optional(), startedAt: z.string().nullable().optional(), completedAt: z.string().nullable().optional(), lastError: z.string().nullable().optional(), source: z.string().optional(), userEmail: z.string().nullable().optional(), userName: z.string().nullable().optional(), canShowMentions: z.boolean().optional()}); export const getApiTopicsTopicIdImportsResponse = z.object({imports: z.array(importStatus).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 }; /** * List imports * * GET /topics/{topicId}/imports * * Also accepts auth: ApiKeyQuery */ export const getApiTopicsTopicIdImports = async (args: GetApiTopicsTopicIdImportsArgs, options?: GetApiTopicsTopicIdImportsOptions) => { const {baseUrl = "https://api.youscan.io/api/external", fetch: fetchFn = fetch, ApiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (ApiKeyHeader !== undefined) headers.set("X-API-KEY", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/topics/${encodeURIComponent(String(args.topicId))}/imports${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(getApiTopicsTopicIdImportsResponse, await res.json()) };