import {z} from 'zod' export type GetApiV1ApplicationsApplicationIdSessionsSessionIdArgs = {applicationId: number, sessionId: number}; export type GetApiV1ApplicationsApplicationIdSessionsSessionIdOptions = {baseUrl?: string | undefined, managerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const product = z.object({name: z.string()}); export const additionalCost = z.object({price: z.number()}); export const priceDetail = z.object({price: z.number().optional(), adjustmentContextId: z.string().optional(), adjustmentReferenceId: z.string().optional(), adjustmentEffectiveFrom: z.string().optional(), adjustmentEffectiveUntil: z.string().optional()}); export const cartItem = z.object({name: z.string().optional(), sku: z.string().min(1), quantity: z.number().int().gte(1), returnedQuantity: z.number().int().optional(), remainingQuantity: z.number().int().optional(), price: z.number().optional(), category: z.string().optional(), product: product.optional(), weight: z.number().optional(), height: z.number().optional(), width: z.number().optional(), length: z.number().optional(), position: z.number().optional(), attributes: z.object({}).optional(), additionalCosts: z.record(z.string(), additionalCost).optional(), catalogItemID: z.number().int().optional(), selectedPriceType: z.string().optional(), adjustmentReferenceId: z.string().optional(), adjustmentEffectiveFrom: z.string().optional(), adjustmentEffectiveUntil: z.string().optional(), prices: z.record(z.string(), priceDetail).optional()}); export const applicationSession = z.object({id: z.number().int(), created: z.string(), integrationId: z.string().max(1000), storeIntegrationId: z.string().min(1).max(1000).optional(), applicationId: z.number().int(), profileId: z.number().int().optional(), profileintegrationid: z.string().max(1000).optional(), coupon: z.string(), referral: z.string(), state: z.enum(["open", "closed", "partially_returned", "cancelled"]), cartItems: z.array(cartItem), discounts: z.record(z.string(), z.number()), totalDiscounts: z.number(), total: z.number(), attributes: z.object({}).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 Application session * * GET /v1/applications/{applicationId}/sessions/{sessionId} * * Also accepts auth: management_key */ export const getApiV1ApplicationsApplicationIdSessionsSessionId = async (args: GetApiV1ApplicationsApplicationIdSessionsSessionIdArgs, options?: GetApiV1ApplicationsApplicationIdSessionsSessionIdOptions) => { const {baseUrl = "https://yourbaseurl.talon.one", fetch: fetchFn = fetch, managerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (managerAuth !== undefined) headers.set("Authorization", managerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/applications/${encodeURIComponent(String(args.applicationId))}/sessions/${encodeURIComponent(String(args.sessionId))}${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(applicationSession, await res.json()) };