import {z} from 'zod' export type Customer = {name: string, dunsNumber: string, synopsysCustomerId: string, vendorCustomerInfo?: Record | undefined}; export type LicenseRequestItem = {title: string, aux?: Record | undefined, qty?: number | undefined}; export type LicenseHostInfo = {id: string, name: string}; export type ProvideLicenseRequestDto = {customer: Customer, licenseRequests?: Array | undefined, host: LicenseHostInfo}; export type CreateApiVendorApiGetLicensesArgs = {'x-api-key'?: string | undefined, body: ProvideLicenseRequestDto}; export type CreateApiVendorApiGetLicensesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const provideSyncDataItemResponse = z.object({fileName: z.string(), fileType: z.string(), data: z.string(), downloadType: z.literal("Sync")}); export const apiBase = z.object({url: z.string(), header: z.object({}).optional(), staticBody: z.object({}).optional()}); export const provideAsyncDataItemResponse = z.object({fileName: z.string(), fileType: z.string(), downloadApi: apiBase, pollStartDateTime: z.string().optional(), pollIntervalSec: z.number().optional(), downloadType: z.literal("Sync")}); export const provideLicenseResponse = z.object({files: z.array(z.union([provideSyncDataItemResponse, provideAsyncDataItemResponse]))}); 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 }; /** * POST /vendor-api/get-licenses */ export const createApiVendorApiGetLicenses = async (args: CreateApiVendorApiGetLicensesArgs, options?: CreateApiVendorApiGetLicensesOptions) => { const {baseUrl = "http://localhost:2000", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/vendor-api/get-licenses${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(provideLicenseResponse, await res.json()) };