import {z} from 'zod' export type GetApiPublicSelfServeV1TopicPolicyArgs = {'app-instance'?: string | undefined}; export type GetApiPublicSelfServeV1TopicPolicyOptions = {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 getApiPublicSelfServeV1TopicPolicyResponse = z.array(topicPolicyResourceV1); 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 TopicPolicy resources * * GET /public/self-serve/v1/topic-policy */ export const getApiPublicSelfServeV1TopicPolicy = async (args: GetApiPublicSelfServeV1TopicPolicyArgs, options: GetApiPublicSelfServeV1TopicPolicyOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['app-instance'] != null) query.append("app-instance", String(args['app-instance'])) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiPublicSelfServeV1TopicPolicyResponse, await res.json()) };