import {z} from 'zod' export type CreateApiV2RosterViewSharesViewIdImportArgs = {viewId: string, server_id: string}; export type CreateApiV2RosterViewSharesViewIdImportOptions = {baseUrl?: string | undefined, UserToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2RosterViewSharesViewIdImportResponse = z.object({id: z.string(), shareId: z.string(), serverId: z.string(), name: z.string(), description: z.string().max(300).optional(), sourceCode: z.string(), sourceVersion: z.literal(1), createdBy: z.string(), spec: z.object({schemaVersion: z.literal(1), columns: z.array(z.object({id: z.string(), label: z.string(), metricId: z.string(), description: z.string().optional(), parameters: z.object({}).optional(), format: z.string().optional()})), sort: z.array(z.object({columnId: z.string(), direction: z.enum(["asc", "desc"])})).optional(), filters: z.array(z.object({columnId: z.string(), operator: z.enum(["eq", "neq", "gt", "gte", "lt", "lte", "in", "contains"]), value: z.unknown()})).optional(), highlights: z.array(z.object({id: z.string(), target: z.enum(["row", "column", "cell"]), columnId: z.string().optional(), when: z.object({columnId: z.string().optional(), operator: z.enum(["eq", "neq", "gt", "gte", "lt", "lte", "in", "contains"]), value: z.unknown()}).optional(), tone: z.enum(["red", "amber", "green", "blue", "purple", "gray"]), gradient: z.object({columnId: z.string(), min: z.union([z.number(), z.literal("NaN"), z.literal("Infinity"), z.literal("-Infinity")]), max: z.union([z.number(), z.literal("NaN"), z.literal("Infinity"), z.literal("-Infinity")]), from: z.string(), to: z.string()}).optional()})).optional(), limit: z.union([z.number(), z.literal("NaN"), z.literal("Infinity"), z.literal("-Infinity")]).optional()}).optional(), createdAt: z.string(), updatedAt: z.string()}); 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 }; /** * Save a shared view template to an authorized destination server * * POST /v2/roster/view-shares/{viewId}/import * * Also accepts auth: BotToken */ export const createApiV2RosterViewSharesViewIdImport = async (args: CreateApiV2RosterViewSharesViewIdImportArgs, options?: CreateApiV2RosterViewSharesViewIdImportOptions) => { const {baseUrl = "https://api.clashk.ing", fetch: fetchFn = fetch, UserToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.server_id != null) query.append("server_id", String(args.server_id)) if (UserToken !== undefined) headers.set('authorization', `Bearer ${UserToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/roster/view-shares/${encodeURIComponent(String(args.viewId))}/import${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2RosterViewSharesViewIdImportResponse, await res.json()) };