All Articles

Building Node-Codementor

The problem

I recently had to file my taxes and even though Codementor has a UI for viewing all payouts received I needed something a bit more granular with quarterly/monthly breakdown of payments so I can get my payout data easily when tax season comes around again. I decided to build something custom for myself. Fortunately, Codementor has a well documented API with endpoints for fetching all your data. I wrote a small script that gets your data using the api and saves it to a secure google sheet.

I might make the script open-source at some point but this article is about the node-codementor abstraction I wrote that the script uses to interact with the api.

What is Node-Codementor?

In simple terms node-codementor is an npm package that lets you interact with the codementor api using your api-key. It has methods such directPayments() and sessions() (and a lot more) which respectively fetch your payments and your sessions data.

Stack

The package was built with Typescript, mostly because i’m a big fan of Types and it’s a lot easier to ship a package due to the automatically generated Type file; which means it can be easily used in both Javascript and Typescript environments.

Process

Since I wanted to complete this project very quickly I followed the KISS principle. The package exports a class that you instantiate using your api-key. The Class itself is a namespace for housing all available api methods. Each method within the class returns a publicly available function which is called from a subclass tied to an individual endpoint, For Example; The freelanceJobs method was implemented like so;

 async freelanceJobs(startingAfter?: string) {
        return await new FreelanceJobs(this.self.apiKey).fetch(startingAfter);
    }

I decided to build it this way for a couple of reasons;

Firstly, I wanted the code to be easy to read/understand. Having all the available methods exported from one place makes the code less opaque and a lot easier to maintain.

Secondly, having each endpoint in its own file makes extending and adding custom logic to said endpoint a breeze.

Each Endpoint Class inherits from a Base class which contains a request function. It is this function that makes the api call to codementor using axios.

The function takes 3 arguments (resource, params and httpMethod). The resource argument is the relateive url specific to the endpoint you’re trying to hit.

The optional params argument is an object which contains all request parameters and the httpMethod argument maps to the HTTP Method for the request (The Codementor api only supports GET and POST). The request function creates an axios config object using the passed in arguments and makes the http request to codementor returning a promise.

Final thoughts and next steps;

The package has been published and is available on npm here. I still need to add a couple of more features and loads of tests (PRs welcome!)

You can visit the github page for more details and documentation. If you find any bugs please file an issue on the github page and i’ll attend to it ASAP.

Thanks For Reading!