import {z} from 'zod' export type GetApiV1LicensesArgs = {page?: number | undefined, pageSize?: number | undefined}; export type GetApiV1LicensesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const licensePublicDto = z.object({id: z.string(), key: z.string(), customerId: z.string(), projectId: z.string().nullable(), replacesId: z.string().nullable(), kind: z.enum(["PRODUCTION", "POC", "DEMO", "EVAL"]), status: z.enum(["ACTIVE", "REVOKED", "REPLACED"]), startsAt: z.string(), expiresAt: z.string(), createdAt: z.string()}); export const pagedLicensePublicDto = z.object({items: z.array(licensePublicDto), total: z.number(), page: z.number(), pageSize: z.number()}); 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 }; /** * List licenses for the caller customer (paginated) * * GET /v1/licenses */ export const getApiV1Licenses = async (args: GetApiV1LicensesArgs, options: GetApiV1LicensesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/licenses${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(pagedLicensePublicDto, await res.json()) };