import {z} from 'zod' export type ACLRuleDeviceFilter = {type: string}; export type ACLRuleUpdate = {type: string, enabled: boolean, name: string, description?: string | undefined, action: 'ALLOW' | 'BLOCK', enforcingDeviceFilter?: ACLRuleDeviceFilter | undefined, index?: number | undefined, sourceFilter: unknown, destinationFilter: unknown}; export type CreateApiV1SitesSiteIdAclRulesArgs = {siteId: string, body: ACLRuleUpdate}; export type CreateApiV1SitesSiteIdAclRulesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const aCLRuleDeviceFilter = z.object({type: z.string()}); export const userDefinedOrDerivedEntityMetadata = z.object({origin: z.string()}); export const aCLRule = z.object({type: z.string(), id: z.string(), enabled: z.boolean(), name: z.string().min(1), description: z.string().optional(), action: z.enum(["ALLOW", "BLOCK"]), enforcingDeviceFilter: aCLRuleDeviceFilter.optional(), index: z.number().int().gte(0), sourceFilter: z.unknown(), destinationFilter: z.unknown(), metadata: userDefinedOrDerivedEntityMetadata}); 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 }; /** * Create ACL Rule * * POST /v1/sites/{siteId}/acl-rules */ export const createApiV1SitesSiteIdAclRules = async (args: CreateApiV1SitesSiteIdAclRulesArgs, options?: CreateApiV1SitesSiteIdAclRulesOptions) => { const {baseUrl = "https://api.ui.com/v1/connector/consoles/942A6FF664C0000000000964970E0000000009E710560000000068A9873F:9999999999/proxy/network/integration", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/sites/${encodeURIComponent(String(args.siteId))}/acl-rules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(aCLRule, await res.json()) };