import {z} from 'zod' export type ScreenshotReplaceForm = {storageId: number, name: string, usePreviousTags?: boolean | undefined}; export type UpdateApiProjectsProjectIdScreenshotsScreenshotIdArgs = {projectId: number, screenshotId: number, body: ScreenshotReplaceForm}; export type UpdateApiProjectsProjectIdScreenshotsScreenshotIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const screenshotTagPosition = z.object({x: z.number().int().optional(), y: z.number().int().optional(), width: z.number().int().optional(), height: z.number().int().optional()}); export const screenshotTag = z.object({id: z.number().int().optional(), screenshotId: z.number().int().optional(), stringId: z.number().int().optional(), position: screenshotTagPosition.optional(), createdAt: z.string().optional()}); export const screenshot = z.object({id: z.number().int().optional(), userId: z.number().int().optional(), url: z.string().optional(), webUrl: z.string().optional(), name: z.string().optional(), size: z.object({width: z.number().int().optional(), height: z.number().int().optional()}).optional(), tagsCount: z.number().int().optional(), tags: z.array(screenshotTag).optional(), labels: z.array(z.number().int()).optional(), labelIds: z.array(z.number().int()).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const screenshotResource = z.object({data: screenshot}); 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 }; /** * Update Screenshot * * PUT /projects/{projectId}/screenshots/{screenshotId} */ export const updateApiProjectsProjectIdScreenshotsScreenshotId = async (args: UpdateApiProjectsProjectIdScreenshotsScreenshotIdArgs, options?: UpdateApiProjectsProjectIdScreenshotsScreenshotIdOptions) => { const {baseUrl = "https://api.crowdin.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.projectId))}/screenshots/${encodeURIComponent(String(args.screenshotId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(screenshotResource, await res.json()) };