import {z} from 'zod' export type TermOperation = {op: 'replace' | 'test', path: '/text' | '/description' | '/partOfSpeech' | '/status' | '/type' | '/gender' | '/url' | '/note'}; export type TermPatch = Array; export type PatchApiGlossariesGlossaryIdTermsTermIdArgs = {glossaryId: number, termId: number, body: TermPatch}; export type PatchApiGlossariesGlossaryIdTermsTermIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const term = z.object({id: z.number().int().optional(), userId: z.number().int().optional(), glossaryId: z.number().int().optional(), languageId: z.string().optional(), text: z.string().optional(), description: z.string().optional(), partOfSpeech: z.string().optional(), status: z.string().optional(), type: z.string().optional(), gender: z.string().optional(), note: z.string().optional(), url: z.string().optional(), conceptId: z.number().int().optional(), lemma: z.string().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const crowdinTermResource = z.object({data: term}); 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 Term * * PATCH /glossaries/{glossaryId}/terms/{termId} */ export const patchApiGlossariesGlossaryIdTermsTermId = async (args: PatchApiGlossariesGlossaryIdTermsTermIdArgs, options?: PatchApiGlossariesGlossaryIdTermsTermIdOptions) => { 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(/\/+$/, '')}/glossaries/${encodeURIComponent(String(args.glossaryId))}/terms/${encodeURIComponent(String(args.termId))}${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(crowdinTermResource, await res.json()) };