/* * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. */ import * as z from "zod/v4-mini"; import { ShippoCore } from "../core.js"; import { encodeFormQuery, encodeSimple } from "../lib/encodings.js"; import * as M from "../lib/matchers.js"; import { compactMap } from "../lib/primitives.js"; import { safeParse } from "../lib/schemas.js"; import { RequestOptions } from "../lib/sdks.js"; import { extractSecurity, resolveGlobalSecurity } from "../lib/security.js"; import { pathToFunc } from "../lib/url.js"; import * as components from "../models/components/index.js"; import { ConnectionError, InvalidRequestError, RequestAbortedError, RequestTimeoutError, UnexpectedClientError, } from "../models/errors/httpclienterrors.js"; import { ResponseValidationError } from "../models/errors/responsevalidationerror.js"; import { SDKValidationError } from "../models/errors/sdkvalidationerror.js"; import { ShippoError } from "../models/errors/shippoerror.js"; import * as operations from "../models/operations/index.js"; import { APICall, APIPromise } from "../types/async.js"; import { Result } from "../types/fp.js"; /** * Retrieve shipment rates in currency * * @remarks * Returns all available shipping rates for a shipment object. * * When you create a new valid shipment object, Shippo automatically calculates all available rates. Depending on your shipment data, there may be none, one or multiple rates. * * By default, the calculated rates will return the price in two currencies under the `amount` and `amount_local` keys, respectively. The `amount` key will contain the price of a rate expressed in the currency that is used in the country from where the parcel originates, and the `amount_local` key will contain the price expressed in the currency that is used in the country the parcel is shipped to. You can request rates with prices expressed in a different currency by adding the currency code to the end of the resource URL. The full list of supported currencies along with their codes can be viewed on open exchange rates. * * Note: re-requesting the rates with a different currency code will re-queue the shipment (i.e. set the Shipment's `status` to `QUEUED`) and the converted currency rates will only be available when the Shipment's `status` is set to `SUCCESS`. */ export function ratesListShipmentRatesByCurrencyCode( client: ShippoCore, request: operations.ListShipmentRatesByCurrencyCodeRequest, options?: RequestOptions, ): APIPromise< Result< components.RatePaginatedList, | ShippoError | ResponseValidationError | ConnectionError | RequestAbortedError | RequestTimeoutError | InvalidRequestError | UnexpectedClientError | SDKValidationError > > { return new APIPromise($do( client, request, options, )); } async function $do( client: ShippoCore, request: operations.ListShipmentRatesByCurrencyCodeRequest, options?: RequestOptions, ): Promise< [ Result< components.RatePaginatedList, | ShippoError | ResponseValidationError | ConnectionError | RequestAbortedError | RequestTimeoutError | InvalidRequestError | UnexpectedClientError | SDKValidationError >, APICall, ] > { const parsed = safeParse( request, (value) => z.parse( operations.ListShipmentRatesByCurrencyCodeRequest$outboundSchema, value, ), "Input validation failed", ); if (!parsed.ok) { return [parsed, { status: "invalid" }]; } const payload = parsed.value; const body = null; const pathParams = { CurrencyCode: encodeSimple("CurrencyCode", payload.CurrencyCode, { explode: false, charEncoding: "percent", }), ShipmentId: encodeSimple("ShipmentId", payload.ShipmentId, { explode: false, charEncoding: "percent", }), }; const path = pathToFunc("/shipments/{ShipmentId}/rates/{CurrencyCode}")( pathParams, ); const query = encodeFormQuery({ "page": payload.page, "results": payload.results, }); const headers = new Headers(compactMap({ Accept: "application/json", "SHIPPO-API-VERSION": encodeSimple( "SHIPPO-API-VERSION", client._options.shippoApiVersion, { explode: false, charEncoding: "none" }, ), })); const secConfig = await extractSecurity(client._options.apiKeyHeader); const securityInput = secConfig == null ? {} : { apiKeyHeader: secConfig }; const requestSecurity = resolveGlobalSecurity(securityInput); const context = { options: client._options, baseURL: options?.serverURL ?? client._baseURL ?? "", operationID: "ListShipmentRatesByCurrencyCode", oAuth2Scopes: null, resolvedSecurity: requestSecurity, securitySource: client._options.apiKeyHeader, retryConfig: options?.retries || client._options.retryConfig || { strategy: "none" }, retryCodes: options?.retryCodes || ["429", "500", "502", "503", "504"], }; const requestRes = client._createRequest(context, { security: requestSecurity, method: "GET", baseURL: options?.serverURL, path: path, headers: headers, query: query, body: body, userAgent: client._options.userAgent, timeoutMs: options?.timeoutMs || client._options.timeoutMs || -1, }, options); if (!requestRes.ok) { return [requestRes, { status: "invalid" }]; } const req = requestRes.value; const doResult = await client._do(req, { context, errorCodes: ["400", "4XX", "5XX"], retryConfig: context.retryConfig, retryCodes: context.retryCodes, }); if (!doResult.ok) { return [doResult, { status: "request-error", request: req }]; } const response = doResult.value; const [result] = await M.match< components.RatePaginatedList, | ShippoError | ResponseValidationError | ConnectionError | RequestAbortedError | RequestTimeoutError | InvalidRequestError | UnexpectedClientError | SDKValidationError >( M.json(200, components.RatePaginatedList$inboundSchema), M.fail([400, "4XX"]), M.fail("5XX"), )(response, req); if (!result.ok) { return [result, { status: "complete", request: req, response }]; } return [result, { status: "complete", request: req, response }]; }