import {z} from 'zod' export type TopicPolicyKind = 'TopicPolicy'; export type ApplicationPolicyMetadata = {name: string}; export type ConstraintKind = 'AllowedKeys' | 'Match' | 'NoneOf' | 'OneOf' | 'Range'; export type AllowedKeys = {constraint: ConstraintKind, optional?: boolean | undefined, keys: Array}; export type Match = {constraint: ConstraintKind, optional?: boolean | undefined, pattern: string}; export type NoneOf = {constraint: ConstraintKind, optional?: boolean | undefined, values: Array}; export type OneOf = {constraint: ConstraintKind, optional?: boolean | undefined, values: Array}; export type Range = {constraint: ConstraintKind, optional?: boolean | undefined, min: number, max: number}; export type PolicyConstraintDto = AllowedKeys | Match | NoneOf | OneOf | Range; export type MapPolicyConstraintDto = {[key: string]: PolicyConstraintDto}; export type ApplicationPolicySpec = {policies: MapPolicyConstraintDto}; export type TopicPolicyResourceV1 = {apiVersion: string, kind: TopicPolicyKind, metadata: ApplicationPolicyMetadata, spec: ApplicationPolicySpec}; export type UpdateApiPublicSelfServeV1TopicPolicyArgs = {dryMode?: boolean | undefined, body: TopicPolicyResourceV1}; export type UpdateApiPublicSelfServeV1TopicPolicyOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const topicPolicyKind = z.literal("TopicPolicy"); export const applicationPolicyMetadata = z.object({name: z.string()}); export const constraintKind = z.enum(["AllowedKeys", "Match", "NoneOf", "OneOf", "Range"]); export const allowedKeys = z.object({constraint: constraintKind, optional: z.boolean().optional(), keys: z.array(z.string())}); export const match = z.object({constraint: constraintKind, optional: z.boolean().optional(), pattern: z.string()}); export const noneOf = z.object({constraint: constraintKind, optional: z.boolean().optional(), values: z.array(z.string())}); export const oneOf = z.object({constraint: constraintKind, optional: z.boolean().optional(), values: z.array(z.string())}); export const range = z.object({constraint: constraintKind, optional: z.boolean().optional(), min: z.number().int(), max: z.number().int()}); export const policyConstraintDto = z.discriminatedUnion("constraint", [allowedKeys, match, noneOf, oneOf, range]); export const mapPolicyConstraintDto = z.record(z.string(), policyConstraintDto); export const applicationPolicySpec = z.object({policies: mapPolicyConstraintDto}); export const topicPolicyResourceV1 = z.object({apiVersion: z.string(), kind: topicPolicyKind, metadata: applicationPolicyMetadata, spec: applicationPolicySpec}); export const upsertResult = z.enum(["Created", "Updated", "NotChanged"]); export const topicPolicyApplyResultV1 = z.object({resource: topicPolicyResourceV1, upsertResult: upsertResult}); 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 or update a TopicPolicy * * PUT /public/self-serve/v1/topic-policy */ export const updateApiPublicSelfServeV1TopicPolicy = async (args: UpdateApiPublicSelfServeV1TopicPolicyArgs, options: UpdateApiPublicSelfServeV1TopicPolicyOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryMode != null) query.append("dryMode", String(args.dryMode)) if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/self-serve/v1/topic-policy${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(topicPolicyApplyResultV1, await res.json()) };