@wireapp/api-client
Version:
Wire API Client to send and receive data.
62 lines (61 loc) • 2.43 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var AssetUtil_1 = require("./AssetUtil");
var random_1 = require("../shims/node/random");
var AssetRetentionPolicy_1 = require("./AssetRetentionPolicy");
var buffer_1 = require("../shims/node/buffer");
var AssetAPI = (function () {
function AssetAPI(client) {
this.client = client;
}
AssetAPI.prototype.getAsset = function (key, token) {
if (!AssetUtil_1.isValidKey(key)) {
throw TypeError('Expected key to only contain alphanumeric values and dashes.');
}
if (token && !AssetUtil_1.isValidToken(token)) {
throw TypeError('Expected token to be base64 encoded string.');
}
return this.client
.sendRequest({
method: 'get',
url: AssetAPI.ASSET_URL + "/" + key,
responseType: 'arraybuffer',
params: {
asset_token: token,
},
}, true)
.then(function (response) { return response.data; });
};
AssetAPI.prototype.postAsset = function (asset, options) {
var BOUNDARY = "Frontier" + random_1.unsafeAlphanumeric();
var metadata = JSON.stringify(Object.assign({
public: true,
retention: AssetRetentionPolicy_1.AssetRetentionPolicy.PERSISTENT,
}, options));
var body = '';
body += "--" + BOUNDARY + "\r\n";
body += 'Content-Type: application/json;charset=utf-8\r\n';
body += "Content-length: " + metadata.length + "\r\n";
body += '\r\n';
body += metadata + "\r\n";
body += "--" + BOUNDARY + "\r\n";
body += 'Content-Type: application/octet-stream\r\n';
body += "Content-length: " + asset.length + "\r\n";
body += "Content-MD5: " + buffer_1.base64MD5FromBuffer(asset.buffer) + "\r\n";
body += '\r\n';
var footer = "\r\n--" + BOUNDARY + "--\r\n";
return this.client
.sendRequest({
method: 'post',
url: AssetAPI.ASSET_URL,
headers: {
'Content-Type': "multipart/mixed; boundary=" + BOUNDARY,
},
data: buffer_1.concatToBuffer(body, asset, footer),
})
.then(function (response) { return response.data; });
};
AssetAPI.ASSET_URL = '/assets/v3';
return AssetAPI;
}());
exports.AssetAPI = AssetAPI;
;