Skip to content

Commit

Permalink
refactor: use import type and add esm dist
Browse files Browse the repository at this point in the history
  • Loading branch information
ruscon committed Mar 12, 2021
1 parent 5b622ca commit 637158d
Show file tree
Hide file tree
Showing 147 changed files with 461 additions and 332 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module.exports = {
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/no-require-imports': ['error'],
'@typescript-eslint/no-use-before-define': ['error'],
'@typescript-eslint/consistent-type-imports': ['error'],
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
Expand Down
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/1-bug-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ about: Report a reproducible bug
title: ''
labels: ''
assignees: ''

---

<!-- Click "Preview" for a more readable version --
Expand Down
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/2-feature-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ about: Suggest an idea or feature
title: 'feat: '
labels: feature
assignees: ruscon

---

## Issue Number: \[N/A]
Expand Down
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/3-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ about: Found a typo or something that isn't crystal clear in our docs?
title: 'docs: '
labels: documentation
assignees: ruscon

---

<!-- Click "Preview" for a more readable version --
Expand Down
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/4-question.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ about: Get help using Geocoder
title: 'question: '
labels: question
assignees: ''

---

<!-- Click "Preview" for a more readable version --
Expand Down
37 changes: 17 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ const geocoder: Geocoder = new Geocoder(provider);
console.error(err);
}

try {
const suggestions: Suggestion[] = await geocoder.suggest({
address: '1158 E 89th St',
});
console.info('suggestions', suggestions);
} catch (err) {
console.error(err);
}
try {
const suggestions: Suggestion[] = await geocoder.suggest({
address: '1158 E 89th St',
});

console.info('suggestions', suggestions);
} catch (err) {
console.error(err);
}
})();
```

Expand Down Expand Up @@ -155,8 +155,8 @@ geocoder.setLogger(logger);
lon: -87.5960762,
countryCode: 'US',
// language: 'en', // default
// limit: 5, // default
// withRaw: false, // default
// limit: 5, // default
// withRaw: false, // default
});

console.info('locations', locations);
Expand All @@ -183,14 +183,14 @@ Legend:
| Provider | Geocode | Reverse | Suggest | Place Details |
| :---------------------------------------------------------------------------------------- | :------ | :------ | :------ | :------------ |
| [Algolia Places](https://community.algolia.com/places/documentation.html) | 🆘 | 🆘️ | 🆘 | 🆘 |
| [ArcGIS Online](https://developers.arcgis.com/documentation/) | | | ✅️ | |
| [ArcGIS Online](https://developers.arcgis.com/documentation/) ||| ✅️ ||
| [Bing Maps](https://docs.microsoft.com/en-us/bingmaps/) | 🆘 | 🆘️ | 🔍️ | 🆘 |
| [Geonames](http://www.geonames.org/export/web-services.html) | 🆘 | 🆘️ | 🔍️ | 🆘 |
| [Google Maps](https://developers.google.com/maps/documentation/geocoding/) | | | | |
| [Here](https://developer.here.com/documentation/geocoder/topics/quick-start-geocode.html) | | | | |
| [Google Maps](https://developers.google.com/maps/documentation/geocoding/) |||||
| [Here](https://developer.here.com/documentation/geocoder/topics/quick-start-geocode.html) |||||
| [LocationIQ](https://locationiq.com/docs) | 🆘 | 🆘️ | 🔍️ | 🔍 |
| [Mapbox](https://docs.mapbox.com/api) | 🆘 | 🆘️ | 🔍️ | 🔍 |
| [MapQuest](http://developer.mapquest.com/web/products/dev-services/geocoding-ws) | | | 🚫️ | 🚫 |
| [MapQuest](http://developer.mapquest.com/web/products/dev-services/geocoding-ws) ||| 🚫️ | 🚫 |
| [Mapzen](https://www.mapzen.com/documentation/) | 🆘 | 🆘️ | 🔍️ | 🔍 |
| [Nominatim](https://nominatim.org/release-docs/develop/) | 🆘 | 🆘️ | 🔍️ | 🔍 |
| [OpenCage](https://opencagedata.com/api) | 🆘 | 🆘️ | 🔍️ | 🔍 |
Expand All @@ -216,10 +216,7 @@ const axios: AxiosInstance = Axios.create({
timeout: 5000,
});

const chainProvider: ChainProvider = new ChainProvider([
new MapQuestProvider(axios, 'YOUR_API_KEY'),
new HereProvider(axios, 'YOUR_APP_ID', 'YOUR_APP_CODE'),
]);
const chainProvider: ChainProvider = new ChainProvider([new MapQuestProvider(axios, 'YOUR_API_KEY'), new HereProvider(axios, 'YOUR_APP_ID', 'YOUR_APP_CODE')]);

const geocoder: ProviderAggregator = new ProviderAggregator([chainProvider]);

Expand Down Expand Up @@ -280,7 +277,7 @@ const locations: Location[] = geocoder
```

