import {z} from 'zod' export type ShippingAddress = {name?: string | undefined, surname?: string | undefined, address?: string | undefined, city?: string | undefined, postal?: string | undefined, municipality?: string | undefined, neighbourhood?: string | undefined, region?: string | undefined, country?: string | undefined}; export type PackagesDimensions = {length?: number | undefined, width?: number | undefined, height?: number | undefined, weight?: number | undefined}; export type FulfillmentRatesFields = {order_id?: number | undefined, location_id?: number | undefined, origin_address?: ({country?: string | undefined, region?: string | undefined, municipality?: string | undefined, postal?: string | undefined}) | undefined, shipping_address?: ShippingAddress | undefined, receiver_email?: string | undefined, packages_dimensions?: Array | undefined}; export type FulfillmentRates = {rates?: FulfillmentRatesFields | undefined}; export type CreateApiFulfillmentsRatesJsonArgs = {body: FulfillmentRates}; export type CreateApiFulfillmentsRatesJsonOptions = {baseUrl?: string | undefined, BasicAuthentication?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const rate = z.object({fulfillment_company: z.string().optional(), service_label: z.string().optional(), service_code: z.string().optional(), amount: z.string().optional(), public_amount: z.string().optional()}); export const createApiFulfillmentsRatesJsonResponse = z.array(rate); 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 }; /** * Rates for fulfillment. * * POST /fulfillments/rates.json * * Also accepts auth: loginParameter + authtokenParameter, OAuth2 */ export const createApiFulfillmentsRatesJson = async (args: CreateApiFulfillmentsRatesJsonArgs, options?: CreateApiFulfillmentsRatesJsonOptions) => { const {baseUrl = "https://api.jumpseller.com/v1", fetch: fetchFn = fetch, BasicAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuthentication !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuthentication.username}:${BasicAuthentication.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/fulfillments/rates.json${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(createApiFulfillmentsRatesJsonResponse, await res.json()) };