import {z} from 'zod' export type GetApiApiRequestFormArgs = {offset?: number | undefined, limit?: number | undefined, sortOrder?: ('asc' | 'desc') | undefined, sortBy?: ('name' | 'createdAt' | 'updatedAt') | undefined, nameFilter?: string | undefined, exactMatch?: boolean | undefined}; export type GetApiApiRequestFormOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const paginatedRequestForm = z.object({data: z.array(z.object({id: z.string(), name: z.string(), fields: z.array(z.discriminatedUnion("type", [z.object({id: z.string(), type: z.enum(["DROPDOWN", "CHECKBOXES"]), label: z.string(), required: z.boolean().optional(), options: z.array(z.object({value: z.string()}))}), z.object({id: z.string(), type: z.literal("SHORT_ANSWER"), label: z.string(), required: z.boolean().optional()}), z.object({id: z.string(), type: z.literal("ACCESS_DURATION"), label: z.literal("How long do you need access to this data?"), required: z.literal(true)})])), policy: z.object({version: z.string().optional(), rules: z.array(z.object({conditions: z.union([z.object({all: z.array(z.unknown())}), z.object({any: z.array(z.unknown())}), z.object({not: z.unknown()})]), event: z.discriminatedUnion("type", [z.object({type: z.literal("approve"), params: z.object({comment: z.string().optional(), duration: z.number().gte(1).optional(), durationUnit: z.enum(["HOURS", "DAYS", "WEEKS", "MONTHS", "YEARS"]).optional()})}), z.object({type: z.literal("deny"), params: z.object({comment: z.string().optional()})}), z.object({type: z.literal("review"), params: z.object({stages: z.array(z.union([z.object({all: z.array(z.union([z.object({type: z.literal("USER"), id: z.string(), displayName: z.string().optional()}), z.object({type: z.literal("GROUP"), id: z.number().optional(), iamId: z.string().optional(), name: z.string()}), z.object({type: z.literal("PERMISSION"), permission: z.enum(["GOVERNANCE", "USER_ADMIN", "AUDIT"])}), z.object({type: z.literal("DOMAIN PERMISSION"), permission: z.enum(["Manage Policies", "Audit Activity", "Manage Data Products", "Manage Identifiers"]), domainId: z.string().optional()}), z.object({type: z.literal("ATTRIBUTE"), key: z.string(), value: z.string()}), z.object({type: z.literal("CATALOG PERMISSION")})]))}), z.object({any: z.array(z.union([z.object({type: z.literal("USER"), id: z.string(), displayName: z.string().optional()}), z.object({type: z.literal("GROUP"), id: z.number().optional(), iamId: z.string().optional(), name: z.string()}), z.object({type: z.literal("PERMISSION"), permission: z.enum(["GOVERNANCE", "USER_ADMIN", "AUDIT"])}), z.object({type: z.literal("DOMAIN PERMISSION"), permission: z.enum(["Manage Policies", "Audit Activity", "Manage Data Products", "Manage Identifiers"]), domainId: z.string().optional()}), z.object({type: z.literal("ATTRIBUTE"), key: z.string(), value: z.string()}), z.object({type: z.literal("CATALOG PERMISSION")})]))})]))})}), z.object({type: z.literal("dataProductPolicy")})])})), defaultEvent: z.discriminatedUnion("type", [z.object({type: z.literal("approve"), params: z.object({comment: z.string().optional(), duration: z.number().gte(1).optional(), durationUnit: z.enum(["HOURS", "DAYS", "WEEKS", "MONTHS", "YEARS"]).optional()})}), z.object({type: z.literal("deny"), params: z.object({comment: z.string().optional()})}), z.object({type: z.literal("review"), params: z.object({stages: z.array(z.union([z.object({all: z.array(z.union([z.object({type: z.literal("USER"), id: z.string(), displayName: z.string().optional()}), z.object({type: z.literal("GROUP"), id: z.number().optional(), iamId: z.string().optional(), name: z.string()}), z.object({type: z.literal("PERMISSION"), permission: z.enum(["GOVERNANCE", "USER_ADMIN", "AUDIT"])}), z.object({type: z.literal("DOMAIN PERMISSION"), permission: z.enum(["Manage Policies", "Audit Activity", "Manage Data Products", "Manage Identifiers"]), domainId: z.string().optional()}), z.object({type: z.literal("ATTRIBUTE"), key: z.string(), value: z.string()}), z.object({type: z.literal("CATALOG PERMISSION")})]))}), z.object({any: z.array(z.union([z.object({type: z.literal("USER"), id: z.string(), displayName: z.string().optional()}), z.object({type: z.literal("GROUP"), id: z.number().optional(), iamId: z.string().optional(), name: z.string()}), z.object({type: z.literal("PERMISSION"), permission: z.enum(["GOVERNANCE", "USER_ADMIN", "AUDIT"])}), z.object({type: z.literal("DOMAIN PERMISSION"), permission: z.enum(["Manage Policies", "Audit Activity", "Manage Data Products", "Manage Identifiers"]), domainId: z.string().optional()}), z.object({type: z.literal("ATTRIBUTE"), key: z.string(), value: z.string()}), z.object({type: z.literal("CATALOG PERMISSION")})]))})]))})}), z.object({type: z.literal("dataProductPolicy")})])}), dataUseAgreements: z.array(z.object({id: z.string(), name: z.string(), body: z.string()})).optional(), createdBy: z.object({globalUserId: z.string(), name: z.string()}), createdAt: z.union([z.string(), z.string()]), updatedBy: z.object({globalUserId: z.string(), name: z.string()}), updatedAt: z.union([z.string(), z.string()]), version: z.string(), associatedDomains: z.array(z.string())})), meta: z.object({offset: z.number().gte(0).optional(), limit: z.number().gte(1).optional(), totalCount: z.number(), pageCount: z.number(), currentPage: z.number(), totalPages: z.number()})}); 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 }; /** * Search request forms * * GET /api/request-form */ export const getApiApiRequestForm = async (args: GetApiApiRequestFormArgs, options?: GetApiApiRequestFormOptions) => { const {baseUrl = "https://na.api.immutacloud.com/marketplace", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.nameFilter != null) query.append("nameFilter", String(args.nameFilter)) if (args.exactMatch != null) query.append("exactMatch", String(args.exactMatch)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/request-form${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(paginatedRequestForm, await res.json()) };