import {z} from 'zod' export type OrderPrimaryKeyInput = string | number; export type SearchWarehouseRequest = {destination_id: number, order_id?: OrderPrimaryKeyInput | undefined, store_id: number, variants: Array<{qty: number, variant_id: number}>}; export type CreateApiV3ShippingCostsSearchWarehouseArgs = {body: SearchWarehouseRequest}; export type CreateApiV3ShippingCostsSearchWarehouseOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const location = z.object({id: z.number().int(), subdistrict_name: z.string(), city_name: z.string(), province_name: z.string(), kelurahan_name: z.string().nullable(), display: z.string()}); export const searchWarehouseResult = z.object({destination: location.optional(), distance_km: z.number().optional(), warehouse: z.object({business: z.object({id: z.number().int().optional(), username: z.string().optional()}).optional(), id: z.number().int().optional(), lincah_origin_id: z.string().optional(), mengantar_origin_id: z.string().optional(), name: z.string().optional(), unique_id: z.string().optional(), warehouse_address: z.object({address: z.string().optional(), city: z.string().optional(), id: z.number().int().optional(), kelurahan: z.string().nullable().optional(), location: z.number().int().optional(), postal_code: z.string().optional(), province: z.string().optional(), subdistrict: z.string().optional()}).optional()}).optional(), warehouse_partner: z.object({id: z.number().int().optional(), ka_origin_id: z.string().optional(), lincah_origin_id: z.string().optional(), mengantar_origin_id: z.string().optional(), partner_id: z.number().int().optional()}).optional()}); export const searchWarehousesResponseBody = z.object({data: z.array(searchWarehouseResult), is_paginated: z.literal(false)}); 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 }; /** * Search warehouses * * POST /v3/shipping-costs/search-warehouse * * Also accepts auth: apiKeyAuth */ export const createApiV3ShippingCostsSearchWarehouse = async (args: CreateApiV3ShippingCostsSearchWarehouseArgs, options?: CreateApiV3ShippingCostsSearchWarehouseOptions) => { const {baseUrl = "https://api.scalev.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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/shipping-costs/search-warehouse${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(searchWarehousesResponseBody, await res.json()) };