import {z} from 'zod' export type GetApiApiCompaniesCompanyIdArgs = {companyId: string, includeLogo?: boolean | undefined}; export type GetApiApiCompaniesCompanyIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const commonAddressDto = z.object({name: z.string().min(1).max(200).nullable().optional(), address1: z.string().min(1).max(200), address2: z.string().min(1).max(200).nullable().optional(), address3: z.string().min(1).max(200).nullable().optional(), city: z.string().min(1).max(200), stateProvince: z.string().min(1).max(200).nullable().optional(), postalCode: z.string().min(1).max(200), country: z.string().min(1).max(200).nullable().optional(), phone: z.string().nullable().optional(), email: z.string().nullable().optional()}); export const companyInfoDto = z.object({id: z.string().min(24).max(24), isPrimary: z.boolean(), name: z.string().min(1).max(200), shipToAddress: commonAddressDto, remitToAddress: commonAddressDto, websiteUrl: z.string().nullable().optional(), phoneNumber: z.string().nullable().optional(), faxNumber: z.string().nullable().optional(), email: z.string().nullable().optional(), logoDataUrl: z.string().nullable().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 info for a specific company. * * GET /api/companies/{companyId} */ export const getApiApiCompaniesCompanyId = async (args: GetApiApiCompaniesCompanyIdArgs, options?: GetApiApiCompaniesCompanyIdOptions) => { const {baseUrl = "https://api.fulcrumpro.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includeLogo != null) query.append("includeLogo", String(args.includeLogo)) const url = `${baseUrl.replace(/\/+$/, '')}/api/companies/${encodeURIComponent(String(args.companyId))}${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(companyInfoDto, await res.json()) };