import {z} from 'zod' export type CreateApiConfigUpdateArgs = {Prefer?: 'respond-async' | undefined, body: {_config?: {[key: string]: unknown} | undefined, _filter?: {[key: string]: unknown} | undefined, _group?: string | undefined, name: string, parameters: {[key: string]: unknown}, opt?: ({obscure?: boolean | undefined, noObscure?: boolean | undefined, noOutput?: boolean | undefined, nonInteractive?: boolean | undefined, continue?: boolean | undefined, all?: boolean | undefined, state?: string | undefined, result?: string | undefined}) | undefined}}; export type CreateApiConfigUpdateOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const configProviderOptionAny = z.union([z.string(), z.number(), z.boolean(), z.array(z.unknown()), z.object({Value: z.boolean(), Valid: z.boolean()})]).nullable(); export const configProviderOptionType = z.enum(["Bits", "bool", "CommaSepList", "Duration", "Encoding", "int", "mtime|atime|btime|ctime", "SizeSuffix", "SpaceSepList", "string", "stringArray", "Time", "Tristate", "float64"]); export const configProviderOptionExample = z.object({Help: z.string(), Value: z.string(), Provider: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const configProviderOption = z.object({Advanced: z.boolean(), Default: configProviderOptionAny, DefaultStr: z.string(), Exclusive: z.boolean(), FieldName: z.string(), Help: z.string(), Hide: z.number().int(), IsPassword: z.boolean(), Name: z.string(), NoPrefix: z.boolean(), Required: z.boolean(), Sensitive: z.boolean(), Type: configProviderOptionType, Value: configProviderOptionAny, ValueStr: z.string(), Examples: z.array(configProviderOptionExample).optional(), ShortOpt: z.string().optional(), Provider: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const createApiConfigUpdateResponse = z.object({State: z.string().optional(), Option: configProviderOption.nullable().optional(), Error: z.string().optional(), Result: 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 remote configuration * * POST /config/update */ export const createApiConfigUpdate = async (args: CreateApiConfigUpdateArgs, options?: CreateApiConfigUpdateOptions) => { const {baseUrl = "http://localhost:5572", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Prefer != null) headers.set("Prefer", String(args.Prefer)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/config/update${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(createApiConfigUpdateResponse, await res.json()) };