import {z} from 'zod' export type ConfigPatch = {data: {type: 'config', attributes: {item?: string | undefined, value?: string | undefined}}}; export type PatchApiApiV2UiConfigsIdArgs = {id: number, body: ConfigPatch}; export type PatchApiApiV2UiConfigsIdOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const configPostPatchResponse = z.object({jsonapi: z.object({version: z.string(), ext: z.array(z.string()).optional()}), links: z.object({self: z.string()}), data: z.object({id: z.number().int(), type: z.literal("config"), attributes: z.union([z.object({configSectionId: z.number().int(), item: z.literal("serverLogLevel"), value: z.union([z.literal("0"), z.literal("10"), z.literal("20"), z.literal("30"), z.literal("40"), z.literal("50")]), valueBoundaries: z.object({min: z.number().int().optional(), max: z.number().int().optional(), maxLength: z.number().int().optional(), binaryValues: z.array(z.string()).optional()}).optional()}), z.object({configSectionId: z.number().int(), item: z.literal("notificationsProxyType"), value: z.union([z.literal("HTTP"), z.literal("HTTPS"), z.literal("SOCKS4"), z.literal("SOCKS5")]), valueBoundaries: z.object({min: z.number().int().optional(), max: z.number().int().optional(), maxLength: z.number().int().optional(), binaryValues: z.array(z.string()).optional()}).optional()}), z.object({configSectionId: z.number().int(), item: z.string(), value: z.string(), valueBoundaries: z.object({min: z.number().int().optional(), max: z.number().int().optional(), maxLength: z.number().int().optional(), binaryValues: z.array(z.string()).optional()}).optional()})]), links: z.object({self: z.string()}), relationships: z.object({configSection: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.object({type: z.literal("configSection"), id: z.number().int()}).nullable().optional()})})}), included: z.array(z.object({id: z.number().int(), type: z.literal("configSection"), attributes: z.object({sectionName: 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 }; /** * Update Configs * * PATCH /api/v2/ui/configs/{id} */ export const patchApiApiV2UiConfigsId = async (args: PatchApiApiV2UiConfigsIdArgs, options: PatchApiApiV2UiConfigsIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/ui/configs/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(configPostPatchResponse, await res.json()) };