Skip to content
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

GO-180 exclude types geocoding parameter #33

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## DEVEL
### New Features
- synchronized geocoding types with current geocoding API
- added `excludeTypes` option to geocoding API

### Bug Fixes
### Others
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 26 additions & 16 deletions src/services/geocoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export type CommonForwardAndReverseGeocodingOptions =
limit?: number;

/**
* Filter of feature types to return. If not specified, feature of all available types except `poi` are returned.
* Filter of feature types to return.
* If not specified, feature of all available types except `poi` are returned (`types = ["poi"]`, `excludeTypes = true`).
*/
types?: (
| "country"
Expand All @@ -53,6 +54,11 @@ export type CommonForwardAndReverseGeocodingOptions =
| "address"
| "poi"
)[];

/**
* Set to `true` to use all available feature types except those mentioned in `types`. Default value is `false` if `types` is specified.
*/
excludeTypes?: boolean;
};

export type GeocodingOptions = CommonForwardAndReverseGeocodingOptions & {
Expand Down Expand Up @@ -237,7 +243,7 @@ export type GeocodingSearchResult = {

function addLanguageGeocodingOptions(
searchParams: URLSearchParams,
options: LanguageGeocodingOptions
options: LanguageGeocodingOptions,
) {
const { language } = options;

Expand All @@ -248,19 +254,19 @@ function addLanguageGeocodingOptions(
const languages = Array.from(
new Set(
(Array.isArray(language) ? language : [language]).map((lang) =>
lang === LanguageGeocoding.AUTO ? getAutoLanguageGeocoding() : lang
)
)
lang === LanguageGeocoding.AUTO ? getAutoLanguageGeocoding() : lang,
),
),
).join(",");

searchParams.set("language", languages);
}

function addCommonForwardAndReverseGeocodingOptions(
searchParams: URLSearchParams,
options: CommonForwardAndReverseGeocodingOptions
options: CommonForwardAndReverseGeocodingOptions,
) {
const { apiKey, limit, types } = options;
const { apiKey, limit, types, excludeTypes } = options;

searchParams.set("key", apiKey ?? config.apiKey);

Expand All @@ -272,12 +278,16 @@ function addCommonForwardAndReverseGeocodingOptions(
searchParams.set("types", types.join(","));
}

if (excludeTypes != undefined) {
searchParams.set("excludeTypes", String(excludeTypes));
}

addLanguageGeocodingOptions(searchParams, options);
}

function addForwardGeocodingOptions(
searchParams: URLSearchParams,
options: GeocodingOptions
options: GeocodingOptions,
) {
addCommonForwardAndReverseGeocodingOptions(searchParams, options);

Expand All @@ -290,7 +300,7 @@ function addForwardGeocodingOptions(
if (proximity != undefined) {
searchParams.set(
"proximity",
proximity === "ip" ? proximity : proximity.join(",")
proximity === "ip" ? proximity : proximity.join(","),
);
}

Expand Down Expand Up @@ -318,15 +328,15 @@ function addForwardGeocodingOptions(
*/
async function forward(
query: string,
options: GeocodingOptions = {}
options: GeocodingOptions = {},
): Promise<GeocodingSearchResult> {
if (typeof query !== "string" || query.trim().length === 0) {
throw new Error("The query must be a non-empty string");
}

const endpoint = new URL(
`geocoding/${encodeURIComponent(query)}.json`,
defaults.maptilerApiURL
defaults.maptilerApiURL,
);

const { searchParams } = endpoint;
Expand Down Expand Up @@ -356,15 +366,15 @@ async function forward(
*/
async function reverse(
position: Position,
options: ReverseGeocodingOptions = {}
options: ReverseGeocodingOptions = {},
): Promise<GeocodingSearchResult> {
if (!Array.isArray(position) || position.length < 2) {
throw new Error("The position must be an array of form [lng, lat].");
}

const endpoint = new URL(
`geocoding/${position[0]},${position[1]}.json`,
defaults.maptilerApiURL
defaults.maptilerApiURL,
);

addCommonForwardAndReverseGeocodingOptions(endpoint.searchParams, options);
Expand Down Expand Up @@ -393,7 +403,7 @@ async function reverse(
*/
async function byId(
id: string,
options: ByIdGeocodingOptions = {}
options: ByIdGeocodingOptions = {},
): Promise<GeocodingSearchResult> {
const endpoint = new URL(`geocoding/${id}.json`, defaults.maptilerApiURL);
endpoint.searchParams.set("key", options.apiKey ?? config.apiKey);
Expand Down Expand Up @@ -423,7 +433,7 @@ async function byId(
*/
async function batch(
queries: string[],
options: GeocodingOptions = {}
options: GeocodingOptions = {},
): Promise<GeocodingSearchResult[]> {
if (!queries.length) {
return [];
Expand All @@ -435,7 +445,7 @@ async function batch(

const endpoint = new URL(
`geocoding/${joinedQuery}.json`,
defaults.maptilerApiURL
defaults.maptilerApiURL,
);

const { searchParams } = endpoint;
Expand Down