import {z} from 'zod' export type ApiKeyEnum = 'api-key'; export type ApiKeyUpdateRequestResourceObject = {type: ApiKeyEnum, id: string, attributes: {name?: (string | null) | undefined, status?: ('active' | 'disabled' | null) | undefined}}; export type ApiKeyUpdateRequest = {data: ApiKeyUpdateRequestResourceObject}; export type PatchApiApiApiKeysIdArgs = {id: string, 'additional-fields[api-key]'?: Array<'redacted_key'> | undefined, 'fields[api-key]'?: (Array<'created' | 'id' | 'name' | 'redacted_key' | 'scopes' | 'status'>) | undefined, revision: string, body: ApiKeyUpdateRequest}; export type PatchApiApiApiKeysIdOptions = {baseUrl?: string | undefined, KlaviyoAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const apiKeyEnum = z.literal("api-key"); export const objectLinks = z.object({self: z.string()}); export const agentHints = z.object({next_steps: z.array(z.string()).optional()}); export const responseMeta = z.object({agent_hints: agentHints.optional()}); export const patchApiKeyResponse = z.object({data: z.object({type: apiKeyEnum, id: z.string(), attributes: z.object({name: z.string(), status: z.enum(["active", "disabled"]), scopes: z.array(z.enum(["accounts:read", "accounts:write", "agents:read", "agents:write", "applications:read", "applications:write", "billing:read", "billing:write", "brands:read", "brands:write", "campaigns:read", "campaigns:write", "catalogs:read", "catalogs:write", "comments:read", "comments:write", "conversations:read", "conversations:write", "coupon-codes:read", "coupon-codes:write", "coupons:read", "coupons:write", "credentials:read", "credentials:write", "custom-objects:read", "custom-objects:write", "data-privacy:read", "data-privacy:write", "events:read", "events:write", "experiments:read", "flows:read", "flows:write", "folders:read", "folders:write", "forms:read", "forms:write", "images:read", "images:write", "lists:read", "lists:write", "locations:read", "locations:write", "messages:read", "messages:write", "metrics:read", "metrics:write", "profiles:read", "profiles:write", "push-tokens:read", "push-tokens:write", "reviews:read", "reviews:write", "segments:read", "segments:write", "sender-config:read", "sender-config:write", "social:read", "subscriptions:read", "subscriptions:write", "tags:read", "tags:write", "templates:read", "templates:write", "tracking-settings:read", "tracking-settings:write", "translations:read", "translations:write", "usage:read", "users:read", "users:write", "web-feeds:read", "web-feeds:write", "webhooks:read", "webhooks:write"])), created: z.string(), redacted_key: z.string().nullable().optional()}), links: objectLinks}), links: objectLinks.optional(), meta: responseMeta.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 }; /** * Update API Key * * PATCH /api/api-keys/{id} */ export const patchApiApiApiKeysId = async (args: PatchApiApiApiKeysIdArgs, options?: PatchApiApiApiKeysIdOptions) => { const {baseUrl = "https://a.klaviyo.com", fetch: fetchFn = fetch, KlaviyoAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (args['additional-fields[api-key]'] != null) query.append("additional-fields[api-key]", args['additional-fields[api-key]'].join(',')) if (args['fields[api-key]'] != null) query.append("fields[api-key]", args['fields[api-key]'].join(',')) if (args.revision != null) headers.set("revision", String(args.revision)) if (KlaviyoAPIKey !== undefined) headers.set("Authorization", KlaviyoAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/api-keys/${encodeURIComponent(String(args.id))}${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(patchApiKeyResponse, await res.json()) };