import {z} from 'zod' export type GetApiV1AppsSearchArgs = {appId?: string | undefined, name?: string | undefined, version?: string | undefined, title?: string | undefined, tooltip?: string | undefined, description?: string | undefined, intent_name?: string | undefined, intent_displayName?: string | undefined, intent_context?: string | undefined}; export type GetApiV1AppsSearchOptions = {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 }; /** * Retrieve a list of applications based on parameters provided. Depending on implementation, parameter values should self describe search format and type (e.g. Regex) * * GET /v1/apps/search */ export const getApiV1AppsSearch = async (args: GetApiV1AppsSearchArgs, options: GetApiV1AppsSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.appId != null) query.append("appId", String(args.appId)) if (args.name != null) query.append("name", String(args.name)) if (args.version != null) query.append("version", String(args.version)) if (args.title != null) query.append("title", String(args.title)) if (args.tooltip != null) query.append("tooltip", String(args.tooltip)) if (args.description != null) query.append("description", String(args.description)) if (args.intent_name != null) query.append("intent_name", String(args.intent_name)) if (args.intent_displayName != null) query.append("intent_displayName", String(args.intent_displayName)) if (args.intent_context != null) query.append("intent_context", String(args.intent_context)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/apps/search${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(applicationSearchResponseV1, await res.json()) };