import {z} from 'zod' export type Consumer = {id?: string | undefined, username?: string | undefined, tags?: Array | undefined, created_at?: number | undefined, custom_id?: string | undefined}; export type ConsumerJWTConfig = {id?: string | undefined, key?: string | undefined, secret?: string | undefined, tags?: Array | undefined, consumer?: Consumer | undefined, algorithm?: string | undefined, publicKey?: string | undefined, created_at?: number | undefined, rsa_public_key?: string | undefined}; export type CreateApiV1KongAdminConsumersConsumerIdJwtArgs = {consumerId: string, body: ConsumerJWTConfig}; export type CreateApiV1KongAdminConsumersConsumerIdJwtOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const consumer = z.object({id: z.string().optional(), username: z.string().optional(), tags: z.array(z.string()).optional(), created_at: z.number().int().optional(), custom_id: z.string().optional()}); export const consumerJWTConfig = z.object({id: z.string().optional(), key: z.string().optional(), secret: z.string().optional(), tags: z.array(z.string()).optional(), consumer: consumer.optional(), algorithm: z.string().optional(), publicKey: z.string().optional(), created_at: z.number().int().optional(), rsa_public_key: z.string().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 }; /** * Add JWT credential to consumer * * POST /v1/kong-admin/consumers/{consumerId}/jwt */ export const createApiV1KongAdminConsumersConsumerIdJwt = async (args: CreateApiV1KongAdminConsumersConsumerIdJwtArgs, options?: CreateApiV1KongAdminConsumersConsumerIdJwtOptions) => { const {baseUrl = "http://api.floik.com", 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/kong-admin/consumers/${encodeURIComponent(String(args.consumerId))}/jwt${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(consumerJWTConfig, await res.json()) };