import {z} from 'zod' export type LabelScreenshotAssignForm = {screenshotIds: Array}; export type CreateApiProjectsProjectIdLabelsLabelIdScreenshotsArgs = {projectId: number, labelId: number, body: LabelScreenshotAssignForm}; export type CreateApiProjectsProjectIdLabelsLabelIdScreenshotsOptions = {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 pagination = z.object({offset: z.number().int().optional(), limit: z.number().int().optional()}); export const screenshotCollectionResource = z.object({data: z.array(screenshotResource), pagination: pagination.optional()}); 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 }; /** * Assign Label to Screenshots * * POST /projects/{projectId}/labels/{labelId}/screenshots */ export const createApiProjectsProjectIdLabelsLabelIdScreenshots = async (args: CreateApiProjectsProjectIdLabelsLabelIdScreenshotsArgs, options?: CreateApiProjectsProjectIdLabelsLabelIdScreenshotsOptions) => { 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))}/labels/${encodeURIComponent(String(args.labelId))}/screenshots${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(screenshotCollectionResource, await res.json()) };