The `using()` method allows you to choose the `provider` to use by its class name.
When you deal with multiple providers, you may want to choose one of them. The
When you deal with multiple providers, you may want to choose one of them. The
default behavior is to use the first one but it can be annoying.

## Versioning
Expand Down
44 changes: 44 additions & 0 deletions bin/npm-dev-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { readFileSync, writeFileSync } from 'fs';
import { execSync } from 'child_process';
import parseArgs from 'minimist';
import { SemVer } from 'semver';

function getCurrentGitBranch(): string {
return execSync('git branch --show-current').toString().trim();
}

/**
* @var {string} p - path to directory where package.json is located
*/
const { p: packageJsonDir = '.' } = parseArgs(process.argv.slice(2));

void (async (): Promise<void> => {
const packageJsonPath = `${packageJsonDir}/package.json`;
const packageJson = JSON.parse(readFileSync(packageJsonPath).toString());
const { version } = packageJson;
const branchName: string = getCurrentGitBranch();

const ticketNumber: string | undefined = /^(feature|feat|hotifx|fix)\/(\d+)/gim.exec(branchName)?.[0]?.split('/').pop();
const semVer: SemVer = new SemVer(version);

if (!semVer) {
throw new Error(`Can't find package.json version`);
}

console.log('Input data', { packageJsonPath, branchName, ticketNumber, version: semVer.format() });

if (!semVer.prerelease.length && !ticketNumber) {
throw new Error(`Your branch should contain the ticket number or the package.json version should be with the prerelease version`);
}
if (semVer.prerelease.length && Number.isSafeInteger(semVer.prerelease[semVer.prerelease.length - 1])) {
// @ts-ignore
semVer.prerelease[semVer.prerelease.length - 1]++;
} else if (ticketNumber) {
semVer.prerelease = [`dev`, ticketNumber, 0];
}

console.log('Output data', { packageJsonPath, branchName, ticketNumber, version: semVer.format() });

packageJson.version = semVer.format();
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 4));
})();
36 changes: 19 additions & 17 deletions bin/prepublish.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { writeFileSync, copyFileSync } from 'fs';
import originPackage from '../package.json';
import fs from 'fs';
import path from 'path';

const distPackage: Record<string, any> = originPackage;
distPackage.module = './index.js';
distPackage.main = './index.js';
distPackage.types = './index.d.ts';
delete distPackage.scripts;
delete distPackage.devDependencies;
delete distPackage.config;
delete distPackage.husky;
delete distPackage.files;
delete distPackage.directories;
delete distPackage['lint-staged'];
async function run(): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { scripts, devDependencies, husky, files, 'lint-staged': lintStaged, directories, config, ...packageJson } = JSON.parse(
fs.readFileSync(path.join(process.cwd(), 'package.json')).toString(),
);

writeFileSync('./dist/package.json', JSON.stringify(distPackage, null, ' '));
packageJson.main = './cjs/index.js';
packageJson.module = './esm/index.js';
packageJson.typings = './types/index.d.ts';
packageJson.sideEffects = false;

const copyFiles = ['README.md'];
for (const file of copyFiles) {
copyFileSync(`./${file}`, `./dist/${file}`);
fs.writeFileSync('./dist/package.json', JSON.stringify(packageJson, null, 4));

const copyFiles = ['README.md'];
for (const file of copyFiles) {
fs.copyFileSync(`./${file}`, `./dist/${file}`);
}
}

void run();
15 changes: 9 additions & 6 deletions src/command/abstract-location.command.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { AxiosResponse } from 'axios';
import type { AxiosResponse } from 'axios';
import { UnsupportedAccuracyException } from '../exception';
import { QueryInterface } from '../interface';
import { AccuracyEnum, Location } from '../model';
import { AbstractLocationTransformer } from '../transformer';
import { WorldCountry, WorldCountryUtil } from '../util/world-country';
import { WorldCountryState, WorldCountryStateUtil } from '../util/world-country-state';
import type { QueryInterface } from '../interface';
import type { Location } from '../model';
import { AccuracyEnum } from '../model';
import type { AbstractLocationTransformer } from '../transformer';
import type { WorldCountry } from '../util/world-country';
import { WorldCountryUtil } from '../util/world-country';
import type { WorldCountryState } from '../util/world-country-state';
import { WorldCountryStateUtil } from '../util/world-country-state';
import { AbstractCommand } from './abstract.command';

