import {z} from 'zod' export type GetApiApiV1CommonActionsArgs = {project_id?: number | undefined, document_id?: number | undefined, view_actions?: Array | undefined, jq_filters?: {[key: string]: string} | undefined}; export type GetApiApiV1CommonActionsOptions = {baseUrl: string, AuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const action = z.object({id: z.number().int().optional(), name: z.string().max(50).optional(), message: z.string().nullable().optional(), view_action: z.string().max(50).nullable().optional(), object_pk: z.string().max(36).nullable().optional(), model_name: z.string().max(50).nullable().optional(), date: z.string().optional(), user__name: z.string().optional(), user__initials: z.string().optional(), user_photo_url: z.string().optional(), request_data: z.object({}).nullable().optional()}); export const getApiApiV1CommonActionsResponse = z.array(action); 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 }; /** * Action List * * GET /api/v1/common/actions/ */ export const getApiApiV1CommonActions = async (args: GetApiApiV1CommonActionsArgs, options: GetApiApiV1CommonActionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, AuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.project_id != null) query.append("project_id", String(args.project_id)) if (args.document_id != null) query.append("document_id", String(args.document_id)) for (const item of args.view_actions ?? []) query.append("view_actions", String(item)) if (args.jq_filters != null) query.append("jq_filters", JSON.stringify(args.jq_filters)) if (AuthToken !== undefined) headers.set("Authorization", AuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/common/actions/${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(getApiApiV1CommonActionsResponse, await res.json()) };