import {z} from 'zod' export type GetApiV1ProjectsProjectIdHistoryArgs = {limit?: string | undefined, includeAutoSnapshots?: string | undefined, projectId: string}; export type GetApiV1ProjectsProjectIdHistoryOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1ProjectsProjectIdHistoryResponse = z.object({snapshots: z.array(z.object({id: z.string(), projectId: z.string(), version: z.number(), createdById: z.string(), createdAt: z.string(), metadata: z.object({auto: z.boolean(), reason: z.enum(["auto", "manual"]), kind: z.enum(["manual", "autosave", "auto", "export"]), label: z.string().nullable(), createdByName: z.string().nullable(), createdAt: z.string(), teamId: z.string().nullable(), extra: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()]).nullable())}).nullable()}))}); 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 project snapshots * * GET /v1/projects/{projectId}/history */ export const getApiV1ProjectsProjectIdHistory = async (args: GetApiV1ProjectsProjectIdHistoryArgs, options: GetApiV1ProjectsProjectIdHistoryOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = 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.includeAutoSnapshots != null) query.append("includeAutoSnapshots", String(args.includeAutoSnapshots)) if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/history${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(getApiV1ProjectsProjectIdHistoryResponse, await res.json()) };