import {z} from 'zod' export type CreateApiV1FeaturesGetArgs = {'x-api-version': string, body: {feature_id: string}}; export type CreateApiV1FeaturesGetOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1FeaturesGetResponse = 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 }; /** * Retrieves a single feature by its ID. * * POST /v1/features.get */ export const createApiV1FeaturesGet = async (args: CreateApiV1FeaturesGetArgs, options?: CreateApiV1FeaturesGetOptions) => { 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.get${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(createApiV1FeaturesGetResponse, await res.json()) };