import {z} from 'zod' export type ProjectPlaceholderOperation = {op: 'replace' | 'test', path: '/index' | '/type' | '/isBlocking' | '/formats'}; export type ProjectPlaceholderPatch = Array; export type PatchApiProjectsProjectIdPlaceholdersProjectPlaceholderIdArgs = {projectId: number, projectPlaceholderId: number, body: ProjectPlaceholderPatch}; export type PatchApiProjectsProjectIdPlaceholdersProjectPlaceholderIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const projectPlaceholderResponse = z.object({id: z.number().int().nullable().optional(), customPlaceholderId: z.number().int().nullable().optional(), type: z.string().nullable().optional(), index: z.number().int().nullable().optional(), isBlocking: z.boolean().nullable().optional(), formats: z.array(z.string()).optional()}); export const projectPlaceholderResource = z.object({data: projectPlaceholderResponse}); 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 }; /** * Edit Project Placeholder * * PATCH /projects/{projectId}/placeholders/{projectPlaceholderId} */ export const patchApiProjectsProjectIdPlaceholdersProjectPlaceholderId = async (args: PatchApiProjectsProjectIdPlaceholdersProjectPlaceholderIdArgs, options?: PatchApiProjectsProjectIdPlaceholdersProjectPlaceholderIdOptions) => { 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))}/placeholders/${encodeURIComponent(String(args.projectPlaceholderId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(projectPlaceholderResource, await res.json()) };