import {z} from 'zod' export type GetApiV1IntegrationsProductsSearchPaginateArgs = {cursor: string, pageSize?: number | undefined}; export type GetApiV1IntegrationsProductsSearchPaginateOptions = {baseUrl?: string | undefined, apiKeyHeaderApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const fulfillmentInventoryBundleEnumProductType = z.enum(["pick_and_pack", "insert", "packaging"]); export const fulfillmentPublicApiBundleApiResourceLotControlLotControlOutput = z.object({lot_number: z.string().nullable(), expiration_date: z.string().nullable()}); export const fulfillmentPublicApiBundleApiResourceLotControlLotControlWithQuantityOutput = z.object({lot: fulfillmentPublicApiBundleApiResourceLotControlLotControlOutput.nullable(), quantity_on_hand: z.number().int()}); export const fulfillmentPublicApiBundleApiResourceWarehouseWarehouseDetailOutput = z.object({id: z.number().int(), name: z.string(), identifier: z.string()}); export const fulfillmentPublicApiBundleApiResourceLocationLocationListOutput = z.object({quantity_final: z.number().int(), quantity_available: z.number().int(), quantity_on_hand: z.number().int(), quarantined_quantity: z.number().int(), quantity_unavailable: z.number().int(), quantity_by_lot: z.array(fulfillmentPublicApiBundleApiResourceLotControlLotControlWithQuantityOutput), warehouse: fulfillmentPublicApiBundleApiResourceWarehouseWarehouseDetailOutput}); export const fulfillmentPublicApiBundleApiResourceInventoryInventoryDetailOutput = z.object({quantity_total_final: z.number().int(), quantity_total_available: z.number().int(), quantity_total_on_hand: z.number().int(), quantity_total_quarantined: z.number().int(), quantity_total_unavailable: z.number().int(), locations: z.array(fulfillmentPublicApiBundleApiResourceLocationLocationListOutput)}); export const shipMonkOMSHsCodeEnumHsCodeRegion = z.enum(["us", "uk", "eu"]); export const fulfillmentAdminApiBundleApiResourceProductHsCodeDetailOutput = z.object({hs_code: z.string(), region: shipMonkOMSHsCodeEnumHsCodeRegion}); export const fulfillmentCommonBundleApiResourceIntegrationDataOutput = z.object({name: z.string(), value: z.object({})}); export const shipMonkMeasurementValueObjectWeightUnit = z.enum(["kg", "g", "lb", "oz"]); export const shipMonkMeasurementOutputWeightOutput = z.object({value: z.number(), unit: shipMonkMeasurementValueObjectWeightUnit}); export const shipMonkMeasurementValueObjectLengthUnit = z.enum(["m", "cm", "mm", "ft", "in"]); export const shipMonkMeasurementOutputDimensionsOutput = z.object({length: z.number(), width: z.number(), height: z.number(), unit: shipMonkMeasurementValueObjectLengthUnit}); export const fulfillmentPublicApiBundleApiResourceProductBundleProductOutput = z.object({sku: z.string(), quantity: z.number().int()}); export const fulfillmentPublicApiBundleApiResourceProductBundleOutput = z.object({bundle_sku: z.string(), products: z.array(fulfillmentPublicApiBundleApiResourceProductBundleProductOutput)}); export const shipMonkOMSProductEnumProductHandlingUnitType = z.enum(["inner_carton", "master_carton", "pallet"]); export const fulfillmentPublicApiBundleApiResourceProductProductHandlingUnitOutput = z.object({type: shipMonkOMSProductEnumProductHandlingUnitType, weight: shipMonkMeasurementOutputWeightOutput.nullable(), dimensions: shipMonkMeasurementOutputDimensionsOutput.nullable(), count: z.number().int()}); export const fulfillmentPublicApiBundleApiResourceProductProductListOutput = z.object({id: z.number().int(), sku: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string(), inventory_on_hand_last_updated_at: z.string().nullable(), is_active: z.boolean().nullable(), product_type: z.string(), type: fulfillmentInventoryBundleEnumProductType, replacement_cost: z.number().nullable(), replacement_cost_currency: z.string().nullable(), inventory: fulfillmentPublicApiBundleApiResourceInventoryInventoryDetailOutput, hs_code: z.string().nullable(), hs_codes: z.array(fulfillmentAdminApiBundleApiResourceProductHsCodeDetailOutput), manufacturer_product_id: z.string().nullable(), standardized_product_id: z.string().nullable(), barcodes: z.array(z.string()), country_code: z.string().nullable(), stock_out_days: z.number().int(), custom_data: z.array(fulfillmentCommonBundleApiResourceIntegrationDataOutput), weight: shipMonkMeasurementOutputWeightOutput.nullable(), dimensions: shipMonkMeasurementOutputDimensionsOutput.nullable(), product_bundles: z.array(fulfillmentPublicApiBundleApiResourceProductBundleOutput), product_handling_units: z.array(fulfillmentPublicApiBundleApiResourceProductProductHandlingUnitOutput).nullable()}); export const fulfillmentPublicApiBundleApiResourceProductProductSearchResultsOutput = z.object({data: z.array(fulfillmentPublicApiBundleApiResourceProductProductListOutput), total: z.number().int(), next_cursor: z.string().nullable()}); 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 }; /** * Fetch results from Products for Inventory Sync * * GET /v1/integrations/products/search/paginate */ export const getApiV1IntegrationsProductsSearchPaginate = async (args: GetApiV1IntegrationsProductsSearchPaginateArgs, options?: GetApiV1IntegrationsProductsSearchPaginateOptions) => { const {baseUrl = "https://api.shipmonk.com", fetch: fetchFn = fetch, apiKeyHeaderApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (apiKeyHeaderApiKey !== undefined) headers.set("Api-Key", apiKeyHeaderApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/integrations/products/search/paginate${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(fulfillmentPublicApiBundleApiResourceProductProductSearchResultsOutput, await res.json()) };