import {z} from 'zod' export type CreateApiApiPreviewCapsulesIdDependencyReviewArgs = {id: string, body: {note: string, receipt: string}}; export type CreateApiApiPreviewCapsulesIdDependencyReviewOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiPreviewCapsulesIdDependencyReviewResponse = z.object({acknowledged_count: z.number().int().gte(-9007199254740991).lte(9007199254740991), capsule_id: z.string().max(128), pending_count: z.number().int().gte(-9007199254740991).lte(9007199254740991), reviewed_at: z.string().max(64)}).and(z.record(z.string(), z.unknown())); 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 }; /** * Confirm a capsule is still valid after capsules it links to changed, with a short note and the receipt of a full read * * POST /api/preview/capsules/{id}/dependency-review * * Also accepts auth: cookieAuth, oauth2 */ export const createApiApiPreviewCapsulesIdDependencyReview = async (args: CreateApiApiPreviewCapsulesIdDependencyReviewArgs, options: CreateApiApiPreviewCapsulesIdDependencyReviewOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/preview/capsules/${encodeURIComponent(String(args.id))}/dependency-review${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiApiPreviewCapsulesIdDependencyReviewResponse, await res.json()) };