import {z} from 'zod' export type GetApiV2PolicyManifestOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2PolicyManifestResponse = z.object({version: z.unknown(), revision: z.number().int().gte(0).lte(9007199254740991), generatedAt: z.string(), rules: z.array(z.object({provider: z.literal("gamebanana"), submissionType: z.enum(["mod", "sound"]), submissionId: z.string(), kind: z.enum(["hidden", "blacklisted", "takedown", "metadata_correction", "emergency_disable"]), reason: z.string().nullable(), correction: z.object({name: z.string().optional(), description: z.string().optional(), category: z.string().optional(), hero: z.string().optional(), isMap: z.boolean().optional(), isAudio: z.boolean().optional(), isNSFW: z.boolean().optional(), isObsolete: z.boolean().optional(), tags: z.array(z.string()).optional(), metadata: z.object({mapName: z.string().optional(), donationLinks: z.array(z.object({url: z.string(), platform: z.string()})).optional()}).optional(), downloads: z.array(z.object({url: z.string(), file: z.string()})).optional()}).nullable(), updatedAt: 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/policy-manifest */ export const getApiV2PolicyManifest = async (options?: GetApiV2PolicyManifestOptions) => { 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/policy-manifest${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(getApiV2PolicyManifestResponse, await res.json()) };