import {z} from 'zod' export type TermCreateForm = {languageId: string, text: string, description?: string | undefined, partOfSpeech?: ('adjective' | 'adposition' | 'adverb' | 'auxiliary' | 'coordinating conjunction' | 'determiner' | 'interjection' | 'noun' | 'numeral' | 'particle' | 'pronoun' | 'proper noun' | 'subordinating conjunction' | 'verb' | 'other') | undefined, status?: ('preferred' | 'admitted' | 'not recommended' | 'obsolete' | 'draft') | undefined, type?: ('full form' | 'acronym' | 'abbreviation' | 'short form' | 'phrase' | 'variant') | undefined, gender?: ('masculine' | 'feminine' | 'neuter' | 'other') | undefined, note?: string | undefined, url?: string | undefined, conceptId?: number | undefined, translationOfTermId?: number | undefined}; export type CreateApiGlossariesGlossaryIdTermsArgs = {glossaryId: number, body: TermCreateForm}; export type CreateApiGlossariesGlossaryIdTermsOptions = {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 }; /** * Add Term * * POST /glossaries/{glossaryId}/terms */ export const createApiGlossariesGlossaryIdTerms = async (args: CreateApiGlossariesGlossaryIdTermsArgs, options?: CreateApiGlossariesGlossaryIdTermsOptions) => { 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${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(crowdinTermResource, await res.json()) };