import {z} from 'zod' export type GetApiChamberofcommerceChamberOfCommerceNumberCompanyinformationArgs = {chamberOfCommerceNumber: string, Authorization: string, Accept: string}; export type GetApiChamberofcommerceChamberOfCommerceNumberCompanyinformationOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const version = z.object({obsoleteDate: z.string().nullable().optional(), resourceVersion: z.string().optional()}); export const messageWarning = z.array(z.object({type: z.string().optional(), description: z.string().optional(), id: z.string().nullable().optional(), code: z.number().int().optional(), properties: z.array(z.string()).optional()})); export const companyInformation = z.object({name: z.string().max(255).nullable().optional(), sbi: z.object({key: z.number().int().optional(), value: z.string().max(255).optional(), sbiCode: z.string().max(32).optional()}).nullable().optional(), legalForm: z.object({key: z.number().int().optional(), value: z.string().max(50).optional()}).nullable().optional(), website: z.string().max(255).nullable().optional(), address: z.object({street: z.string().max(24).optional(), city: z.string().max(24).optional(), houseNumber: z.number().int().optional(), houseNumberAddition: z.string().max(4).nullable().optional(), postalCode: z.string().max(9).optional(), country: z.object({key: z.number().int().optional(), value: z.string().max(50).optional(), isoCode: z.string().max(2).optional()}).optional()}).optional(), deviatingPostalAddress: z.object({street: z.string().max(24).nullable().optional(), city: z.string().max(24).optional(), houseNumber: z.number().int().nullable().optional(), houseNumberAddition: z.string().max(4).nullable().optional(), postalCode: z.string().min(1).max(9).optional(), country: z.object({key: z.number().int().optional(), value: z.string().max(50).optional(), isoCode: z.string().max(2).optional()}).optional(), poBox: z.number().int().nullable().optional()}).nullable().optional()}).nullable(); export const getApiChamberofcommerceChamberOfCommerceNumberCompanyinformationResponse = z.object({version: version.optional(), messages: messageWarning.optional(), content: companyInformation.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 }; /** * Acquire company information * * GET /chamberofcommerce/{chamberOfCommerceNumber}/companyinformation */ export const getApiChamberofcommerceChamberOfCommerceNumberCompanyinformation = async (args: GetApiChamberofcommerceChamberOfCommerceNumberCompanyinformationArgs, options?: GetApiChamberofcommerceChamberOfCommerceNumberCompanyinformationOptions) => { const {baseUrl = "https://api.loket.nl", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json;version=2018-01-01") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.Accept != null) headers.set("Accept", String(args.Accept)) const url = `${baseUrl.replace(/\/+$/, '')}/chamberofcommerce/${encodeURIComponent(String(args.chamberOfCommerceNumber))}/companyinformation${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(getApiChamberofcommerceChamberOfCommerceNumberCompanyinformationResponse, await res.json()) };