Refactor the Utopian API - Increase Maintainability

Words
356
Reading
2 min
Listen
Play
9y

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

Refactor the Utopian API - Increase Maintainability | Ecency