import {z} from 'zod' export type OperationType = 'unknown' | 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test'; export type JsonPatchOperation = {op: OperationType, path: string, from?: (string | null) | undefined, value: unknown}; export type PatchApiV1ProxyProfilesProxyProfileIdArgs = {proxyProfileId: string, body: Array}; export type PatchApiV1ProxyProfilesProxyProfileIdOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const webhookNotification = z.object({webhookProxyUrl: z.string(), signingKey1: z.string(), signingKey2: z.string().nullable()}); export const connectionPreferences = z.object({sdkPollInterval: z.number().int(), webhookNotification: webhookNotification.nullable()}); export const selectionRuleKind = z.enum(["include", "exclude"]); export const proxyProfileSelectionRule = z.object({kind: selectionRuleKind, productIdFilter: z.string().nullable(), configIdFilter: z.string().nullable(), environmentIdFilter: z.string().nullable(), productNameMatchFilter: z.string().nullable(), configNameMatchFilter: z.string().nullable(), environmentNameMatchFilter: z.string().nullable()}); export const proxyProfileModel = z.object({proxyProfileId: z.string(), name: z.string(), description: z.string().nullable(), lastAccessedAt: z.string().nullable(), connectionPreferences: connectionPreferences, sdkKeySelectionRules: z.array(proxyProfileSelectionRule)}); 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 }; /** * Update Proxy Profile * * PATCH /v1/proxy-profiles/{proxyProfileId} */ export const patchApiV1ProxyProfilesProxyProfileId = async (args: PatchApiV1ProxyProfilesProxyProfileIdArgs, options?: PatchApiV1ProxyProfilesProxyProfileIdOptions) => { const {baseUrl = "https://api.configcat.com", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/proxy-profiles/${encodeURIComponent(String(args.proxyProfileId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(proxyProfileModel, await res.json()) };