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

feat: use google maps api to support secure requests #311

Merged
merged 16 commits into from
Oct 3, 2022
Merged
Changes from 3 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
61 changes: 27 additions & 34 deletions src/providers/googleProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import AbstractProvider, {
EndpointArgument,
ParseArgument,
ProviderOptions,
SearchArgument,
SearchResult,
} from './provider';
Expand All @@ -19,17 +20,25 @@ interface GeocodeError {
stack: string;
}

export type GoogleProviderOptions = LoaderOptions & ProviderOptions;

export default class GoogleProvider extends AbstractProvider<
RequestResult,
google.maps.GeocoderResult
> {
geocoder: () => Promise<google.maps.Geocoder>;
googleApiOptions: LoaderOptions;
loader: Promise<google.maps.Geocoder> | null = null;
geocoder: google.maps.Geocoder | null = null;

constructor(options: GoogleProviderOptions) {
super(options);

constructor(googleApiOptions: LoaderOptions) {
super();
this.googleApiOptions = googleApiOptions;
this.geocoder = this.memoizeGeocoder(this.fetchGeocoder);
if (typeof window !== 'undefined') {
this.loader = new Loader(options).load().then((google) => {
const geocoder = new google.maps.Geocoder();
this.geocoder = geocoder;
return geocoder;
});
}
}

endpoint({ query }: EndpointArgument): never {
Expand All @@ -56,37 +65,21 @@ export default class GoogleProvider extends AbstractProvider<
});
}

memoizeGeocoder(method: () => Promise<google.maps.Geocoder>) {
let geocoder: Promise<google.maps.Geocoder>;

return async function (this: GoogleProvider) {
geocoder = geocoder || method.apply(this);
return geocoder;
};
}

async fetchGeocoder() {
return await new Loader(this.googleApiOptions).load().then((google) => {
return new google.maps.Geocoder();
});
}

async search(
options: SearchArgument,
): Promise<SearchResult<google.maps.GeocoderResult>[]> {
const geoCoder = await this.geocoder();
const response = await geoCoder
.geocode(
{
address: options.query,
},
(
response: google.maps.GeocoderResult[] | null,
_: google.maps.GeocoderStatus,
) => {
return { results: response };
},
)
const geocoder = this.geocoder || (await this.loader);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Very clever!


if (!geocoder) {
throw new Error(
'GoogleMaps GeoCoder is not loaded. Are you trying to run this server side?',
);
}

const response = await geocoder
.geocode({ address: options.query }, (response) => ({
results: response,
}))
.catch((e: GeocodeError) => {
if (e.code !== 'ZERO_RESULTS') {
console.error(`${e.code}: ${e.message}`);
Expand Down