0.7.1 • Published 11 months ago

@justeattakeaway/cc-stampcards-adapter v0.7.1

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
11 months ago

StampCards Adapter

This package is used to supply the frontend with users stampcards. It connects with the stampcards API and returns the transformed data into cards. The package is built in typescript and is a replacement method for getting users stampcards via braze.

The entry point for the package is index.ts with the function stampCardsAdapter being the method supplied by the package. The function accepts 3 parameters token, tenant and url. Here the token is the users JWT, the tenant is the users selected tenant and the url is the API URL for the stampcards API.

The stampCardsAdapter function returns an object with 3 functions (initialise, handleCardClick, handleCardView) and the source string. It also implements the Adapter interface like the braze adapter.

The initialise method requires 3 parameters filters which is an array of filter functions that take an array of cards and return a modified array of cards. The callback which is supplied to the adapter by the consuming application and returns formatted cards. The errorCallback which is called upon any errors occurring during the retrieval of users stampcard data.

Inside the initialise method we call fetch with the provided url and attach the token to the authorization header. If successful the fetch returns response.ok true. The conversationId is then retrieved from the response headers, and we call the .json() method to get the json body response.

Cards are then transformed using the transformIntoCards method, taking the response from the api and creating cards from the inProgress and rewards response attributes and flat mapping them into one array. Filters are then applied to the transformed cards. Cards are then returned via the callback function. After this the createApiSuccessEvent is fired.

Installing the Adapter

To install the package run one of the following:

yarn add @justeattakeaway/cc-stampcards-adapter

npm install @justeattakeaway/cc-stampcards-adapter

Instantiating the Adapter

To use the adapter you first have to call the function with an Object containing following parameters

{
    token: () => '<THIS WOULD BE THE TOKEN>',
    tenant: '<USERS TENANT>',
    url: '<API_URL>'
}

Example adapter call:

import stampcardsAdapter from "@justeattakeaway/cc-stampcards-adapter";

const adapter = stampcardsAdapter({
  token: () => '<THIS WOULD BE THE TOKEN>',
  tenant: '<USERS TENANT>',
  url: '<API_URL>'
});

Using the adapter

Getting Cards

Once the adapter function has been called you then need to do the following to use the adapter.

adapter.initalise(
  [], // filters
  (cards) => {
  // do somthing with cards
  }, (error) => {
  // do somthing with errors
  }
)

Getting Adapter Source

The adapter source can get obtained by using the source attribute on adapter. This is useful when looping over multiple adapters

adapter.source