import {z} from 'zod' export type ConfirmLinkRequest = {telegram_auth?: ({[key: string]: string} | null) | undefined}; export type CreateApiApiPlatformLinkingTokensTokenConfirmArgs = {token: string, body: ConfirmLinkRequest | null}; export type CreateApiApiPlatformLinkingTokensTokenConfirmOptions = {baseUrl: string, HTTPBearerJWT?: string | undefined, fetch?: typeof fetch | undefined}; export const linkType = z.enum(["SERVER", "USER"]); export const confirmLinkResponse = z.object({success: z.boolean(), link_type: linkType.optional(), platform: z.string(), platform_server_id: z.string(), server_name: z.string().nullable(), return_url: z.string().nullable().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 }; /** * Confirm a SERVER link token (user must be authenticated) * * POST /api/platform-linking/tokens/{token}/confirm */ export const createApiApiPlatformLinkingTokensTokenConfirm = async (args: CreateApiApiPlatformLinkingTokensTokenConfirmArgs, options: CreateApiApiPlatformLinkingTokensTokenConfirmOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearerJWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearerJWT !== undefined) headers.set('authorization', `Bearer ${HTTPBearerJWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/platform-linking/tokens/${encodeURIComponent(String(args.token))}/confirm${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(confirmLinkResponse, await res.json()) };