import {z} from 'zod' export type DeploymentConnectivityPolicy = {AllowDeploymentsToNoTargets?: boolean | undefined, ExcludeUnhealthyTargets?: boolean | undefined, SkipMachineBehavior?: ('None' | 'SkipUnavailableMachines') | undefined, TargetRoles?: Array | undefined}; export type RunbookRetentionPolicy = {QuantityToKeep?: number | undefined, ShouldKeepForever?: boolean | undefined, Strategy?: string | undefined, Unit?: ('Days' | 'Items') | undefined}; export type CreateRunbookInDatabaseCommandV2 = {AgentAccess?: ('Allowed' | 'Blocked') | undefined, ConnectivityPolicy?: DeploymentConnectivityPolicy | undefined, DefaultGuidedFailureMode?: ('EnvironmentDefault' | 'Off' | 'On') | undefined, Description?: string | undefined, Environments?: Array | undefined, EnvironmentScope?: ('All' | 'Specified' | 'FromProjectLifecycles') | undefined, ForcePackageDownload?: boolean | undefined, MultiTenancyMode?: ('Untenanted' | 'TenantedOrUntenanted' | 'Tenanted') | undefined, Name: string, ProjectId: string, RunbookTags?: Array | undefined, RunRetentionPolicy?: RunbookRetentionPolicy | undefined, Slug?: string | undefined, SpaceId: string}; export type CreateApiSpacesSpaceIdentifierProjectsProjectIdRunbooksV2Args = {spaceIdentifier: string, projectId: string, body: CreateRunbookInDatabaseCommandV2}; export type CreateApiSpacesSpaceIdentifierProjectsProjectIdRunbooksV2Options = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const createRunbookInDatabaseResponseV2 = z.object({Id: z.string(), Name: z.string().min(1), ProjectId: z.string(), Slug: z.string().min(1)}); 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 a new Database Runbook * * POST /spaces/{spaceIdentifier}/projects/{projectId}/runbooks/v2 * * Also accepts auth: APIKeyQuery, NugetApiKeyHeader */ export const createApiSpacesSpaceIdentifierProjectsProjectIdRunbooksV2 = async (args: CreateApiSpacesSpaceIdentifierProjectsProjectIdRunbooksV2Args, options?: CreateApiSpacesSpaceIdentifierProjectsProjectIdRunbooksV2Options) => { 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(/\/+$/, '')}/spaces/${encodeURIComponent(String(args.spaceIdentifier))}/projects/${encodeURIComponent(String(args.projectId))}/runbooks/v2${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(createRunbookInDatabaseResponseV2, await res.json()) };