@pisell/utils 1.0.17 → 1.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -66,35 +66,47 @@ EscPosPrinter.prototype.clear = function () {
66
66
 
67
67
  // Print the order data.
68
68
  EscPosPrinter.prototype.print = function (host, sn, copies) {
69
+ var that = this;
69
70
  return new Promise(function (resolve, reject) {
70
71
  var xmlobj = new XMLHttpRequest();
71
- var url = "http://" + host + "/cgi-bin/print.cgi?sn=" + sn + "&copies=" + copies;
72
+ xmlobj.timeout = 10000;
73
+ var url = "https://" + host + "/cgi-bin/print.cgi?sn=" + sn + "&copies=" + copies;
72
74
  xmlobj.open("POST", url, true);
73
75
  xmlobj.onreadystatechange = function () {
74
76
  if (xmlobj.readyState === XMLHttpRequest.DONE) {
75
- alert("[status]\n" + xmlobj.status.toString(10) + "\n\n[response]\n" + xmlobj.responseText);
76
- var re = /task_id: (\d+)/;
77
- var matches = re.exec(xmlobj.responseText);
78
- resolve(matches[1]);
77
+ if (xmlobj.status.toString(10) === "200") {
78
+ var re = /task_id: (\d+)/;
79
+ var matches = re.exec(xmlobj.responseText);
80
+ resolve(matches[1] || "");
81
+ } else {
82
+ reject(xmlobj.responseText);
83
+ }
79
84
  }
80
85
  };
81
86
  xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
82
- xmlobj.send(this.orderData);
87
+ xmlobj.send(that.orderData);
83
88
  });
84
89
  };
85
90
 
86
91
  // Query print status.
87
92
  EscPosPrinter.prototype.queryStatus = function (host, sn, task_id) {
88
- var xmlobj = new XMLHttpRequest();
89
- var url = "http://" + host + "/cgi-bin/status.cgi?sn=" + sn + "&task_id=" + task_id;
90
- xmlobj.open("POST", url, true);
91
- xmlobj.onreadystatechange = function () {
92
- if (xmlobj.readyState === XMLHttpRequest.DONE) {
93
- alert("[status]\n" + xmlobj.status.toString(10) + "\n\n[response]\n" + xmlobj.responseText);
94
- }
95
- };
96
- xmlobj.setRequestHeader("Content-Type", "text/plain; charset=uft-8");
97
- xmlobj.send();
93
+ return new Promise(function (resolve, reject) {
94
+ var xmlobj = new XMLHttpRequest();
95
+ xmlobj.timeout = 10000;
96
+ var url = "https://" + host + "/cgi-bin/status.cgi?sn=" + sn + "&task_id=" + task_id;
97
+ xmlobj.open("POST", url, true);
98
+ xmlobj.onreadystatechange = function () {
99
+ if (xmlobj.readyState === XMLHttpRequest.DONE) {
100
+ if (xmlobj.status.toString(10) === "200") {
101
+ resolve(xmlobj.responseText);
102
+ } else {
103
+ reject(xmlobj.responseText);
104
+ }
105
+ }
106
+ };
107
+ xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
108
+ xmlobj.send();
109
+ });
98
110
  };
99
111
 
100
112
  //////////////////////////////////////////////////
@@ -88,37 +88,45 @@ EscPosPrinter.prototype.clear = function() {
88
88
  this.orderData = "";
89
89
  };
90
90
  EscPosPrinter.prototype.print = function(host, sn, copies) {
91
+ var that = this;
91
92
  return new Promise(function(resolve, reject) {
92
93
  var xmlobj = new XMLHttpRequest();
93
- var url = "http://" + host + "/cgi-bin/print.cgi?sn=" + sn + "&copies=" + copies;
94
+ xmlobj.timeout = 1e4;
95
+ var url = "https://" + host + "/cgi-bin/print.cgi?sn=" + sn + "&copies=" + copies;
94
96
  xmlobj.open("POST", url, true);
95
97
  xmlobj.onreadystatechange = function() {
96
98
  if (xmlobj.readyState === XMLHttpRequest.DONE) {
97
- alert(
98
- "[status]\n" + xmlobj.status.toString(10) + "\n\n[response]\n" + xmlobj.responseText
99
- );
100
- var re = /task_id: (\d+)/;
101
- var matches = re.exec(xmlobj.responseText);
102
- resolve(matches[1]);
99
+ if (xmlobj.status.toString(10) === "200") {
100
+ var re = /task_id: (\d+)/;
101
+ var matches = re.exec(xmlobj.responseText);
102
+ resolve(matches[1] || "");
103
+ } else {
104
+ reject(xmlobj.responseText);
105
+ }
103
106
  }
104
107
  };
105
108
  xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
106
- xmlobj.send(this.orderData);
109
+ xmlobj.send(that.orderData);
107
110
  });
108
111
  };
109
112
  EscPosPrinter.prototype.queryStatus = function(host, sn, task_id) {
110
- var xmlobj = new XMLHttpRequest();
111
- var url = "http://" + host + "/cgi-bin/status.cgi?sn=" + sn + "&task_id=" + task_id;
112
- xmlobj.open("POST", url, true);
113
- xmlobj.onreadystatechange = function() {
114
- if (xmlobj.readyState === XMLHttpRequest.DONE) {
115
- alert(
116
- "[status]\n" + xmlobj.status.toString(10) + "\n\n[response]\n" + xmlobj.responseText
117
- );
118
- }
119
- };
120
- xmlobj.setRequestHeader("Content-Type", "text/plain; charset=uft-8");
121
- xmlobj.send();
113
+ return new Promise(function(resolve, reject) {
114
+ var xmlobj = new XMLHttpRequest();
115
+ xmlobj.timeout = 1e4;
116
+ var url = "https://" + host + "/cgi-bin/status.cgi?sn=" + sn + "&task_id=" + task_id;
117
+ xmlobj.open("POST", url, true);
118
+ xmlobj.onreadystatechange = function() {
119
+ if (xmlobj.readyState === XMLHttpRequest.DONE) {
120
+ if (xmlobj.status.toString(10) === "200") {
121
+ resolve(xmlobj.responseText);
122
+ } else {
123
+ reject(xmlobj.responseText);
124
+ }
125
+ }
126
+ };
127
+ xmlobj.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
128
+ xmlobj.send();
129
+ });
122
130
  };
123
131
  EscPosPrinter.prototype.appendText = function(str) {
124
132
  for (var i = 0; i < str.length; i++)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pisell/utils",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "sideEffects": false,
5
5
  "main": "./lib/index.js",
6
6
  "module": "./es/index.js",