import {z} from 'zod' export type V1alpha1InputItem = {from: string, parameterKey?: string | undefined}; export type V1alpha1OutputItem = {name: string, valueFrom: string}; export type V1CreateApplicationTraitRequest = {alias?: string | undefined, description?: string | undefined, properties: string, type: string}; export type V1CreateComponentRequest = {alias?: string | undefined, componentType: string, dependsOn?: Array | undefined, description?: string | undefined, icon?: string | undefined, inputs?: Array | undefined, labels?: {[key: string]: string} | undefined, name: string, outputs?: Array | undefined, properties?: string | undefined, traits?: Array | undefined}; export type CreateApiApiV1ApplicationsAppNameComponentsArgs = {appName: string, body: V1CreateComponentRequest}; export type CreateApiApiV1ApplicationsAppNameComponentsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const v1alpha1InputItem = z.object({from: z.string(), parameterKey: z.string().optional()}); export const v1alpha1OutputItem = z.object({name: z.string(), valueFrom: z.string()}); export const modelJSONStruct = z.object({}); export const v1ApplicationTrait = z.object({alias: z.string().optional(), createTime: z.string(), description: z.string().optional(), properties: modelJSONStruct, type: z.string(), updateTime: z.string()}); export const commonWorkloadGVK = z.object({apiVersion: z.string(), kind: z.string()}); export const commonWorkloadTypeDescriptor = z.object({definition: commonWorkloadGVK.optional(), type: z.string().optional()}); export const v1ComponentBase = z.object({alias: z.string(), componentType: z.string(), createTime: z.string(), creator: z.string().optional(), dependsOn: z.array(z.string()), description: z.string(), icon: z.string().optional(), inputs: z.array(v1alpha1InputItem).optional(), labels: z.record(z.string(), z.string()).optional(), main: z.boolean(), name: z.string(), outputs: z.array(v1alpha1OutputItem).optional(), traits: z.array(v1ApplicationTrait), updateTime: z.string(), workloadType: commonWorkloadTypeDescriptor.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 }; /** * create component for application * * POST /api/v1/applications/{appName}/components */ export const createApiApiV1ApplicationsAppNameComponents = async (args: CreateApiApiV1ApplicationsAppNameComponentsArgs, options: CreateApiApiV1ApplicationsAppNameComponentsOptions) => { 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/applications/${encodeURIComponent(String(args.appName))}/components${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(v1ComponentBase, await res.json()) };