import {z} from 'zod' export type GetApiWorkDefinitionsArgs = {order?: string | undefined, size?: number | undefined, sort?: string | undefined, start?: number | undefined, type?: string | undefined}; export type GetApiWorkDefinitionsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const workDefinitionRepresentation = z.object({id: z.string(), key: z.string(), version: z.number().int(), name: z.string().optional(), category: z.string().optional(), description: z.string().optional(), type: z.string(), appName: z.string().optional(), appKey: z.string().optional(), appIcon: z.string().optional(), appOrder: z.number().int().optional(), appColor: z.string().optional(), translations: z.record(z.string(), z.record(z.string(), z.string())).optional(), appTranslations: z.record(z.string(), z.record(z.string(), z.string())).optional()}); export const dataResponseWorkDefinitionRepresentation = z.object({data: z.array(workDefinitionRepresentation).optional(), total: z.number().int().optional(), start: z.number().int().optional(), sort: z.string().optional(), order: z.string().optional(), size: z.number().int().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 }; /** * Get work definitions * * GET /work-definitions */ export const getApiWorkDefinitions = async (args: GetApiWorkDefinitionsArgs, options?: GetApiWorkDefinitionsOptions) => { const {baseUrl = "http://localhost:8090/inspect-api", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.order != null) query.append("order", String(args.order)) if (args.size != null) query.append("size", String(args.size)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.start != null) query.append("start", String(args.start)) if (args.type != null) query.append("type", String(args.type)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/work-definitions${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(dataResponseWorkDefinitionRepresentation, await res.json()) };