export abstract class AbstractLocationCommand<
Expand Down
8 changes: 4 additions & 4 deletions src/command/abstract-suggest.command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AxiosResponse } from 'axios';
import { QueryInterface } from '../interface';
import { Suggestion } from '../model';
import { AbstractSuggestionTransformer } from '../transformer';
import type { AxiosResponse } from 'axios';
import type { QueryInterface } from '../interface';
import type { Suggestion } from '../model';
import type { AbstractSuggestionTransformer } from '../transformer';
import { AbstractCommand } from './abstract.command';

export abstract class AbstractSuggestCommand<
Expand Down
13 changes: 7 additions & 6 deletions src/command/abstract.command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AxiosInstance, AxiosResponse } from 'axios';
import type { AxiosInstance, AxiosResponse } from 'axios';
import { plainToClass } from 'class-transformer';
import { validateOrReject } from 'class-validator';
import {
Expand All @@ -8,11 +8,12 @@ import {
UnsupportedAccuracyException,
ValidationException,
} from '../exception';
import { QueryInterface } from '../interface';
import { LoggableInterface, LoggableMixin } from '../logger';
import { AccuracyEnum } from '../model';
import { AbstractTransformer } from '../transformer';
import { Type } from '../types';
import type { QueryInterface } from '../interface';
import type { LoggableInterface } from '../logger';
import { LoggableMixin } from '../logger';
import type { AccuracyEnum } from '../model';
import type { AbstractTransformer } from '../transformer';
import type { Type } from '../types';
import { getAvailableAccuracies } from '../util';

export abstract class AbstractCommand<
Expand Down
5 changes: 3 additions & 2 deletions src/command/geocode.command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ExactMatchNotFoundException } from '../exception';
import { GeocodeQueryInterface } from '../interface';
import { GeocodeQuery, Location } from '../model';
import type { GeocodeQueryInterface } from '../interface';
import type { Location } from '../model';
import { GeocodeQuery } from '../model';
import { AbstractLocationCommand } from './abstract-location.command';

export class GeocodeCommand<ProviderRequestType = any, ProviderResponseType = any> extends AbstractLocationCommand<
Expand Down
7 changes: 4 additions & 3 deletions src/command/place-details.command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NotFoundException } from '../exception';
import { PlaceDetailsQueryInterface } from '../interface';
import { Location, PlaceDetailsQuery } from '../model';
import { AbstractLocationTransformer } from '../transformer';
import type { PlaceDetailsQueryInterface } from '../interface';
import type { Location } from '../model';
import { PlaceDetailsQuery } from '../model';
import type { AbstractLocationTransformer } from '../transformer';
import { AbstractLocationCommand } from './abstract-location.command';

export class PlaceDetailsCommand<ProviderRequestType = any, ProviderResponseType = any> extends AbstractLocationCommand<
Expand Down
2 changes: 1 addition & 1 deletion src/command/reverse.command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReverseQueryInterface } from '../interface';
import type { ReverseQueryInterface } from '../interface';
import { ReverseQuery } from '../model';
import { AbstractLocationCommand } from './abstract-location.command';

Expand Down
2 changes: 1 addition & 1 deletion src/command/suggest.command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SuggestQueryInterface } from '../interface';
import type { SuggestQueryInterface } from '../interface';
import { SuggestQuery } from '../model';
import { AbstractSuggestCommand } from './abstract-suggest.command';

Expand Down
2 changes: 1 addition & 1 deletion src/decider/abstract-decider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbstractProvider } from '../model';
import type { AbstractProvider } from '../model';

export abstract class AbstractDecider {
abstract getProvider(providers: AbstractProvider[], forceProvider?: AbstractProvider): Promise<AbstractProvider>;
Expand Down
2 changes: 1 addition & 1 deletion src/decider/stateful-decider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ProviderNotRegisteredException } from '../exception';
import { AbstractProvider } from '../model';
import type { AbstractProvider } from '../model';
import { AbstractDecider } from './abstract-decider';

export class StatefulDecider extends AbstractDecider {
Expand Down
2 changes: 1 addition & 1 deletion src/decider/stateless-decider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ProviderNotRegisteredException } from '../exception';
import { AbstractProvider } from '../model';
import type { AbstractProvider } from '../model';
import { AbstractDecider } from './abstract-decider';

export class StatelessDecider extends AbstractDecider {
Expand Down
2 changes: 1 addition & 1 deletion src/exception/provider-not-registered.exception.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbstractProvider } from '../model';
import type { AbstractProvider } from '../model';
import { GeocoderException } from './geocoder.exception';

