Skip to content

Commit

Permalink
Merge 087bea9 into 7d37146
Browse files Browse the repository at this point in the history
  • Loading branch information
ruscon committed Mar 4, 2019
2 parents 7d37146 + 087bea9 commit d929017
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ You MUST run the `npm test` command.

You MUST write (or update) unit tests.

You MUST run the `npm run pre-commit` hook.
You MUST run the `npm run commit` hook to add a new commit.

You SHOULD write documentation.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ axios.interceptors.request.use((request: AxiosRequestConfig) => {

// You can log all responses
axios.interceptors.response.use(
(response: AxiosResponse<any>): AxiosResponse<any> => {
(response: AxiosResponse): AxiosResponse => {
logger.debug(`api response ${response.status}`, response.data);

return response;
Expand Down
9 changes: 8 additions & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
module.exports = { extends: ['@commitlint/config-conventional'] };
module.exports = {
extends: ['@commitlint/config-conventional'],
scopes: [{ name: 'geocoder' }, { name: 'provider' }, { name: 'tutorial' }],
scopeOverrides: {
fix: [{ name: 'style' }, { name: 'unit' }, { name: 'e2e' }, { name: 'integration' }],
},
allowCustomScopes: true,
};
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "npm run pre-commit",
"post-commit": "git update-index --again"
}
},
Expand All @@ -41,7 +40,13 @@
]
}
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"scripts": {
"commit": "git-cz",
"test": "nyc mocha 'test/unit/**/*.spec.ts' 'test/e2e/**/*.spec.ts'",
"test:unit": "nyc mocha 'test/unit/**/*.spec.ts'",
"test:e2e": "nyc mocha 'test/e2e/**/*.spec.ts'",
Expand All @@ -55,6 +60,7 @@
"remark": "remark README.md CHANGELOG.md CONTRIBUTING.md CODE_OF_CONDUCT.md .github/ -o -f -q ",
"pre-commit": "git add . && npm run format:staged && npm run remark && npm run lint && npm run test && npm run build",
"build": "rimraf dist && tsc",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"prepublishOnly": "npm run build",
"preversion": "npm run pre-commit",
"version": "npm run changelog",
Expand All @@ -68,7 +74,6 @@
"devDependencies": {
"@commitlint/cli": "7.5.2",
"@commitlint/config-conventional": "7.5.0",
"@commitlint/prompt-cli": "7.5.0",
"@commitlint/travis-cli": "7.5.2",
"@types/chai": "4.1.7",
"@types/chai-as-promised": "7.1.0",
Expand All @@ -80,7 +85,9 @@
"axios-mock-adapter": "1.16.0",
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"commitizen": "3.0.7",
"coveralls": "3.0.3",
"cz-conventional-changelog": "2.1.0",
"dotenv": "6.2.0",
"husky": "1.3.1",
"lint-staged": "8.1.5",
Expand Down
2 changes: 1 addition & 1 deletion src/exception/validation.exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GeocoderException } from './geocoder.exception';

export class ValidationException extends GeocoderException {
constructor(private readonly errors: ValidationError[]) {
super('Validation error');
super('Validation Failed.');
}

getValidationErrors(): ValidationError[] {
Expand Down
12 changes: 4 additions & 8 deletions src/geocoder/abstract-geocoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { validateOrReject } from 'class-validator';
import { UnsupportedAccuracyException, ValidationException } from '../exception';
import { GeocodeQueryInterface, GeocoderInterface, ReverseQueryInterface } from '../interface';
import { LoggerInterface, NullLogger } from '../logger';
import { AbstractProvider, AccuracyEnum, Address, GeocodeQuery, Query, ReverseQuery } from '../model';
import { AbstractProvider, AccuracyEnum, Address, GeocodeQuery, ReverseQuery } from '../model';
import { WorldCountry, WorldCountryState, WorldCountryStateUtil, WorldCountryUtil } from '../util';

export abstract class AbstractGeocoder implements GeocoderInterface {
Expand Down Expand Up @@ -35,7 +35,7 @@ export abstract class AbstractGeocoder implements GeocoderInterface {

let addresses: Address[] = await provider.geocode(query);

addresses = await this.addMissingAddressProperties(addresses, query);
addresses = await this.addMissingAddressProperties(addresses);
addresses = this.filterByAccuracy(addresses, query.accuracy);

if (addresses.length > query.limit) {
Expand All @@ -60,7 +60,7 @@ export abstract class AbstractGeocoder implements GeocoderInterface {

let addresses: Address[] = await provider.reverse(query);

addresses = await this.addMissingAddressProperties(addresses, query);
addresses = await this.addMissingAddressProperties(addresses);
addresses = this.filterByAccuracy(addresses, query.accuracy);

if (addresses.length > query.limit) {
Expand All @@ -70,11 +70,7 @@ export abstract class AbstractGeocoder implements GeocoderInterface {
return addresses;
}

private async addMissingAddressProperties(addresses: Address[], query: Query): Promise<Address[]> {
if (!query.fillMissingQueryProperties) {
return addresses;
}

private async addMissingAddressProperties(addresses: Address[]): Promise<Address[]> {
for (const address of addresses) {
if (!address.countryCode || !address.country) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/geocoder/provider-aggregator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractDecider, CircularDecider } from '../decider';
import { ProviderNotRegisteredException } from '../exception/provider-not-registered.exception';
import { ProviderNotRegisteredException } from '../exception';
import { GeocodeQueryInterface, ReverseQueryInterface, Type } from '../interface';
import { LoggerInterface } from '../logger';
import { AbstractProvider, Address } from '../model';
Expand Down
10 changes: 8 additions & 2 deletions src/model/abstract-http-provider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AxiosError, AxiosInstance, AxiosResponse } from 'axios';
import { AbstractProvider, Address, GeocodeQuery, ReverseQuery } from '.';
import { AbstractProvider, AccuracyEnum, Address, GeocodeQuery, ReverseQuery } from '.';
import { InvalidCredentialsException, InvalidServerResponseException, QuotaExceededException } from '../exception';

export abstract class AbstractHttpProvider extends AbstractProvider {
protected constructor(private readonly httpClient: AxiosInstance) {
super();

this.httpClient.interceptors.response.use(
(response: AxiosResponse<any>): AxiosResponse<any> => {
(response: AxiosResponse): AxiosResponse => {
if (!response.data) {
throw new InvalidServerResponseException(`Invalid server response: ${response.config.url}`);
}
Expand All @@ -32,6 +32,12 @@ export abstract class AbstractHttpProvider extends AbstractProvider {
);
}

/**
* @example If the provider doesn't provide separate information about house number, then AccuracyEnum.STREET_NAME should be set.
* @important This information will be used to ignore the provider if query.accuracy is specified.
*/
abstract get maxAccuracy(): AccuracyEnum;

abstract get geocodeUrl(): string;

abstract get reverseUrl(): string;
Expand Down
8 changes: 5 additions & 3 deletions src/model/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ export class Query {
language: string = Query.DEFAULT_RESULT_LANGUAGE;

/**
* Auto fill undefined optional query properties using other optional properties: country, countryCode, zipcode
* TODO not implemented
*
* Auto fill undefined optional query properties using other optional properties: country, countryCode, postalCode
* @example You provide country
* Library search countryCode and zipcode by country
* Library search countryCode and postalCode by country
*
* @example You provide state
* Library search country, countryCode, stateCode and zipcode by country
* Library search country, countryCode, stateCode and postalCode by country
*/
@IsBoolean()
@Transform((v: boolean) => !!v, { toClassOnly: true })
Expand Down
6 changes: 5 additions & 1 deletion src/provider/google-maps.provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxiosInstance, AxiosResponse } from 'axios';
import { InvalidCredentialsException, InvalidServerResponseException, QuotaExceededException } from '../exception';
import { AbstractHttpProvider, Address, GeocodeQuery, ReverseQuery } from '../model';
import { AbstractHttpProvider, AccuracyEnum, Address, GeocodeQuery, ReverseQuery } from '../model';
import { AddressBuilder } from '../model/address-builder';

export interface GoogleMapsProviderGeocodeParamsInterface {
Expand Down Expand Up @@ -37,6 +37,10 @@ export class GoogleMapsProvider extends AbstractHttpProvider {
}
}

get maxAccuracy(): AccuracyEnum {
return AccuracyEnum.HOUSE_NUMBER;
}

get geocodeUrl(): string {
return 'https://maps.googleapis.com/maps/api/geocode/json';
}
Expand Down
6 changes: 5 additions & 1 deletion src/provider/here.provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxiosInstance, AxiosResponse } from 'axios';
import { InvalidCredentialsException } from '../exception';
import { AbstractHttpProvider, Address, GeocodeQuery, ReverseQuery } from '../model';
import { AbstractHttpProvider, AccuracyEnum, Address, GeocodeQuery, ReverseQuery } from '../model';
import { AddressBuilder } from '../model/address-builder';
import { WorldCountry, WorldCountryUtil } from '../util';

Expand Down Expand Up @@ -50,6 +50,10 @@ export class HereProvider extends AbstractHttpProvider {
}
}

get maxAccuracy(): AccuracyEnum {
return AccuracyEnum.HOUSE_NUMBER;
}

/**
* @link {https://developer.here.com/documentation/geocoder/common/request-cit-environment-rest.html}
*/
Expand Down
18 changes: 9 additions & 9 deletions src/provider/map-quest.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export class MapQuestProvider extends AbstractHttpProvider {
}
}

get maxAccuracy(): AccuracyEnum {
return AccuracyEnum.STREET_NAME;
}

get geocodeUrl(): string {
return `https://www.mapquestapi.com/geocoding/v1/address?key=${this.apiKey}`;
}
Expand All @@ -50,7 +54,7 @@ export class MapQuestProvider extends AbstractHttpProvider {
}

/**
* @link {https://developer.mapquest.com/documentation/geocoding-api/address/post/}
* @link {https://developer.mapquest.com/documentation/geocoding-api/address/get/}
*/
async geocode(query: GeocodeQuery): Promise<Address[]> {
const response: AxiosResponse = await this.getHttpClient().get(this.geocodeUrl, {
Expand Down Expand Up @@ -132,20 +136,16 @@ export class MapQuestProvider extends AbstractHttpProvider {
builder.countryCode = location.adminArea1;
if (2 === location.adminArea3.length) {
builder.stateCode = location.adminArea3;
builder.state = undefined;
} else {
builder.stateCode = undefined;
builder.state = location.adminArea3;
}
builder.city = location.adminArea5;
builder.streetName = location.street;
builder.houseNumber = undefined;
builder.postalCode = location.postalCode;

if ([MapQuestLocationQualityEnum.POINT, MapQuestLocationQualityEnum.ADDRESS].includes(location.geocodeQuality)) {
const words: [] = location.street.split(' ');
builder.houseNumber = words.shift();
builder.streetName = words.join(' ');
} else {
builder.streetName = location.street;
}

return builder.build();
},
);
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/geocoder/geocoder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('Geocoder (2e2)', () => {
const accuracy: string = `string`;
geocodeQueryFixture.accuracy = accuracy as AccuracyEnum;

return geocoder.geocode(geocodeQueryFixture).should.be.rejectedWith(ValidationException, 'Validation error');
return geocoder.geocode(geocodeQueryFixture).should.be.rejectedWith(ValidationException, 'Validation Failed.');
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/fixture/provider/map-quest.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const plainParsedResponseObject: AddressInterface[] = [
state: 'New York',
stateCode: 'NY',
city: 'Brooklyn',
streetName: 'E 89th St',
houseNumber: '1158',
streetName: '1158 E 89th St',
houseNumber: undefined,
postalCode: '11236-4763',
provider: MapQuestProvider.name,
},
Expand Down
2 changes: 1 addition & 1 deletion test/unit/exception/validation.exception.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('ValidationException (unit)', () => {
});

it('should return right message', async () => {
return new ValidationException([]).should.have.property('message', 'Validation error');
return new ValidationException([]).should.have.property('message', 'Validation Failed.');
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/geocoder/provider-aggregator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Axios, { AxiosInstance } from 'axios';
import * as chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { ProviderNotRegisteredException } from '../../../src/exception/provider-not-registered.exception';
import { ProviderNotRegisteredException } from '../../../src/exception';
import { ProviderAggregator } from '../../../src/geocoder';
import { GeocodeQueryInterface, ReverseQueryInterface } from '../../../src/interface';
import { GoogleMapsProvider, MapQuestProvider } from '../../../src/provider';
Expand Down

0 comments on commit d929017

Please sign in to comment.