@parkingboss/api
Version:
The Parking Boss API
51 lines (50 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildLoginUrl = exports.build = void 0;
function urlToPath(url, opts = {}) {
const copied = new URL(url.href);
if (opts.skipHash) {
copied.hash = "";
}
// Reminder: replace only replaces the first copy unless the first argument is a global regex
return copied.href.replace(copied.origin, opts.absolute ? copied.origin : "");
}
function isUrlLike(x) {
return typeof x == "string" || x instanceof URL || x instanceof Location;
}
function build(urlOrOpt, maybeOpt) {
const urlArg = isUrlLike(urlOrOpt) ? urlOrOpt : self.location;
const opts = maybeOpt || (isUrlLike(urlOrOpt) ? {} : urlOrOpt);
const url = new URL(urlArg.toString(), "https://api.propertyboss.io/v2");
Object.entries(opts.query || {}).forEach(([key, val]) => {
if (val) {
(Array.isArray(val) ? val : [val]).forEach((v, ix) => {
if (ix == 0) {
url.searchParams.set(key, v);
}
else {
url.searchParams.append(key, v);
}
});
}
else {
url.searchParams.delete(key);
}
});
if (opts.hash) {
url.hash = opts.hash;
}
return urlToPath(url, { skipHash: opts.hash === false, absolute: opts.absolute });
}
exports.build = build;
function buildLoginUrl({ clientId, email, redirectUrl }) {
return build("https://auth.communityboss.app/login", {
query: {
client_id: clientId,
login_hint: email || null,
redirect_uri: redirectUrl || location.href,
},
absolute: true,
});
}
exports.buildLoginUrl = buildLoginUrl;