diff options
| author | mo khan <mo@mokhan.ca> | 2016-12-24 13:02:15 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2016-12-24 13:02:15 -0700 |
| commit | 63c2e0d43ed5c2e4f44bd3cd14f17a44936e12eb (patch) | |
| tree | 6f73277d20aef5615151e08e6c317fe99d9889f1 | |
| parent | 94302563195eb57498e7697b681e2da1f8b0b58f (diff) | |
fix api resolution.
| -rw-r--r-- | app/boot/__tests__/wire-up-components-into_spec.js | 18 | ||||
| -rw-r--r-- | app/boot/wire-up-components-into.js | 20 | ||||
| -rw-r--r-- | app/infrastructure/__tests__/configuration_spec.js | 15 | ||||
| -rw-r--r-- | app/infrastructure/api.js | 24 | ||||
| -rw-r--r-- | app/infrastructure/configuration.js | 11 | ||||
| -rw-r--r-- | app/infrastructure/registry.js | 14 | ||||
| -rw-r--r-- | app/services/commands/login-command.js | 6 | ||||
| -rw-r--r-- | app/services/queries/fetch-workouts.js | 6 |
8 files changed, 83 insertions, 31 deletions
diff --git a/app/boot/__tests__/wire-up-components-into_spec.js b/app/boot/__tests__/wire-up-components-into_spec.js index b30bfbc..4475cc6 100644 --- a/app/boot/__tests__/wire-up-components-into_spec.js +++ b/app/boot/__tests__/wire-up-components-into_spec.js @@ -1,6 +1,7 @@ -import WireUpComponentsInto from '../wire-up-components-into'; -import Registry from '../../infrastructure/registry'; import * as commands from '../../services/commands'; +import Api from '../../infrastructure/api'; +import Registry from '../../infrastructure/registry'; +import WireUpComponentsInto from '../wire-up-components-into'; describe("WireUpComponentsInto", () => { let subject = null; @@ -12,11 +13,18 @@ describe("WireUpComponentsInto", () => { }); describe("#run", () => { - it ("registers each command", function() { + beforeEach(() => { subject.run() + }); + + it ("registers each command", function() { + results = registry.resolveAll('subscriber'); + expect(results.length).toEqual(3); + }); - results = registry.resolveAll('subscriber') - expect(results.length).toEqual(3) + it ("can build the api", function() { + result = registry.resolve('api'); + expect(result).toBeInstanceOf(Api); }); }); }) diff --git a/app/boot/wire-up-components-into.js b/app/boot/wire-up-components-into.js index b4cbc66..7875bdf 100644 --- a/app/boot/wire-up-components-into.js +++ b/app/boot/wire-up-components-into.js @@ -2,7 +2,7 @@ import * as commands from '../services/commands'; import * as queries from '../services/queries'; import Api from '../infrastructure/api'; import ApplicationStorage from '../infrastructure/application-storage'; -import Config from 'react-native-config'; +import Configuration from '../infrastructure/configuration'; import EventAggregator from '../infrastructure/event-aggregator'; import Registry from '../infrastructure/registry'; import Router from '../infrastructure/router' @@ -20,21 +20,25 @@ export default class WireUpComponentsInto { }); }).asSingleton(); this.registry.register('applicationStorage', ApplicationStorage).asSingleton(); - let host = Config.API_HOST; - this.registry.register('sessionsApi', (container) => new Api(host, '/sessions', container.resolve('applicationStorage'))).asSingleton(); - this.registry.register('workoutsApi', (container) => new Api(host, '/workouts', container.resolve('applicationStorage'))).asSingleton(); + this.registry.register('configuration', () => { + return new Configuration('development'); + }).asSingleton(); + this.registry.register('api', Api).asSingleton(); + this.registerSubscribers(commands); this.registerSubscribers(queries); + + this.registry.resolveAll("subscriber").forEach((subscriber) => { + console.log(`subscribing: ${subscriber}`); + subscriber.subscribeTo(this.registry.resolve('eventAggregator')); + }); return this.registry; } registerSubscribers(subscribers) { for (let subscriber in subscribers) { - //console.log(`registering: ${subscriber}`); + console.log(`registering: ${subscriber}`); this.registry.register('subscriber', subscribers[subscriber]).asSingleton(); } - this.registry.resolveAll("subscriber").forEach((subscriber) => { - subscriber.subscribeTo(this.registry.resolve('eventAggregator')); - }); } } diff --git a/app/infrastructure/__tests__/configuration_spec.js b/app/infrastructure/__tests__/configuration_spec.js new file mode 100644 index 0000000..98b3b97 --- /dev/null +++ b/app/infrastructure/__tests__/configuration_spec.js @@ -0,0 +1,15 @@ +import Configuration from '../configuration'; + +describe("Configuration", ()=> { + let subject = null; + + beforeEach(()=> { + subject = new Configuration('development'); + }); + + it ("loads the specified configuration", function() { + let apiHost='http://192.168.128.6:3000' + + expect(subject.value_for("API_HOST")).toEqual(apiHost) + }); +}); diff --git a/app/infrastructure/api.js b/app/infrastructure/api.js index 9c61adc..e2775ed 100644 --- a/app/infrastructure/api.js +++ b/app/infrastructure/api.js @@ -1,13 +1,14 @@ export default class Api { - constructor(host, url, applicationStorage) { - this.url = `${host}/api${url}` + constructor(configuration, applicationStorage) { + this.configuration = configuration; this.storage = applicationStorage; } - get(success) { + get(relativeUrl, success) { + let url = this.buildUrlFor(relativeUrl); this.defaultHeaders((headers) => { - console.log(`GET ${this.url}`); - fetch(this.url, { method: 'GET', headers: headers }) + console.log(`GET ${url}`); + fetch(url, { method: 'GET', headers: headers }) .then((response) => response.json()) .then((json) => success(json)) .catch((error) => console.error(error)) @@ -15,10 +16,11 @@ export default class Api { }); } - post(body, success) { + post(relativeUrl, body, success) { + let url = this.buildUrlFor(relativeUrl); const jsonBody = JSON.stringify(body); - console.log(`POST ${this.url}`); - fetch(this.url, { + console.log(`POST ${url}`); + fetch(url, { method: "POST", headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: jsonBody @@ -42,5 +44,11 @@ export default class Api { .catch((error) => console.error(error)) .done(); } + + buildUrlFor(url) { + //let host = this.configuration.value_for('API_HOST'); + let host = 'https://localhost:3000'; + return `${host}/api${url}` + } } diff --git a/app/infrastructure/configuration.js b/app/infrastructure/configuration.js new file mode 100644 index 0000000..5b519bd --- /dev/null +++ b/app/infrastructure/configuration.js @@ -0,0 +1,11 @@ +import Config from 'react-native-config'; + +export default class Configuration { + constructor(environment) { + this.environment = environment; + } + + value_for(key) { + return Config[key]; + } +} diff --git a/app/infrastructure/registry.js b/app/infrastructure/registry.js index 17440bd..1241552 100644 --- a/app/infrastructure/registry.js +++ b/app/infrastructure/registry.js @@ -12,9 +12,9 @@ export class Resolver { } } - parseConstructor(item) { - let code = item.toString(); - let regex = /function ([a-zA-Z]*)\((.*)\)\{/; + parseConstructor(func) { + let code = func.toString(); + let regex = /function ([a-zA-Z]*)\((.*)\) *\{/; return code.match(regex); } @@ -66,7 +66,13 @@ export default class Registry { } resolve(key) { - return this.registrations[key][0].create(this); + try { + return this.registrations[key][0].create(this); + } catch(error) { + console.error(`ERROR: Could Not Resolve ${key}`); + console.error(error); + throw error; + } } resolveAll(key) { diff --git a/app/services/commands/login-command.js b/app/services/commands/login-command.js index b439ed7..4b28c6d 100644 --- a/app/services/commands/login-command.js +++ b/app/services/commands/login-command.js @@ -1,9 +1,9 @@ import * as events from '../events'; export default class LoginCommand { - constructor(eventAggregator, sessionsApi, applicationStorage) { + constructor(eventAggregator, api, applicationStorage) { this.eventAggregator = eventAggregator; - this.api = sessionsApi; + this.api = api; this.applicationStorage = applicationStorage; } @@ -13,7 +13,7 @@ export default class LoginCommand { notify(event) { let body = { username: event.username, password: event.password }; - this.api.post(body, this.onResponse.bind(this)); + this.api.post('/sessions', body, this.onResponse.bind(this)); } onResponse(json) { diff --git a/app/services/queries/fetch-workouts.js b/app/services/queries/fetch-workouts.js index c606f15..4f37e75 100644 --- a/app/services/queries/fetch-workouts.js +++ b/app/services/queries/fetch-workouts.js @@ -1,9 +1,9 @@ import * as events from '../events'; export default class FetchWorkouts { - constructor(eventAggregator, workoutsApi) { + constructor(eventAggregator, api) { this.eventAggregator = eventAggregator; - this.api = workoutsApi; + this.api = api; } subscribeTo(eventAggregator) { @@ -11,7 +11,7 @@ export default class FetchWorkouts { } notify(event) { - this.api.get(this.onResponse.bind(this)); + this.api.get('/workouts', this.onResponse.bind(this)); } onResponse(json) { |
