import {z} from 'zod' export type GetApiStoresArgs = {id?: number | undefined, storeIdentifier?: string | undefined, name?: string | undefined, sort?: string | undefined, page?: number | undefined, pageSize?: number | undefined}; export type GetApiStoresOptions = {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 serviceChannelServicesMessagingLocationGetLocationsWithTotalResponse = z.object({Locations: z.array(serviceChannelServicesMessagingLocationLocation).optional(), TotalCount: z.number().int().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 }; /** * Retrieve stores * * GET /stores */ export const getApiStores = async (args: GetApiStoresArgs, options?: GetApiStoresOptions) => { 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.id != null) query.append("id", String(args.id)) if (args.storeIdentifier != null) query.append("storeIdentifier", String(args.storeIdentifier)) if (args.name != null) query.append("name", String(args.name)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/stores${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(serviceChannelServicesMessagingLocationGetLocationsWithTotalResponse, await res.json()) };