Co-authored-by: codex <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: cursor[bot] <206951365+cursor[bot]@users.noreply.github.com>
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
const PAIRING_TOKEN_PARAM = "token";
|
|
|
|
function readHashParams(url: URL): URLSearchParams {
|
|
return new URLSearchParams(url.hash.startsWith("#") ? url.hash.slice(1) : url.hash);
|
|
}
|
|
|
|
export function getPairingTokenFromUrl(url: URL): string | null {
|
|
const hashToken = readHashParams(url).get(PAIRING_TOKEN_PARAM)?.trim() ?? "";
|
|
if (hashToken.length > 0) {
|
|
return hashToken;
|
|
}
|
|
|
|
const searchToken = url.searchParams.get(PAIRING_TOKEN_PARAM)?.trim() ?? "";
|
|
return searchToken.length > 0 ? searchToken : null;
|
|
}
|
|
|
|
export function stripPairingTokenFromUrl(url: URL): URL {
|
|
const next = new URL(url.toString());
|
|
const hashParams = readHashParams(next);
|
|
if (hashParams.has(PAIRING_TOKEN_PARAM)) {
|
|
hashParams.delete(PAIRING_TOKEN_PARAM);
|
|
next.hash = hashParams.toString();
|
|
}
|
|
next.searchParams.delete(PAIRING_TOKEN_PARAM);
|
|
return next;
|
|
}
|
|
|
|
export function setPairingTokenOnUrl(url: URL, credential: string): URL {
|
|
const next = new URL(url.toString());
|
|
next.searchParams.delete(PAIRING_TOKEN_PARAM);
|
|
next.hash = new URLSearchParams([[PAIRING_TOKEN_PARAM, credential]]).toString();
|
|
return next;
|
|
}
|