import {z} from 'zod' export type PatchApiApiV2HelperImportFileIdArgs = {'Upload-Offset': number, 'Content-Type': 'application/offset+octet-stream', id: number, body: BodyInit}; export type PatchApiApiV2HelperImportFileIdOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const importFileHelperAPIResponse = z.object({jsonapi: z.object({version: z.string(), ext: z.array(z.string()).optional()}), meta: z.object({file: z.string().optional(), size: z.number().int().optional()}), data: z.array(z.object({}))}); 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 }; /** * Update Helpers * * PATCH /api/v2/helper/importFile/{id} */ export const patchApiApiV2HelperImportFileId = async (args: PatchApiApiV2HelperImportFileIdArgs, options: PatchApiApiV2HelperImportFileIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/offset+octet-stream") headers.set('accept', "application/vnd.api+json") if (args['Upload-Offset'] != null) headers.set("Upload-Offset", String(args['Upload-Offset'])) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/helper/importFile/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(importFileHelperAPIResponse, await res.json()) };