import {z} from 'zod' export type GetApiV1PluginsArgs = {page?: number | undefined, pageSize?: number | undefined, q?: string | undefined, category?: string | undefined, partnerId?: string | undefined}; export type GetApiV1PluginsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const pluginPublicDto = z.object({id: z.string(), packageName: z.string(), slug: z.string(), name: z.string(), description: z.string().nullable(), registry: z.string(), repositoryUrl: z.string().nullable(), iconUrl: z.string().nullable(), downloadCount: z.number(), latestVersion: z.string().nullable(), lastPublish: z.string().nullable(), compatibility: z.string().nullable(), readme: z.string().nullable(), changelog: z.string().nullable(), docs: z.string().nullable(), metadataVersion: z.string().nullable(), partnerId: z.string().nullable(), categories: z.array(z.string()), createdAt: z.string()}); export const pagedPluginPublicDto = z.object({items: z.array(pluginPublicDto), total: z.number(), page: z.number(), pageSize: z.number()}); 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 }; /** * List APPROVED plugins with optional q / category / partner filters * * GET /v1/plugins */ export const getApiV1Plugins = async (args: GetApiV1PluginsArgs, options: GetApiV1PluginsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.q != null) query.append("q", String(args.q)) if (args.category != null) query.append("category", String(args.category)) if (args.partnerId != null) query.append("partnerId", String(args.partnerId)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/plugins${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(pagedPluginPublicDto, await res.json()) };