0

I'm building a content middleware which gather contents from our external publishers. The publishers will share their contents either in rss or json and the key/value field would be different from each other. To make thing easier, I created a config file where I can pre-defined the key/value and the feed type. The problem is, how can I dynamically return this config value based on publishers name.

Example: To get Publisher #1 feed type, I just can use config.articles.rojak_daily.url_feed

my config file /config/index.js

module.exports = {
    batch:100,
    mysql: {
        database: process.env.database,
        host: process.env.host,
        username: process.env.username,
        password: process.env.password
    },
    articles:{
        rojak_daily:{ // Publisher 1
            url: 'xxx',
            url_feed: 'rss',
            id: null,
            Name: 'title',
            Description: 'description',
            Link: 'link',
            DatePublishFrom: 'pubDate',
            LandscapeImage: 's3image',
            SiteName: 'Rojak Daily',
            SiteLogo: null
        },
        rojak_weekly:{ // publisher 2
            url: 'xxx',
            url_feed: 'json',
            id: null,
            Name: 'Name',
            Description: 'Desc',
            Link: 'link',
            DatePublishFrom: 'pubDate',
            LandscapeImage: 's3image',
            SiteName: 'Rojak Weekly',
            SiteLogo: null
        }
    }
}

my main application script

const config = require('@config'); // export from config file

class Main {
    constructor(){
      this.publishers = ['rojak_daily','rojak_weekly'];
    }

    // Main process
    async startExport(){
        try{
            for(let publisher of this.publishers){
                const feedType =  await this.getFeedType(publisher)
                const result = (feedType == 'rss')? await this.rss.start(publisher): await this.json.start(publisher)
                return result
            }
        }catch(err){
            console.log("Error occured: ", err)
        }


    }

    // Get feed type from config
    async getFeedType(publisher){
      return await config.articles.rojak_daily.url_feed; 
      // this only return publisher 1 url feed.
      // my concern is to dynamically passing variable 
      // into this config file (example: config.articles.<publisher>.url_feed)
    }

}

module.exports = Main

2 Answers 2

2
async getFeedType(publisher){
    return await config.articles[publisher].url_feed; 
}

You can access properties of objects by variable

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. You are my life saver. Its working perfectly
0

You could either loop over the articles by using Object.entries(articles) or Object.values(articles) in conjunction with Array.prototype.forEach(), or since you already have the name of the publisher you could access that entry with config.articles[publisher].url_feed, like so:

const config = {
  articles: {
    rojak_daily: { // Publisher 1
      url: 'xxx',
      url_feed: 'rss',
      id: null,
      Name: 'title',
      Description: 'description',
      Link: 'link',
      DatePublishFrom: 'pubDate',
      LandscapeImage: 's3image',
      SiteName: 'Rojak Daily',
      SiteLogo: null
    },
    rojak_weekly: { // publisher 2
      url: 'xxx',
      url_feed: 'json',
      id: null,
      Name: 'Name',
      Description: 'Desc',
      Link: 'link',
      DatePublishFrom: 'pubDate',
      LandscapeImage: 's3image',
      SiteName: 'Rojak Weekly',
      SiteLogo: null
    }
  }
}

const publishers = ['rojak_daily', 'rojak_weekly']

function getFeedType(publisher) {
  return config.articles[publisher].url_feed; 
}

publishers.forEach(publisher => console.log(getFeedType(publisher)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.