npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

onyx-services-kwivrr

v1.0.14

Published

Onyx Services - Kwivrr

Downloads

4

Readme

onyx-services-kwivrr-sdk

The place where your API implementations go. Currently only VibeApi is available but will likely include service provider classes for additional vendors in the future (ie: content, localization, etc.)

Typically used for accessing external resources from within your getStore function -- onyx-app's <StoreWrapper /> provides a vibeApi instance to it's getStore call.

Conventions for Services

  • Always return a Promise from your service calls.
    • This allows for chaining the success / failure result from the calling locationthe calling location
  • Avoid async / await in favor of Promise
  • Camelize returned data structure.
    • Use onyx-common/camelize, just pass the obj in it takes care of the rest:
import camelize from 'onyx-common/camelize'

const obj = {
  first_name: 'Bob',
  last_name: 'Johnson'
}

const camelizedObj = camelize(obj)

/*
camelizedObj  = {
    firstName: 'Bob',
    lastName: 'Johnson'
}
*/
  • Always use traditional function () syntax for class methods
    • If arrow methods () => {} are used, the this context is lost from the compiled class.

fetchWrap helper

fetchWrap wraps the default fetch function to make it a bit more friendly to use. It accepts the following options:

  • url: The URL to use for this request
  • method: POST, DELETE or GET. GET is default
  • data: an array of data to send with request
  • urlMerge: if you provide placeholders in the url in the form of http://google.com/{foo}/{bar} send in an array with { 'foo': '1', 'bar': '2' } and it becomes http://google.com/1/2
  • requestType: json or form to change request headers. Has no impact on GET requests
  • fetchOptions: direct fetch api options to pass through and override any convenience additions

It also wraps the return to make it easier to work with and ensure a promise is always returned.

Learn more

Structure of a Service Class

To make services more manageable, they should be broken out into sub files for each area of responsibility (roughly mapped often to onyx-scenes-* packages, but not neccessarily). responsibilities and then rolled up into a single namespace.

Here's an example of how the VibeApi class is composed:

Base object with constructor and other shared functionality

VibeApi/VibeApiBase.js

const VibeApiBase = function () {}

VibeApiBase.prototype.constructor = function ({ baseUrl }) {
  this.baseUrl = baseUrl
}

VibeApiBase.prototype.getUrl = function (path) {
  return this.baseUrl + path
}

export default VibeApiBase

Add one or more prototype decorators

VibeAPi/VibeApiAuth.js

import formurlencoded from 'onyx-common/form-urlencoded'
import urlencode from 'onyx-common/urlencode'
import fetchWrap from 'onyx-common/fetchWrap'
import camelize from 'onyx-common/camelize'

const VibeApiAuth = ({ prototype }) => {
  prototype.login = function ({ username, password }) {
    // ...rest of function

    // example of how to use fetchWrap helper
    return fetchWrap(payload)
      .then(res => Promise.resolve(normalize(res)))
      .catch(error => Promise.reject(Error('loginError', error)))
  }

  prototype.logout = function ({ authenticationToken }) {
    // ...rest of function

    // example of how to use fetchWrap helper
    return fetchWrap(payload)
      .then(() => Promise.resolve())
      .catch(() => Promise.reject(Error('logoutError')))
  }
}

export default VibeApiAuth

Combine Base & decorator(s)

VibeApi/VibeApi.js

import VibeApi from './VibeApiBase'
import VibeApiAuth from './VibeApiAuth'
// add more decorators in the future
// import VibeApiResourceLibrary from './VibeApiResourceLibrary'

// decorate the prototype
VibeApiAuth(VibeApi)

// add more decorators in the future
// VibeApiResourceLibrary(VibeApi)

export default VibeApi

The final class is now usable like so:

import VibeApiFactory from 'onyx-services/VibeApi'

const VibeApi = new VibeApiFactory({
  baseUrl: '/your/base/url'
})