import {z} from 'zod' export type ASAddress = {street1?: string | undefined, street2?: string | undefined, city?: string | undefined, region?: string | undefined, state?: string | undefined, countryCode?: string | undefined, postalCode?: string | undefined, addressType?: ('commercial' | 'residential') | undefined}; export type AddressValidationRequest = {address: ASAddress, ignoreSecondaryAddressMismatch?: boolean | undefined, useSimplifiedCarrierValidation?: boolean | undefined, includeCandidate?: boolean | undefined}; export type CreateApiApiV1AddressValidateArgs = {body: AddressValidationRequest}; export type CreateApiApiV1AddressValidateOptions = {baseUrl?: string | undefined, oauth2Scheme?: string | undefined, fetch?: typeof fetch | undefined}; export const aSAddress = z.object({street1: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), state: z.string().optional(), countryCode: z.string().optional(), postalCode: z.string().optional(), addressType: z.enum(["commercial", "residential"]).optional()}); export const addressProperties = z.object({poBox: z.boolean().optional(), poBoxType: z.enum(["RR", "HC", "PO_BOX"]).optional(), militaryAddressType: z.enum(["APO", "FPO", "DPO"]).optional()}); export const responseDetail = z.object({errorCode: z.string().optional(), errorDescription: z.string().optional()}); export const addressValidationResponse = z.object({addressValidationId: z.string(), address: aSAddress, valid: z.boolean(), ignoreSecondaryAddressMismatch: z.boolean().optional(), useSimplifiedCarrierValidation: z.boolean().optional(), addressProperties: addressProperties.optional(), candidate: aSAddress.optional(), details: z.array(responseDetail).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 }; /** * Determine if an address is a valid mailing address * * POST /api/v1/address/validate */ export const createApiApiV1AddressValidate = async (args: CreateApiApiV1AddressValidateArgs, options?: CreateApiApiV1AddressValidateOptions) => { const {baseUrl = "https://api.shipium.com", fetch: fetchFn = fetch, oauth2Scheme} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2Scheme !== undefined) headers.set('authorization', `Bearer ${oauth2Scheme}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/address/validate${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(addressValidationResponse, await res.json()) };