export class ProviderNotRegisteredException extends GeocoderException {
Expand Down
2 changes: 1 addition & 1 deletion src/exception/validation.exception.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ValidationError } from 'class-validator';
import type { ValidationError } from 'class-validator';
import { GeocoderException } from './geocoder.exception';

export class ValidationException extends GeocoderException {
Expand Down
7 changes: 4 additions & 3 deletions src/geocoder/abstract-geocoder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ProviderNotRegisteredException } from '../exception';
import { GeocodeQueryInterface, GeocoderInterface, ReverseQueryInterface, SuggestQueryInterface } from '../interface';
import type { GeocodeQueryInterface, GeocoderInterface, ReverseQueryInterface, SuggestQueryInterface } from '../interface';
import { LoggableMixin } from '../logger';
import { AbstractChainProvider, AbstractHttpProvider, AbstractProvider, Location, ProvidableMixin, Suggestion } from '../model';
import { Type } from '../types';
import type { AbstractProvider, Location, Suggestion } from '../model';
import { AbstractChainProvider, AbstractHttpProvider, ProvidableMixin } from '../model';
import type { Type } from '../types';

export abstract class AbstractGeocoder extends ProvidableMixin(LoggableMixin(Object)) implements GeocoderInterface {
abstract geocode(query: GeocodeQueryInterface): Promise<Location[]>;
Expand Down
4 changes: 2 additions & 2 deletions src/geocoder/geocoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GeocodeQueryInterface, ReverseQueryInterface, SuggestQueryInterface } from '../interface';
import { AbstractProvider, Location, Suggestion } from '../model';
import type { GeocodeQueryInterface, ReverseQueryInterface, SuggestQueryInterface } from '../interface';
import type { AbstractProvider, Location, Suggestion } from '../model';
import { AbstractGeocoder } from './abstract-geocoder';

export class Geocoder extends AbstractGeocoder {
Expand Down
7 changes: 4 additions & 3 deletions src/geocoder/provider-aggregator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AbstractDecider, StatelessDecider } from '../decider';
import { GeocodeQueryInterface, ReverseQueryInterface, SuggestQueryInterface } from '../interface';
import { AbstractProvider, Location, Suggestion } from '../model';
import type { AbstractDecider } from '../decider';
import { StatelessDecider } from '../decider';
import type { GeocodeQueryInterface, ReverseQueryInterface, SuggestQueryInterface } from '../interface';
import type { AbstractProvider, Location, Suggestion } from '../model';
import { AbstractGeocoder } from './abstract-geocoder';

export class ProviderAggregator extends AbstractGeocoder {
Expand Down
2 changes: 1 addition & 1 deletion src/interface/geocode-query.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryInterface } from './query.interface';
import type { QueryInterface } from './query.interface';

export interface GeocodeQueryInterface extends QueryInterface {
/**
Expand Down
8 changes: 4 additions & 4 deletions src/interface/geocoder.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Location, Suggestion } from '../model';
import { GeocodeQueryInterface } from './geocode-query.interface';
import { ReverseQueryInterface } from './reverse-query.interface';
import { SuggestQueryInterface } from './suggest-query.interface';
import type { Location, Suggestion } from '../model';
import type { GeocodeQueryInterface } from './geocode-query.interface';
import type { ReverseQueryInterface } from './reverse-query.interface';
import type { SuggestQueryInterface } from './suggest-query.interface';

export interface GeocoderInterface {
geocode(query: GeocodeQueryInterface): Promise<Location[]>;
Expand Down
2 changes: 1 addition & 1 deletion src/interface/http-provider-commands.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GeocodeCommand, PlaceDetailsCommand, ReverseCommand, SuggestCommand } from '../command';
import type { GeocodeCommand, PlaceDetailsCommand, ReverseCommand, SuggestCommand } from '../command';

export interface HttpProviderCommandsInterface {
geocode: GeocodeCommand;
Expand Down
2 changes: 1 addition & 1 deletion src/interface/place-details-query.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryInterface } from './query.interface';
import type { QueryInterface } from './query.interface';

export interface PlaceDetailsQueryInterface extends QueryInterface {
placeId: string;
Expand Down
6 changes: 3 additions & 3 deletions src/interface/provider.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Location } from '../model';
import { GeocoderInterface } from './geocoder.interface';
import { PlaceDetailsQueryInterface } from './place-details-query.interface';
import type { Location } from '../model';
import type { GeocoderInterface } from './geocoder.interface';
import type { PlaceDetailsQueryInterface } from './place-details-query.interface';

export interface ProviderInterface extends GeocoderInterface {
placeDetails(query: PlaceDetailsQueryInterface): Promise<Location>;
Expand Down
Loading

0 comments on commit 637158d

Please sign in to comment.