import {z} from 'zod' export type Variable = {type?: string | undefined, id?: string | undefined, attributes?: ({category?: ('TERRAFORM' | 'ENV') | undefined, description?: string | undefined, hcl?: boolean | undefined, key?: string | undefined, sensitive?: boolean | undefined, value?: string | undefined}) | undefined, relationships?: ({workspace?: ({id?: string | undefined, type?: 'workspace' | undefined}) | undefined}) | undefined}; export type CreateApiWorkspaceWorkspaceIdVariableArgs = {body: {data?: Variable | undefined}}; export type CreateApiWorkspaceWorkspaceIdVariableOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const variable = z.object({type: z.string().optional(), id: z.string().optional(), attributes: z.object({category: z.enum(["TERRAFORM", "ENV"]).optional(), description: z.string().optional(), hcl: z.boolean().optional(), key: z.string().optional(), sensitive: z.boolean().optional(), value: z.string().optional()}).optional(), relationships: z.object({workspace: z.object({id: z.string().optional(), type: z.literal("workspace").optional()}).optional()}).optional()}); export const createApiWorkspaceWorkspaceIdVariableResponse = z.object({data: variable.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 }; /** * Creates an item of type variable and adds it to variable * * POST /workspace/{workspaceId}/variable */ export const createApiWorkspaceWorkspaceIdVariable = async (args: CreateApiWorkspaceWorkspaceIdVariableArgs, options: CreateApiWorkspaceWorkspaceIdVariableOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") const url = `${baseUrl.replace(/\/+$/, '')}/workspace/${encodeURIComponent(String(args.workspaceId))}/variable${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(createApiWorkspaceWorkspaceIdVariableResponse, await res.json()) };