import {z} from 'zod' export type GetApiV2ServerServerIdRostersRosterIdAccountsArgs = {serverId: string, rosterId: string, discordUserId?: string | undefined}; export type GetApiV2ServerServerIdRostersRosterIdAccountsOptions = {baseUrl?: string | undefined, UserToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2ServerServerIdRostersRosterIdAccountsResponse = z.object({remainingSlots: z.union([z.number(), z.literal("NaN"), z.literal("Infinity"), z.literal("-Infinity")]).nullable(), items: z.array(z.object({tag: z.string(), name: z.string(), townhall: z.union([z.number(), z.literal("NaN"), z.literal("Infinity"), z.literal("-Infinity")]), isVerified: z.boolean(), signedUp: z.boolean()}))}); 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 the actor's linked roster accounts * * GET /v2/server/{serverId}/rosters/{rosterId}/accounts * * Also accepts auth: BotToken */ export const getApiV2ServerServerIdRostersRosterIdAccounts = async (args: GetApiV2ServerServerIdRostersRosterIdAccountsArgs, options?: GetApiV2ServerServerIdRostersRosterIdAccountsOptions) => { 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.discordUserId != null) query.append("discordUserId", String(args.discordUserId)) if (UserToken !== undefined) headers.set('authorization', `Bearer ${UserToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/server/${encodeURIComponent(String(args.serverId))}/rosters/${encodeURIComponent(String(args.rosterId))}/accounts${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(getApiV2ServerServerIdRostersRosterIdAccountsResponse, await res.json()) };