Nanoprogrammed Penultimate Musicianship
Wondering what’s next for npm?Check out our public roadmap! »

@apptus/esales-lifestyle-api
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

esales-lifestyle-api

A helper library for making requests to the eSales Lifestyle API. It includes type definitions for all HTTPS responses and the library API.

Installation

npm install @apptus/esales-lifestyle-api

Requirements

  • Support for loading ES modules
  • Browser usage only (for now)
  • Transpiling to desired browser support
  • Language API's (see polyfills)

Read more in the tooling section.

Usage

The full API and all options/return-values are described by the Typescript definitions (dist/lib.d.ts) that is bundled with this library.

import { esales } from '@apptus/esales-lifestyle-api';


const api = esales({
  market: 'GB',
  webApiId: '<ID>',
  touchpoint: 'desktop'
});

async function main() {
  const result = await api.query.searchPage({ q: 'jeans', limit: 60 });
  // Use the search result
}

main().catch(err => console.warn(err));

A CHANGELOG.md is included in the package, but it can't be read online at NPM. unpkg.com can be used to easily view the contents: https://unpkg.com/browse/@apptus/esales-lifestyle-api/CHANGELOG.md

Work in progress

This library is in active development. Highest on the agenda:

  • Open source repository
  • Keeping a CHANGELOG.md (blocked by item above)
  • NodeJS support in addition to browser support

Tooling

This library is packaged as an ES Module and depends on the uuid package. This requires the use of a pre-processor step since bare-imports are not natively supported in browsers (without import maps), but most build tools should handle this.

Additionally, in order to ship smallest possible bundle, the code is using a modern ES2020 syntax. This will likely not work with the desired browser support out of the box, and will likely require transpiling before use. A small motivation for this decision, can be read below. If there is a desire for a different syntax target, we will gladly revisit this decision.

Transpilation

An example of how to transpile this library when building with Webpack is shown below. Something similar can be done with Rollup by using the include property with @rollup/babel-plugin.

// webpack.config.js
const { resolve } = require('path');

module.exports = {
  module: {
    rules: [
      {
        test: /\.m?js$/,
        type: 'javascript/auto',
        include: [resolve(__dirname, 'node_modules/@apptus/esales-lifestyle-api')],
        use: ['babel-loader']
      }
    ]
  }
};

Minified version

There is a optimized minified bundle available from dist/lib.min.mjs. If using Webpack, you can use resolve.alias to modify the entrypoint to this package. With Rollup, this could likely be solved by using @rollup/plugin-alias.

Polyfills

Depending on the level of required browser support, polyfills may need to be included before using this library. It is assumed that the following API's are available:

  • All ES2015 API's
  • window.fetch
  • window.URL
  • Object.entries
  • Object.values
  • navigator.sendBeacon (used for notifications sent to eSales)

Motivation for bundling modern code

It's getting more and more critical to deliver well optimized web pages and apps. A big source of increased code sizes comes from dependencies that are transpiled to e.g. ES5, or bundling polyfills.

A short example: At the time of this writing (05/11/2020), @auth0/auth0-spa-js is bundled with ES5 is it's target, and includes it's polyfills in the bundle. It's at 102KB minified. By removing polyfills, which can easily become duplicated, we save 36KB. By setting the target to ES2018 we save an additonal 28KB, for a total of 37KB.

Many dependencies are likely not this drastic, but it often becomes "Death by a thousand cuts" due to many small increases in code size from many dependencies.

Because every page or app likely have their own targeted browser support, it becomes impossible to have a single ES target that fits everyone. This is the reason the bundle for this library is currently delivered as ES2020, so that the page or app can transpile this library to match their own browser support.

License

MIT

Install

npm i @apptus/esales-lifestyle-api

DownloadsWeekly Downloads

469

Version

1.2.0

License

MIT

Unpacked Size

198 kB

Total Files

112

Last publish

Collaborators

  • avatar
  • avatar