import {z} from 'zod' export type GetApiFieldsArgs = {search?: string | undefined, entity?: ('project' | 'user' | 'task' | 'file' | 'translation' | 'string' | 'term' | 'concept') | undefined, type?: ('checkbox' | 'radiobuttons' | 'date' | 'datetime' | 'number' | 'labels' | 'select' | 'multiselect' | 'text' | 'textarea' | 'url') | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiFieldsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const field = z.object({id: z.number().int().optional(), name: z.string().optional(), slug: z.string().optional(), description: z.string().optional(), type: z.string().optional(), config: z.object({}).optional(), entities: z.array(z.enum(["project", "user", "task", "file", "translation", "string", "term", "concept"])).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const fieldResource = z.object({data: field}); export const advancedPagination = z.object({offset: z.number().int().optional(), limit: z.number().int().optional(), total: z.number().int().optional()}); export const fieldCollectionResource = z.object({data: z.array(fieldResource), pagination: advancedPagination.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 Fields * * GET /fields */ export const getApiFields = async (args: GetApiFieldsArgs, options?: GetApiFieldsOptions) => { 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.search != null) query.append("search", String(args.search)) if (args.entity != null) query.append("entity", String(args.entity)) if (args.type != null) query.append("type", String(args.type)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/fields${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(fieldCollectionResource, await res.json()) };