@arc-publishing/sdk-amp
Version:
Arc AMP SDK to work with Google AMP Pages
51 lines • 2.24 kB
JavaScript
import { __awaiter, __generator } from "tslib";
import AMP from './amp';
import { isAPIErrorResponse } from '@arc-publishing/sdk-subs-core/lib/utils/APIErrorResponse';
import { logPrefix } from './constants';
export default function checkAMPReaderID(readerId) {
return __awaiter(this, void 0, void 0, function () {
var error, existingReaderId, isLoggedIn, ampReaderId, allUserReaderIds, ids, resp;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (typeof window === 'undefined') {
return [2, ''];
}
if (!AMP._Identity) {
error = "".concat(logPrefix, " no Identity provided in AMP.options");
console.error(error);
throw new Error(error);
}
existingReaderId = AMP.ampReaderId;
return [4, AMP._Identity.isLoggedIn()];
case 1:
isLoggedIn = _a.sent();
ampReaderId = readerId || '';
if (ampReaderId && (!existingReaderId || existingReaderId !== ampReaderId)) {
AMP.ampReaderId = ampReaderId;
}
if (AMP.ampReaderId) {
ampReaderId = AMP.ampReaderId;
}
if (!isLoggedIn) return [3, 4];
allUserReaderIds = [];
return [4, AMP.getAMPReaderIDs()];
case 2:
ids = _a.sent();
if (Array.isArray(ids)) {
allUserReaderIds = ids;
}
if (!(ampReaderId && !allUserReaderIds.includes(ampReaderId))) return [3, 4];
return [4, AMP.saveAMPReaderID(ampReaderId)];
case 3:
resp = _a.sent();
if (isAPIErrorResponse(resp)) {
throw resp;
}
_a.label = 4;
case 4: return [2, ampReaderId];
}
});
});
}
//# sourceMappingURL=checkAMPReaderID.js.map