import {z} from 'zod' export type AppImageV1 = {url?: string | undefined}; export type IconV1 = {icon?: string | undefined}; export type NameValuePair = {name?: string | undefined, value?: string | undefined}; export type IntentV1 = {name: string, displayName?: string | undefined, contexts?: Array | undefined, customConfig?: Record | undefined}; export type ApplicationV1 = {appId: string, name: string, manifest: string, manifestType: string, version?: string | undefined, title?: string | undefined, tooltip?: string | undefined, description?: string | undefined, images?: Array | undefined, contactEmail?: string | undefined, supportEmail?: string | undefined, publisher?: string | undefined, icons?: Array | undefined, customConfig?: Array | undefined, intents?: Array | undefined}; export type CreateApiV1AppsArgs = {body: ApplicationV1}; export type CreateApiV1AppsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const appImageV1 = z.object({url: z.string().optional()}); export const iconV1 = z.object({icon: z.string().optional()}); export const nameValuePair = z.object({name: z.string().optional(), value: z.string().optional()}); export const intentV1 = z.object({name: z.string(), displayName: z.string().optional(), contexts: z.array(z.string()).optional(), customConfig: z.object({}).optional()}); export const applicationV1 = z.object({appId: z.string(), name: z.string(), manifest: z.string(), manifestType: z.string(), version: z.string().optional(), title: z.string().optional(), tooltip: z.string().optional(), description: z.string().optional(), images: z.array(appImageV1).optional(), contactEmail: z.string().optional(), supportEmail: z.string().optional(), publisher: z.string().optional(), icons: z.array(iconV1).optional(), customConfig: z.array(nameValuePair).optional(), intents: z.array(intentV1).optional()}); export const applicationSearchResponseV1 = z.object({applications: z.array(applicationV1).optional(), message: 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 }; /** * Create a new application definition * * POST /v1/apps */ export const createApiV1Apps = async (args: CreateApiV1AppsArgs, options: CreateApiV1AppsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/apps${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(applicationSearchResponseV1, await res.json()) };