import {z} from 'zod' export type GetApiV2ProfilesIdArgs = {id: string}; export type GetApiV2ProfilesIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2ProfilesIdResponse = z.union([z.object({version: z.literal("1"), payload: z.object({mods: z.array(z.object({remoteId: z.string(), selectedDownload: z.object({remoteId: z.string(), file: z.string(), url: z.string(), size: z.number()}).optional(), selectedDownloads: z.array(z.object({remoteId: z.string(), file: z.string(), url: z.string(), size: z.number()})).optional(), fileTree: z.object({files: z.array(z.object({name: z.string(), path: z.string(), size: z.number(), is_selected: z.boolean(), archive_name: z.string()})), total_files: z.number(), has_multiple_files: z.boolean()}).optional()}))})}), z.object({version: z.literal("2"), payload: z.object({mods: z.array(z.object({remoteId: z.string(), selectedDownload: z.object({remoteId: z.string(), file: z.string(), url: z.string(), size: z.number()}).optional(), selectedDownloads: z.array(z.object({remoteId: z.string(), file: z.string(), url: z.string(), size: z.number()})).optional(), fileTree: z.object({files: z.array(z.object({name: z.string(), path: z.string(), size: z.number(), is_selected: z.boolean(), archive_name: z.string()})), total_files: z.number(), has_multiple_files: z.boolean()}).optional()})), loadOrder: z.array(z.string())})}), z.object({version: z.literal("3"), payload: z.object({mods: z.array(z.object({remoteId: z.string(), selectedDownload: z.object({remoteId: z.string(), file: z.string(), url: z.string(), size: z.number()}).optional(), selectedDownloads: z.array(z.object({remoteId: z.string(), file: z.string(), url: z.string(), size: z.number()})).optional(), fileTree: z.object({files: z.array(z.object({name: z.string(), path: z.string(), size: z.number(), is_selected: z.boolean(), archive_name: z.string()})), total_files: z.number(), has_multiple_files: z.boolean()}).optional(), submissionType: z.enum(["mod", "sound"])})), loadOrder: z.array(z.string())})})]); 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 }; /** * GET /v2/profiles/{id} */ export const getApiV2ProfilesId = async (args: GetApiV2ProfilesIdArgs, options?: GetApiV2ProfilesIdOptions) => { const {baseUrl = "https://api.deadlockmods.app/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/profiles/${encodeURIComponent(String(args.id))}${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(getApiV2ProfilesIdResponse, await res.json()) };