import {z} from 'zod' export type GetApiApiProjectsPublicArgs = {limit?: number | undefined, offset?: number | undefined, project?: string | undefined, projectTypes?: string | undefined, sort?: string | undefined}; export type GetApiApiProjectsPublicOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const projectType = z.enum(["kws", "audio", "object-detection", "image", "accelerometer", "other"]); export const projectPublicData = z.object({id: z.number().int(), name: z.string(), description: z.string(), created: z.string(), owner: z.string(), ownerAvatar: z.string().optional(), publicUrl: z.string(), projectType: projectType, pageViewCount: z.number().int(), cloneCount: z.number().int(), totalSamplesCount: z.string().optional(), trainingAccuracy: z.number().optional(), testAccuracy: z.number().optional(), readme: z.object({markdown: z.string(), html: z.string()}).optional(), tags: z.array(z.string())}); export const listPublicProjectsResponse = z.object({success: z.boolean(), error: z.string().optional(), projects: z.array(projectPublicData), totalProjectCount: z.number().int()}); 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 public projects * * GET /api/projects/public */ export const getApiApiProjectsPublic = async (args: GetApiApiProjectsPublicArgs, options?: GetApiApiProjectsPublicOptions) => { const {baseUrl = "https://studio.edgeimpulse.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.project != null) query.append("project", String(args.project)) if (args.projectTypes != null) query.append("projectTypes", String(args.projectTypes)) if (args.sort != null) query.append("sort", String(args.sort)) const url = `${baseUrl.replace(/\/+$/, '')}/api/projects/public${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(listPublicProjectsResponse, await res.json()) };