import {z} from 'zod' export type GetApiLicensesLicensesCurrentUsageOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const licenseLimitUsageResource = z.object({CurrentUsage: z.number().int().optional(), Disposition: z.enum(["Information", "Notice", "Warning", "Error"]).optional(), EffectiveLimit: z.number().int().optional(), EffectiveLimitDescription: z.string().optional(), IsUnlimited: z.boolean().optional(), LicensedLimit: z.number().int().optional(), LicenseLimitDescription: z.string().optional(), LimitStatus: z.enum(["UnderLimit", "AlmostAtLimit", "AtLimit", "InOverrun", "ExceedingLimit"]).optional(), LimitUsageDescription: z.string().optional(), Message: z.string().optional(), Name: z.string().optional(), TargetTypes: z.array(z.string()).optional()}); export const spaceLicenseUsageResource = z.object({MachinesCount: z.number().int().optional(), ProjectsCount: z.number().int().optional(), SpaceName: z.string().optional(), TenantsCount: z.number().int().optional()}); export const licenseUsageResource = z.object({Id: z.string().optional(), IsPtm: z.boolean().optional(), LastModifiedBy: z.string().optional(), LastModifiedOn: z.string().optional(), Limits: z.array(licenseLimitUsageResource).optional(), Links: z.record(z.string(), z.string()).optional(), SpacesUsage: z.array(spaceLicenseUsageResource).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 }; /** * Get the usage of the current Octopus server. * * GET /licenses/licenses-current-usage * * Also accepts auth: APIKeyQuery, NugetApiKeyHeader */ export const getApiLicensesLicensesCurrentUsage = async (options?: GetApiLicensesLicensesCurrentUsageOptions) => { const {baseUrl = "https://demo.octopus.app/api", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-Octopus-ApiKey", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/licenses/licenses-current-usage${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(licenseUsageResource, await res.json()) };