import {z} from 'zod' export type ApiKeyCreate = {name: string, credit_limit?: (number | null) | undefined, expires_at?: (string | null) | undefined, default_routing_strategy?: ('reliability' | 'balanced' | 'latency' | 'throughput' | 'cache' | null) | undefined}; export type CreateApiV1AccountKeysArgs = {body: ApiKeyCreate}; export type CreateApiV1AccountKeysOptions = {baseUrl?: string | undefined, provisioningBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiKeyCreateResponse = z.object({id: z.number().int(), name: z.string(), key: z.string(), created_at: z.string(), expires_at: z.string().nullable(), is_enabled: z.boolean(), credit_limit: z.number().nullable(), default_routing_strategy: z.enum(["reliability", "balanced", "latency", "throughput", "cache"]).nullable(), message: z.string()}); 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 an API key * * POST /v1/account/keys */ export const createApiV1AccountKeys = async (args: CreateApiV1AccountKeysArgs, options?: CreateApiV1AccountKeysOptions) => { const {baseUrl = "https://api.naga.ac", fetch: fetchFn = fetch, provisioningBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (provisioningBearerAuth !== undefined) headers.set('authorization', `Bearer ${provisioningBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/account/keys${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(apiKeyCreateResponse, await res.json()) };