import {z} from 'zod' export type GetApiProjectsProjectIdBranchesNameCompareArgs = {'X-PhraseApp-OTP'?: string | undefined, project_id: string, name: string}; export type GetApiProjectsProjectIdBranchesNameCompareOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const branchComparisonChange = z.object({from: z.record(z.string(), z.unknown()).nullable().optional(), to: z.record(z.string(), z.unknown()).nullable().optional()}); export const branchComparisonDiff = z.object({base_changes: z.array(branchComparisonChange).optional(), head_changes: z.array(branchComparisonChange).optional(), conflicts: z.record(z.string(), z.object({base: z.array(z.record(z.string(), z.unknown())).optional(), head: z.array(z.record(z.string(), z.unknown())).optional()})).optional()}); export const branchComparison = z.object({translation_keys: branchComparisonDiff.optional(), translations: branchComparisonDiff.optional(), locales: branchComparisonDiff.optional(), tags: branchComparisonDiff.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 }; /** * Compare branches * * GET /projects/{project_id}/branches/{name}/compare */ export const getApiProjectsProjectIdBranchesNameCompare = async (args: GetApiProjectsProjectIdBranchesNameCompareArgs, options?: GetApiProjectsProjectIdBranchesNameCompareOptions) => { const {baseUrl = "https://eu.phrase.com/control-hub", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-PhraseApp-OTP'] != null) headers.set("X-PhraseApp-OTP", String(args['X-PhraseApp-OTP'])) if (args.name != null) query.append("name", String(args.name)) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.project_id))}/branches/${encodeURIComponent(String(args.name))}/compare${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(branchComparison, await res.json()) };