import {z} from 'zod' export type CreateApiDestiny2SearchDestinyPlayerByBungieNameMembershipTypeArgs = {membershipType: number}; export type CreateApiDestiny2SearchDestinyPlayerByBungieNameMembershipTypeOptions = {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 createApiDestiny2SearchDestinyPlayerByBungieNameMembershipTypeResponse = z.object({Response: z.array(userUserInfoCard).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 }; /** * Returns a list of Destiny memberships given a global Bungie Display Name. This method will hide overridden memberships due to cross save. * * POST /Destiny2/SearchDestinyPlayerByBungieName/{membershipType}/ */ export const createApiDestiny2SearchDestinyPlayerByBungieNameMembershipType = async (args: CreateApiDestiny2SearchDestinyPlayerByBungieNameMembershipTypeArgs, options?: CreateApiDestiny2SearchDestinyPlayerByBungieNameMembershipTypeOptions) => { const {baseUrl = "https://www.bungie.net/Platform", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/Destiny2/SearchDestinyPlayerByBungieName/${encodeURIComponent(String(args.membershipType))}/${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(createApiDestiny2SearchDestinyPlayerByBungieNameMembershipTypeResponse, await res.json()) };