UNPKG

@arc-publishing/sdk-identity

Version:
93 lines 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteAvatar = exports.uploadAvatar = void 0; var tslib_1 = require("tslib"); var APIErrorResponse_1 = require("../serviceHelpers/APIErrorResponse"); var JSONResponseHandler_1 = tslib_1.__importDefault(require("../serviceHelpers/JSONResponseHandler")); var identity_1 = tslib_1.__importDefault(require("./identity")); var constants_1 = require("./constants"); function uploadAvatar(inputField) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { console.warn('[Arc Identity SDK]: uploadAvatar is deprecated'); return [2, identity_1.default.heartbeat().then(function () { var fileInput; if (typeof inputField === 'string') { fileInput = document.getElementById(inputField.replace(/^#/, '')); } else { fileInput = inputField; } if (!fileInput) { throw Error("".concat(constants_1.logPrefix, " element provided not found ").concat(typeof inputField === 'string' ? "(".concat(inputField, ")") : '', ".")); } else if (!fileInput.files || !fileInput.files.length) { throw { message: "No image found in the input field provided ".concat(typeof inputField === 'string' ? "(".concat(inputField, ")") : '', "."), code: 'MISSING_IMAGE' }; } else if (fileInput.files[0].type !== 'image/jpeg') { throw { message: 'Image type not supported', code: 'UNSUPPORTED_IMAGE_TYPE' }; } else if (fileInput.files[0].size > 3e6) { throw { message: 'Image too big. Only images up to 3 MB are supported.', code: 'IMAGE_TOO_LARGE' }; } else { var imageFile = fileInput.files[0]; var body_1 = new FormData(); body_1.append('avatar', imageFile); return identity_1.default.heartbeat().then(function () { return fetch("".concat(identity_1.default.apiOrigin, "/identity/public/v1/avatar"), { method: 'PUT', headers: { Authorization: "Bearer ".concat(identity_1.default.userIdentity.accessToken) }, body: body_1 }) .then(JSONResponseHandler_1.default) .then(function (json) { if ((0, APIErrorResponse_1.isAPIErrorResponse)(json)) { throw json; } else { fileInput.value = ''; identity_1.default.userProfile = json; return json; } }); }); } })]; }); }); } exports.uploadAvatar = uploadAvatar; function deleteAvatar() { console.warn('[Arc Identity SDK]: deleteAvatar is deprecated'); return identity_1.default.heartbeat().then(function () { return identity_1.default.heartbeat().then(function () { return fetch("".concat(identity_1.default.apiOrigin, "/identity/public/v1/avatar"), { method: 'DELETE', headers: tslib_1.__assign({ Authorization: "Bearer ".concat(identity_1.default.userIdentity.accessToken) }, constants_1.headers) }) .then(JSONResponseHandler_1.default) .then(function (json) { if ((0, APIErrorResponse_1.isAPIErrorResponse)(json)) { throw json; } else { if (identity_1.default.userProfile) { identity_1.default.userProfile.picture = ''; } return json; } }); }); }); } exports.deleteAvatar = deleteAvatar; //# sourceMappingURL=avatar.js.map