@lambdaschool/apios

A module to exchange a username and password for a pre-authed axios client. This can be used on the client or server.

Usage no npm install needed!

<script type="module">
  import lambdaschoolApios from 'https://cdn.skypack.dev/@lambdaschool/apios';
</script>

README

@lambdaschool/apios

A module to exchange a username and password for a pre-authed axios client. This can be used on the client or server.

Overview

When making a request to the lambda school api, you need to have a jwt set as the Bearer token. Doing this manually can be tedious and error prone. Apios will create an axios client that has the Authorization header set correctly.

Usage

npm i @lambdaschool/apios

Apios exports a single function that behaves slightly differently, depending on if you are on the server or the client.

Server

On the server, the apios function will always return a promise that resolves to an axios client. It can be used in 2 ways:

Usage 1

const client = await apios('my-email@aol.com', 'passw0rd');
const users = await client.get('/db/users') // Will make a request to api.lambdaschool.com/db/users, grabbing the correct domain from process.env.DOMAIN

These should be the same credentials you would use to login to auth.lambdaschool.com/sign-in through the browser. This form is useful when making a new request from the server.

Usage 2

myExpressApp.get('/', async (req, res, next) => {
  const client = await apios(req);
  const users = await client.get('/db/users'); // Will make a request to api.lambdaschool.com/db/users, grabbing the correct domain from process.env.DOMAIN
});

If the request object has cookies attached to it (e.g. the request was made to our fictional express app from lambdaschool.com and the user was logged in), the jwt will be parsed from the cookie and used to set the credentials on the Axios client.

Client

On the client, the apios function will always return an axios client directly (instead of a promise). It ignores any parameters passed in.

const client = await apios();
const users = await client.get('/db/users'); // Will make a request to api.lambdaschool.com/db/users, grabbing the correct domain from the current url

Development

npm test

Publishing

When ready to publish, follow these steps:

  1. Verify that all PRs have been merged into the staging branch.
  2. Open up a PR to merge the staging branch into the master branch. Typically the PR is named after the version that will be published, for example v2.1.0.
  3. After reviewing the changes that will be merged into master, determine the appropriate version change, major, minor, or patch.
  4. While on the staging branch locally, update the CHANGELOG.md file with an entry for the new version you're about to publish. Commit your changes.
  5. While still on the staging branch locally, version the project using npm version <semver> where <semver> is major, minor, or patch. For example: npm version patch.
  6. git push your local staging branch up to origin.
  7. When the PR has been reviewed and merged into master, the package will be published by CI after the tests pass.