1

I am still trying to get in my head the object fundamentals in javascript which seems to be quite different than classical paradigm. I have written a toy example to fetch weather data, the code is below:

import axios from 'axios'

const weatherService = {
  fetch: async endpoint => await axios.get(endpoint)
}

const weatherApi = {
  currentWeather: async city => {
    try {
      const { data: { data } } = await this.service.fetch(this.config.endpoints.curr)
      return this.handleCurrentData(data)
    } catch(e) {
      this.handleError(e)
    }
  },
  hourlyForecast: async city => {
    try {
      const { data: { data } } = await this.service.fetch(this.config.endpoints.hour)
      return this.handleHourlyData(data)
    } catch(e) {
      this.handleError(e)
    }
  }
};

const defaultConfig = {
  key: process.env.API_KEY,
  endpoints: {
    curr: `/current/geosearch?key=${this.key}`,
    hour: `/forecast/3hourly/geosearch?key=${this.key}`
  }
};

const api = (api, config, service) => {
  return {
    Object.create(weatherApi),
    Object.create(service),
    Object.create(config),
    ...obj
  }
};

// dependency injection for testing
module.exports = (obj, config=defaultConfig, service=weatherService) => {
  return api(obj, config, service)
};


// Usage
const weatherAPI = require('api')({
  handleCurrentData: (data) => console.log(data),
  handleHourlyData: (data) => console.log(data)
});

weatherAPI.currentWeather('London');
weatherAPI.hourlyWeather('London');

I would like to know if I am going in the correct direction? If not what are improvement in thinking process as well as in code needed?

PS: I know I could have written the above api easily by exporting functions but this is an attempt to learn object composition.

CodeYogi
  • 1,352
  • 1
  • 18
  • 41
  • Well, both your `endpoints` will end with `?key=undefined`... – Patrick Roberts Jul 14 '17 at 03:38
  • Also unless your API endpoints return JSON like `{ data: { data: { ... } } }`, that object decomposition, won't work. – Patrick Roberts Jul 14 '17 at 03:44
  • `{ Object.create(weatherApi), Object.create(service), Object.create(config), ...obj }` is a syntax error – Bergi Jul 14 '17 at 03:52
  • What is your understanding of "object composition" (in the classical paradigm), and how did you try to apply it to JS? I don't see anything I'd call "composition" in the code you posted. – Bergi Jul 14 '17 at 03:54
  • @Bergi thanks, I am still learning es6 but my bigger concern is learning the core concepts. – CodeYogi Jul 14 '17 at 03:54
  • @Bergi ok, let me know then. – CodeYogi Jul 14 '17 at 03:56
  • @Bergi according to me object composition is all about delegating behaviour. I agree that I may be wrong hence I am here. – CodeYogi Jul 14 '17 at 03:57
  • @CodeYogi Ah, in that sense you did fine, however for delegating behaviour you don't need objects - in JS, you can delegate directly to functions. "[object composition](https://en.wikipedia.org/wiki/Object_composition)" means (at least to me) that objects contain/consist of other objects. – Bergi Jul 14 '17 at 04:08
  • @Bergi if you feel that there can be some improvements then please feel free to post an answer. – CodeYogi Jul 14 '17 at 06:51
  • As I said, your approach is fine. If you did try it and get any errors (I think you should have?), please tell us what those were so that we can help you fix them. – Bergi Jul 14 '17 at 07:05

0 Answers0