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

@tradeshift/io

v4.0.0

Published

ts.io - Tradeshift App Messaging Library

Downloads

19

Readme

travis](https://travis-ci.org/Tradeshift/io) npm Greenkeeper badge

ts.io

Tradeshift App Messaging Library

This is the standard way for apps on the client-side of the Tradeshift Platform to

  • communicate with the Tradeshift® Chrome™,
  • communicate with other apps,
  • open other apps in separate iframes/popups and wait for the result of user interactions.

Why?

  • allow third-party app developers to utilize first-party app panels & modals all across the Tradeshift Web UI,
  • allow first-party legacy apps to utilize first-party app panels & modals all across the Tradeshift Web UI,
  • allow pinning ts.ui versions for specific legacy apps within the Tradeshift Platform to facilitate easy upgrades,
  • have a generic, controlled and secure way of communicating between apps, regardless of iframe sandboxing or cross-domain requests.

Limitations

  • There can be only one App (Client) in a single Window context (iframe/popup).
  • Any postMessages sent by an App (Client) are sent to window.top where the Hub (The Broker) runs.
  • Any postMessages received by an App (Client) are sent from window.top through the Hub (The Broker).
  • The Tradeshift® Chrome™ keeps track of all apps and decides which ones have access to which ones.
    • Spawned iframes can only communicate with their spawner and their own spawnees and Tradeshift.Chrome.

Installation

npm install @tradeshift/io

Usage

import io from '@tradeshift/io';

// Create the App and connect to the Hub
const app = io();

// Spawn an App:
(async () => {
  // Do the actual spawn call
  const [err, data] = await app.spawn(targetApp, targetData);

  if (err) {
    // Something went horribly wrong while spawning app
  } else {
    doSomethingWith(data);
  }
})();

// Allow your app to be spawned by others:
app.define({
  async spawn(data) {
    // Wait for user input here...
    const userData = await myPrompt();

    // Send response back to parent
    return userData;
  }
});

ts.io API reference (quick overview)

Spawning another app

import io from '@tradeshift/io';
const app = io();

const [err, data] = await app.spawn(targetApp, targetData);

Defining app lifecycle when others spawn your app

To handle the request from other apps, an app needs to define a spawn handler. The recommended API uses promises to allow an async/await style handler, and makes it possible to do a series of async transitions before returning the value to the parent app.

import io from '@tradeshift/io';
const app = io();

app.define({
  async spawn(data) {
    // CODE HERE: Animate opening your UI and wait for user input.

    // Wait for user input here...
    const userData = await myPrompt();

    // CODE HERE: Animate closing your UI.

    // Send response back to parent
    return userData;
  }
});

Callback based Spawn handler if you prefer:

app.define({
  spawn(data, submit, parent) {
    // Send response back to parent
    submit(userData);
  }
});

Talking to other apps w/ events

import io from '@tradeshift/io';
const app = io();

// Events
/*
 * IMPORTANT!
 * You can register as many handlers as you'd like,
 * but if multiples match the same topic,
 * they all will be called, in the order they were registered.
 */

// Listen to all events sent to my App
app.on('*', event => {});

// Listen to events for a specific topic (sent to my App)
const myListener = event => {};
const myTopic = 'my-topic';
const unlisten = app.on(myTopic, myListener);
// Stop listening
unlisten();
// OR
app.off(myTopic, myListener); // nice to have

// This listener will only be called once
app.once(myTopic, myListener); // nice to have

// Send events to other Apps
// app.emit(topic[, data], app);
app.emit('fly-high', { flameColor: 'red' }, 'Tradeshift.FlamingSkull');

Experimental request API

(async () => {
  const [err, response] = await app1.emit('topic-with-request', 'App2', data);
  if (err) {
    // Something went horribly wrong while emitting
  } else {
    doSomethingWith(response);
  }
})();
app1.emit('topic-with-request', 'App2', data);
app1.emit('topic-without-request', 'App2', data);

app2.on('topic-with-request', event => {
  return 'my-response';
});

app2.on('topic-without-request', event => {});