import {z} from 'zod' export type CarrierAccountCanadaPostCreateParameters = {canada_post_terms: boolean, company: string, email: string, full_name: string, phone: string}; export type CarrierAccountCanadaPostCreateRequest = {carrier: 'canada_post', parameters: CarrierAccountCanadaPostCreateParameters}; export type CarrierAccountChronopostCreateRequest = {carrier: 'chronopost', parameters: Record}; export type CarrierAccountColissimoCreateRequest = {carrier: 'colissimo', parameters: Record}; export type CarrierAccountCorreosCreateRequest = {carrier: 'correos', parameters: Record}; export type CarrierAccountDeutschePostCreateRequest = {carrier: 'deutsche_post', parameters: Record}; export type CarrierAccountDHLExpressCreateRequestParameters = {user_accepted_terms_and_conditions: boolean}; export type CarrierAccountDHLExpressCreateRequest = {carrier: 'dhl_express', parameters: CarrierAccountDHLExpressCreateRequestParameters}; export type CarrierAccountDpdDeCreateRequest = {carrier: 'dpd_de', parameters: Record}; export type CarrierAccountDPDUKCreateRequest = {carrier: 'dpd_uk', parameters: Record}; export type CarrierAccountFedExCreateRequest = {carrier: 'fedex', parameters: Record}; export type CarrierAccountHermesUKCreateRequest = {carrier: 'hermes_uk', parameters: Record}; export type CarrierAccountMondialRelayCreateRequest = {carrier: 'mondial_relay', parameters: Record}; export type CarrierAccountPosteItalianeCreateRequest = {carrier: 'poste_italiane', parameters: Record}; export type CarrierAccountUPSCreateRequestParameters = {billing_address_city: string, billing_address_country_iso2: string, billing_address_state: string, billing_address_street1: string, billing_address_street2?: string | undefined, billing_address_zip: string, company?: string | undefined, email?: string | undefined, full_name?: string | undefined, phone?: string | undefined, pickup_address_city: string, pickup_address_country_iso2: string, pickup_address_same_as_billing_address?: boolean | undefined, pickup_address_state: string, pickup_address_street1: string, pickup_address_street2?: string | undefined, pickup_address_zip: string, ups_agreements: boolean}; export type CarrierAccountUPSCreateRequest = {carrier: 'ups', parameters?: CarrierAccountUPSCreateRequestParameters | undefined}; export type CarrierAccountUSPSCreateRequest = {carrier: 'usps', parameters: Record}; export type CarrierAccountRoyalMailCreateRequestParameters = {client_id: string, client_secret: string, partner_token: string}; export type CarrierAccountRoyalMailCreateRequest = {carrier: 'royal_mail', parameters: CarrierAccountRoyalMailCreateRequestParameters}; export type CarrierAccountRoyalMailSfCreateRequestParameters = {royal_mail_sf_agreements: boolean}; export type CarrierAccountRoyalMailSfCreateRequest = {carrier: 'royal_mail_sf', parameters: CarrierAccountRoyalMailSfCreateRequestParameters}; export type CreateApiCarrierAccountsRegisterNewArgs = {'SHIPPO-API-VERSION'?: string | undefined, 'SHIPPO-ACCOUNT-ID'?: string | undefined, body: CarrierAccountCanadaPostCreateRequest | CarrierAccountChronopostCreateRequest | CarrierAccountColissimoCreateRequest | CarrierAccountCorreosCreateRequest | CarrierAccountDeutschePostCreateRequest | CarrierAccountDHLExpressCreateRequest | CarrierAccountDpdDeCreateRequest | CarrierAccountDPDUKCreateRequest | CarrierAccountFedExCreateRequest | CarrierAccountHermesUKCreateRequest | CarrierAccountMondialRelayCreateRequest | CarrierAccountPosteItalianeCreateRequest | CarrierAccountUPSCreateRequest | CarrierAccountUSPSCreateRequest | CarrierAccountRoyalMailCreateRequest | CarrierAccountRoyalMailSfCreateRequest}; export type CreateApiCarrierAccountsRegisterNewOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const connectExistingOwnAccountParameters = z.unknown(); export const fedExConnectExistingOwnAccountParameters = z.object({first_name: z.string(), last_name: z.string(), phone_number: z.string(), from_address_st: z.string(), from_address_city: z.string(), from_address_state: z.string(), from_address_zip: z.string(), from_address_country_iso2: z.string(), use_multi_factor_registration: z.boolean(), verification_option: z.enum(["SMS", "EMAIL", "CALL", "INVOICE"]), verification_pin: z.string().optional(), verification_invoice_number: z.string().optional(), verification_invoice_amount: z.string().optional(), verification_invoice_date: z.string().optional(), verification_invoice_currency: z.string().optional()}); export const uPSConnectExistingOwnAccountParameters = z.object({account_number: z.string(), aia_country_iso2: z.string().optional(), billing_address_city: z.string(), billing_address_country_iso2: z.string().min(2).max(2), billing_address_state: z.string().min(2).max(2), billing_address_street1: z.string(), billing_address_street2: z.string().optional(), billing_address_zip: z.string().min(5).max(5), collec_country_iso2: z.string().min(2).max(2), collec_zip: z.string().min(5).max(5), company: z.string(), currency_code: z.string().min(3).max(3).optional(), email: z.string(), full_name: z.string(), has_invoice: z.boolean(), invoice_controlid: z.string().min(4).max(4).optional(), invoice_date: z.string().optional(), invoice_number: z.string().min(1).max(15).optional(), invoice_value: z.string().optional(), phone: z.string(), title: z.string(), ups_agreements: z.boolean()}); export const carrierAccountServiceLevel = z.object({name: z.string().optional(), token: z.string().optional(), supports_return_labels: z.boolean().optional()}); export const carrierAccount = z.object({account_id: z.string(), active: z.boolean().optional(), carrier: z.string(), parameters: z.union([connectExistingOwnAccountParameters, fedExConnectExistingOwnAccountParameters, uPSConnectExistingOwnAccountParameters]).optional(), carrier_name: z.string().optional(), is_shippo_account: z.boolean().optional(), metadata: z.string().optional(), object_id: z.string().optional(), object_owner: z.string().optional(), service_levels: z.array(carrierAccountServiceLevel).optional(), test: z.boolean().optional()}); 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 }; /** * Add a Shippo carrier account * * POST /carrier_accounts/register/new */ export const createApiCarrierAccountsRegisterNew = async (args: CreateApiCarrierAccountsRegisterNewArgs, options?: CreateApiCarrierAccountsRegisterNewOptions) => { const {baseUrl = "https://api.goshippo.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['SHIPPO-API-VERSION'] != null) headers.set("SHIPPO-API-VERSION", String(args['SHIPPO-API-VERSION'])) if (args['SHIPPO-ACCOUNT-ID'] != null) headers.set("SHIPPO-ACCOUNT-ID", String(args['SHIPPO-ACCOUNT-ID'])) if (APIKeyHeader !== undefined) headers.set("Authorization", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/carrier_accounts/register/new${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(carrierAccount, await res.json()) };