import {z} from 'zod' export type GetApiApiAdminTenantsTenantIdPeppolArgs = {tenant_id: string, verify?: boolean | undefined}; export type GetApiApiAdminTenantsTenantIdPeppolOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const peppolRegistrationStatus = z.enum(["not_registered", "e-invoice", "other"]); export const serviceGroupInfo = z.object({participant_identifier: z.string().nullable().optional(), participant_scheme: z.string().nullable().optional(), service_metadata_references: z.array(z.record(z.string(), z.unknown())).nullable().optional()}); export const businessCardInfo = z.object({participant_identifier: z.string().nullable().optional(), participant_scheme: z.string().nullable().optional(), business_entity: z.record(z.string(), z.unknown()).nullable().optional()}); export const documentTypeInfo = z.object({document_type_code: z.string(), process_identifier: z.string(), endpoints: z.array(z.record(z.string(), z.unknown())).nullable().optional()}); export const sMPStatus = z.object({service_group: serviceGroupInfo.nullable().optional(), business_card: businessCardInfo.nullable().optional(), document_types: z.array(documentTypeInfo).nullable().optional()}); export const peppolPublicationState = z.enum(["not_published", "pending", "published"]); export const peppolIdPublication = z.object({peppol_id: z.string(), state: peppolPublicationState.optional(), latest_event: z.string().nullable().optional(), source: z.string().nullable().optional(), detail: z.string().nullable().optional(), observed_at: z.string().nullable().optional()}); export const peppolStatusResponse = z.object({peppol_id: z.string(), state: peppolRegistrationStatus.optional(), smp: sMPStatus, last_checked_at: z.string(), verified: z.boolean().optional(), publications: z.array(peppolIdPublication).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 }; /** * Get Tenant Peppol Status * * GET /api/admin/tenants/{tenant_id}/peppol/ */ export const getApiApiAdminTenantsTenantIdPeppol = async (args: GetApiApiAdminTenantsTenantIdPeppolArgs, options?: GetApiApiAdminTenantsTenantIdPeppolOptions) => { 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.verify != null) query.append("verify", String(args.verify)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/admin/tenants/${encodeURIComponent(String(args.tenant_id))}/peppol/${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(peppolStatusResponse, await res.json()) };