import {z} from 'zod' export type SupplierState = 'Active' | 'Archived'; export type GetApiCleemyProcurementApiSuppliersArgs = {state?: SupplierState | undefined, 'search.terms'?: string | undefined, 'fields.root'?: 'count' | undefined, page?: number | undefined, limit?: number | undefined}; export type GetApiCleemyProcurementApiSuppliersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const supplierState = z.enum(["Active", "Archived"]); export const timeUnit = z.enum(["Day", "Week", "Month", "Year"]); export const paymentMethod = z.enum(["BankTransfer", "PhysicalCard", "Check", "DirectDebit", "SingleUseCard"]); export const costCenterReference = z.object({id: z.number().int().optional(), name: z.string().optional(), code: z.string().optional(), href: z.string().optional()}); export const establishmentReference = z.object({id: z.number().int().optional(), name: z.string().optional(), href: z.string().optional()}); export const supplier = z.object({id: z.number().int().optional(), name: z.string().optional(), state: supplierState.optional(), territory: z.object({id: z.number().int().optional(), name: z.string().optional(), href: z.string().optional()}).optional(), territoryId: z.string().optional(), legalRegistration: z.object({identifier: z.string().nullable().optional(), vatNumber: z.string().nullable().optional()}).optional(), contacts: z.array(z.object({firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), email: z.string().nullable().optional(), phone: z.string().nullable().optional()})).optional(), paymentTerm: z.object({value: z.number().int().optional(), unit: timeUnit.optional()}).nullable().optional(), bankAccount: z.object({iban: z.string().nullable().optional(), bic: z.string().nullable().optional()}).nullable().optional(), preferredNature: z.object({id: z.number().int().optional(), name: z.string().optional(), href: z.string().optional()}).nullable().optional(), preferredNatureId: z.number().int().nullable().optional(), preferredPaymentMethod: paymentMethod.nullable().optional(), preferredCostCenter: costCenterReference.optional(), preferredCostCenterId: z.number().int().nullable().optional(), establishments: z.array(establishmentReference).optional(), flag: z.enum(["Unflagged", "Flagged"]).optional()}); export const getApiCleemyProcurementApiSuppliersResponse = z.object({items: z.array(supplier), prev: z.union([z.string(), z.number().int()]).nullable().optional(), next: z.union([z.string(), z.number().int()]).nullable().optional(), count: z.number().int().gte(0).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 }; /** * List Suppliers * * GET /cleemy-procurement/api/suppliers */ export const getApiCleemyProcurementApiSuppliers = async (args: GetApiCleemyProcurementApiSuppliersArgs, options?: GetApiCleemyProcurementApiSuppliersOptions) => { const {baseUrl = "https://example.ilucca.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.state != null) query.append("state", String(args.state)) if (args['search.terms'] != null) query.append("search.terms", String(args['search.terms'])) if (args['fields.root'] != null) query.append("fields.root", String(args['fields.root'])) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/cleemy-procurement/api/suppliers${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(getApiCleemyProcurementApiSuppliersResponse, await res.json()) };