import {z} from 'zod' export type CreateApiV1FeaturesCreateArgs = {'x-api-version': string, body: {name: string, type: 'boolean' | 'metered' | 'credit_system' | 'ai_credit_system', consumable?: boolean | undefined, display?: {singular: string, plural: string} | undefined, credit_schema?: Array<{metered_feature_id: string, credit_cost: number}> | undefined, model_markups?: ({[key: string]: {markup?: number | undefined, input_cost?: number | undefined, output_cost?: number | undefined}} | null) | undefined, default_markup?: number | undefined, provider_markups?: ({[key: string]: {markup: number}} | null) | undefined, event_names?: Array | undefined, feature_id: string}}; export type CreateApiV1FeaturesCreateOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1FeaturesCreateResponse = z.object({id: z.string(), name: z.string(), type: z.enum(["boolean", "metered", "credit_system", "ai_credit_system"]), consumable: z.boolean(), event_names: z.array(z.string()).optional(), credit_schema: z.array(z.object({metered_feature_id: z.string(), credit_cost: z.number()})).optional(), model_markups: z.record(z.string(), z.object({markup: z.number().gte(-100).optional(), input_cost: z.number().gte(0).optional(), output_cost: z.number().gte(0).optional()})).nullable().optional(), default_markup: z.number().gte(-100).optional(), provider_markups: z.record(z.string(), z.object({markup: z.number().gte(-100)})).nullable().optional(), display: z.object({singular: z.string().nullable().optional(), plural: z.string().nullable().optional()}).optional(), archived: z.boolean()}); 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 }; /** * Creates a new feature. * * POST /v1/features.create */ export const createApiV1FeaturesCreate = async (args: CreateApiV1FeaturesCreateArgs, options?: CreateApiV1FeaturesCreateOptions) => { const {baseUrl = "https://api.useautumn.com", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-version'] != null) headers.set("x-api-version", String(args['x-api-version'])) if (secretKey !== undefined) headers.set('authorization', `Bearer ${secretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/features.create${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(createApiV1FeaturesCreateResponse, await res.json()) };