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

76 lines (75 loc) • 2.83 kB
JavaScript
var P = Object.defineProperty;
var u = (s, t, r) => t in s ? P(s, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : s[t] = r;
var i = (s, t, r) => u(s, typeof t != "symbol" ? t + "" : t, r);
import { SETUP_ENDPOINT_PATH as l } from "./constants.js";
import { parseSearchParams as d } from "../../Http/utils.js";
import { noop as f, deepFreeze as x } from "../../../utils/common.js";
import { EMPTY_OBJECT as _ } from "../../../utils/value/constants.js";
import { createPromisor as g } from "../../../primitives/async/promisor/main.js";
import { struct as y, withFreezeProxyHandlers as E, asPlainObject as b } from "../../../utils/struct/main.js";
import { isPlainObject as v, isUndefined as O } from "../../../utils/value/is.js";
import { isAbortSignal as C, abortSignalForAny as S } from "../../../utils/abort/main.js";
class N {
constructor(t) {
i(this, "_endpoints", _);
i(this, "_extraConfig", _);
i(this, "_revokeEndpointsProxy", f);
i(this, "_beforeHttp", async () => {
await this._refreshPromisor.promise.catch(f);
});
i(this, "_refreshPromisor", g((t, r) => {
const o = C(r) ? S([r, t]) : t;
return this._fetchSetupEndpoint(o);
}));
this._session = t;
let r;
this.refresh = (o) => (this._refreshPromisor(o).catch(f), r ?? (r = this._refreshPromisor.promise.finally(() => r = void 0).then(({ endpoints: n, ...e }) => {
this._resetEndpoints(), { proxy: this._endpoints, revoke: this._revokeEndpointsProxy } = this._getEndpointsProxy(n), this._extraConfig = x(e);
})));
}
get endpoints() {
return this._endpoints;
}
get extraConfig() {
return this._extraConfig;
}
_fetchSetupEndpoint(t) {
return this._session.http(null, {
method: "POST",
path: l,
errorLevel: "fatal",
loadingContext: this.loadingContext,
signal: t
});
}
_getEndpointsProxy(t) {
const r = new Set(Object.keys(t)), o = y();
return Proxy.revocable(
_,
E({
get: (n, e, c) => r.has(e) ? (o[e] ?? (o[e] = (() => {
const { method: a = "GET", url: h } = t[e];
if (!O(h || void 0))
return (...m) => {
const p = this._getHttpOptions(a, h, ...m);
return this._session.http(this._beforeHttp, p);
};
})()), o[e]) : Reflect.get(n, e, c)
})
);
}
_getHttpOptions(t, r, ...o) {
const { loadingContext: n } = this, [e, c] = o, { path: a, query: h } = b(c), m = h && d(h);
if (v(a))
for (const p of Object.keys(a))
r = r.replace(`{${p}}`, a[p]);
return { loadingContext: n, ...e, method: t, params: m, path: r };
}
_resetEndpoints() {
this._revokeEndpointsProxy(), this._revokeEndpointsProxy = f, this._endpoints = _;
}
}
export {
N as SetupContext,
N as default
};