Skip to content

API Overrides

Kyle La Barge edited this page Jul 19, 2019 · 20 revisions

Compatibility

  • ✅ 2.1 | ✅ 2.0 | ⛔ 1.9 | ...

Background

QZ Tray 2.0 is bundled with RSVP to provide ECMAScript 6 Promise support. If RSVP is not desired, it can be overridden using qz.api.setPromiseType(...) to avoid ReferenceError: RSVP is not defined or Uncaught TypeError: Cannot read property 'promise' of null. Override examples are provided below.

Native Promises

  1. Override RSVP with native Promises using qz.api.setPromiseType(...).
qz.api.setPromiseType(function promise(resolver) { return new Promise(resolver); });

BlueBird

  1. Include the new promise library:
<script type="text/javascript" src="https://cdn.jsdelivr.net/bluebird/latest/bluebird.js"></script>

or via npm : npm install bluebird

  1. Override RSVP with Bluebird using qz.api.setPromiseType(...).
// var Promise = require('bluebird');
qz.api.setPromiseType(function promise(resolver) { return new Promise(resolver); });

Q

  1. Include the new promise library:
<script type="text/javascript" src="https://rawgit.com/kriskowal/q/v1/q.js"></script>

or via npm : npm install q

  1. Override RSVP with Q using qz.api.setPromiseType(...).
var Q = require('q');
qz.api.setPromiseType(Q.Promise);

RSVP

Keep RSVP, but use it through a framework that doesn't pollute the global namespace:

var RSVP = require('rsvp');
qz.api.setPromiseType(function promise(resolver) { return new RSVP.Promise(resolver); });

WebSocket

As of Node 6.5, WebSockets are only available through 3rd party libraries causing Error: WebSocket not supported by this browser.

var WebSocket = require('ws');
qz.api.setWebSocketType(WebSocket);

Override SHA256

A hashing algorithm is required for signature validation. Use qz.api.setSha256Type(...) to override the default hashing library and avoid TypeError: _qz.tools.hash is not a function.

Native

Since QZ Tray 2.0.5, the native browser crypto can be used. This will only work with HTTPS pages.

Node 6.5

qz.api.setSha256Type(function(data) {
   return crypto.createHash('sha256').update(data).digest('hex');
});
  • 2.0.1 and older only. Newer versions include this logic by default.

Node 4.5

  • Requires sha.js
var createHash = require('sha.js');
qz.api.setSha256Type(function(data) {
    return createHash('sha256').update(data).digest('hex');
});

AngularJS

import * as sha256 from 'js-sha256';
qz.api.setSha256Type(function(data) { return sha256(data); });

Node Quickstart

Install dependencies:

npm install qz-tray ws q

Provide API overrides and start talking to QZ Tray:

var qz = require('qz-tray');
qz.api.setPromiseType(require('q').Promise);
qz.api.setWebSocketType(require('ws'));

qz.websocket.connect()
.then(qz.printers.getDefault)
.then(function(printer) {
   console.log("The default printer is: " + printer);
})
.then(qz.websocket.disconnect)
.then(function() {
   process.exit(0);
})
.catch(function(err) {
   console.error(err);
   process.exit(1);
});

AngularJS Quickstart

This is only for the API overrides. To set up signing, see assets/signing/sign-message.ts.

Install dependencies:

npm install qz-tray js-sha256

Provide API overrides and start talking to QZ Tray:

import * as qz from 'qz-tray';
import { sha256 } from 'js-sha256';

qz.api.setSha256Type(data => sha256(data));
qz.api.setPromiseType(resolver => new Promise(resolver));

qz.websocket.connect()
 .then(qz.printers.getDefault)
 .then(printer => console.log("The default printer is: " + printer))
 .then(qz.websocket.disconnect)
 .catch(err => console.error(err));
Clone this wiki locally