forked from MadisonReed/usps-webtools
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from derek-watson14/multiple-validator
- Loading branch information
Showing
6 changed files
with
260 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import type { Address, MultipleAddress } from "./usps.js"; | ||
import type USPSClass from "./usps.js"; | ||
import type { AddressValidateResponse } from "./address-validate.js"; | ||
import properCase from "./utils/proper-case.js"; | ||
import callUSPS from "./utils/request.js"; | ||
|
||
// See page 4, "AddressValidateRequest / Address /" section of: https://www.usps.com/business/web-tools-apis/address-information-api.pdf | ||
|
||
export interface MultipleAddressValidateRequest { | ||
Address: MultipleAddress[]; | ||
Revision: number; | ||
} | ||
|
||
// eslint-disable-next-line sonarjs/cognitive-complexity, func-names | ||
export default async function ( | ||
this: USPSClass, | ||
addresses: Address[], | ||
): Promise<MultipleAddress[]> { | ||
if (addresses.length > 5) { | ||
throw new Error("Maximum of 5 addresses allowed per request.") | ||
} | ||
|
||
if (Array.isArray(addresses) === false) { | ||
throw new TypeError("Must pass an array of addresses. For single address use 'verify' method."); | ||
} | ||
|
||
const Addresses: MultipleAddress[] = addresses.map((address: Address, index: number) => ({ | ||
'@ID': index.toString(), | ||
Address1: address.Address2 ?? "", | ||
Address2: address.Address1 ?? "", | ||
City: address.City ?? "", | ||
State: address.State ?? "", | ||
Urbanization: address.Urbanization ?? "", | ||
Zip5: address.Zip5 ?? "", | ||
// USPS expects Zip4 after Zip5 | ||
// eslint-disable-next-line sort-keys | ||
Zip4: address.Zip4 ?? "", | ||
})); | ||
|
||
const parameters: MultipleAddressValidateRequest = { | ||
Revision: 1, | ||
// USPS expects Address to come after Revision | ||
// eslint-disable-next-line sort-keys | ||
Address: Addresses, | ||
}; | ||
|
||
let response: AddressValidateResponse[]; | ||
try { | ||
response = (await callUSPS( | ||
"Verify", | ||
"AddressValidate", | ||
"Address", | ||
this.config, | ||
parameters, | ||
)) as AddressValidateResponse[]; | ||
if (response) { | ||
return response.map((addr) => { | ||
const fAddr = { ...addr }; | ||
|
||
const switchAddresses = fAddr.Address1; | ||
fAddr.Address1 = fAddr.Address2; | ||
fAddr.Address2 = switchAddresses; | ||
if (this.config.properCase) { | ||
fAddr.Address1 = fAddr.Address1 | ||
? properCase(fAddr.Address1) | ||
: undefined; | ||
fAddr.Address2 = fAddr.Address2 | ||
? properCase(fAddr.Address2) | ||
: undefined; | ||
fAddr.City = fAddr.City ? properCase(fAddr.City) : undefined; | ||
fAddr.FirmName = fAddr.FirmName | ||
? properCase(fAddr.FirmName) | ||
: undefined; | ||
} | ||
fAddr.Zip4 = | ||
typeof fAddr.Zip4 === "object" | ||
? undefined | ||
: fAddr.Zip4?.toString(); | ||
return fAddr as MultipleAddress; | ||
}); | ||
} | ||
throw new Error("Can't find results"); | ||
} catch (error) { | ||
throw new Error(error as string); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* eslint-disable sonarjs/no-duplicate-string */ | ||
import test from "ava"; | ||
|
||
import USPS from "../src/usps.js"; | ||
|
||
const usps = new USPS({ | ||
userId: process.env["USPS_ID"]!, | ||
}); | ||
|
||
const fourAddresses = [ | ||
{ | ||
Address1: "11205 SE 233RD PL.", | ||
Address2: "Apartment 2", | ||
City: "Kent", | ||
State: "WA", | ||
Zip5: "98031", | ||
}, | ||
{ | ||
Address1: "11205 SE 233RD PL.", | ||
Address2: "UNIT 2", | ||
City: "Kent", | ||
State: "WA", | ||
Zip5: "98031", | ||
}, | ||
{ | ||
Address1: "11205 southeast 233Road PLace.", | ||
Address2: "Building 2", | ||
City: "Kent", | ||
State: "WA", | ||
Zip5: "98031", | ||
}, | ||
{ | ||
Address1: "11205 SE 233RD PL.", | ||
Address2: "Floor 2", | ||
City: "Kent", | ||
State: "WA", | ||
Zip5: "98031", | ||
}, | ||
]; | ||
|
||
const moreAddresses = [ | ||
{ | ||
Address1: "11205 SE 233RD PL.", | ||
Address2: "Apartment 4", | ||
City: "Kent", | ||
State: "WA", | ||
Zip5: "98031", | ||
}, | ||
{ | ||
Address1: "11205 southeast 233Road PLace.", | ||
Address2: "Building 3", | ||
City: "Kent", | ||
State: "WA", | ||
Zip5: "98031", | ||
}, | ||
] | ||
|
||
const invalidAddress = { | ||
Address1: "1212 s kingsway rd", | ||
City: "seffner", | ||
State: "fl", | ||
Zip5: "33584", | ||
} | ||
|
||
test("Multiple address verify should return the same number of addresses.", async (t) => { | ||
const addresses = await usps.verifyMultiple(fourAddresses); | ||
t.is(addresses.length, 4); | ||
}); | ||
|
||
test("Multiple address verify should only accept addesses in an array.", async (t) => { | ||
const error = await t.throwsAsync(async () => { | ||
// @ts-expect-error Testing invalid input | ||
await usps.verifyMultiple(fourAddresses[0]); | ||
}); | ||
t.is( | ||
error?.message, | ||
"Must pass an array of addresses. For single address use 'verify' method.", | ||
); | ||
}); | ||
|
||
test("Multiple address verify should throw an error if more than 5 addresses are passed.", async (t) => { | ||
const error = await t.throwsAsync(async () => { | ||
await usps.verifyMultiple([...fourAddresses, ...moreAddresses]); | ||
}); | ||
t.is( | ||
error?.message, | ||
"Maximum of 5 addresses allowed per request.", | ||
); | ||
}); | ||
|
||
test("Multiple address verify should validate each address in the same way as a single verify would.", async (t) => { | ||
const addresses = await usps.verifyMultiple(fourAddresses); | ||
t.is(addresses[0]?.Address2, "APT 2"); | ||
t.is(addresses[1]?.Address2, "UNIT 2"); | ||
t.is(addresses[2]?.Address2, "BLDG 2"); | ||
t.is(addresses[3]?.Address2, "FL 2"); | ||
}); | ||
|
||
test("Multiple address verify should handle proper case the same as single verify.", async (t) => { | ||
const uspsCase = new USPS({ | ||
properCase: true, | ||
userId: process.env["USPS_ID"]!, | ||
}); | ||
const addresses = await uspsCase.verifyMultiple(moreAddresses); | ||
t.is(addresses[0]?.Address1, "11205 SE 233rd Pl"); | ||
t.is(addresses[0]?.Address2, "Apt 4"); | ||
t.is(addresses[0]?.City, "Kent"); | ||
}); | ||
|
||
test("Multiple address verify should contain error message for any unverifiable addresses in a list.", async (t) => { | ||
const addresses = await usps.verifyMultiple([...moreAddresses, invalidAddress]); | ||
t.false("Error" in addresses[0]!); | ||
t.false("Error" in addresses[1]!); | ||
t.is(addresses[2]?.Error?.Description, "Multiple addresses were found for the information you entered, and no default exists."); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
/* eslint-disable sonarjs/no-duplicate-string */ | ||
|
||
import test from "ava"; | ||
|
||
import USPS from "../src/usps.js"; | ||
|