@adyen/adyen-platform-experience-web
Version:

68 lines (67 loc) • 2.2 kB
JavaScript
var c = Object.defineProperty;
var f = (i, t, r) => t in i ? c(i, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : i[t] = r;
var s = (i, t, r) => f(i, typeof t != "symbol" ? t + "" : t, r);
import { http as u } from "../../Http/http.js";
import { ErrorTypes as m } from "../../Http/utils.js";
import { AUTO_REFRESH as E, MAX_AGE_MS as d } from "./constants.js";
import { isPlainObject as _, isString as h, isUndefined as A } from "../../../utils/value/is.js";
import { ERR_SESSION_EXPIRED as H } from "../../../primitives/context/session/constants.js";
import { enumerable as l } from "../../../utils/struct/property.js";
import { isAbortSignal as O, abortSignalForAny as R } from "../../../utils/abort/main.js";
class y {
constructor(t) {
s(this, "assert", (t) => {
if (_(t)) {
const r = h(t.id) ? t.id.trim() : void 0, e = h(t.token) ? t.token.trim() : void 0;
if (r && e) return;
}
throw void 0;
});
s(this, "deadline", (t) => {
const r = [];
let e, a;
try {
({ iat: e, exp: a } = JSON.parse(atob(t == null ? void 0 : t.token.split(".")[1]))), r.push(a);
} catch {
e = Date.now();
}
if (!A(d)) {
const n = new Date(e);
r.push(n.setMilliseconds(n.getMilliseconds() + d));
}
return r;
});
s(this, "http", async (t, r, e) => {
const { headers: a, signal: n, ...p } = e;
try {
const o = {
...p,
headers: {
...a,
...t && { Authorization: `Bearer ${t.token}` }
},
errorHandler: this._errorHandler,
signal: O(n) ? R([r, n]) : r
};
return await u(o);
} catch (o) {
throw (o == null ? void 0 : o.type) === m.EXPIRED_TOKEN ? H : o;
}
});
this.onSessionCreate = t, this._errorHandler = this._errorHandler.bind(this), Object.defineProperties(this, {
autoRefresh: l(E),
onRefresh: l((r, e) => this.onSessionCreate(e))
});
}
_errorHandler(t) {
try {
this.errorHandler && this.errorHandler(t);
} catch {
}
throw t;
}
}
export {
y as AuthSessionSpecification,
y as default
};