import {z} from 'zod' export type GetApiV1AppsAppIdArgs = {appId: string}; export type GetApiV1AppsAppIdOptions = {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 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 }; /** * Retrieve an application definition * * GET /v1/apps/{appId} */ export const getApiV1AppsAppId = async (args: GetApiV1AppsAppIdArgs, options: GetApiV1AppsAppIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/apps/${encodeURIComponent(String(args.appId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(applicationV1, await res.json()) };