import {z} from 'zod' export type GetApiApiV2PoliciesIdArgs = {id: string, field_mask?: string | undefined, 'policy_instance.name'?: string | undefined, 'policy_instance.instance_label'?: string | undefined}; export type GetApiApiV2PoliciesIdOptions = {baseUrl: string, AuthorizerAPIKey?: string | undefined, TenantID?: string | undefined, fetch?: typeof fetch | undefined}; export const apiModule = z.object({ast: z.unknown(), id: z.string().optional(), package_path: z.string().optional(), package_root: z.string().optional(), raw: z.string().optional()}); export const v2GetPolicyResponse = z.object({result: apiModule.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 Policy * * GET /api/v2/policies/{id} */ export const getApiApiV2PoliciesId = async (args: GetApiApiV2PoliciesIdArgs, options: GetApiApiV2PoliciesIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, AuthorizerAPIKey, TenantID} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.field_mask != null) query.append("field_mask", String(args.field_mask)) if (args['policy_instance.name'] != null) query.append("policy_instance.name", String(args['policy_instance.name'])) if (args['policy_instance.instance_label'] != null) query.append("policy_instance.instance_label", String(args['policy_instance.instance_label'])) if (AuthorizerAPIKey !== undefined) headers.set("authorization", AuthorizerAPIKey) if (TenantID !== undefined) headers.set("aserto-tenant-id", TenantID) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/policies/${encodeURIComponent(String(args.id))}${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(v2GetPolicyResponse, await res.json()) };