@parkingboss/api
Version:
The Parking Boss API
46 lines (45 loc) • 1.58 kB
JavaScript
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;
}
export 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 });
}
export 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,
});
}