Making GitHub-Invite, My First NPM Package

Obum
5 min readJun 27, 2020

After taking part in 30 days of code, AKINJOBI Sodiq asked us, backend participants, to use the GitHub API to create a web app that can send invitations to GitHub users to join GitHub organizations. At the end of the project, I came up with different working versions of the same GitHub-Invite which are:

  1. Node Monolith Version
  2. Static Frontend Version
  3. NPM package

Why I made GitHub-Invite

To me, it first sounded unreasonable. After all, the moment you create an organization on GitHub, GitHub directly gives you the ability to invite members to that organization based on their emails or GitHub username. However, two things changed my mind:

  1. Someone requested it, Others may need it. I felt because AKINJOBI Sodiq needed it, it will do good to help him. Besides, not just him, but any other person that will ever need such a tool.
  2. Command-Line version
Create a new repo from the command-line using repo-gen package

I saw the above tweet and loved the fact that for once, one could create a GitHub repo directly from the command line, without having to open a browser and I felt it could be the same for inviting members to a GitHub organization. Besides, some developers extensively use the command line, so the github-invite’s npm package could help them.

Demanding access tokens

GitHub deprecating username/password authentication for API calls in favor of personal access tokens or OAuth tokens left me with no choice than to use tokens to make invitations in the GitHub-Invite project. I decided to use personal access tokens since any GitHub user can easily create one from their account settings at https://github.com/settings/tokens/new

Note: Personal Access Token used to make invitations through GitHub-Invite must have at least the admin:org permission, and it must be the token of an admin since only admins/owners can invite other members to a GitHub organization.

a screenshot of the admin:org permission of personal access tokens on GitHub accounts
admin:org permission of personal access token

Node Monolith Version

So I began the GitHub-Invite project with the usual NodeJS/Express server for the web app, made its frontend, and published it on GitHub.

A demo of using the node-monolith version of GitHub-Invite
A demo of using the node-monolith version of GitHub-Invite

It has its benefits and drawbacks

  • Pros: I made the frontend to be served to GitHub users that were to become members of an organization. It read ‘Get Invited’. The user enters their username and will receive an email from GitHub, all things being equal, and the app will tell them to check their inbox.
screenshot of a successful invitation with GitHub-Invite
successful invitation done by GitHub-Invite

This monolith server version could be set up by some IT administrator and they could give the link to the necessary members.

  • Cons: Was it really a disadvantage? Probably only to those that would find it demanding to host monolith servers instead of static files. Besides, it is a growing trend that JavaScript and APIs or JAMSTACK are preferred to monolith, server-run web apps. I knew no easier way to demand the admin’s access token in the server than by importing as a dependency. One has to add an org.js file that exports the organization’s name and admin’s token for the server to accept usernames and send invitations.
A screenshot of README on Node Monolith version of GitHub-Invite, describing how to add an org.js file.
Exporting organization’s name and admin’s access token from org.js file.

Static Frontend Version

The above led to the next version. I brought the invitation logic to the frontend, added more styles, made sure it was mobile responsive, and then published it on GitHub. Besides, it was made of static files, so I could host it with GitHub pages at https://obumnwabude.github.io/github-invite/

All I did was to rename the folder containing this version to docs and then enable hosting from the folder in the repo settings. There is no analytics or third-party tool attached to ensure tokens are not stolen. GitHub pages are served with HTTPS, so security is assured. What I love most about this version, is the fact that it is mobile responsive, who knows any could access directly from their phone.

A demo of using the static frontend version of GitHub-Invite on mobile

The NPM package

For me, this was the killer. Like I said earlier, Samson’s tweet about repo-gen was a motivation to making a command-line interface for GitHub-Invite. To use it, make sure Node/NPM is installed on your computer, if not download it from https://nodejs.org and install it. Then run the following commands in the command line:

npm i -g github-invite

Then perform your invitation. Feel free to either use this tool interactively by typing github-invite and pressing enter

github-invite

OR

Feeding in all the required arguments at the same time:

github-invite <org> <username> <token>

As need may arise, the help is always there to guide you, just type

github-invite –h
A screenshot of GitHub-Invite’s help on the command-line
GitHub-Invite’s help on the command-line

I learned a lot while creating this package. It is actually my first package on NPM and I can say creating and publishing npm packages is easy. I used Chalk to make the feedback colorful and use Inquirer to make github-invite interactive.

demo of using GitHub-Invite in the command-line
demo of using GitHub-Invite in the command-line

I made sure I handled errors and gave proper feedback were possible both in this NPM package and the web apps mentioned earlier.

A screenshot of when an invalid GitHub username is given to command-line GitHub-Invite
Invalid GitHub username given to command-line GitHub-Invite

Check out the NPM package github-invite at https://npmjs.com/package/github-invite

Check out the static frontend version at https://obumnwabude.github.io/github-invite

Check out the code of GitHub-Invite at https://github.com/obumnwabude/github-invite/

Thank you!

--

--

(Obumuneme Nwabude) => Mobile and Fullstack Web Developer. => Loves the Flutter Framework. => Active in Tech Communities. => Writes Articles.