import {z} from 'zod' export type FacilityId = number; export type PatchApiFacilitiesPublicFacilityIdArgs = {facilityId: FacilityId, body: {address?: ({address1?: string | undefined, address2?: (string | null) | undefined, city?: string | undefined, country?: string | undefined, postal_code?: string | undefined, state?: string | undefined, erp_code?: string | undefined}) | undefined, attention?: string | undefined, name?: string | undefined, salesperson?: ({email?: string | undefined} | null) | undefined}}; export type PatchApiFacilitiesPublicFacilityIdOptions = {baseUrl?: string | undefined, appId?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({id: z.number().int().optional(), address1: z.string().max(250).optional(), address2: z.string().max(250).nullable().optional(), city: z.string().max(100).optional(), state: z.string().optional(), postal_code: z.string().optional(), country: z.string().max(3).optional(), erp_code: z.string().max(50).optional()}); export const salesPerson = z.object({uuid: z.string().optional(), first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), erp_code: z.string().optional()}); export const facility = z.object({account_id: z.number().int().optional(), address: address.optional(), attention: z.string().optional(), id: z.number().int().optional(), name: z.string().optional(), salesperson: salesPerson.optional()}).nullable(); 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 }; /** * Update a Facility * * PATCH /facilities/public/{facilityId} */ export const patchApiFacilitiesPublicFacilityId = async (args: PatchApiFacilitiesPublicFacilityIdArgs, options?: PatchApiFacilitiesPublicFacilityIdOptions) => { const {baseUrl = "https://api.paperlessparts.com/v1", fetch: fetchFn = fetch, appId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (appId !== undefined) headers.set("Authorization", appId) const url = `${baseUrl.replace(/\/+$/, '')}/facilities/public/${encodeURIComponent(String(args.facilityId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(facility, await res.json()) };