import {z} from 'zod' export type HiddenProfileBadgesType = Array; export type ProfileBadgePreviewRequest = {hidden_profile_badges?: HiddenProfileBadgesType | undefined, premium_badge_masked?: boolean | undefined, premium_badge_timestamp_hidden?: boolean | undefined, operator_pass_badge_timestamp_hidden?: boolean | undefined, operator_pass_badge_count_hidden?: boolean | undefined, premium_badge_hidden?: boolean | undefined, premium_badge_sequence_hidden?: boolean | undefined, operator_pass_badge_hidden?: boolean | undefined}; export type CreateApiUsersMeProfileBadgesPreviewArgs = {body: ProfileBadgePreviewRequest}; export type CreateApiUsersMeProfileBadgesPreviewOptions = {baseUrl?: string | undefined, sessionToken?: string | undefined, fetch?: typeof fetch | undefined}; export const hexString16Type = z.string(); export const profileBadgeResponse = z.object({key: z.string(), kind: z.string(), label: z.string(), asset: hexString16Type.nullable(), asset_url: z.string().nullable(), small_asset: hexString16Type.optional(), aspect_ratio: z.number().gt(0).nullable(), caption: z.string().nullable(), href: z.string().nullable(), count: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable().optional(), accessible_label: z.string().nullable().optional(), details: z.object({title: z.string(), lines: z.array(z.string())}).nullable().optional(), quote: z.string().nullable().optional()}); export const profileBadgeListResponse = z.array(profileBadgeResponse); export const ownProfileBadgeResponse = z.object({key: z.string(), kind: z.string(), label: z.string(), asset: hexString16Type.nullable(), asset_url: z.string().nullable(), small_asset: hexString16Type.optional(), aspect_ratio: z.number().gt(0).nullable(), caption: z.string().nullable(), href: z.string().nullable(), count: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable().optional(), accessible_label: z.string().nullable().optional(), details: z.object({title: z.string(), lines: z.array(z.string())}).nullable().optional(), quote: z.string().nullable().optional(), hidden: z.boolean()}); export const profileBadgePreviewResponse = z.object({profile_badges: profileBadgeListResponse, own_badges: z.array(ownProfileBadgeResponse)}); 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 }; /** * Preview profile badges * * POST /users/@me/profile-badges/preview */ export const createApiUsersMeProfileBadgesPreview = async (args: CreateApiUsersMeProfileBadgesPreviewArgs, options?: CreateApiUsersMeProfileBadgesPreviewOptions) => { const {baseUrl = "https://web.fluxer.app/api/v1", fetch: fetchFn = fetch, sessionToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sessionToken !== undefined) headers.set("Authorization", sessionToken) const url = `${baseUrl.replace(/\/+$/, '')}/users/@me/profile-badges/preview${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(profileBadgePreviewResponse, await res.json()) };