import {z} from 'zod' export type GetApiSetupV1CustomersArgs = {locationId?: string | undefined, groupId?: string | undefined, email?: string | undefined, lastname?: string | undefined, deleted?: boolean | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiSetupV1CustomersOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const contactViewModel = z.object({phoneType: z.string().nullable().optional(), homePhone: z.string().nullable().optional(), mobilePhone: z.string().nullable().optional(), businessPhone: z.string().nullable().optional(), businessPhoneExt: z.string().nullable().optional(), skypeUsername: z.string().nullable().optional(), conferenceInfo: z.string().nullable().optional()}); export const addressViewModel = z.object({addressLine1: z.string().nullable().optional(), addressLine2: z.string().nullable().optional(), city: z.string().nullable().optional(), state: z.string().nullable().optional(), country: z.string().nullable().optional(), postalCode: z.string().nullable().optional()}); export const customFieldViewModel = z.object({name: z.string().nullable().optional(), value: z.string().nullable().optional()}); export const customerViewModel = z.object({object: z.string().nullable().optional(), id: z.string().nullable().optional(), locationId: z.string().nullable().optional(), businessName: z.string().nullable().optional(), email: z.string().nullable().optional(), name: z.string().nullable().optional(), lastname: z.string().nullable().optional(), firstname: z.string().nullable().optional(), companyName: z.string().nullable().optional(), gender: z.string().nullable().optional(), birthdate: z.string().nullable().optional(), lastVisitDate: z.string().nullable().optional(), groupId: z.string().nullable().optional(), resourceId: z.string().nullable().optional(), createdOn: z.string().nullable().optional(), createdBy: z.string().nullable().optional(), registeredBy: z.string().nullable().optional(), verifiedBy: z.string().nullable().optional(), modifiedBy: z.string().nullable().optional(), modifiedOn: z.string().nullable().optional(), verificationDate: z.string().nullable().optional(), registrationDate: z.string().nullable().optional(), emailInfo: z.boolean().nullable().optional(), emailPromotion: z.boolean().nullable().optional(), disabled: z.boolean().nullable().optional(), inviteEmailSent: z.string().nullable().optional(), welcomeEmailSent: z.string().nullable().optional(), deletedStatus: z.boolean().optional(), deletedTime: z.string().nullable().optional(), notificationType: z.string().nullable().optional(), subscriptionId: z.string().nullable().optional(), latitude: z.string().nullable().optional(), longitude: z.string().nullable().optional(), stripeCustomerId: z.string().nullable().optional(), contact: contactViewModel.optional(), address: addressViewModel.optional(), customFields: z.record(z.string(), customFieldViewModel).nullable().optional()}); export const customerListViewModel = z.object({object: z.string().nullable().optional(), url: z.string().nullable().optional(), hasMore: z.boolean().optional(), count: z.number().int().optional(), total: z.number().int().optional(), data: z.array(customerViewModel).nullable().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 }; /** * List Customers * * GET /setup/v1/customers */ export const getApiSetupV1Customers = async (args: GetApiSetupV1CustomersArgs, options?: GetApiSetupV1CustomersOptions) => { const {baseUrl = "https://sandbox-api.onsched.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.locationId != null) query.append("locationId", String(args.locationId)) if (args.groupId != null) query.append("groupId", String(args.groupId)) if (args.email != null) query.append("email", String(args.email)) if (args.lastname != null) query.append("lastname", String(args.lastname)) if (args.deleted != null) query.append("deleted", String(args.deleted)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/setup/v1/customers${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(customerListViewModel, await res.json()) };