import {z} from 'zod' export type GetApiPublicSelfServeV1ResourcePolicyArgs = {'application-instance'?: string | undefined, application?: string | undefined}; export type GetApiPublicSelfServeV1ResourcePolicyOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const resourcePolicyKind = z.literal("ResourcePolicy"); export const mapLabelKeyString = z.record(z.string(), z.string()); export const resourcePolicyMetadata = z.object({name: z.string(), labels: mapLabelKeyString.optional()}); export const policyKind = z.enum(["ApplicationGroup", "ApplicationInstancePermission", "Connector", "Subject", "Topic"]); export const celRule = z.object({condition: z.string(), errorMessage: z.string()}); export const resourcePolicySpec = z.object({targetKind: policyKind, description: z.string().optional(), rules: z.array(celRule).optional()}); export const resourcePolicyResourceV1 = z.object({apiVersion: z.string(), kind: resourcePolicyKind, metadata: resourcePolicyMetadata, spec: resourcePolicySpec}); export const getApiPublicSelfServeV1ResourcePolicyResponse = z.array(resourcePolicyResourceV1); 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 }; /** * List ResourcePolicy resources * * GET /public/self-serve/v1/resource-policy */ export const getApiPublicSelfServeV1ResourcePolicy = async (args: GetApiPublicSelfServeV1ResourcePolicyArgs, options: GetApiPublicSelfServeV1ResourcePolicyOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['application-instance'] != null) query.append("application-instance", String(args['application-instance'])) if (args.application != null) query.append("application", String(args.application)) if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/self-serve/v1/resource-policy${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(getApiPublicSelfServeV1ResourcePolicyResponse, await res.json()) };