-
Notifications
You must be signed in to change notification settings - Fork 33
Add US Autocomplete (V2) API client #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
89817d8
Add US Autocomplete (V2) API client
mae-smarty e91625e
refactored source to const
andrea-wait-smarty 44c389e
Merge branch 'master' of github.com:smarty/smartystreets-javascript-s…
mae-smarty 4d2ca59
standardizing source constructing
mae-smarty ff70ee6
tests updated to match source changes
mae-smarty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,72 @@ | ||
| import SmartySDK from "smartystreets-javascript-sdk"; | ||
|
|
||
| // This example is for US Autocomplete (V2). It has the same name as a previous product | ||
| // which has been deprecated since 2022, which we refer to as US Autocomplete Basic. | ||
| // If you are still using US Autocomplete Basic, this SDK will not work. | ||
|
|
||
| const SmartyCore = SmartySDK.core; | ||
| const Lookup = SmartySDK.usAutocomplete.Lookup; | ||
|
|
||
| // for client-side requests (browser/mobile), use this code: | ||
| // let key = process.env.SMARTY_EMBEDDED_KEY; | ||
| // const credentials = new SmartyCore.SharedCredentials(key); | ||
|
|
||
| // for Server-to-server requests, use this code: | ||
| let authId = process.env.SMARTY_AUTH_ID; | ||
| let authToken = process.env.SMARTY_AUTH_TOKEN; | ||
| const credentials = new SmartyCore.BasicAuthCredentials(authId, authToken); | ||
|
|
||
| // The appropriate license values to be used for your subscriptions | ||
| // can be found on the Subscription page of the account dashboard. | ||
| // https://www.smarty.com/docs/cloud/licensing | ||
| let clientBuilder = new SmartyCore.ClientBuilder(credentials); | ||
| // .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API | ||
|
|
||
| let client = clientBuilder.buildUsAutocompleteClient(); | ||
|
|
||
| // Documentation for input fields can be found at: | ||
| // https://www.smarty.com/docs/apis/us-autocomplete-v2/reference#http-request-input-fields | ||
|
|
||
| // *** Simple Lookup *** | ||
| let lookup = new Lookup("4770 Lincoln"); | ||
| // uncomment the following line to add a custom parameter | ||
| // lookup.addCustomParameter("max_results", 3); | ||
|
|
||
| await handleRequest(lookup, "Simple Lookup"); | ||
|
|
||
| // *** Using Filter and Prefer *** | ||
| lookup = new Lookup("4770 Lincoln"); | ||
|
|
||
| lookup.maxResults = 10; | ||
| lookup.includeOnlyCities = ["Chicago,La Grange,IL", "Blaine,WA"]; | ||
| lookup.preferStates = ["IL"]; | ||
| lookup.preferRatio = 33; | ||
| lookup.source = "all"; | ||
|
|
||
| await handleRequest(lookup, "Using Filter and Prefer"); | ||
|
|
||
| // *** Using 'selected' to Expand Secondaries *** | ||
| // Take an entry_id from a previous result that has secondaries and pass it back as the selected address. | ||
| const entryId = lookup.result.find((suggestion) => suggestion.entryId)?.entryId; | ||
| if (entryId) { | ||
| lookup = new Lookup("4770 Lincoln"); | ||
| lookup.selected = entryId; | ||
| await handleRequest(lookup, "Using 'selected' to Expand Secondaries"); | ||
| } | ||
|
|
||
| // ************************************************ | ||
|
|
||
| function logSuggestions(response, message) { | ||
| console.log(message); | ||
| console.log(response.result); | ||
| console.log("*********************"); | ||
| } | ||
|
|
||
| async function handleRequest(lookup, lookupType) { | ||
| try { | ||
| const results = await client.send(lookup); | ||
| logSuggestions(results, lookupType); | ||
| } catch (err) { | ||
| console.log(err); | ||
| } | ||
| } |
This file contains hidden or 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,78 @@ | ||
| import { | ||
| ClientBuilder, | ||
| BasicAuthCredentials, | ||
| LookupUSAutocomplete, | ||
| } from "smartystreets-javascript-sdk"; | ||
|
|
||
| // This example is for US Autocomplete (V2). It has the same name as a previous product | ||
| // which has been deprecated since 2022, which we refer to as US Autocomplete Basic. | ||
| // If you are still using US Autocomplete Basic, this SDK will not work. | ||
|
|
||
| // for client-side requests (browser/mobile), use this code: | ||
| // import { SharedCredentials } from "smartystreets-javascript-sdk"; | ||
| // const key: string = process.env.SMARTY_EMBEDDED_KEY!; | ||
| // const credentials = new SharedCredentials(key); | ||
|
|
||
| // for Server-to-server requests, use this code: | ||
| const authId = process.env.SMARTY_AUTH_ID!; | ||
| const authToken = process.env.SMARTY_AUTH_TOKEN!; | ||
| const credentials = new BasicAuthCredentials(authId, authToken); | ||
|
|
||
| // The appropriate license values to be used for your subscriptions | ||
| // can be found on the Subscription page of the account dashboard. | ||
| // https://www.smarty.com/docs/cloud/licensing | ||
| const clientBuilder = new ClientBuilder(credentials); | ||
| // .withBaseUrl("YOUR URL") // withBaseUrl() should be used if you are self-hosting the Smarty API | ||
|
|
||
| const client = clientBuilder.buildUsAutocompleteClient(); | ||
|
|
||
| // Documentation for input fields can be found at: | ||
| // https://www.smarty.com/docs/apis/us-autocomplete-v2/reference#http-request-input-fields | ||
|
|
||
| // ************************************************ | ||
|
|
||
| function logSuggestions(response: LookupUSAutocomplete, message: string): void { | ||
| console.log(message); | ||
| console.log(response.result); | ||
| console.log("*********************"); | ||
| } | ||
|
|
||
| async function handleRequest(lookup: LookupUSAutocomplete, lookupType: string): Promise<void> { | ||
| try { | ||
| const results = await client.send(lookup); | ||
| logSuggestions(results, lookupType); | ||
| } catch (err: unknown) { | ||
| console.error(err); | ||
| } | ||
| } | ||
|
|
||
| async function main(): Promise<void> { | ||
| // *** Simple Lookup *** | ||
| let lookup = new LookupUSAutocomplete("4770 Lincoln"); | ||
| // uncomment the following line to add a custom parameter | ||
| // lookup.addCustomParameter("max_results", "3"); | ||
|
|
||
| await handleRequest(lookup, "Simple Lookup"); | ||
|
|
||
| // *** Using Filter and Prefer *** | ||
| lookup = new LookupUSAutocomplete("4770 Lincoln"); | ||
|
|
||
| lookup.maxResults = 10; | ||
| lookup.includeOnlyCities = ["Chicago,La Grange,IL", "Blaine,WA"]; | ||
| lookup.preferStates = ["IL"]; | ||
| lookup.preferRatio = 33; | ||
| lookup.source = "all"; | ||
|
|
||
| await handleRequest(lookup, "Using Filter and Prefer"); | ||
|
|
||
| // *** Using 'selected' to Expand Secondaries *** | ||
| // Take an entry_id from a previous result that has secondaries and pass it back as the selected address. | ||
| const entryId = lookup.result.find((suggestion) => suggestion.entryId)?.entryId; | ||
| if (entryId) { | ||
| lookup = new LookupUSAutocomplete("4770 Lincoln"); | ||
| lookup.selected = entryId; | ||
| await handleRequest(lookup, "Using 'selected' to Expand Secondaries"); | ||
| } | ||
| } | ||
|
|
||
| main(); |
This file contains hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| import { UndefinedLookupError } from "../Errors.js"; | ||
| import Request from "../Request.js"; | ||
| import Suggestion, { RawUsAutocompleteSuggestion } from "./Suggestion.js"; | ||
| import buildInputData from "../util/buildInputData.js"; | ||
| import apiToSDKKeyMap from "../util/apiToSDKKeyMap.js"; | ||
| import { Sender } from "../types.js"; | ||
| import Lookup from "./Lookup.js"; | ||
|
|
||
| const keyTranslationFormat = apiToSDKKeyMap.usAutocomplete; | ||
|
|
||
| export default class Client { | ||
| private sender: Sender; | ||
|
|
||
| constructor(sender: Sender) { | ||
| this.sender = sender; | ||
| } | ||
|
|
||
| send(lookup: Lookup): Promise<Lookup> { | ||
| if (typeof lookup === "undefined") throw new UndefinedLookupError(); | ||
|
|
||
| const request = new Request(); | ||
| request.parameters = buildInputData(lookup, keyTranslationFormat); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| this.sender | ||
| .send(request) | ||
| .then((response) => { | ||
| if (response.error) return reject(response.error); | ||
|
|
||
| const payload = response.payload as { | ||
| suggestions: RawUsAutocompleteSuggestion[] | null; | ||
| }; | ||
| lookup.result = | ||
| payload.suggestions === null | ||
| ? [] | ||
| : payload.suggestions.map((suggestion) => new Suggestion(suggestion)); | ||
| resolve(lookup); | ||
| }) | ||
| .catch(reject); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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,47 @@ | ||
| import Suggestion from "./Suggestion.js"; | ||
|
|
||
| export type Geolocation = "city" | "none" | (string & {}); | ||
| export type AutocompleteSource = "all" | "postal" | (string & {}); | ||
|
|
||
| export default class Lookup { | ||
| result: Suggestion[]; | ||
| search: string | undefined; | ||
| selected: string | undefined; | ||
| exclude: string | undefined; | ||
| maxResults: number | undefined; | ||
| includeOnlyCities: string[]; | ||
| includeOnlyStates: string[]; | ||
| includeOnlyZIPCodes: string[]; | ||
| excludeStates: string[]; | ||
| preferCities: string[]; | ||
| preferStates: string[]; | ||
| preferZIPCodes: string[]; | ||
| preferRatio: number | undefined; | ||
| preferGeolocation: Geolocation | undefined; | ||
| source: AutocompleteSource | undefined; | ||
| customParameters: Record<string, string>; | ||
|
|
||
| constructor(search?: string) { | ||
| this.result = []; | ||
|
|
||
| this.search = search; | ||
| this.selected = undefined; | ||
| this.exclude = undefined; | ||
| this.maxResults = undefined; | ||
| this.includeOnlyCities = []; | ||
| this.includeOnlyStates = []; | ||
| this.includeOnlyZIPCodes = []; | ||
| this.excludeStates = []; | ||
| this.preferCities = []; | ||
| this.preferStates = []; | ||
| this.preferZIPCodes = []; | ||
| this.preferRatio = undefined; | ||
| this.preferGeolocation = undefined; | ||
| this.source = undefined; | ||
| this.customParameters = {}; | ||
| } | ||
|
|
||
| addCustomParameter(key: string, value: string): void { | ||
| this.customParameters[key] = value; | ||
| } | ||
| } |
This file contains hidden or 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,38 @@ | ||
| export interface RawUsAutocompleteSuggestion { | ||
| smarty_key?: string; | ||
| entry_id?: string; | ||
| street_line?: string; | ||
| secondary?: string; | ||
| city?: string; | ||
| state?: string; | ||
| zipcode?: string; | ||
| entries?: number; | ||
| source?: string; | ||
| } | ||
|
|
||
| export default class Suggestion { | ||
| smartyKey: string; | ||
| entryId: string; | ||
| streetLine: string; | ||
| secondary: string; | ||
| city: string; | ||
| state: string; | ||
| zipcode: string; | ||
| entries: number; | ||
| source: string | undefined; | ||
|
|
||
| constructor(responseData: RawUsAutocompleteSuggestion) { | ||
| this.smartyKey = responseData.smarty_key ?? ""; | ||
| this.entryId = responseData.entry_id ?? ""; | ||
| this.streetLine = responseData.street_line ?? ""; | ||
| this.secondary = responseData.secondary ?? ""; | ||
| this.city = responseData.city ?? ""; | ||
| this.state = responseData.state ?? ""; | ||
| this.zipcode = responseData.zipcode ?? ""; | ||
| this.entries = responseData.entries ?? 0; | ||
|
|
||
| if (responseData.source) { | ||
| this.source = responseData.source; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.