import {z} from 'zod' export type GetApiTmsTmIdSegmentsArgs = {tmId: number, orderBy?: 'id' | undefined, limit?: number | undefined, offset?: number | undefined, croql?: string | undefined}; export type GetApiTmsTmIdSegmentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tmSegmentRecord = z.array(z.object({id: z.number().int().optional(), languageId: z.string().optional(), text: z.string().optional(), usageCount: z.number().int().optional(), createdBy: z.number().int().optional(), updatedBy: z.number().int().optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional()})); export const tmSegment = z.object({id: z.number().int().optional(), records: tmSegmentRecord.optional()}); export const tmSegmentResource = z.object({data: tmSegment}); export const pagination = z.object({offset: z.number().int().optional(), limit: z.number().int().optional()}); export const tmSegmentCollectionResource = z.object({data: z.array(tmSegmentResource), pagination: pagination.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 }; /** * List TM Segments * * GET /tms/{tmId}/segments */ export const getApiTmsTmIdSegments = async (args: GetApiTmsTmIdSegmentsArgs, options?: GetApiTmsTmIdSegmentsOptions) => { const {baseUrl = "https://api.crowdin.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.croql != null) query.append("croql", String(args.croql)) const url = `${baseUrl.replace(/\/+$/, '')}/tms/${encodeURIComponent(String(args.tmId))}/segments${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(tmSegmentCollectionResource, await res.json()) };