import {z} from 'zod' export type Address = {address1?: string | undefined, address2?: string | undefined, address3?: string | undefined, address4?: string | undefined, address5?: string | undefined, address6?: string | undefined, address7?: string | undefined, zip?: string | undefined, city?: string | undefined, country_code?: string | undefined}; export type GPSCoordinates = {gps_longitude: number, gps_latitude: number}; export type TimeInterval = {from: string, to: string}; export type OpeningDay = {day_id: string, time_intervals: Array}; export type Store = {store_id: string, store_code: string, name: string, store_address?: Address | undefined, contact_email?: string | undefined, manager_name?: string | undefined, phone_number?: string | undefined, partner_id: string, system_id: string, gps_coordinates?: GPSCoordinates | undefined, url_address?: string | undefined, opening?: Array | undefined, image_urls?: Array | undefined, visibility_for_customer?: (0 | 1) | undefined, short_description?: string | undefined, description?: string | undefined, registration_id?: string | undefined, last_change?: string | undefined, state?: (0 | 1 | 2) | undefined}; export type CreateApiStoresArgs = {'Accept-Language'?: string | undefined, body: {store: Store}}; export type CreateApiStoresOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiStoresResponse = z.object({data: z.object({store_id: z.string().optional()}).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 }; /** * Create a store * * POST /stores * * Also accepts auth: basicAuth */ export const createApiStores = async (args: CreateApiStoresArgs, options?: CreateApiStoresOptions) => { const {baseUrl = "https://sandbox.crmcarecloud.com/webservice/rest-api/enterprise-interface/v1.0", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/stores${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(createApiStoresResponse, await res.json()) };