@ag-digital/pantry

A Session Management Library

Usage no npm install needed!

<script type="module">
  import agDigitalPantry from 'https://cdn.skypack.dev/@ag-digital/pantry';
</script>

README

Pantry

Session Management library

Things it does

  • Provides a store for session information to be stored on the server
  • Can retrieve cookies stored
  • Save Cookies to the store
  • Make a request using the cookies gathered

How it works

By default the Pantry uses redis to store the cookies, so ensure that is installed

Then prepare the pantry by providing the redis options

const redisOptions = { host: '127.0.0.1', port: '6379' }
pantry(redisOptions)

Then this Pantry can have request chains from it in order to perform certain functions

return pantry(options)
  .retrieveSession(key)
  .request(cookies => requestFunction(query, url, cookies))
  .saveSession()
  .done()

This will return a promise containing the pantry that has build up containing properties such as key, cookies and body. Each function chained overwrites an object in pantry with the values it returns and are passed in. This allows future functions chained to access the results.

You can also use then to continue chaining additional promises.

return pantry(options)
  .retrieveSession(key)
  .request(cookies => requestFunction(query, url, cookies))
  .saveSession()
  .then(res => transform(res, type))
  .then(res => lookupInfo(res.body.id))
  .catch(err => throw err)

Chain Functions

retrieveSession

Retrieves the cookies from the store based on a key provided

pantry(options)
  .retrieveSession(key)
  • Arguments

    • key

The session key used to retrieve the cookies

  • Returns

    • cookies

request

Retrieves the cookies from the store based on a key provided

pantry(options)
  .request(cookies => requestFunction(query, url, cookies))
  • Arguments

    • request

A function that takes in cookies and should return an object containing a key, body and cookies.

  • Returns

    • key

    The session key returned
    • body

    The request response body
    • cookies

    The cookies returned

saveSession

Saves the cookies in the store based on a key provided

pantry(options)
  .saveSession()
  • Arguments

    • key

The session key used to save the cookies

  • cookies

The cookies array to save

  • Returns

    • Nothing

then

Ends the chain and behaves exactly like chaining then after a promise

pantry(options)
  .request(cookies => requestFunction(query, url, cookies))
  .then(res => transform(res, type))

done

Ends the chain and returns a promise containing the object built up inside pantry

pantry(options)
  .request(cookies => requestFunction(query, url, cookies))
  .done()