Skip to content

Commit

Permalink
feat(Geo): getAvailableMaps and getDefaultMap - Geo milestone 1 - PR 4 (
Browse files Browse the repository at this point in the history
aws-amplify#8620)

Co-authored-by: Amplifiyer <[email protected]>
  • Loading branch information
TreTuna and Amplifiyer committed Sep 23, 2021
1 parent 4c1c1fe commit 229f93e
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 11 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export const parseMobileHubConfig = (config): AmplifyConfig => {
amplifyConfig.Geo = Object.assign({}, config.geo);
}

// Geo
if (config['geo']) {
amplifyConfig.Geo = Object.assign({}, config.geo);
}

amplifyConfig.Analytics = Object.assign(
{},
amplifyConfig.Analytics,
Expand Down
85 changes: 83 additions & 2 deletions packages/geo/__tests__/Geo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

import { Credentials } from '@aws-amplify/core';
import { GeoClass } from '../src/Geo';
import { AmazonLocationServicesProvider } from '../src/Providers/AmazonLocationServicesProvider';
import { awsConfig } from './data';
import { credentials, awsConfig } from './data';

describe('Geo', () => {
afterEach(() => {
Expand Down Expand Up @@ -72,4 +72,85 @@ describe('Geo', () => {
expect(config).toEqual(awsConfig.geo);
});
});

describe('get map resources', () => {
test('should fail if there is no provider', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const geo = new GeoClass();
geo.configure(awsConfig);
geo.removePluggable('AmazonLocationServices');

expect(() => geo.getAvailableMaps()).toThrow(
'No plugin found in Geo for the provider'
);
expect(() => geo.getDefaultMap()).toThrow(
'No plugin found in Geo for the provider'
);
});

test('should tell you if there are no available map resources', () => {
const geo = new GeoClass();
geo.configure({});

expect(() => geo.getAvailableMaps()).toThrow(
"No map resources found in amplify config, run 'amplify add geo' to create them and ensure to run `amplify push` after"
);
});

test('should get all available map resources', () => {
const geo = new GeoClass();
geo.configure(awsConfig);

const maps = [];
const availableMaps = awsConfig.geo.maps.items;
for (const mapName in availableMaps) {
const style = availableMaps[mapName].style;
maps.push({ mapName, style });
}

expect(geo.getAvailableMaps()).toEqual(maps);
});

test('should tell you if there is no default map resource', () => {
const geo = new GeoClass();
geo.configure({});

expect(() => geo.getDefaultMap()).toThrow(
"No map resources found in amplify config, run 'amplify add geo' to create them and ensure to run `amplify push` after"
);
});

test('should tell you if there is no map resources when running getDefaultMapResource', () => {
const geo = new GeoClass();
geo.configure({});

expect(() => geo.getDefaultMap()).toThrow(
"No map resources found in amplify config, run 'amplify add geo' to create them and ensure to run `amplify push` after"
);
});

test('should tell you if there is no map resources when running getDefaultMapResource', () => {
const geo = new GeoClass();
geo.configure({ geo: { maps: { testMap: { style: 'teststyle' } } } });

expect(() => geo.getDefaultMap()).toThrow(
"No default map resource found in amplify config, run 'amplify add geo' to create one and ensure to run `amplify push` after"
);
});

test('should get the default map resource', () => {
const geo = new GeoClass();
geo.configure(awsConfig);

const mapName = awsConfig.geo.maps.default;
const style = awsConfig.geo.maps.items[mapName].style;
const testMap = { mapName, style };

const defaultMapsResource = geo.getDefaultMap();
expect(defaultMapsResource).toEqual(testMap);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
*/

import { AmazonLocationServicesProvider } from '../../src/Providers/AmazonLocationServicesProvider';
import {
credentials,
awsConfig,
} from '../data';
import { awsConfig } from '../data';

describe('AmazonLocationServicesProvider', () => {
afterEach(() => {
Expand Down Expand Up @@ -57,4 +54,60 @@ describe('AmazonLocationServicesProvider', () => {
expect(config).toEqual(awsConfig.geo);
});
});

describe('get map resources', () => {
test('should tell you if there are no available map resources', () => {
const provider = new AmazonLocationServicesProvider();
provider.configure();
expect(() => provider.getAvailableMaps()).toThrow(
"No map resources found in amplify config, run 'amplify add geo' to create them and ensure to run `amplify push` after"
);
});

test('should get all available map resources', () => {
const provider = new AmazonLocationServicesProvider();
provider.configure(awsConfig.geo);

const maps = [];
const availableMaps = awsConfig.geo.maps.items;
for (const mapName in availableMaps) {
const style = availableMaps[mapName].style;
maps.push({ mapName, style });
}

expect(provider.getAvailableMaps()).toEqual(maps);
});

test('should tell you if there is no map resources available when calling getDefaultMap', () => {
const provider = new AmazonLocationServicesProvider();
provider.configure();

expect(() => provider.getDefaultMap()).toThrow(
"No map resources found in amplify config, run 'amplify add geo' to create them and ensure to run `amplify push` after"
);
});

test('should tell you if there is no default map resource', () => {
const provider = new AmazonLocationServicesProvider();
provider.configure({
maps: { testMap: { style: 'teststyle' } },
});

expect(() => provider.getDefaultMap()).toThrow(
"No default map resource found in amplify config, run 'amplify add geo' to create one and ensure to run `amplify push` after"
);
});

test('should get the default map resource', () => {
const provider = new AmazonLocationServicesProvider();
provider.configure(awsConfig.geo);

const mapName = awsConfig.geo.maps.default;
const style = awsConfig.geo.maps.items[mapName].style;
const testMap = { mapName, style };

const defaultMapsResource = provider.getDefaultMap();
expect(defaultMapsResource).toEqual(testMap);
});
});
});
42 changes: 41 additions & 1 deletion packages/geo/src/Geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '@aws-amplify/core';
import { AmazonLocationServicesProvider } from './Providers/AmazonLocationServicesProvider';

import { GeoConfig, GeoProvider } from './types';
import { GeoConfig, GeoProvider, MapStyle } from './types';

const logger = new Logger('Geo');

Expand All @@ -36,6 +36,10 @@ export class GeoClass {
logger.debug('Geo Options', this._config);
}

/**
* get the name of the module category
* @returns {string} name of the module category
*/
public getModuleName() {
return GeoClass.MODULE;
}
Expand Down Expand Up @@ -100,6 +104,42 @@ export class GeoClass {
}
return this._config;
}

/**
* Get the map resources that are currently available through the provider
* @param {string} provider
* @returns - Array of available map resources
*/
public getAvailableMaps(provider = DEFAULT_PROVIDER): MapStyle[] | string {
const prov = this._pluggables.find(
pluggable => pluggable.getProviderName() === provider
);

if (prov === undefined) {
logger.debug('No plugin found with providerName', provider);
throw 'No plugin found in Geo for the provider';
}

return prov.getAvailableMaps();
}

/**
* Get the map resource set as default in amplify config
* @param {string} provider
* @returns - Map resource set as the default in amplify config
*/
public getDefaultMap(provider = DEFAULT_PROVIDER): MapStyle | string {
const prov = this._pluggables.find(
pluggable => pluggable.getProviderName() === provider
);

if (prov === undefined) {
logger.debug('No plugin found with providerName', provider);
throw 'No plugin found in Geo for the provider';
}

return prov.getDefaultMap();
}
}

export const Geo = new GeoClass();
Expand Down
47 changes: 43 additions & 4 deletions packages/geo/src/Providers/AmazonLocationServicesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
*/

import { ConsoleLogger as Logger, Credentials } from '@aws-amplify/core';
import {
GeoConfig,
GeoProvider,
} from '../types';
import { GeoConfig, GeoProvider, MapStyle } from '../types';

const logger = new Logger('AmazonLocationServicesProvider');

Expand All @@ -36,17 +33,21 @@ export class AmazonLocationServicesProvider implements GeoProvider {
this._config = config ? config : {};
logger.debug('Geo Options', this._config);

this.getAvailableMaps.bind(this);
this.getDefaultMap.bind(this);
}

/**
* get the category of the plugin
* @returns {string} name of the category
*/
public getCategory(): string {
return AmazonLocationServicesProvider.CATEGORY;
}

/**
* get provider name of the plugin
* @returns {string} name of the provider
*/
public getProviderName(): string {
return AmazonLocationServicesProvider.PROVIDER_NAME;
Expand All @@ -63,4 +64,42 @@ export class AmazonLocationServicesProvider implements GeoProvider {
this._config = Object.assign({}, this._config, config);
return this._config;
}

/**
* Get the map resources that are currently available through the provider
* @returns - Array of available map resources
*/
public getAvailableMaps(): MapStyle[] {
if (!this._config.maps) {
throw "No map resources found in amplify config, run 'amplify add geo' to create them and ensure to run `amplify push` after";
}

const mapStyles: MapStyle[] = [];
const availableMaps = this._config.maps.items;

for (const mapName in availableMaps) {
const style = availableMaps[mapName].style;
mapStyles.push({ mapName, style });
}

return mapStyles;
}

/**
* Get the map resource set as default in amplify config
* @returns - Map resource set as the default in amplify config
*/
public getDefaultMap(): MapStyle {
if (!this._config.maps) {
throw "No map resources found in amplify config, run 'amplify add geo' to create them and ensure to run `amplify push` after";
}
if (!this._config.maps.default) {
throw "No default map resource found in amplify config, run 'amplify add geo' to create one and ensure to run `amplify push` after";
}

const mapName = this._config.maps.default;
const style = this._config.maps.items[mapName].style;

return { mapName, style };
}
}
6 changes: 6 additions & 0 deletions packages/geo/src/types/Geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ export type GeoConfig = {
};
place_indexes?: {};
};

export type MapStyle = {
mapName: string;
style: string;
};

10 changes: 10 additions & 0 deletions packages/geo/src/types/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@
* and limitations under the License.
*/

import { MapStyle } from './Geo';

export interface GeoProvider {
// get the category name for the provider
getCategory(): string;

// get provider name
getProviderName(): string;

// configure your provider
configure(config: object): object;

// get the available map resources
getAvailableMaps(): string | MapStyle[];

// get the map resource listed as default
getDefaultMap(): string | MapStyle;
}

0 comments on commit 229f93e

Please sign in to comment.