import {z} from 'zod' export type GetApiTranslationRequestsArgs = {page?: number | undefined, per_page?: number | undefined, 'filter[status]'?: string | undefined, 'filter[asset_id]'?: string | undefined, 'filter[created_at_before]'?: string | undefined, 'filter[created_at_after]'?: string | undefined, sort?: string | undefined}; export type GetApiTranslationRequestsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const translationRequestItemAttributes = z.object({id: z.string().optional(), language_code: z.string().optional(), source_language_code: z.string().nullable().optional(), status: z.enum(["cancelled", "completed", "deleted", "failed", "manually_translating", "processing", "reopened", "requested", "re_requested"]).optional(), base_asset_id: z.string().optional(), asset_version_id: z.string().optional(), integration_id: z.string().optional(), creator_id: z.string().optional(), notes: z.string().nullable().optional(), due_date: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const translationRequestItem = z.object({type: z.string().optional(), id: z.string().optional(), attributes: translationRequestItemAttributes.optional()}); export const translationRequestList = z.object({data: z.array(translationRequestItem).optional(), links: z.object({first: z.string().optional(), last: z.string().optional(), prev: z.string().nullable().optional(), next: z.string().nullable().optional()}).optional(), meta: z.object({current_page: z.number().int().optional(), from: z.number().int().optional(), last_page: z.number().int().optional(), links: z.array(z.object({url: z.string().nullable().optional(), label: z.string().optional(), active: z.boolean().optional()})).optional(), path: z.string().optional(), per_page: z.number().int().optional(), to: z.number().int().optional(), total: z.number().int().optional()}).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 all translation requests * * GET /translation-requests */ export const getApiTranslationRequests = async (args: GetApiTranslationRequestsArgs, options?: GetApiTranslationRequestsOptions) => { const {baseUrl = "https://enterprise.knak.io/api/published/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args['filter[asset_id]'] != null) query.append("filter[asset_id]", String(args['filter[asset_id]'])) if (args['filter[created_at_before]'] != null) query.append("filter[created_at_before]", String(args['filter[created_at_before]'])) if (args['filter[created_at_after]'] != null) query.append("filter[created_at_after]", String(args['filter[created_at_after]'])) if (args.sort != null) query.append("sort", String(args.sort)) const url = `${baseUrl.replace(/\/+$/, '')}/translation-requests${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(translationRequestList, await res.json()) };