UNPKG

@arc-publishing/sdk-identity

Version:
85 lines 3.6 kB
import { __assign, __awaiter, __generator } from "tslib"; import { isAPIErrorResponse } from '../serviceHelpers/APIErrorResponse'; import JSONResponseHandler from '../serviceHelpers/JSONResponseHandler'; import { dispatchLogoutEvent } from '../sdk/logout'; import Identity from './identity'; import { headers, logPrefix } from './constants'; export default function requestDeleteAccount() { return __awaiter(this, void 0, void 0, function () { var isLoggedIn; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, Identity.isLoggedIn()]; case 1: isLoggedIn = _a.sent(); if (!isLoggedIn) { throw new Error("".concat(logPrefix, " Missing or invalid jwt")); } return [2, fetch("".concat(Identity.apiOrigin, "/identity/public/v1/user/anonymize"), { method: 'DELETE', headers: __assign({ Authorization: "Bearer ".concat(Identity.userIdentity.accessToken) }, headers) }) .then(JSONResponseHandler) .then(function (json) { if (isAPIErrorResponse(json)) { throw json; } else { return json; } })]; } }); }); } export function approveDeleteAccount(nonce) { return Identity.heartbeat() .then(function () { return fetch("".concat(Identity.apiOrigin, "/identity/public/v1/user/anonymize/approve/").concat(nonce), { method: 'PUT', headers: __assign({ Authorization: "Bearer ".concat(Identity.userIdentity.accessToken) }, headers), body: JSON.stringify({}) }); }) .then(JSONResponseHandler) .then(function (json) { if (isAPIErrorResponse(json)) { throw json; } else { Identity.clearSession(); dispatchLogoutEvent(); return json; } }); } export function declineDeleteAccount(nonce, reason) { return __awaiter(this, void 0, void 0, function () { var isLoggedIn; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, Identity.isLoggedIn()]; case 1: isLoggedIn = _a.sent(); if (!isLoggedIn) { throw new Error("".concat(logPrefix, " Missing or invalid jwt")); } return [2, fetch("".concat(Identity.apiOrigin, "/identity/public/v1/user/anonymize/decline/").concat(nonce), { method: 'PUT', headers: __assign({ Authorization: "Bearer ".concat(Identity.userIdentity.accessToken) }, headers), body: JSON.stringify({ reason: reason }) }) .then(JSONResponseHandler) .then(function (json) { if (isAPIErrorResponse(json)) { throw json; } else { return json; } })]; } }); }); } //# sourceMappingURL=requestDeleteAccount.js.map