import {z} from 'zod' export type InputFieldModel = {name?: string | undefined, label?: string | undefined, description?: string | undefined, valueType?: ('string' | 'integer' | 'boolean') | undefined, inputType?: ('text' | 'password') | undefined, isRequired?: boolean | undefined, placeholder?: string | undefined, maxLength?: number | undefined, sortOrder?: number | undefined, encrypt?: boolean | undefined}; export type PlatformSchema = {platform?: ('EPIC' | 'STEAM' | 'GOG') | undefined, description?: string | undefined, fields?: Array | undefined}; export type UpdateApi1PlatformSchemasPlatformArgs = {body: PlatformSchema}; export type UpdateApi1PlatformSchemasPlatformOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const inputFieldModel = z.object({name: z.string().optional(), label: z.string().optional(), description: z.string().optional(), valueType: z.enum(["string", "integer", "boolean"]).optional(), inputType: z.enum(["text", "password"]).optional(), isRequired: z.boolean().optional(), placeholder: z.string().optional(), maxLength: z.number().int().gte(1).optional(), sortOrder: z.number().int().optional(), encrypt: z.boolean().optional()}); export const platformSchema = z.object({platform: z.enum(["EPIC", "STEAM", "GOG"]).optional(), description: z.string().optional(), fields: z.array(inputFieldModel).optional()}); export const platformSchemaResponse = z.object({data: platformSchema.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 }; /** * PUT /1/platform-schemas/{platform} */ export const updateApi1PlatformSchemasPlatform = async (args: UpdateApi1PlatformSchemasPlatformArgs, options: UpdateApi1PlatformSchemasPlatformOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/1/platform-schemas/${encodeURIComponent(String(args.platform))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(platformSchemaResponse, await res.json()) };