import {z} from 'zod' export type CreateApiV1OrdersSendArgs = {authorization: string, 'login-token': string, 'user-id': string, body: {employeeId: string, employerId: string, servicesIds: Array, providersIds: Array<{serviceId?: string | undefined, providerId: string}>, quantities?: {[key: string]: number} | undefined, providerId?: string | undefined, brandId?: string | undefined, dueDate?: string | undefined, dueDates?: Array | undefined, providerCreated?: boolean | undefined, metadata?: {[key: string]: unknown} | undefined, priority?: ('normal' | 'high') | undefined}}; export type CreateApiV1OrdersSendOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1OrdersSendResponse = z.union([z.object({success: z.literal(true), orderId: z.string(), orderNumber: z.string(), message: z.string().optional(), unavailableServices: z.array(z.object({serviceId: z.string(), serviceName: z.string().optional(), reason: z.string()})).optional(), partialSuccess: z.boolean().optional()}), z.object({success: z.literal(true), status: z.literal("split"), message: z.string().optional(), orderResults: z.array(z.object({orderId: z.string(), orderNumber: z.string(), providerId: z.string()})), unavailableServices: z.array(z.object({serviceId: z.string(), serviceName: z.string().optional(), reason: z.string()})).optional(), partialSuccess: z.boolean().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 }; /** * Send Order for Employee * * POST /v1/orders/send */ export const createApiV1OrdersSend = async (args: CreateApiV1OrdersSendArgs, options?: CreateApiV1OrdersSendOptions) => { const {baseUrl = "https://api.bluehive.com", 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 (args.authorization != null) headers.set("authorization", String(args.authorization)) if (args['login-token'] != null) headers.set("login-token", String(args['login-token'])) if (args['user-id'] != null) headers.set("user-id", String(args['user-id'])) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/orders/send${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(createApiV1OrdersSendResponse, await res.json()) };