import {z} from 'zod' export type CommonHTTPOption = {bearerToken?: string | undefined, caFile?: string | undefined, certFile?: string | undefined, insecureSkipTLS?: boolean | undefined, keyFile?: string | undefined, password?: string | undefined, plainHTTP?: boolean | undefined, username?: string | undefined}; export type V1InstallPluginRequest = {disable?: boolean | undefined, options?: CommonHTTPOption | undefined, url: string}; export type CreateApiApiV1ManagePluginsPluginIdInstallArgs = {pluginId: string, body: V1InstallPluginRequest}; export type CreateApiApiV1ManagePluginsPluginIdInstallOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const typesKubernetesSecret = z.object({name: z.string(), namespace: z.string()}); export const typesKubernetesService = z.object({name: z.string(), namespace: z.string(), port: z.number().int()}); export const typesPermission = z.object({action: z.string(), resource: z.string()}); export const typesWorkspace = z.object({name: z.string()}); export const typesIncludes = z.object({catalog: z.string().optional(), icon: z.string(), label: z.string(), name: z.string(), permission: typesPermission.optional(), relatedRoute: z.array(z.string()), to: z.string(), type: z.string(), workspace: typesWorkspace}); export const typesInfoLink = z.object({name: z.string(), url: z.string()}); export const typesBuildInfo = z.object({branch: z.string().optional(), hash: z.string().optional(), repo: z.string().optional(), time: z.number().int().optional()}); export const typesLogos = z.object({large: z.string(), small: z.string()}); export const typesScreenshots = z.object({name: z.string(), path: z.string()}); export const typesInfo = z.object({author: typesInfoLink, build: typesBuildInfo, description: z.string(), links: z.array(typesInfoLink), logos: typesLogos, screenshots: z.array(typesScreenshots), updated: z.string(), version: z.string()}); export const v1PolicyRule = z.object({apiGroups: z.array(z.string()).optional(), nonResourceURLs: z.array(z.string()).optional(), resourceNames: z.array(z.string()).optional(), resources: z.array(z.string()).optional(), verbs: z.array(z.string())}); export const typesRequirement = z.object({velauxVersion: z.string()}); export const typesHeader = z.object({name: z.string(), value: z.string()}); export const typesRoute = z.object({headers: z.array(typesHeader).optional(), method: z.string(), path: z.string(), permission: typesPermission.optional(), resourceMap: z.record(z.string(), z.string()).optional()}); export const v1ManagedPluginDTO = z.object({authSecret: typesKubernetesSecret.optional(), authType: z.string().optional(), backend: z.boolean(), backendService: typesKubernetesService, backendType: z.string(), baseURL: z.string(), category: z.string(), class: z.string(), defaultNavURL: z.string(), enabled: z.boolean(), id: z.string(), includes: z.array(typesIncludes), info: typesInfo, jsonSetting: z.object({}), kubePermissions: z.array(v1PolicyRule).optional(), module: z.string(), name: z.string(), proxy: z.boolean(), requirement: typesRequirement.optional(), routes: z.array(typesRoute).optional(), secureJsonFields: z.record(z.string(), z.boolean()), subType: z.string(), type: 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 }; /** * Install one specific plugin * * POST /api/v1/manage/plugins/{pluginId}/install */ export const createApiApiV1ManagePluginsPluginIdInstall = async (args: CreateApiApiV1ManagePluginsPluginIdInstallArgs, options: CreateApiApiV1ManagePluginsPluginIdInstallOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/manage/plugins/${encodeURIComponent(String(args.pluginId))}/install${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(v1ManagedPluginDTO, await res.json()) };