(opts: {
apiUrl: string
openApiUrl: string
callbackUrl: string
jwtToken: string | null
verificationId?: string
})
| 92 | } |
| 93 | |
| 94 | export function getInitScript(opts: { |
| 95 | apiUrl: string |
| 96 | openApiUrl: string |
| 97 | callbackUrl: string |
| 98 | jwtToken: string | null |
| 99 | verificationId?: string |
| 100 | }): string { |
| 101 | const { apiUrl, openApiUrl, callbackUrl, jwtToken, verificationId } = opts |
| 102 | const jwtJson = jwtToken ? JSON.stringify(jwtToken) : 'null' |
| 103 | const verificationIdJson = verificationId ? JSON.stringify(verificationId) : 'null' |
| 104 | const buttonScript = getButtonInjectionScript() |
| 105 | |
| 106 | return ` |
| 107 | (function() { |
| 108 | const apiUrl = ${JSON.stringify(apiUrl)}; |
| 109 | const callbackUrl = ${JSON.stringify(callbackUrl)}; |
| 110 | const openApiUrl = ${JSON.stringify(openApiUrl)}; |
| 111 | const jwtFromServer = ${jwtJson}; |
| 112 | const verificationIdFromUrl = ${verificationIdJson}; |
| 113 | |
| 114 | function updateScalarAuth(scalarApiReference, token) { |
| 115 | const authConfig = { |
| 116 | preferredSecurityScheme: 'bearerAuth', |
| 117 | securitySchemes: { bearerAuth: { token } }, |
| 118 | }; |
| 119 | if (scalarApiReference?.updateConfiguration) { |
| 120 | scalarApiReference.updateConfiguration({ authentication: authConfig }); |
| 121 | } else if (scalarApiReference?.updateAuthentication) { |
| 122 | scalarApiReference.updateAuthentication(authConfig); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | const storedToken = localStorage.getItem('scalar-token'); |
| 127 | const token = jwtFromServer || storedToken; |
| 128 | |
| 129 | let scalarApiReference = null; |
| 130 | try { |
| 131 | scalarApiReference = Scalar.createApiReference('#scalar-container', { |
| 132 | url: openApiUrl, |
| 133 | theme: 'moon', |
| 134 | authentication: { |
| 135 | preferredSecurityScheme: 'bearerAuth', |
| 136 | securitySchemes: { bearerAuth: { token: token || '' } }, |
| 137 | }, |
| 138 | }); |
| 139 | } catch (error) { |
| 140 | console.error('Failed to initialize Scalar:', error); |
| 141 | } |
| 142 | |
| 143 | if (jwtFromServer) { |
| 144 | localStorage.setItem('scalar-token', jwtFromServer); |
| 145 | history.replaceState({}, '', '/reference'); |
| 146 | updateScalarAuth(scalarApiReference, jwtFromServer); |
| 147 | } else if (verificationIdFromUrl) { |
| 148 | const banner = document.createElement('div'); |
| 149 | banner.id = 'verify-banner'; |
| 150 | banner.style.cssText = 'position:fixed;top:0;left:0;right:0;padding:12px 16px;background:#1e3a5f;color:#fff;display:flex;align-items:center;justify-content:center;gap:12px;flex-wrap:wrap;z-index:10000;font-size:14px;'; |
| 151 | banner.innerHTML = '<span>Enter the 6-digit code from your email:</span><form style="display:inline-flex;gap:8px;align-items:center;flex-wrap:wrap;"><input type="text" inputmode="numeric" pattern="\\\\d*" maxlength="6" placeholder="000000" style="width:80px;padding:6px;font-size:14px;border-radius:4px;"/><button type="submit" style="padding:6px 12px;background:#667eea;color:#fff;border:none;border-radius:4px;cursor:pointer;">Verify</button><span data-verify-error="" aria-live="polite" style="color:#ef4444;font-size:12px;margin-left:8px;"></span></form>'; |
no test coverage detected