@bizjournals/js-providers
v0.2.1
Published
ACBJ javascript state providers
Downloads
311
Keywords
Readme
Providers
Providers engage Bizjournal's APIs to provide critical data to our applications. A provider has three parts: resolution, parsing, and caching/storing.
Resolution uses a resolver, defaulted to axios.get to obtain data from the API endpoint.
When the method resolve is called the specific end point must be provided.
Parsing occurs when the response is received but before caching/storing takes place. The method
parser is called after resolve hits an API end point and receives a successful response.
Caching/storing occurs after parsing, and if a cached response can be provided prior to hitting
an API end point the promise will resolve immediately with those results. Caching is not required
for a provider, but when used it must be an instance of the StorageAbstract found in the
js-storage module.
Installing
Using npm:
npm install @bizjournals/js-providersUsing yarn:
yarn add @bizjournals/js-providersConfiguring your environment:
You will need to configure an NPM_TOKEN within your project to include these
private modules. The best idea is to use an existing project as an example.
For simple setups the .gitlab-ci.yml file within this project as a guide to accessing project variables via the .npmrc file
Examples
The Providers are used within our Vue environments but do not need to be. They do use ES6 and must be compiled down to support our support matrix.
import { meta } from "@bizjournals/utilities";
import { UserProvider, MarketProvider } from "@bizjournals/js-providers";
let market = meta("market:json");
Promise.resolve([
UserProvider.resolve(`/context/user-state?v=${new Date().getTime()}`),
MarketProvider.resolve(`/api/market/${market.market_code}`)
]).then(([user, market]) => {
console.log(user);
console.log(market);
window.analytics = window.analytics || {};
window.analytics.source = user.analytics.source;
window.analytics.accountType = user.analytics.accountType;
window._satReady = true;
// do something with the user and market variables
})ProviderAbstract
This is the base class with the primary API methods. Each of the Providers are extensions
of this class and may contain additional functionality. The resolve method follows the API
argument pattern of whatever the resolver is set to, by default axios.get.
API
constructor(StorageReference) : ProviderAbstract
resolve(...args) : Promise
resolver() : Function
parser({ data }) : Mixed
withCache(StorageReference) : ProviderAbstract
hasCache() : Boolean
checkCache() : Object|null
MarketProvider
This hooks into local storage to store returns from the api underneath a namespaced prefix.
API
Some slight variations from the default API for this module.
constructor(marketCode : String)
resolve(endpoint : String, options : Object)
Local Storage
The reference used presently is bizj.m.market.${marketCode} which will only save when there is
sufficient memory for the browser agent to store the results. Otherwise, it will proceed without
conflict and retrieve and return the data from the API endpoint.
Returns
{
analytics_code: String,
circ_product_id: String,
city: String,
is_virtual: Boolean,
local_sales_tax: String,
market_abbrev: String,
market_code: String,
market_color: String,
market_id: String,
market_name: String,
primary_zip_code: String,
region: String,
region_objective: String,
state: String,
timezone: String,
utc_dst_offset: String,
utc_std_offset: String,
markets: Array,
channels: Array,
relationships: {
navigation: Object,
journal: Object,
commerce: Object,
social: Object
}
}UserProvider
This hooks into local storage to store returns from the API endpoint provided.
API
Some slight variations from the default API for this module. The tearDown method
is used to destroy references which are needed to perform a sign out on our site.
constructor()
resolve(endpoint : String, options : Object)
tearDown()
Local Storage
The reference used presently is bizj.m.user which will only save when there is
sufficient memory for the browser agent to store the results. Otherwise, it will proceed without
conflict and retrieve and return the data from the API endpoint.
Returns
{
analytics: {
source: String,
accountType: String
},
cookies: Array,
email: String,
emailProducts: Object,
fromPortal: Boolean,
hasCart: Boolean,
noembargo: Array,
noembargo_subs: Array,
saved_article_ids: Array,
type: String,
welcome: String,
}