import {z} from 'zod' export type GetApiCustomersCustomerIdArgs = {'Accept-Language'?: string | undefined, customer_id: string, customer_short_salutation?: boolean | undefined}; export type GetApiCustomersCustomerIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const address = z.object({address1: z.string().optional(), address2: z.string().optional(), address3: z.string().optional(), address4: z.string().optional(), address5: z.string().optional(), address6: z.string().optional(), address7: z.string().optional(), zip: z.string().optional(), city: z.string().optional(), country_code: z.string().optional()}); export const customAgreements = z.object({agreement_id: z.string(), agreement_value: z.union([z.literal(0), z.literal(1), z.literal(2)])}); export const agreement = z.object({agreement_gtc: z.union([z.literal(0), z.literal(1), z.literal(2)]).optional(), agreement_profiling: z.union([z.literal(0), z.literal(1), z.literal(2)]).optional(), agreement_marketing_communication: z.union([z.literal(0), z.literal(1), z.literal(2)]).optional(), custom_agreements: z.array(customAgreements).optional()}); export const consents = z.object({consent_id: z.string(), consent_value: z.union([z.literal(0), z.literal(1), z.literal(2)]), last_change: z.string().optional()}); export const personalInformation = z.object({salutation: z.string().optional(), gender: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]).nullable().optional(), first_name: z.string().optional(), last_name: z.string().optional(), pre_nominals: z.string().optional(), post_nominals: z.string().optional(), birthdate: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), language_id: z.string(), store_id: z.string().optional(), photo_url: z.string().optional(), address: address.optional(), agreement: agreement.optional(), consents: z.array(consents).optional()}); export const customer = z.object({customer_id: z.string().optional(), personal_information: personalInformation, doi_email: z.string().optional(), doi_phone: z.string().optional(), last_change: z.string().optional(), state: z.union([z.literal(0), z.literal(1), z.literal(2)]).optional()}); export const getApiCustomersCustomerIdResponse = z.object({data: customer.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 }; /** * Get information about a customer * * GET /customers/{customer_id} * * Also accepts auth: basicAuth */ export const getApiCustomersCustomerId = async (args: GetApiCustomersCustomerIdArgs, options?: GetApiCustomersCustomerIdOptions) => { 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('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args.customer_short_salutation != null) query.append("customer_short_salutation", String(args.customer_short_salutation)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.customer_id))}${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(getApiCustomersCustomerIdResponse, await res.json()) };