|
| 1 | +type BuildOAuthSuccessUrlParams = { |
| 2 | + pageUrl: URL; |
| 3 | + basePath: string; |
| 4 | + origin: string; |
| 5 | + isStudio: boolean; |
| 6 | +}; |
| 7 | + |
| 8 | +export const STUDIO_PROMPT_KEY = 'studioPrompt'; |
| 9 | +const ABSOLUTE_URL = /^[a-zA-Z][a-zA-Z\d+.-]*:/; |
| 10 | + |
| 11 | +function isAbsoluteUrl(value: string): boolean { |
| 12 | + return ABSOLUTE_URL.test(value) || value.startsWith('//'); |
| 13 | +} |
| 14 | + |
| 15 | +function stashStudioPrompt(prompt: string, isStudio: boolean): void { |
| 16 | + if (!isStudio || !prompt) { |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + try { |
| 21 | + sessionStorage.setItem(STUDIO_PROMPT_KEY, prompt); |
| 22 | + } catch { |
| 23 | + // ignore |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function formatUrl(url: URL, original: string): string { |
| 28 | + if (isAbsoluteUrl(original)) { |
| 29 | + return url.toString(); |
| 30 | + } |
| 31 | + |
| 32 | + return `${url.pathname}${url.search}${url.hash}`; |
| 33 | +} |
| 34 | + |
| 35 | +function stripPromptFromTarget(target: string, isStudio: boolean): string { |
| 36 | + if (!isStudio) { |
| 37 | + return target; |
| 38 | + } |
| 39 | + |
| 40 | + try { |
| 41 | + const url = new URL(target, window.location.origin); |
| 42 | + const prompt = url.searchParams.get('prompt'); |
| 43 | + |
| 44 | + if (prompt) { |
| 45 | + stashStudioPrompt(prompt, isStudio); |
| 46 | + url.searchParams.delete('prompt'); |
| 47 | + } |
| 48 | + |
| 49 | + return formatUrl(url, target); |
| 50 | + } catch { |
| 51 | + return target; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function appendQuery(target: string, params: URLSearchParams): string { |
| 56 | + const query = params.toString(); |
| 57 | + if (!query) { |
| 58 | + return target; |
| 59 | + } |
| 60 | + |
| 61 | + const hashIndex = target.indexOf('#'); |
| 62 | + const hash = hashIndex >= 0 ? target.slice(hashIndex) : ''; |
| 63 | + const base = hashIndex >= 0 ? target.slice(0, hashIndex) : target; |
| 64 | + const separator = base.includes('?') ? '&' : '?'; |
| 65 | + |
| 66 | + return `${base}${separator}${query}${hash}`; |
| 67 | +} |
| 68 | + |
| 69 | +export function buildOAuthSuccessUrl({ |
| 70 | + pageUrl, |
| 71 | + basePath, |
| 72 | + origin, |
| 73 | + isStudio |
| 74 | +}: BuildOAuthSuccessUrlParams): string { |
| 75 | + const params = new URLSearchParams(pageUrl.search); |
| 76 | + const redirect = params.get('redirect'); |
| 77 | + |
| 78 | + if (redirect) { |
| 79 | + params.delete('redirect'); |
| 80 | + } |
| 81 | + |
| 82 | + if (isStudio) { |
| 83 | + const prompt = params.get('prompt'); |
| 84 | + if (prompt) { |
| 85 | + stashStudioPrompt(prompt, isStudio); |
| 86 | + params.delete('prompt'); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + let target = redirect ? stripPromptFromTarget(redirect, isStudio) : basePath; |
| 91 | + target = appendQuery(target, params); |
| 92 | + |
| 93 | + if (isAbsoluteUrl(target)) { |
| 94 | + return target; |
| 95 | + } |
| 96 | + |
| 97 | + return origin + target; |
| 98 | +} |
0 commit comments