import {z} from 'zod' export type GetApiOdataLocationsServiceForServiceRequestArgs = {$expand?: string | undefined, $select?: string | undefined, $filter?: string | undefined, $orderby?: string | undefined, $top?: number | undefined, $skip?: number | undefined, $count?: boolean | undefined}; export type GetApiOdataLocationsServiceForServiceRequestOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceChannelServicesMessagingLocationNoteLocationNote = z.object({Id: z.number().int().optional(), HeaderId: z.number().int().optional(), Value: z.string().optional(), Header: z.string().optional()}); export const serviceChannelServicesMessagingLocationLocation = z.object({Id: z.number().int().optional(), SubscriberId: z.number().int().optional(), SubscriberName: z.string().optional(), StoreId: z.string().optional(), Name: z.string().optional(), ShortName: z.string().optional(), Latitude: z.number().optional(), Longitude: z.number().optional(), Distance: z.number().optional(), Radius: z.number().optional(), LocationTypeId: z.number().int().optional(), Address1: z.string().optional(), Address2: z.string().optional(), City: z.string().optional(), State: z.string().optional(), StateId: z.number().int().optional(), Region: z.string().optional(), District: z.string().optional(), Zip: z.string().optional(), Country: z.string().optional(), CountryId: z.number().int().optional(), ClosedDate: z.string().optional(), OpenDate: z.string().optional(), Status: z.string().optional(), Email: z.string().optional(), FaxNumber: z.string().optional(), Phone: z.string().optional(), Contact: z.string().optional(), HasInventory: z.boolean().optional(), SquareValue: z.number().optional(), SquareMeasure: z.string().optional(), TimeZoneInfoId: z.number().int().optional(), TimeZoneShortDescription: z.string().optional(), TimeZoneMsName: z.string().optional(), TimeZoneIanaName: z.string().optional(), TypeLabel: z.string().optional(), Supplier: z.string().optional(), CountyId: z.number().int().optional(), Notes: z.array(serviceChannelServicesMessagingLocationNoteLocationNote).optional(), TimeShiftToEST: z.number().int().optional(), AddressTypeId: z.number().int().optional(), FormattedAddress: z.string().optional(), LocationFormattedAddressTail: z.string().optional(), IsInternational: z.boolean().optional(), Address: z.string().optional()}); export const getApiOdataLocationsServiceForServiceRequestResponse = z.array(serviceChannelServicesMessagingLocationLocation); 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 }; /** * Retrieve locations * * GET /odata/locations/Service.ForServiceRequest() */ export const getApiOdataLocationsServiceForServiceRequest = async (args: GetApiOdataLocationsServiceForServiceRequestArgs, options?: GetApiOdataLocationsServiceForServiceRequestOptions) => { const {baseUrl = "https://sb2api.servicechannel.com/v3", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.$expand != null) query.append("$expand", String(args.$expand)) if (args.$select != null) query.append("$select", String(args.$select)) if (args.$filter != null) query.append("$filter", String(args.$filter)) if (args.$orderby != null) query.append("$orderby", String(args.$orderby)) if (args.$top != null) query.append("$top", String(args.$top)) if (args.$skip != null) query.append("$skip", String(args.$skip)) if (args.$count != null) query.append("$count", String(args.$count)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/odata/locations/Service.ForServiceRequest()${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(getApiOdataLocationsServiceForServiceRequestResponse, await res.json()) };