import {z} from 'zod' export type GetApiApiV4JourneyJourneyIdSalesArgs = {journey_id: string}; export type GetApiApiV4JourneyJourneyIdSalesOptions = {baseUrl?: string | undefined, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const contact = z.object({mobile_cc: z.string().optional(), mobile_num: z.string().optional(), name: z.string().optional(), stop_action: z.enum(["pickup", "dropoff"]).optional()}); export const meetingPoint = z.object({id: z.string(), loc: z.array(z.number()), name: z.string()}); export const stopResponse = z.object({addr: z.string().optional(), city: z.string().optional(), contact: contact.optional(), contacts: z.array(contact).optional(), country: z.string().optional(), hit_at: z.string().optional(), id: z.string().optional(), instr: z.string().optional(), loc: z.array(z.number()), meeting_point: meetingPoint.optional(), name: z.string().optional(), num: z.string().optional(), postal_code: z.string().optional()}); export const typeObject = z.union([z.object({charge_code: z.string().optional(), country: z.string().optional(), description: z.string().optional(), dropoff: stopResponse.optional(), end_at: z.string().optional(), id: z.string().optional(), pickup: stopResponse.optional(), region: z.string().optional(), start_at: z.string().optional()}), z.object({description: z.string().optional(), rectified_sale_id: z.string().optional()})]); export const concept = z.object({type: z.string().optional(), type_object: typeObject.optional()}); export const priceDetail = z.object({discount: z.number().int().optional(), tax_rate: z.number().int().optional(), tax_type: z.string().optional(), total: z.number().int().optional()}); export const sale = z.object({code: z.string().optional(), concept: concept.optional(), currency: z.string().optional(), invoice_date: z.string().optional(), price_details: priceDetail.optional()}); export const getApiApiV4JourneyJourneyIdSalesResponse = z.array(sale); 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 journey's sales * * GET /api/v4/journey/{journey_id}/sales */ export const getApiApiV4JourneyJourneyIdSales = async (args: GetApiApiV4JourneyJourneyIdSalesArgs, options?: GetApiApiV4JourneyJourneyIdSalesOptions) => { const {baseUrl = "https://cabify-sandbox.com", fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/journey/${encodeURIComponent(String(args.journey_id))}/sales${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(getApiApiV4JourneyJourneyIdSalesResponse, await res.json()) };