import {z} from 'zod' export type CreateApiV2ProfilesArgs = {body: {hardwareId: string, name: string, version: string, profile: {version: '1', payload: {mods: Array<{remoteId: string, selectedDownload?: {remoteId: string, file: string, url: string, size: number} | undefined, selectedDownloads?: Array<{remoteId: string, file: string, url: string, size: number}> | undefined, fileTree?: {files: Array<{name: string, path: string, size: number, is_selected: boolean, archive_name: string}>, total_files: number, has_multiple_files: boolean} | undefined}>}} | {version: '2', payload: {mods: Array<{remoteId: string, selectedDownload?: {remoteId: string, file: string, url: string, size: number} | undefined, selectedDownloads?: Array<{remoteId: string, file: string, url: string, size: number}> | undefined, fileTree?: {files: Array<{name: string, path: string, size: number, is_selected: boolean, archive_name: string}>, total_files: number, has_multiple_files: boolean} | undefined}>, loadOrder: Array}} | {version: '3', payload: {mods: Array<{remoteId: string, selectedDownload?: {remoteId: string, file: string, url: string, size: number} | undefined, selectedDownloads?: Array<{remoteId: string, file: string, url: string, size: number}> | undefined, fileTree?: {files: Array<{name: string, path: string, size: number, is_selected: boolean, archive_name: string}>, total_files: number, has_multiple_files: boolean} | undefined, submissionType: 'mod' | 'sound'}>, loadOrder: Array}}}}; export type CreateApiV2ProfilesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2ProfilesResponse = z.object({id: z.string().nullable(), status: z.enum(["success", "error"]), error: 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 }; /** * POST /v2/profiles */ export const createApiV2Profiles = async (args: CreateApiV2ProfilesArgs, options?: CreateApiV2ProfilesOptions) => { const {baseUrl = "https://api.deadlockmods.app/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/profiles${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(createApiV2ProfilesResponse, await res.json()) };