import {z} from 'zod' export type GetApiApiAdminTenantsArgs = {skip?: number | undefined, limit?: number | undefined}; export type GetApiApiAdminTenantsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const tenantPlan = z.enum(["starter", "pro", "enterprise"]); export const enrollmentState = z.enum(["not_required", "not_enrolled", "pending", "enrolled", "failed"]); export const tenantResponse = z.object({name: z.string(), description: z.string().nullable().optional(), plan: tenantPlan.optional(), credit_balance: z.number().int().optional(), peppol_ids: z.array(z.string()).nullable().optional(), ibans: z.array(z.string()).nullable().optional(), company_number: z.string().nullable().optional(), company_tax_id: z.string().nullable().optional(), company_name: z.string().nullable().optional(), company_address: z.string().nullable().optional(), company_zip: z.string().nullable().optional(), company_city: z.string().nullable().optional(), company_country: z.string().nullable().optional(), company_email: z.string().nullable().optional(), smp_registration: z.boolean().nullable().optional(), smp_registration_date: z.string().nullable().optional(), bcc_recipient_email: z.string().nullable().optional(), id: z.string(), created_at: z.string(), updated_at: z.string(), enrollment_state: enrollmentState.optional(), status_message: z.string().nullable().optional(), route_override: z.string().nullable().optional()}); export const tenantListResponse = z.object({tenants: z.array(tenantResponse), total: z.number().int()}); 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 Tenants * * GET /api/admin/tenants */ export const getApiApiAdminTenants = async (args: GetApiApiAdminTenantsArgs, options?: GetApiApiAdminTenantsOptions) => { const {baseUrl = "https://api.e-invoice.be", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.skip != null) query.append("skip", String(args.skip)) if (args.limit != null) query.append("limit", String(args.limit)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/admin/tenants${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(tenantListResponse, await res.json()) };