UNPKG

@iotize/device-client.js

Version:

IoTize Device client for Javascript

89 lines (88 loc) 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var result_code_1 = require("../../api/response/result-code"); var core_1 = require("../../../core"); var response_error_1 = require("../../api/response/response-error"); var Response = /** @class */ (function () { function Response(data) { this.data = data; this._body = undefined; } Response.SUCCESS = function (data) { return Response.create(result_code_1.ResultCode.IOTIZE_200_NO_ERROR, data); }; Response.ERROR = function (errorCode) { errorCode = errorCode || result_code_1.ResultCode.IOTIZE_400_BAD_REQUEST; return Response.create(errorCode); }; Response.create = function (codeRet, data) { if (data && data.length > 0) { var frame = new Uint8Array(1 + data.length); frame[0] = codeRet; frame.set(data, 1); var response = new Response(frame); return response; } else { return new Response(Uint8Array.from([codeRet])); } }; Response.prototype.body = function (decoder) { this.successful(); if (decoder) { return decoder.decode(this.rawBody()); } else if (!this._body) { if (this.decoder) { this._body = this.decoder.decode(this.rawBody()); } else { this._body = this.rawBody(); } } return this._body; }; Response.prototype.rawBody = function () { if (this.data != null && this.data.length > 1) { return this.data.slice(1); } else { return new Uint8Array(0); } }; Response.prototype.setBodyDecoder = function (decoder) { this.decoder = decoder; return this; }; Response.prototype.hasBody = function () { return this.rawBody() != null; }; Response.prototype.codeRet = function () { return this.data[0]; }; Response.prototype.toString = function () { return "ResponseMessage[codeRet=\"" + this.codeRet() + "\";data=\"" + (this.hasBody() ? core_1.FormatHelper.toHexString(this.rawBody()) : "NO DATA") + "\";decoder=" + (this.decoder ? this.decoder.constructor.name : "NONE") + "]"; }; Response.prototype.toBytes = function () { return this.data; }; Response.prototype.setBody = function (body) { this._body = body; }; Response.prototype.isSuccess = function () { return (this.codeRet() & 0x80) == 0; }; Response.prototype.isSuccessful = function () { return (this.codeRet() & 0x80) == 0; }; Response.prototype.successful = function () { if (!this.isSuccessful()) { throw new response_error_1.ResponseError(this, this._request); } }; Response.prototype.setRequest = function (request) { this._request = request; }; return Response; }()); exports.Response = Response;