import {z} from 'zod' export type GetApiV1OrdersIdArgs = {id: string, include?: string | undefined, 'fields[orders]'?: string | undefined}; export type GetApiV1OrdersIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const ordersAttributes = z.object({order_number: z.number().int().optional(), currency_code: z.string().optional(), fulfilled_at: z.string().nullable().optional(), created_at: z.string().optional(), total_price_in_cents: z.number().int().optional(), subtotal_in_cents: z.number().int().optional(), sales_tax_amount_in_cents: z.number().int().optional(), discount_amount_in_cents: z.number().int().optional(), formatted_total_price: z.string().optional(), formatted_subtotal: z.string().optional(), currency_symbol: z.string().optional()}); export const resourceIdentifier = z.object({id: z.string(), type: z.string()}); export const resourceIdentifiers = z.array(resourceIdentifier); export const ordersShowResponse = z.object({data: z.object({id: z.string().optional(), type: z.literal("orders").optional(), attributes: ordersAttributes.optional(), relationships: z.object({site: z.object({data: resourceIdentifier.optional()}).optional(), customer: z.object({data: resourceIdentifier.optional()}).optional(), order_items: z.object({data: resourceIdentifiers.optional()}).optional()}).optional()}).optional(), links: z.object({self: z.string().optional(), current: z.string().optional()}).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 }; /** * Order details * * GET /v1/orders/{id} */ export const getApiV1OrdersId = async (args: GetApiV1OrdersIdArgs, options?: GetApiV1OrdersIdOptions) => { const {baseUrl = "https://api.kajabi.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.include != null) query.append("include", String(args.include)) if (args['fields[orders]'] != null) query.append("fields[orders]", String(args['fields[orders]'])) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/orders/${encodeURIComponent(String(args.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(ordersShowResponse, await res.json()) };