import {z} from 'zod' export type EnterpriseStringMassOperation = {op: 'replace' | 'remove' | 'add', path: unknown}; export type EnterpriseStringMassPatch = Array; export type PatchApiProjectsProjectIdStringsArgs = {projectId: number, updateOption?: ('keep_translations_and_approvals' | 'keep_translations' | 'clear_translations_and_approvals') | undefined, body: EnterpriseStringMassPatch}; export type PatchApiProjectsProjectIdStringsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const enterpriseAdditionalFields = z.record(z.string(), z.union([z.string(), z.number().int(), z.boolean(), z.object({})])); export const fileBasedString = z.object({id: z.number().int().optional(), projectId: z.number().int().optional(), branchId: z.number().int().nullable().optional(), identifier: z.string().optional(), text: z.union([z.string(), z.object({})]).nullable().optional(), type: z.string().optional(), context: z.string().optional(), maxLength: z.number().int().optional(), isHidden: z.boolean().optional(), isDuplicate: z.boolean().optional(), masterStringId: z.number().int().nullable().optional(), hasPlurals: z.boolean().optional(), isIcu: z.boolean().optional(), labelIds: z.array(z.number().int()).optional(), webUrl: z.string().optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional(), fields: enterpriseAdditionalFields.optional(), fileId: z.number().int().optional(), directoryId: z.number().int().nullable().optional(), revision: z.number().int().optional()}); export const enterpriseFileBasedStringResource = z.object({data: fileBasedString}); export const enterpriseFileBasedStringUpdatedCollectionResource = z.object({data: z.array(enterpriseFileBasedStringResource)}); 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 }; /** * String Batch Operations * * PATCH /projects/{projectId}/strings */ export const patchApiProjectsProjectIdStrings = async (args: PatchApiProjectsProjectIdStringsArgs, options?: PatchApiProjectsProjectIdStringsOptions) => { 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") if (args.updateOption != null) query.append("updateOption", String(args.updateOption)) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.projectId))}/strings${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(enterpriseFileBasedStringUpdatedCollectionResource, await res.json()) };