/*
* 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";
/**
* List all carrier parcel templates
*
* @remarks
* List all carrier parcel template objects.
Use the following query string params to filter the results as needed.
*
* - `include=all` (the default). Includes templates from all carriers
* - `include=user`. Includes templates only from carriers which the user has added (whether or not they're currently enabled)
* - `include=enabled`. includes templates only for carriers which the user has added and enabled
* - `carrier=*token*`. filter by specific carrier, e.g. fedex, usps
*
*/
export function carrierParcelTemplatesList(
client: ShippoCore,
include?: operations.Include | undefined,
carrier?: string | undefined,
options?: RequestOptions,
): APIPromise<
Result<
components.CarrierParcelTemplateList,
| ShippoError
| ResponseValidationError
| ConnectionError
| RequestAbortedError
| RequestTimeoutError
| InvalidRequestError
| UnexpectedClientError
| SDKValidationError
>
> {
return new APIPromise($do(
client,
include,
carrier,
options,
));
}
async function $do(
client: ShippoCore,
include?: operations.Include | undefined,
carrier?: string | undefined,
options?: RequestOptions,
): Promise<
[
Result<
components.CarrierParcelTemplateList,
| ShippoError
| ResponseValidationError
| ConnectionError
| RequestAbortedError
| RequestTimeoutError
| InvalidRequestError
| UnexpectedClientError
| SDKValidationError
>,
APICall,
]
> {
const input: operations.ListCarrierParcelTemplatesRequest = {
include: include,
carrier: carrier,
};
const parsed = safeParse(
input,
(value) =>
z.parse(
operations.ListCarrierParcelTemplatesRequest$outboundSchema,
value,
),
"Input validation failed",
);
if (!parsed.ok) {
return [parsed, { status: "invalid" }];
}
const payload = parsed.value;
const body = null;
const path = pathToFunc("/parcel-templates")();
const query = encodeFormQuery({
"carrier": payload.carrier,
"include": payload.include,
});
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: "ListCarrierParcelTemplates",
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.CarrierParcelTemplateList,
| ShippoError
| ResponseValidationError
| ConnectionError
| RequestAbortedError
| RequestTimeoutError
| InvalidRequestError
| UnexpectedClientError
| SDKValidationError
>(
M.json(200, components.CarrierParcelTemplateList$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 }];
}