import {z} from 'zod' export type EthernetConfig = {host: string, port: number, timeout?: number | undefined}; export type PowerSheaveHardwareUpdate = {plugin_type?: 'power_sheave' | undefined, ethernet?: (EthernetConfig | null) | undefined}; export type PowerSheaveSettingsUpdate = {plugin_type?: 'power_sheave' | undefined, speed_differential_percentage?: (number | null) | undefined, reverse_direction?: (boolean | null) | undefined, control_mode?: ('open' | 'closed' | null) | undefined, ecd_gain?: (number | null) | undefined}; export type DockingSettingsUpdate = {plugin_type?: 'docking_mechanism' | undefined, dock_timeout_seconds?: (number | null) | undefined, undock_timeout_seconds?: (number | null) | undefined, motor_speed?: (number | null) | undefined, docking_reversing_timeout_seconds?: (number | null) | undefined, undock_wind_delay_seconds?: (number | null) | undefined, undock_completion_delay_seconds?: (number | null) | undefined}; export type UpdatePluginSettingsRequest = {hardware?: (PowerSheaveHardwareUpdate | null) | undefined, settings?: (PowerSheaveSettingsUpdate | DockingSettingsUpdate | null) | undefined}; export type PatchApiApiV1PluginsPluginIdSettingsArgs = {plugin_id: string, body: UpdatePluginSettingsRequest}; export type PatchApiApiV1PluginsPluginIdSettingsOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pluginConfigSchema = z.object({name: z.string(), version: z.string(), description: z.string(), requirements: z.record(z.string(), z.unknown()).optional(), hardware: z.record(z.string(), z.unknown()).nullable().optional(), settings: z.record(z.string(), z.unknown()).nullable().optional()}); export const updatePluginSettingsResponse = z.object({success: z.boolean(), message: z.string(), updated_config: pluginConfigSchema.nullable().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 plugin settings * * PATCH /api/v1/plugins/{plugin_id}/settings */ export const patchApiApiV1PluginsPluginIdSettings = async (args: PatchApiApiV1PluginsPluginIdSettingsArgs, options: PatchApiApiV1PluginsPluginIdSettingsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/plugins/${encodeURIComponent(String(args.plugin_id))}/settings${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(updatePluginSettingsResponse, await res.json()) };