import {z} from 'zod' export type BusinessName = string; export type BusinessCreateUpdateOrgId = number; export type Groups = Array; export type BusinessReach = 'LOCAL' | 'GLOBAL'; export type BusinessCountry = string; export type BusinessZipcode = string | null; export type RequiredBusinessCity = string; export type BusinessRegion = string | null; export type BusinessCategories = Array; export type BusinessCode = string; export type BusinessStatus = 'open' | 'closed' | 'closed_temporarily'; export type BusinessOpeningDate = unknown; export type AddressFull = string | null; export type Address2 = string | null; export type BusinessLatitude = number; export type BusinessLongitude = number; export type BusinessWebsite = string; export type BusinessFacebookPage = string; export type BusinessTwitter = string; export type BusinessShortDescription = string; export type BusinessLongDescription = string; export type TimeWindow = string; export type Day = Array; export type OpenHours = {monday: Day, tuesday: Day, wednesday: Day, thursday: Day, friday: Day, saturday: Day, sunday: Day}; export type SpecificHours = {open: Array<{starts_at?: string | undefined, ends_at?: string | undefined, open_hours?: Array | undefined}>, close: Array<{starts_at?: string | undefined, ends_at?: string | undefined}>}; export type Contact = {name?: string | undefined, email?: string | undefined, phone_numbers?: Array | undefined, fax?: string | undefined}; export type BusinessContactsCreate = Array; export type Photos = {LOGO?: string | undefined, primary?: string | undefined, secondary?: Array | undefined}; export type BusinessNationalIdentificationNumber = string; export type CustomFieldsUpdate = {[key: string]: unknown}; export type CreateApiBusinessArgs = {body: {name: BusinessName, org_id?: BusinessCreateUpdateOrgId | undefined, groups?: Groups | undefined, reach?: BusinessReach | undefined, country: BusinessCountry, zipcode?: BusinessZipcode | undefined, city?: RequiredBusinessCity | undefined, region?: BusinessRegion | undefined, categories: BusinessCategories, code?: BusinessCode | undefined, status?: BusinessStatus | undefined, opening_date?: BusinessOpeningDate | undefined, address_full?: AddressFull | undefined, address2?: Address2 | undefined, lat?: BusinessLatitude | undefined, long?: BusinessLongitude | undefined, website_url?: BusinessWebsite | undefined, facebook_url?: BusinessFacebookPage | undefined, twitter_url?: BusinessTwitter | undefined, description_short?: BusinessShortDescription | undefined, description_long?: BusinessLongDescription | undefined, open_hours?: OpenHours | undefined, specific_hours?: SpecificHours | undefined, contacts: BusinessContactsCreate, photos?: Photos | undefined, national_identification_number?: BusinessNationalIdentificationNumber | undefined, custom_fields?: CustomFieldsUpdate | undefined}}; export type CreateApiBusinessOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const requestStatus = z.literal("success"); export const businessId = z.string(); export const createApiBusinessResponse = z.object({status: requestStatus.optional(), id: businessId.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 business * * POST /business */ export const createApiBusiness = async (args: CreateApiBusinessArgs, options?: CreateApiBusinessOptions) => { const {baseUrl = "https://api.partoo.co/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/business${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(createApiBusinessResponse, await res.json()) };