Refactor the Utopian API - Increase Maintainability

in #utopian-io6 years ago



This refactoring a step in the right direction as the code is currently messy. The codebase has been migrated to Typescript (no strict typing yet). This allows for future maintenance and refactoring to be much easier on any developer planning to contribute. This is in preparation for the registration API that will be implemented soon.

All compiled files get outputted into the dist folder. No more finding which directory the script belongs in since there is now a consistent output with compiled JS files.

During the port, a few bugs have been fixed as a result of Typescript's type checking. Thus already increasing Typescript's value and necessity for future development.

Bugs as a result of type errors

These bug fixes were a necessary change in order to ensure that the compilation of all the typescript files were successful.

utopian-bot.js

The variable i and posts was undefined in the current scope. The correct variable references are index and scoredPosts.

Change from

if (i + 1 === posts.length) {
    conn.close();
    process.exit(0);
}

Change to

if (index + 1 === scoredPosts.length) {
    conn.close();
    process.exit(0);
}

https://github.com/samrg472/api.utopian.io/commit/e36a2761b4cca237fe03798c4d83a2a412a46693#diff-6400fb412e7d4938f13996590829a323L285

updater-posts.js

next(e) is usually an express call. However, this script does not use express, removing the invocation fixes this issue.

Change from

.catch(e => {
    console.log(`ERROR UPDATING POST ${e}\n`);
    next(e)
})

Change to

.catch(e => {
    console.log(`ERROR UPDATING POST ${e}\n`);
})

https://github.com/samrg472/api.utopian.io/commit/e36a2761b4cca237fe03798c4d83a2a412a46693#diff-40e126890bb12bec4d502e91597fddbdL71

Other changes

Since babel is no longer in use, the copy and babel gulp tasks have been removed. Instead, it has been replaced with a ts compilation task that will compile the entire app.

What needs to be done

As a result of the refactor and new typescript compiler, some changes will need to be made on the server. The change is small but necessary. Whenever changes get pulled in npm run build will need to be executed to update the app (runs a compilation). Afterwards all future script runs won't require any compilation.

It used to be setup to require a babel compilation on every run and then run it live. This change will make start up a bit faster.

This is in preparation of the new registration API that was announced not to long ago. The future work that will go into it will be based off of this refactor to ease maintenance and future development.

Links

Commit: https://github.com/samrg472/api.utopian.io/commit/e36a2761b4cca237fe03798c4d83a2a412a46693
Pull request: https://github.com/utopian-io/api.utopian.io/pull/36



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

[utopian-moderator]

Congratulations @samrg472! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Hey @samrg472 I am @utopian-io. I have just upvoted you at 5% Power!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I want to see you often!
  • Work on your followers to increase the votes/rewards. My vote is now primarily based on that, humans rule. Good luck!
  • Wondering why other contributions got more? I introduced a competition factor. My vote is also based on how competitive the category used is.

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Vote for my Witness. Lets GROW TOGETHER!

Screen Shot 2017-11-19 at 16.54.19.png

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Great information

This is some excellent info :)

delete

Congratulations @samrg472! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @samrg472! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the total payout received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Sam is very wise when it comes to coding, much more than he gives himself credit for. I am sure this contribution will help immensely.

This is sure to help out a great deal, wonderful job!

Coin Marketplace

STEEM 0.29
TRX 0.11
JST 0.033
BTC 63945.57
ETH 3135.76
USDT 1.00
SBD 4.00