import {z} from 'zod' export type RolloutData = {labels: {[key: string]: number}, latest_weight?: number | undefined}; export type ConditionData = {kind?: string | undefined, attribute?: string | undefined, value: unknown, values?: Array | undefined, pattern?: string | undefined}; export type OverrideData = {name?: (string | null) | undefined, description?: (string | null) | undefined, conditions: Array, rollout: RolloutData}; export type VersionLabelData = {target_type: 'version', serialized_value: string, version?: number | undefined}; export type LatestLabelData = {target_type: 'latest'}; export type CodeDefaultLabelData = {target_type: 'code_default'}; export type LabelRefLabelData = {target_type: 'label', target_label: string}; export type VariableDefinitionCreateRequest = {name: string, description: string | null, json_schema: {[key: string]: unknown} | null, template_inputs_schema?: ({[key: string]: unknown} | null) | undefined, type_id?: (string | null) | undefined, rollout: RolloutData, overrides: Array, aliases?: Array | undefined, example?: (string | null) | undefined, external?: boolean | undefined, labels?: ({[key: string]: VersionLabelData | LatestLabelData | CodeDefaultLabelData | LabelRefLabelData}) | undefined, initial_serialized_value?: string | undefined}; export type CreateApiV1ProjectsProjectIdVariablesArgs = {project_id: string, body: VariableDefinitionCreateRequest}; export type CreateApiV1ProjectsProjectIdVariablesOptions = {baseUrl: string, OAuth2AuthorizationCodeBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const jSONBData: z.ZodType = z.union([z.record(z.string(), z.lazy(() => jSONBData)), z.array(z.lazy(() => jSONBData)), z.string(), z.number().int(), z.boolean(), z.number()]).nullable(); export const labelRead = z.object({id: z.string(), variable_definition_id: z.string(), label: z.string(), target_type: z.string(), target_label: z.string().nullable(), version_id: z.string().nullable(), version_number: z.number().int().nullable(), serialized_value: z.string().nullable(), updated_at: z.string(), updated_by: z.string().nullable(), updated_by_name: z.string().nullable()}); export const versionRead = z.object({id: z.string(), variable_definition_id: z.string(), version: z.number().int(), serialized_value: z.string(), description: z.string().nullable(), created_at: z.string(), created_by: z.string().nullable(), created_by_name: z.string().nullable()}); export const rolloutData = z.object({labels: z.record(z.string(), z.number()), latest_weight: z.number().optional()}); export const conditionData = z.object({kind: z.string().optional(), attribute: z.string().optional(), value: z.unknown(), values: z.array(z.unknown()).optional(), pattern: z.string().optional()}); export const overrideData = z.object({name: z.string().nullable().optional(), description: z.string().nullable().optional(), conditions: z.array(conditionData), rollout: rolloutData}); export const variableDefinitionRead = z.object({id: z.string(), project_id: z.string(), type_id: z.string().nullable(), type_name: z.string().nullable(), kind: z.enum(["general", "prompt"]), name: z.string(), display_name: z.string().nullable(), prompt_slug: z.string().nullable(), description: z.string().nullable(), json_schema: jSONBData.nullable(), template_inputs_schema: jSONBData.nullable(), labels: z.record(z.string(), labelRead), latest_version: versionRead.nullable(), rollout: rolloutData, overrides: z.array(overrideData), aliases: z.array(z.string()).nullable(), example: z.string().nullable(), external: z.boolean(), created_at: z.string(), updated_at: z.string(), created_by_name: z.string().nullable(), updated_by_name: z.string().nullable()}); 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 Variable * * POST /v1/projects/{project_id}/variables/ */ export const createApiV1ProjectsProjectIdVariables = async (args: CreateApiV1ProjectsProjectIdVariablesArgs, options: CreateApiV1ProjectsProjectIdVariablesOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2AuthorizationCodeBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2AuthorizationCodeBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2AuthorizationCodeBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.project_id))}/variables/${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(variableDefinitionRead, await res.json()) };