import {z} from 'zod' export type GetApiDestiny2StatsLeaderboardsClansGroupIdArgs = {groupId: number, maxtop?: number | undefined, modes?: string | undefined, statid?: string | undefined}; export type GetApiDestiny2StatsLeaderboardsClansGroupIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const userUserInfoCard = z.object({supplementalDisplayName: z.string().optional(), iconPath: z.string().optional(), crossSaveOverride: z.number().int().optional(), applicableMembershipTypes: z.array(z.number().int()).optional(), isPublic: z.boolean().optional(), membershipType: z.number().int().optional(), membershipId: z.number().int().optional(), displayName: z.string().optional(), bungieGlobalDisplayName: z.string().optional(), bungieGlobalDisplayNameCode: z.number().int().optional()}); export const destinyHistoricalStatsDestinyPlayer = z.object({destinyUserInfo: userUserInfoCard.optional(), characterClass: z.string().optional(), classHash: z.number().int().optional(), raceHash: z.number().int().optional(), genderHash: z.number().int().optional(), characterLevel: z.number().int().optional(), lightLevel: z.number().int().optional(), bungieNetUserInfo: userUserInfoCard.optional(), clanName: z.string().optional(), clanTag: z.string().optional(), emblemHash: z.number().int().optional()}); export const destinyHistoricalStatsDestinyHistoricalStatsValuePair = z.object({value: z.number().optional(), displayValue: z.string().optional()}); export const destinyHistoricalStatsDestinyHistoricalStatsValue = z.object({statId: z.string().optional(), basic: destinyHistoricalStatsDestinyHistoricalStatsValuePair.optional(), pga: destinyHistoricalStatsDestinyHistoricalStatsValuePair.optional(), weighted: destinyHistoricalStatsDestinyHistoricalStatsValuePair.optional(), activityId: z.number().int().optional()}); export const destinyHistoricalStatsDestinyLeaderboardEntry = z.object({rank: z.number().int().optional(), player: destinyHistoricalStatsDestinyPlayer.optional(), characterId: z.number().int().optional(), value: destinyHistoricalStatsDestinyHistoricalStatsValue.optional()}); export const destinyHistoricalStatsDestinyLeaderboard = z.object({statId: z.string().optional(), entries: z.array(destinyHistoricalStatsDestinyLeaderboardEntry).optional()}); export const getApiDestiny2StatsLeaderboardsClansGroupIdResponse = z.object({Response: z.record(z.string(), z.record(z.string(), destinyHistoricalStatsDestinyLeaderboard)).optional(), ErrorCode: z.number().int().optional(), ThrottleSeconds: z.number().int().optional(), ErrorStatus: z.string().optional(), Message: z.string().optional(), MessageData: z.record(z.string(), z.string()).optional(), DetailedErrorTrace: z.string().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 }; /** * Gets leaderboards with the signed in user's friends and the supplied destinyMembershipId as the focus. PREVIEW: This endpoint is still in beta, and may experience rough edges. The schema is in final form, but there may be bugs that prevent desirable operation. * * GET /Destiny2/Stats/Leaderboards/Clans/{groupId}/ */ export const getApiDestiny2StatsLeaderboardsClansGroupId = async (args: GetApiDestiny2StatsLeaderboardsClansGroupIdArgs, options?: GetApiDestiny2StatsLeaderboardsClansGroupIdOptions) => { const {baseUrl = "https://www.bungie.net/Platform", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.maxtop != null) query.append("maxtop", String(args.maxtop)) if (args.modes != null) query.append("modes", String(args.modes)) if (args.statid != null) query.append("statid", String(args.statid)) const url = `${baseUrl.replace(/\/+$/, '')}/Destiny2/Stats/Leaderboards/Clans/${encodeURIComponent(String(args.groupId))}/${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(getApiDestiny2StatsLeaderboardsClansGroupIdResponse, await res.json()) };