import {z} from 'zod' export type PublicFacilityAddressRequest = {street1: string, street2?: (string | null) | undefined, city: string, stateOrProvince?: (string | null) | undefined, postalCode: string, country?: string | undefined}; export type PublicFacilityRequest = {externalId: string, name: string, address?: (PublicFacilityAddressRequest | null) | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined, phoneExtension?: (string | null) | undefined, notes?: (string | null) | undefined}; export type BulkUpsertFacilitiesRequest = {facilities: Array}; export type CreateApiV2FacilitiesBulkArgs = {body: BulkUpsertFacilitiesRequest}; export type CreateApiV2FacilitiesBulkOptions = {baseUrl?: string | undefined, httpBasic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const publicFacilityResultStatus = z.enum(["SUCCESS", "FAILED"]); export const publicFacilityResult = z.object({index: z.number().int().optional(), status: publicFacilityResultStatus.optional(), externalId: z.string().nullable().optional(), facilityId: z.string().nullable().optional(), error: z.string().nullable().optional()}); export const bulkUpsertFacilitiesResponse = z.object({total: z.number().int().optional(), successCount: z.number().int().optional(), failureCount: z.number().int().optional(), results: z.array(publicFacilityResult).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 }; /** * Bulk create or update facilities * * POST /v2/facilities/bulk */ export const createApiV2FacilitiesBulk = async (args: CreateApiV2FacilitiesBulkArgs, options?: CreateApiV2FacilitiesBulkOptions) => { const {baseUrl = "https://api.prod.goaugment.com", fetch: fetchFn = fetch, httpBasic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (httpBasic !== undefined) headers.set('authorization', `Basic ${btoa(`${httpBasic.username}:${httpBasic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/facilities/bulk${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(bulkUpsertFacilitiesResponse, await res.json()) };