import {z} from 'zod' export type LicenseRequestDeploymentScope = {id?: (string | null) | undefined, baseDomains: Array}; export type LicenseDeliveryMethod = 'MANUAL'; export type LicenseRequestContactRole = 'TECHNICAL' | 'ADMINISTRATOR'; export type LicenseRequestContact = {email: string, givenName: string, familyName: string, roles: Array, displayName?: (string | null) | undefined}; export type LicenseRequestDeployment = {deployment: LicenseRequestDeploymentScope, organizationName: string, organizationUnit?: (string | null) | undefined, locality?: (string | null) | undefined, state?: (string | null) | undefined, country?: (string | null) | undefined, licenseDeliveryMethod?: LicenseDeliveryMethod | undefined, contacts: Array}; export type GenerateLicenseRequestRequest = {algorithm?: (string | null) | undefined, use?: (string | null) | undefined, deployment: LicenseRequestDeployment, serialNumber?: (number | null) | undefined}; export type CreateApiLicenseRequestGenerateArgs = {body: GenerateLicenseRequestRequest}; export type CreateApiLicenseRequestGenerateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const licenseRequestDeploymentScope = z.object({id: z.string().nullable().optional(), baseDomains: z.array(z.string())}); export const licenseRequestTenant = z.object({name: z.string(), slug: z.string()}); export const licenseRequestOwner = z.object({email: z.string(), displayName: z.string()}); export const licenseRequestContactRole = z.enum(["TECHNICAL", "ADMINISTRATOR"]); export const licenseRequestContact = z.object({email: z.string(), givenName: z.string(), familyName: z.string(), roles: z.array(licenseRequestContactRole), displayName: z.string().nullable().optional()}); export const licenseDeliveryMethod = z.literal("MANUAL"); export const localInstanceCertificate = z.object({commonName: z.string(), organizationName: z.string().nullable().optional(), organizationUnit: z.string().nullable().optional(), locality: z.string().nullable().optional(), state: z.string().nullable().optional(), country: z.string().nullable().optional(), email: z.string().nullable().optional(), serialNumber: z.number().int()}); export const localInstancePublicKey = z.object({alias: z.string(), providerId: z.string(), kid: z.string().nullable().optional(), publicJwk: z.record(z.string(), z.unknown())}); export const licenseRequestCertificateSigningRequest = z.object({derBase64: z.string(), pem: z.string(), fingerprint: z.string()}); export const licenseRecipientCertificate = z.object({derBase64: z.string(), pem: z.string(), chainBase64: z.array(z.string()), fingerprint: z.string()}); export const licenseRecipientPublicKey = z.object({kid: z.string(), publicJwk: z.record(z.string(), z.unknown()), thumbprint: z.string(), certificate: licenseRecipientCertificate.nullable().optional()}); export const licenseRequestPayload = z.object({version: z.string(), requestId: z.string(), generatedAt: z.string(), deployment: licenseRequestDeploymentScope, platformIssuer: z.string(), tenant: licenseRequestTenant, owner: licenseRequestOwner, contacts: z.array(licenseRequestContact), organizationName: z.string().nullable().optional(), organizationUnit: z.string().nullable().optional(), licenseDeliveryMethod: licenseDeliveryMethod, localCertificate: localInstanceCertificate, localPublicKey: localInstancePublicKey, certificateSigningRequest: licenseRequestCertificateSigningRequest.nullable(), licenseRecipient: licenseRecipientPublicKey.nullable()}); export const licenseRequestSignature = z.object({algorithm: z.string(), keyId: z.string().nullable().optional(), signatureBase64: z.string()}); export const licenseRequestResponse = z.object({request: licenseRequestPayload, requestJson: z.string(), signature: licenseRequestSignature, artifactDigest: z.string().nullable().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 }; /** * Generate a signed local license request * * POST /license-request/generate */ export const createApiLicenseRequestGenerate = async (args: CreateApiLicenseRequestGenerateArgs, options?: CreateApiLicenseRequestGenerateOptions) => { const {baseUrl = "https://api.example.com/api/platform/setup/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(/\/+$/, '')}/license-request/generate${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(licenseRequestResponse, await res.json()) };