import {z} from 'zod' export type ITenantVariableScope = {EnvironmentIds?: Array | undefined}; export type SensitiveValue = {HasValue?: boolean | undefined, Hint?: string | undefined, NewValue?: string | undefined}; export type PropertyValueResource = {IsSensitive?: boolean | undefined, SensitiveValue?: SensitiveValue | undefined, Value?: string | undefined}; export type TenantCommonVariablePayload = {Id?: string | undefined, OwnerId: string, Scope: ITenantVariableScope, TemplateId: string, Value: PropertyValueResource}; export type ModifyCommonVariablesByTenantIdCommand = {ConcurrencyToken?: string | undefined, SpaceId: string, TenantId: string, Variables: Array}; export type UpdateApiTenantsTenantIdCommonvariablesArgs = {tenantId: string, body: ModifyCommonVariablesByTenantIdCommand}; export type UpdateApiTenantsTenantIdCommonvariablesOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const commonVariableScope = z.object({EnvironmentIds: z.array(z.string())}); export const sensitiveValue = z.object({HasValue: z.boolean().optional(), Hint: z.string().optional(), NewValue: z.string().optional()}); export const propertyValueResource = z.object({IsSensitive: z.boolean().optional(), SensitiveValue: sensitiveValue.optional(), Value: z.string().optional()}); export const actionTemplateParameterResource = z.object({DefaultValue: propertyValueResource.optional(), DisplaySettings: z.record(z.string(), z.string()).optional(), HelpText: z.string().optional(), Id: z.string().optional(), Label: z.string().optional(), Name: z.string().optional()}); export const tenantCommonVariableV2 = z.object({Id: z.string().min(1), LibraryVariableSetId: z.string(), LibraryVariableSetName: z.string().optional(), Scope: commonVariableScope, Template: actionTemplateParameterResource.optional(), TemplateId: z.string().min(1), Value: propertyValueResource}); export const modifyCommonVariablesByTenantIdResponse = z.object({ConcurrencyToken: z.string().min(1), TenantId: z.string(), Variables: z.array(tenantCommonVariableV2)}); 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 or Update the common variables associated with the tenant. * * PUT /tenants/{tenantId}/commonvariables * * Also accepts auth: APIKeyQuery, NugetApiKeyHeader */ export const updateApiTenantsTenantIdCommonvariables = async (args: UpdateApiTenantsTenantIdCommonvariablesArgs, options?: UpdateApiTenantsTenantIdCommonvariablesOptions) => { const {baseUrl = "https://demo.octopus.app/api", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-Octopus-ApiKey", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/tenants/${encodeURIComponent(String(args.tenantId))}/commonvariables${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(modifyCommonVariablesByTenantIdResponse, await res.json()) };