import {z} from 'zod' export type URI = string; export type Algorithm = string; export type Hash = string; export type Checksum = {algorithm: Algorithm, hash: Hash}; export type KeyValuePairs = {[key: string]: unknown}; export type CreateAppPkg = {appPkgName: string, appPkgPath: URI, appPkgVersion: string, appProvider?: string | undefined, checksum: Checksum, userDefinedData?: KeyValuePairs | undefined}; export type CreateApiAppPackagesArgs = {body: CreateAppPkg}; export type CreateApiAppPackagesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const appPkgId = z.string(); export const appDId = z.string(); export const appProvider = z.string(); export const appName = z.string(); export const appSoftwareVersion = z.string(); export const appDVersion = z.string(); export const algorithm = z.string(); export const hash = z.string(); export const checksum = z.object({algorithm: algorithm, hash: hash}); export const appPkgSWImageInfo = z.unknown(); export const appPkgArtifactInfo = z.unknown(); export const onboardingState = z.enum(["CREATED", "UPLOADING", "PROCESSING", "ONBOARDED"]); export const appPkgOperationalState = z.enum(["ENABLED", "DISABLED"]); export const usageState = z.enum(["IN_USE", "NOT_IN_USE"]); export const keyValuePairs = z.record(z.string(), z.unknown()); export const linkType = z.object({href: z.string()}); export const appPkgInfoLinks = z.object({self: linkType, appD: linkType, appPkgContent: linkType}); export const appPkgInfo = z.object({id: appPkgId, appDId: appDId, appProvider: appProvider.optional(), appName: appName, appSoftwareVersion: appSoftwareVersion, appDVersion: appDVersion, checksum: checksum, softwareImages: appPkgSWImageInfo, additionalArtifacts: appPkgArtifactInfo.optional(), onboardingState: onboardingState, operationalState: appPkgOperationalState, usageState: usageState, userDefinedData: keyValuePairs.optional(), _links: appPkgInfoLinks}); export const createApiAppPackagesResponse = z.array(appPkgInfo); 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 resource for on-boarding an application package to a MEO * * POST /app_packages */ export const createApiAppPackages = async (args: CreateApiAppPackagesArgs, options?: CreateApiAppPackagesOptions) => { const {baseUrl = "https://localhost/app_pkgm/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/app_packages${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(createApiAppPackagesResponse, await res.json()) };