import {z} from 'zod' export type GetApiApiLookupArgs = {peppol_id: string}; export type GetApiApiLookupOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const queryMetadata = z.object({identifierScheme: z.string(), identifierValue: z.string(), smlDomain: z.string(), timestamp: z.string(), version: z.string()}); export const dnsRecord = z.object({ip: z.string()}); export const dnsInfo = z.object({status: z.string(), smpHostname: z.string().nullable().optional(), smlHostname: z.string(), dnsRecords: z.array(dnsRecord), error: z.string().nullable().optional(), lookupMethod: z.string().nullable().optional()}); export const supportedDocumentType = z.object({scheme: z.string(), value: z.string()}); export const processIdentifier = z.object({scheme: z.string(), value: z.string()}); export const certificate = z.object({status: z.string(), details: z.record(z.string(), z.unknown()).nullable().optional(), error: z.string().nullable().optional()}); export const processEndpoint = z.object({transportProfile: z.string(), address: z.string(), serviceActivationDate: z.string().nullable().optional(), serviceExpirationDate: z.string().nullable().optional(), serviceDescription: z.string().nullable().optional(), technicalContactUrl: z.string().nullable().optional(), technicalInformationUrl: z.string().nullable().optional(), certificate: certificate.nullable().optional()}); export const process = z.object({processId: processIdentifier, endpoints: z.array(processEndpoint)}); export const endpoint = z.object({status: z.string(), url: z.string(), documentTypes: z.array(supportedDocumentType), processes: z.array(process).nullable().optional(), error: z.string().nullable().optional()}); export const serviceMetadata = z.object({status: z.string(), queryTimeMs: z.number(), endpoints: z.array(endpoint), error: z.string().nullable().optional()}); export const businessEntity = z.object({name: z.string().nullable().optional(), countryCode: z.string().nullable().optional(), registrationDate: z.string().nullable().optional(), additionalInformation: z.array(z.string()).nullable().optional()}); export const appSchemasPeppolIdBusinessCard = z.object({status: z.string(), entities: z.array(businessEntity), queryTimeMs: z.number(), error: z.string().nullable().optional()}); export const peppolIdLookupResponse = z.object({queryMetadata: queryMetadata, status: z.string(), errors: z.array(z.string()), dnsInfo: dnsInfo, serviceMetadata: serviceMetadata, businessCard: appSchemasPeppolIdBusinessCard, certificates: z.array(certificate), executionTimeMs: 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 }; /** * Lookup Peppol ID * * GET /api/lookup */ export const getApiApiLookup = async (args: GetApiApiLookupArgs, options?: GetApiApiLookupOptions) => { const {baseUrl = "https://api.e-invoice.be", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.peppol_id != null) query.append("peppol_id", String(args.peppol_id)) const url = `${baseUrl.replace(/\/+$/, '')}/api/lookup${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(peppolIdLookupResponse, await res.json()) };