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

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