import {z} from 'zod' export type GitReferenceResource = {GitCommit?: string | undefined, GitRef?: string | undefined}; export type SelectedGitResource = {ActionName: string, GitReferenceResource: GitReferenceResource, GitResourceReferenceName?: string | undefined}; export type SelectedPackage = {ActionName?: string | undefined, PackageReferenceName?: string | undefined, StepName?: string | undefined, Version?: string | undefined}; export type CreateRunbookSnapshotCommand = {Name: string, Notes?: string | undefined, ProjectId: string, Publish?: string | undefined, RunbookId: string, SelectedGitResources?: Array | undefined, SelectedPackages?: Array | undefined, SpaceId: string}; export type CreateApiSpacesSpaceIdentifierProjectsProjectIdRunbookSnapshotsArgs = {spaceIdentifier: string, projectId: string, body: CreateRunbookSnapshotCommand}; export type CreateApiSpacesSpaceIdentifierProjectsProjectIdRunbookSnapshotsOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const snapshotGitReferenceResource = z.object({GitCommit: z.string().optional(), GitRef: z.string().optional(), VariablesGitCommit: z.string().optional()}); export const gitReferenceResource = z.object({GitCommit: z.string().optional(), GitRef: z.string().optional()}); export const selectedGitResource = z.object({ActionName: z.string().min(1), GitReferenceResource: gitReferenceResource, GitResourceReferenceName: z.string().optional()}); export const selectedPackage = z.object({ActionName: z.string().optional(), PackageReferenceName: z.string().optional(), StepName: z.string().optional(), Version: z.string().optional()}); export const runbookSnapshotResource = z.object({Assembled: z.string(), FrozenProjectVariableSetId: z.string().min(1), FrozenRunbookProcessId: z.string().min(1), GitReference: snapshotGitReferenceResource.optional(), Id: z.string().optional(), LastModifiedBy: z.string().optional(), LastModifiedOn: z.string().optional(), LibraryVariableSetSnapshotIds: z.array(z.string()).optional(), Links: z.record(z.string(), z.string()).optional(), Name: z.string().min(1), Notes: z.string().optional(), ProjectId: z.string(), ProjectVariableSetSnapshotId: z.string().min(1), RunbookId: z.string(), SelectedGitResources: z.array(selectedGitResource).optional(), SelectedPackages: z.array(selectedPackage).optional(), SpaceId: z.string(), VariableSnapshotConcurrencyToken: z.string().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 }; /** * Create a Runbook Snapshot * * POST /spaces/{spaceIdentifier}/projects/{projectId}/runbookSnapshots * * Also accepts auth: APIKeyQuery, NugetApiKeyHeader */ export const createApiSpacesSpaceIdentifierProjectsProjectIdRunbookSnapshots = async (args: CreateApiSpacesSpaceIdentifierProjectsProjectIdRunbookSnapshotsArgs, options?: CreateApiSpacesSpaceIdentifierProjectsProjectIdRunbookSnapshotsOptions) => { 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))}/runbookSnapshots${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(runbookSnapshotResource, await res.json()) };