import {z} from 'zod' export type OrderLineItemMutationRequest = {line_item_id?: (string | null) | undefined, item_id?: (string | null) | undefined, item_external_id?: (string | null) | undefined, custom_item_name?: (string | null) | undefined, quantity?: (number | null) | undefined, unit_price?: (number | null) | undefined, tax_rate?: (number | null) | undefined, tax_type?: (string | null) | undefined, total_price?: (number | null) | undefined, total_price_without_tax?: (number | null) | undefined, currency?: (string | null) | undefined, row_type?: (string | null) | undefined, section_label?: (string | null) | undefined, section_type?: (string | null) | undefined, custom_fields?: {[key: string]: unknown} | undefined}; export type OrderBulkOrderRequest = {external_id: string, items?: (Array | null) | undefined, line_items?: (Array | null) | undefined, company_external_id?: (string | null) | undefined, company_id?: (string | null) | undefined, delivery_status?: (string | null) | undefined, order_at?: (string | null) | undefined, status?: (string | null) | undefined, currency?: (string | null) | undefined}; export type OrderBulkCreateRequest = {orders?: Array | undefined, create_missing_items?: boolean | undefined, trigger_workflows?: boolean | undefined}; export type CreateApiV2PublicOrdersBulkArgs = {workspace_id?: (string | null) | undefined, 'X-Workspace-Code'?: string | undefined, body: OrderBulkCreateRequest}; export type CreateApiV2PublicOrdersBulkOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationMeta = z.object({page: z.number().int().gte(1), page_size: z.number().int().gte(1).lte(500), total: z.number().int().gte(0), next_cursor: z.string().nullable().optional()}); export const toastMessageI18n = z.object({en: z.string(), ja: z.string()}); export const toastMessage = z.object({variant: z.enum(["success", "error", "info", "warning"]), code: z.string(), message: z.string(), i18n: toastMessageI18n}); export const envelopeMeta = z.object({ctx_id: z.string(), pagination: paginationMeta.optional(), toast: toastMessage.optional()}); export const bulkCreatePublicOrdersApiV2PublicOrdersBulkPost200Envelope = z.object({success: z.literal(true), data: z.record(z.string(), z.unknown()), meta: envelopeMeta}); 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 }; /** * Bulk Create Public Orders * * POST /v2/public/orders/bulk */ export const createApiV2PublicOrdersBulk = async (args: CreateApiV2PublicOrdersBulkArgs, options?: CreateApiV2PublicOrdersBulkOptions) => { const {baseUrl = "https://api.sanka.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.workspace_id != null) query.append("workspace_id", String(args.workspace_id)) if (args['X-Workspace-Code'] != null) headers.set("X-Workspace-Code", String(args['X-Workspace-Code'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/public/orders/bulk${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(bulkCreatePublicOrdersApiV2PublicOrdersBulkPost200Envelope, await res.json()) };