import {z} from 'zod' export type GetApiInvestigationsTypesArgs = {q?: ({archived?: boolean | undefined}) | undefined, pageNum?: number | undefined, numRowsPerPage?: number | undefined, sortColumn?: ('name' | 'use_case' | 'journeys_count') | undefined, sortOrder?: ('DESC' | 'ASC') | undefined, onlyPermittedTypes?: boolean | undefined, includeJourneys?: boolean | undefined}; export type GetApiInvestigationsTypesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getInvestigationTypes = z.object({investigation_types: z.array(z.object({investigationTypeId: z.number().optional(), name: z.string().optional(), description: z.string().optional(), archivedAt: z.string().optional(), isBeingUsed: z.boolean().optional(), useCase: z.string().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 }; /** * Get list of Investigation types * * GET /investigations/types * * Also accepts auth: basic */ export const getApiInvestigationsTypes = async (args: GetApiInvestigationsTypesArgs, options?: GetApiInvestigationsTypesOptions) => { const {baseUrl = "https://sandbox.alloy.co/v1", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", JSON.stringify(args.q)) if (args.pageNum != null) query.append("pageNum", String(args.pageNum)) if (args.numRowsPerPage != null) query.append("numRowsPerPage", String(args.numRowsPerPage)) if (args.sortColumn != null) query.append("sortColumn", String(args.sortColumn)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.onlyPermittedTypes != null) query.append("onlyPermittedTypes", String(args.onlyPermittedTypes)) if (args.includeJourneys != null) query.append("includeJourneys", String(args.includeJourneys)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/investigations/types${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(getInvestigationTypes, await res.json()) };